losetup -d not working

时间:2015-12-04 16:51:28

标签: device-driver mount strace unmount

我在/ tmp文件夹中设置了5个带有备份文件的循环设备作为原始40 MB文件。

$ losetup -l
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop1         0      0         1  0 /tmp/1
/dev/loop2         0      0         0  0 /tmp/2
/dev/loop3         0      0         0  0 /tmp/3
/dev/loop4         0      0         0  0 /tmp/4
/dev/loop5         0      0         0  0 /tmp/5

我使用lvm为前4个设备创建一个raid阵列。因此,前4个设备忙碌如下:

$ lsblk 
NAME                    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda                       8:0    0 931.5G  0 disk 
├─sda1                    8:1    0   100M  0 part /boot/efi
├─sda2                    8:2    0   900M  0 part 
├─sda3                    8:3    0   128M  0 part 
├─sda4                    8:4    0 372.6G  0 part 
├─sda5                    8:5    0 140.5G  0 part /
├─sda6                    8:6    0    15G  0 part 
├─sda7                    8:7    0 186.3G  0 part 
├─sda8                    8:8    0  37.3G  0 part [SWAP]
├─sda9                    8:9    0  93.1G  0 part 
└─sda10                   8:10   0  40.8G  0 part 
loop1                     7:1    0  78.1M  0 loop 
├─vol_vg-raid4_rmeta_0  252:0    0     4M  0 lvm  
│ └─vol_vg-raid4        252:8    0    48M  0 lvm  
└─vol_vg-raid4_rimage_0 252:1    0    16M  0 lvm  
  └─vol_vg-raid4        252:8    0    48M  0 lvm  
loop2                     7:2    0  78.1M  0 loop 
├─vol_vg-raid4_rmeta_1  252:2    0     4M  0 lvm  
│ └─vol_vg-raid4        252:8    0    48M  0 lvm  
└─vol_vg-raid4_rimage_1 252:3    0    16M  0 lvm  
  └─vol_vg-raid4        252:8    0    48M  0 lvm  
loop3                     7:3    0  78.1M  0 loop 
├─vol_vg-raid4_rmeta_2  252:4    0     4M  0 lvm  
│ └─vol_vg-raid4        252:8    0    48M  0 lvm  
└─vol_vg-raid4_rimage_2 252:5    0    16M  0 lvm  
  └─vol_vg-raid4        252:8    0    48M  0 lvm  
loop4                     7:4    0  78.1M  0 loop 
├─vol_vg-raid4_rmeta_3  252:6    0     4M  0 lvm  
│ └─vol_vg-raid4        252:8    0    48M  0 lvm  
└─vol_vg-raid4_rimage_3 252:7    0    16M  0 lvm  
  └─vol_vg-raid4        252:8    0    48M  0 lvm  

我现在尝试删除循环设备1.我在控制台或dmseg中没有收到任何警告/错误:

$ sudo losetup -d /dev/loop1 
shehbaz@donjaffer:~$ 

现在,我再按以下方式列出所有设备:

$ losetup -l
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop1         0      0         1  0 /tmp/1
/dev/loop2         0      0         0  0 /tmp/2
/dev/loop3         0      0         0  0 /tmp/3
/dev/loop4         0      0         0  0 /tmp/4
/dev/loop5         0      0         0  0 /tmp/5
shehbaz@donjaffer:~$

我也看到lsblk输出与我上面提供的相同。

我尝试在我之前尝试过的命令上运行strace(看看是否有某些权限被拒绝或者其他形式的错误未被报告),我得到以下输出:

