没有明确原因的错误是最烦人的问题。虽然Linux通常在其错误消息中是特定的,但这是它丢球的少数几次之一。
得到“No place left on device error”的解释很少,尤其是当您的驱动器远未满时。为什么会出现此错误消息?尽管磁盘上有足够的空间,为什么您不能创建新文件?最重要的是,你如何解决它?
让我们来了解一下。
您的硬盘实际上有足够的空间(Hard Drive Actually Have Enough Space)吗?
在你急于寻找解决方案之前,首先要确保存在问题。毕竟,如果您的系统上的磁盘空间实际上已经用完,则无需恐慌。您可以删除不必要的数据以释放更多空间。
有两个Linux系统命令可用于获取有关磁盘空间的信息——du 和 df。du 命令估计磁盘空间使用情况,而 df 命令分析磁盘上的可用空间。将它们与sudo结合使用可以准确报告磁盘上实际可用的空间量。
- 让我们从 du 命令开始。它的输出可能会很长而且很麻烦,所以我们将使用 -s 和 -h 标志。-s 总结结果,而 -h 确保它是人类可读的。所以输入命令 sudo du -sh / where / 指向基目录。
- 不要担心所有的权限被拒绝消息——这就是它在系统的所有子目录中搜索的方式。根据驱动器的大小,您可能需要让它运行一段时间,因为它会递归地遍历所有内容。
- 当 du 完成扫描时,它将输出一个简单的目录列表以及它们的大小。例如,这是应用到普通目录的 du 命令的输出。
现在我们将使用 df 查看有多少可用空间。df 命令使用起来要简单得多,因为它只显示已挂载的文件系统及其使用统计信息。再一次,我们将使用 -h 标志使输出可读。
须藤 df -h(sudo df -h )
这个想法是计算 du 和 df 命令的结果。任何差异都指向磁盘空间尽管未被任何文件或文件夹使用,但仍无法免费使用。
为什么Linux 显示(Does Linux Show)“设备上没有剩余空间(Space Left)”?
如果您看到“设备上没有剩余空间”错误消息,尽管您的磁盘上有足够的可用空间,这不是硬件有问题。找出错误的确切原因可能需要一些故障排除。
- 最近删除的文件:看到此错误的最常见原因是最近删除的文件。通常在进程仍在使用文件时删除文件,即使文件已经消失,也会保留空间。
- 没有足够(Enough Inodes)的 inode :另一个常见的原因是没有足够的inode(inodes)。索引节点是Unix文件系统的索引页,保存着存储中每个文件的元数据。但是,inode 不是无限的,在存储空间之前用尽 inode 可能会给您“设备上没有剩余空间”错误。
- 硬盘(Hard)驱动器故障:当然,硬盘驱动器可能只是出现故障,并且大部分明显的可用空间都被坏扇区占用了。由于系统无法将任何文件写入这些位置,因此会引发错误。
修复 1:使用已删除文件重新启动进程(Using Deleted Files)
“设备上没有剩余空间”错误的最可能原因是仍在使用已删除文件的进程。值得庆幸的是,修复此错误很容易。您只需要重新启动该过程(restart the process)即可释放保留的存储空间。
- 要查找有问题的进程,您需要使用 lsof 和 grep 命令。lsof 命令将为您提供正在运行的进程正在使用的所有打开文件的列表,而 grep 可以将选择范围缩小到已删除的文件。所以输入 sudo lsof / | grep 删除其中 / 是基目录和 | 用于将 lsof 的输出链接到 grep 的管道。
- 现在您可以使用 sudo systemctl restart service_name 命令重新启动受影响的进程,其中服务名称是搜索中出现的服务的名称。
- 如果有多个这样的进程,或者您只是不想费心寻找特定的服务,您可以使用 sudo systemctl daemon-reload 命令将它们全部重置。这会重新生成所有依赖项,同时考虑文件系统中的任何更改。
在此之后,进程占用的存储空间应该再次可用,允许您写入文件而不会遇到任何错误。
修复 2:检查索引节点
虽然每个驱动器都有大量的 inode,但它是有限的。如果您的系统中充斥着数量惊人的文件,则有可能在驱动器的存储容量耗尽之前达到此限制。这就是为什么最好拥有大文件而不是太多的小文件。
您可以使用 df 命令的 -i 标志轻松检查 inode 的可用性。像这样:
须藤 df -i(sudo df -i )
这将清楚地显示文件系统中存在的 inode 总数以及当前使用的数量。如果您的系统上没有可用的 inode,这就是“设备上没有剩余空间”错误的根源。
由于 inode 仅在首次格式化驱动器时创建,因此无法生成更多 inode。您所能做的就是删除任何不必要的文件以释放 inode 以供将来的文件使用。
修复 3:标记坏块
数据(Data)损坏是任何硬盘驱动器的必然命运。虽然新驱动器不会遇到此问题,但较旧的硬盘将逐渐开始“坏”。
这意味着硬盘驱动器的某些部分变得不可用,即使文件系统仍然认为它们可以正常工作。这些坏块错误地膨胀了驱动器上的明显可用空间,而实际上没有文件可以写入它们。这会导致“设备上没有剩余空间”错误,因为操作系统尝试在这些位置存储任何数据但未能成功。
没有真正的方法可以解决此问题,因为坏扇区是硬盘驱动器物理磨损的结果。您唯一能做的就是标记坏扇区,以便文件系统不再引用它们。
这样做需要您从Live CD启动,因为您无法在正在运行的驱动器中执行文件系统检查。(File System Check)完成后,只需使用以下命令:
sudo fsck -vcck /dev/sda
这会将 /dev/sda 替换为您要修复的驱动器的路径。这将自动检测驱动器上的所有坏块并将它们标记为不可用。但是,它不会为您提供任何额外的存储容量,因此请准备好清理一些无用的文件以释放驱动器上的空间。
修复“设备上没有剩余空间”错误的最佳方法是什么?
在Ubuntu(Ubuntu)或任何其他Linux发行版上看到“设备上没有剩余空间”错误的通常原因是正在运行的进程仍在使用已删除的文件。这会保留文件占用的存储空间,防止其他操作将数据写入该空间。
解决这个问题也是最简单的,因为您需要做的就是重新启动有问题的进程。然而,导致错误的其他原因并不容易解决。
无论您的驱动器受到坏数据块的困扰还是 inode 用完,都没有直接的方法可以解决这些问题。您必须删除现有文件以便为写入新数据腾出空间。
Top 3 Ways to Fix “No Space Left on Device” Error in Linux
Εrrors without a cleаr cause are the most irritating problems to deal with. And while Linux is usually specific in its error messages, this is one of the few times whеn it drops the ball.
Getting a “No place left on device error” explains very little, especially if your drive is far from full. Why is this error message showing up? Why can’t you create new files despite having sufficient space on the disk? And most importantly, how do you fix it?
Let’s find out.
Does Your Hard Drive Actually Have Enough Space?
Before you rush to find a fix, make sure there is a problem, to begin with. After all, if disk space has actually run out on your system, there is no need to panic. You can just remove unnecessary data to free up more space.
There are two Linux system commands for getting information about disk space – du and df. The du command estimates the disk space usage, while the df command analyzes the free space present on the disk. Using them in conjunction with sudo gives you an accurate report of how much space is actually available on the disk.
- Let’s start with the du command. Its output can be rather long and cumbersome to go through, so we will use the -s and -h flags. -s summarizes the results, while -h ensures it is human readable. So enter the command sudo du -sh / where / points to the base directory.
- Don’t worry about all the permission denied messages – that’s how it trawls through all subdirectories of the system. Depending on the size of the drive you may have to leave it running for a while, as it recursively goes through everything.
- When du finishes scanning, it will output a simple list of directories alongside their sizes. For example, this is the output of the du command applied to an ordinary directory.
Now we will use df to see how much free space is available. The df command is much simpler to use since it only shows the mounted filesystems and their usage statistics. Once again, we will use the -h flag to make the output human readable.
sudo df -h
The idea is to tally the results of the du and df commands. Any discrepancy points to disk space that is not freely available despite not being used by any files or folders.
Why Does Linux Show “No Space Left on Device”?
If you are seeing the “No Space Left on Device” error message despite having sufficient free space on your disk, it’s not the hardware that’s at fault. Figuring out the exact cause for the error can take a bit of troubleshooting.
- Recently Deleted File: The most common reason for seeing this error is a recently deleted file. Often a file is deleted while a process is still using it, keeping the space reserved even though the file is already gone.
- Not Enough Inodes: Another frequent cause is not having enough inodes. Inodes are the index pages of a Unix filesystem, holding the metadata of every file on storage. However, inodes aren’t unlimited, and running out of inodes before the storage space can give you the “No space left on device” error.
- Failing Hard Drive: Then of course there is the possibility that the hard drive is simply failing, and much of the apparent free space is taken up by bad sectors. Since the system fails to write any files to these locations, it throws an error.
Fix 1: Restart Processes Using Deleted Files
The most probable cause of the “No space left on device” error is a process still using a deleted file. Thankfully, fixing this error is easy. You just need to restart the process to free up the reserved storage.
- To find the problematic process, you need to use the lsof and grep commands. The lsof command will give you a list of all open files being used by running processes, and grep can narrow down the selection to the ones that are deleted. So enter sudo lsof / | grep deleted where / is the base directory and | a pipe for linking lsof’s output to grep.
- Now you can restart the affected process with the sudo systemctl restart service_name command, where the service name is the name of the service that turned up in the search.
- If there are multiple processes like this or you just don’t want to bother with finding specific services, you can just reset them all with the sudo systemctl daemon-reload command. This regenerates all dependencies, taking any changes in the filesystem into account.
After this the storage space being held up by the process should be available again, allowing you to write files without running into any errors.
Fix 2: Check Inodes
While each drive has a large number of inodes, it is finite. And if your system is littered with an incredible number of files, it is possible to reach this limit before exhausting the storage capacity of the drive. This is why it’s better to have large files rather than too many small ones.
You can easily check the availability of inodes by using the -i flag with the df command. Like this:
sudo df -i
This will clearly tell the total number of inodes present in the filesystem, along with the amount currently in use. If there are no free inodes left on your system, this is the source of the “No space left on device” error.
As inodes are only created upon first formatting the drive, there is no way to generate more inodes. All you can do is delete any unnecessary files to free up inodes for future files.
Fix 3: Mark Bad Blocks
Data corruption is the inevitable fate of any hard drive. While a new drive will not run into this issue, older hard disks will gradually start going “bad”.
What this means is that portions of the hard drive are rendered unusable, even though the filesystem still considers them functional. These bad blocks falsely inflate the apparent free space on the drive, when in truth no files can be written to them. This leads to a “No space left on device” error, as the operating system tries and fails to store any data on these locations.
There is no real way to fix this issue, as bad sectors are a result of the physical wear and tear of a hard drive. The only thing you can do is mark the bad sectors so that they are no longer referenced by the file system.
Doing this requires you to boot from a Live CD, as you cannot perform a File System Check within a running drive. Once you do that, just use the following command:
sudo fsck -vcck /dev/sda
This replaces the /dev/sda with the path of the drive you want to repair. This will automatically detect all bad blocks on the drive and mark them as unusable. It won’t give you any extra storage capacity, however, so be prepared to clean up some useless files to free up space on the drive.
What Is the Best Way to Fix the “No Space Left on Device” Error?
The usual reason for seeing a “No space left on device” error on Ubuntu or any other Linux distro is a deleted file still being used by a running process. This reserves the storage that was occupied by the file, preventing other operations from writing data to that space.
Fixing this problem is also the easiest, as all you need to do is to restart the process in question. The other causes of the error, however, are not that easy to solve.
Whether your drive is plagued by bad data blocks or has run out of inodes, there is no direct way to fix these issues. You must delete existing files to make room for new data to be written.