inode用尽

我无法更新Ubuntu,因为我的inode使用率达到99%。有什么最简单的方法可以解决这个问题吗?
谢谢您的帮助。

2在StackOverflow上有一个非常类似的问题被提出了。也许那个链接可以帮到你:http://stackoverflow.com/questions/653096/howto-free-inode-usage - user2405
1而ServerFault http://serverfault.com/questions/185553/what-does-it-mean-if-inode-usage-is-high - user2405
AskUbuntu上也有类似的情况。 - Pablo Bianchi
我觉得看一下这个链接也会很有帮助。https://stackoverflow.com/questions/653096/how-to-free-inode-usage - Enkum
4个回答

在分区格式化时,会设置inode的数量。通常创建的inode数量对于几乎任何目的都足够;然而,如果您有大量非常小的文件,则可能在磁盘未满之前用尽了inode。
您需要找到系统上使用inode的成千上万个小文件,并将它们删除或移动到已经专门设置了大量可用inode的分区中。在分区格式化后,无法更改可用inode的数量。 paxdiablo on stackoverflow编写的脚本可能是一种方便的方式,可以检查您可能没有意识到的过多使用小文件的情况。以下是该脚本:
#!/bin/bash
# count_em - count files in all subdirectories under current directory.
echo 'echo $(ls -a "$1" | wc -l) $1' >/tmp/count_em_$$
chmod 700 /tmp/count_em_$$
find . -mount -type d -print0 | xargs -0 -n1 /tmp/count_em_$$ | sort -n
rm -f /tmp/count_em_$$

将这个脚本放在文本文件~/bin/count_em中,然后执行以下命令。
chmod +x ~/bin/count_em

要使其可执行。如果您必须创建目录~/bin,那么它还不在可执行路径中,所以只需注销并重新登录即可。
要运行程序,只需输入
count_em

并且它会按目录列出当前目录和子目录中所有文件的数量,最后显示具有最高计数的目录。非常方便!

这行代码 "chmode +x ~/bin/count_em" 实际上应该是 "chmod +x ~/bin/count_em"。 - user169677
1需要删除所有这些用户创建的文件吗,还是将它们移动到tar归档中就可以了? - amc
将它们归档是一个很好的处理方式,它不会删除用户的数据,但会让他们意识到自己的行为不令人满意,因为他们将无法像以前那样使用这些文件。 不过,很有可能这么多的文件是由于某些没有被使用的东西生成的,它们可以安全地被删除,但这也存在一定的风险。 - LovesTha
一个很好的替代你脚本的单行命令可能是 sudo du -a -d 1 --inodes . | sort -nr | head -20 - Charles Green
1当然,如果你的inode完全用尽了,这是行不通的,因为它会创建临时文件 :( - Molomby

你还可以使用以下命令按照索引节点数量显示排序后的目录列表:du --inodes -d 3 / | sort -n | tail 从那里,你可以确定要删除的目录。

2在Ubuntu 14.04上出现了“未识别的选项'--inodes'”。 - Molomby
2为什么你在2019年还在使用Ubuntu 14.04?我看到它的coreutils版本是8.21,发布于2013年2月。而--inodes选项则是在2013年7月才被添加进去的,哈哈。 - ZN13
使用 df --inodes 而不是 du --inodes - Rick James

当你的索引节点用尽时,你有两个选择: 1. 从磁盘中删除一些文件(通常是小文件)。 2. 重新格式化受影响的驱动器,以便获得更多的索引节点。
第一个选择在其他答案中已经讨论过了,但没有答案涉及第二个选择——我将在这里进行说明。
以这个例子来说,假设受影响的磁盘是 `/dev/sda1`。
要在格式化时给定一定数量的索引节点,可以使用命令 `mke2fs`,并带上 `-i` 选项或 `-N` 选项。根据manpage上的描述:
-i bytes-per-inode
      Specify  the  bytes/inode ratio.  mke2fs creates an inode for every bytes-per-inode
      bytes of space on the disk.  The larger the bytes-per-inode ratio, the fewer inodes
      will  be  created.  This value generally shouldn't be smaller than the blocksize of
      the file system, since in that case more inodes would be  made  than  can  ever  be
      used.   Be  warned  that  it  is not possible to change this ratio on a file system
      after it is created, so be careful deciding the correct value for  this  parameter.
      Note  that  resizing  a  file  system changes the number of inodes to maintain this
      ratio.

-N number-of-inodes
      Overrides  the  default calculation of the number of inodes that should be reserved
      for the file system (which is based on the number of blocks and the bytes-per-inode
      ratio).  This allows the user to specify the number of desired inodes directly.

使用默认的inode数(每个inode大小为16384字节),格式化设备时,使用以下普通命令:(对于1TB磁盘,这将创建大约6000万个inodes)

sudo mke2fs -t ext4 /dev/sda1

要创建比以前多一倍的i节点分区,请使用以下命令:(请注意,字节/ i节点的值必须减半 - 对于一个1 TB的磁盘,这将创建大约1.2亿个i节点)

sudo mke2fs -i 8192 -t ext4 /dev/sda1

最后,要手动设置特定数量的i节点,请使用以下命令:(创建具有2亿个i节点的文件系统)

sudo mke2fs -N 200000000 -t ext4 /dev/sda1

我发现inode的使用量来自/root/.local目录,然后删除了这个文件夹。

2不应该使用/root/.local,而且它只是为了兼容性而存在。你需要找出是什么在向这个目录写入许多小文件。 - fabricator4