$ sudo strace losetup -d /dev/loop1
execve("/sbin/losetup", ["losetup", "-d", "/dev/loop1"], [/* 17 vars */]) = 0
brk(0)                                  = 0x1985000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fc13f271000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=108331, ...}) = 0
mmap(NULL, 108331, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fc13f256000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libsmartcols.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260R\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=134536, ...}) = 0
mmap(NULL, 2233952, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fc13ee2e000
mprotect(0x7fc13ee4e000, 2093056, PROT_NONE) = 0
mmap(0x7fc13f04d000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1f000) = 0x7fc13f04d000
mmap(0x7fc13f04f000, 1632, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fc13f04f000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`\v\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1869392, ...}) = 0
mmap(NULL, 3972864, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fc13ea64000
mprotect(0x7fc13ec24000, 2097152, PROT_NONE) = 0
mmap(0x7fc13ee24000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c0000) = 0x7fc13ee24000
mmap(0x7fc13ee2a000, 16128, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fc13ee2a000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fc13f255000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fc13f253000
arch_prctl(ARCH_SET_FS, 0x7fc13f253740) = 0
mprotect(0x7fc13ee24000, 16384, PROT_READ) = 0
mprotect(0x7fc13f04d000, 4096, PROT_READ) = 0
mprotect(0x60f000, 4096, PROT_READ)     = 0
mprotect(0x7fc13f273000, 4096, PROT_READ) = 0
munmap(0x7fc13f256000, 108331)          = 0
brk(0)                                  = 0x1985000
brk(0x19a6000)                          = 0x19a6000
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=7216688, ...}) = 0
mmap(NULL, 7216688, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fc13e382000
close(3)                                = 0
stat("/sys/block", {st_mode=S_IFDIR|0755, st_size=0, ...}) = 0
uname({sys="Linux", node="donjaffer", ...}) = 0
stat("/dev/loop-control", {st_mode=S_IFCHR|0660, st_rdev=makedev(10, 237), ...}) = 0
stat("/dev/loop1", {st_mode=S_IFBLK|0660, st_rdev=makedev(7, 1), ...}) = 0
open("/dev/loop1", O_RDONLY|O_CLOEXEC)  = 3
ioctl(3, LOOP_CLR_FD)                   = 0
close(3)                                = 0
close(1)                                = 0
close(2)                                = 0
exit_group(0)                           = ?
+++ exited with 0 +++
shehbaz@donjaffer:~$ 

我仍然看到使用losetup命令的所有设备。 -d选项不能正常工作吗?我也试过卸载/ dev / loop1,但它告诉我/ dev / loop1没有挂载。

$ umount -l /dev/loop1
umount: /dev/loop1: not mounted
shehbaz@donjaffer:~$

请帮忙。谢谢。

4 个答案:

答案 0 :(得分:2)

关于上述代码片段的重要注释,来自Tom hale:

bash脚本中使用的losttup命令对IFS值敏感。

尝试一下,看看自己

line="/dev/loop8 /somefile"
losetup $line     # this works
IFS=""
losetup $line     # this one will fail

如果变量($ line)包含多个参数,则IFS值允许Lostup检测到它们。通过清除它,最后一个失败者认为complete变量只是1个参数,显然失败了。

答案 1 :(得分:1)

我想说驱动程序会保持文件打开,直到你关闭它上面打开的最后一个文件描述符,因此文件仍在使用中并使用循环设备,直到你关闭它。

这在Linux上很常见,实际上,如果你创建一个文件并打开它,只有当文件系统上打开的所有文件描述符都将被关闭时,文件系统所占用的空间才可用。

因此,在你停止你在其上创建的lvm raid之前,它将一直使用。

因此,在发布之前,您无法重用loopX;如果您尝试使用它,我猜您会收到错误,losetup -f也不会提出错误。

答案 2 :(得分:1)

这似乎是由旧FS挂载点上的工作目录进程引起的。

令人沮丧的是,似乎没有办法告诉他们卸载后的流程。

我提出util-linux issue "losetup -d exits 0 when it doesn't succeed" #484,维护者表明它是内核问题。

如果环回设备没有被自动删除,我编写了以下代码来打印挂载点的先前用法。

#!/bin/bash

set -euo pipefail
shopt -s failglob

# Unmount the entries given in $1, (one per line)
unmount() {
  # https://unix.stackexchange.com/questions/9784/how-can-i-read-line-by-line-from-a-variable-in-bash
  # You need printf '%s\n' "$var" here because if you use printf '%s' "$var" 
  # on a variable that doesn't end with a newline then the while loop will
  # completely miss the last line of the variable.
  # printf '%s\n' "$1" | while IFS= read -r dir; do # each mount point
  IFS= printf '%s\n' "$1" | while read -r dir; do # each mount point
    ! /usr/bin/mountpoint -q -- "$dir" && continue

    # Bug: can only find processes using mountpoint before unmount
    # https://github.com/karelzak/util-linux/issues/484
    open_files=$(sudo lsof "$dir" 2>/dev/null || true)
    # Exits non-zero if object not in use
    # Avoid "WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs"

    mountpoints=$(awk -v dir="$dir" 'BEGIN{dir="^" dir "$"} $2 ~ dir {print $1}' /etc/mtab)

    while sudo "/bin/umount" -Rdl -- "$dir"; do # returns false when already unmounted
      # -R  Recursive unmount
      # -d  Remove the associated loop device
      # -l  Lazy remove filesystem references immediately
      # -v  Verbose
      sleep 0.01;
    done

    if loop_dev=$(losetup -a --list | grep --fixed-strings "$mountpoints"); then
      printf "WARNING: loop device remains after unmount:\n%s\n" "$loop_dev" 2>&1
      printf "%s\n" "$open_files" 2>&1
    fi
  done
}

# # List of mount points to umount before checking /etc/mtab
# # List any contained loopback files here
# sub_mounts='/media/backup
# /media/backup-NEW'
# unmount "$sub_mounts"

# sort -r to unmount subiretories before parents:
mtab_mounts=$(/bin/awk '$2 ~ /^\/media/ {print $2}' /etc/mtab | sort -r)
unmount "$mtab_mounts"

答案 3 :(得分:0)

尝试删除循环时未收到错误消息的原因是内核不会将其作为错误返回。多年以后的现在,情况似乎仍然如此。

ioctl(3, LOOP_CLR_FD)                   = 0

即使lsof没有显示任何使循环设备保持打开状态的进程,内核也不会释放循环,除非您还获得了lvm来释放设备。 (我想,没有RAID,但仍然很重要),在使用PV禁用LV之后,我只能摆脱自己的循环。就我而言,它会在我消失后立即消失:

lvchange -an computer-vg/root

甚至无需再次运行losetup -d