大网数据广告活动
服务器运维问答

loop device does not auto detach on umount

· 52CE 我爱测

I have a backup script in which I umount a squashfs file, I do backup then mount it back again. A simplified version of the script is: umount /home/backup/auto/mnt/os mksquashfs /src-dir /home/backup/auto/os.sqfs.img mount -t squ…

问题原文

原帖链接 · NickSoft · CC BY-SA 4.0

I have a backup script in which I umount a squashfs file, I do backup then mount it back again. A simplified version of the script is:

umount /home/backup/auto/mnt/os
mksquashfs /src-dir /home/backup/auto/os.sqfs.img
mount -t squashfs -o loop /home/backup/auto/os.sqfs.img /home/backup/auto/mnt/os

The idea is to have access at all times to backedup data.

And this worked for long time. However I got an error of not having enough loopback devices and I saw that umount does not detach loop devices mounted with mount anymore.

Lately I had two major changes:

  1. I updated the kernel to 2.6.32-754.11.1.el6.i686
  2. I moved the OS to an SSD.

I just noticed that there is new kernel update 2.6.32-754.12.1.el6, so I updated, but it did not fix the problem.

OS: CentOS 6.10 i686

Update: This is on the faulty system:

# strace -e trace=ioctl,mount mount -t squashfs -o loop os.sqfs.img mnt/os
ioctl(3, LOOP_GET_STATUS, {number=0, offset=0, flags=0, name="/home/squash/web.img", ...}) = 0
ioctl(3, LOOP_GET_STATUS, {number=0, offset=0, flags=0, name="/home/squash/web.img", ...}) = -1 ENXIO (No such device or address)
ioctl(4, LOOP_SET_FD, 0x3)              = 0
ioctl(4, LOOP_SET_STATUS64, {offset=0, number=0, flags=0, file_name="/home/backup/auto/os.sqfs.img", ...}) = 0
mount("/dev/loop1", "mnt/os", "squashfs", MS_MGC_VAL, NULL) = 0
+++ exited with 0 +++

# cat /etc/mtab|grep 'mnt/os'
/dev/loop1 /home/backup/auto/mnt/os squashfs ro,relatime 0 0

# ls -la /etc/mtab
lrwxrwxrwx. 1 root root 12 Apr 17 02:53 /etc/mtab -> /proc/mounts

# notice that here there is no even the umount call. Am I missing something?
# strace -e trace=ioctl,umount,mount umount mnt/os
+++ exited with 0 +++

And this is on another system with the same version of everything, but it's 64 bit:

# strace -e trace=ioctl,mount mount -t squashfs -o loop os.sqfs.img mnt/os
ioctl(3, LOOP_GET_STATUS, {number=0, offset=0, flags=0, name="/storage/backup/auto/home.sqfs.img", ...}) = 0
ioctl(3, LOOP_GET_STATUS, {number=1, offset=0, flags=0, name="/storage/backup/auto/mail.sqfs.img", ...}) = 0
ioctl(3, LOOP_GET_STATUS, {number=1, offset=0, flags=0, name="/storage/backup/auto/mail.sqfs.img", ...}) = -1 ENXIO (No such device or address)
ioctl(4, LOOP_SET_FD, 0x3)              = 0
ioctl(4, LOOP_SET_STATUS64, {offset=0, number=0, flags=0, file_name="/storage/backup/auto/os.sqfs.img", ...}) = 0
mount("/dev/loop2", "mnt/os", "squashfs", MS_MGC_VAL, NULL) = 0
+++ exited with 0 +++

# strace -e trace=ioctl,umount,mount umount mnt/os
umount("/storage/backup/auto/mnt/os", 0) = 0
ioctl(3, LOOP_CLR_FD)                   = 0
+++ exited with 0 +++

# losetup -a
/dev/loop0: [0904]:35656625 (/storage/backup/auto/home.sqfs.img)
/dev/loop1: [0904]:35656626 (/storage/backup/auto/mail.sqfs.img)

I don't see LO_FLAGS_AUTOCLEAR on the good system too. but I do see difference in umount.

I reinstalled util-linux-ng - it didn't help. There are no updates for this package and it has never been updated. Maybe it's related to the kernel. I wander why it's working just fine on the 64 bit OS. I might install a 32 bit VM to test if it'll do the same.

-- FIX --

The problem was that I made /etc/mtab a link to /proc/mounts during system copy using a live CD of CentOS 7 and I forgot about it. I had to re-install grub, so I chroot to the os and made the link because grub-install wasn't working. I forgot to restore it. I fixed this by:

rm -f /etc/mtab && cat /proc/mounts |grep -v rootfs > /etc/mtab

So it works now:

root@home auto# smount os.sqfs.img mnt/os
root@home auto# losetup -a
/dev/loop0: [0816]:7090072 (/home/squash/web.img)
/dev/loop1: [0816]:7864675 (/home/backup/auto/os.sqfs.img)
/dev/loop4: [0816]:7864569 (/home/backup/auto/web.sqfs.img)
root@home auto# umount mnt/os
root@home auto# losetup -a
/dev/loop0: [0816]:7090072 (/home/squash/web.img)
/dev/loop4: [0816]:7864569 (/home/backup/auto/web.sqfs.img)

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 4.0

This is what I've gathered by poking around the source for util-linux-ng 2.17.2, which was included with CentOS 6:

On kernels < 2.6.37, Linux requires that an entry be written to /etc/mtab for loop mounted filesystems in order to autoclear the loop device later. If /etc/mtab is not writable at mount time or the entry was removed from /etc/mtab then the loop device won't be autocleared. On kernels >= 2.6.37 the kernel remembers the loop device backing store path and it is not required to be in /etc/mtab.

I was able to reproduce this on CentOS 6.10 by making /etc/mtab immutable, with chattr +i /etc/mtab. Simply deleting it wasn't enough; it would be recreated with at least a partial mount table. This is not something that should happen in normal operation, so if you find this file has been set immutable you should look for an administrator mistake or a security compromise.

If /etc/mtab doesn't exist or is incomplete on your older Linux system, you can make a copy from /proc/mounts. Don't do this on modern Linux systems though, where /etc/mtab is instead a symbolic link to /proc/self/mounts left in place for backward compatibility with ancient code that expects a mount table at that location.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-04-18)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。