当前位置: 首页 > 工具软件 > Device > 使用案例 >

【Linux工具】losetup: /dev/loop0: failed to set up loop device: Device or resource busy解决办法

邓鸿彩
2023-12-01

1 问题现象

如题, 详细错误如下:尝试了几个loop位置, 都失败了,错误信息都是 failed to set up loop device: Device or resource busy :

neo@neo-hw-matebookxpro:~/neo-practice/neos/lesson10
$ sudo losetup /dev/loop0 hd.img 
losetup: hd.img: failed to set up loop device: Device or resource busy
neo@neo-hw-matebookxpro:~/neo-practice/neos/lesson10
$ sudo losetup /dev/loop8 hd.img 
losetup: hd.img: failed to set up loop device: Device or resource busy
neo@neo-hw-matebookxpro:~/neo-practice/neos/lesson10
$ sudo losetup /dev/loop7 hd.img 
losetup: hd.img: failed to set up loop device: Device or resource busy

查看了一下, loop设备编号挺多的呀:

$ ll /dev/loop*
brw-rw---- 1 root disk  7,   0 Nov 12 23:56 /dev/loop0
brw-rw---- 1 root disk  7,   1 Oct 31 23:06 /dev/loop1
brw-rw---- 1 root disk  7,  10 Sep  9 00:08 /dev/loop10
brw-rw---- 1 root disk  7,  11 Oct 25 22:03 /dev/loop11
brw-rw---- 1 root disk  7,  12 Oct 25 22:03 /dev/loop12
brw-rw---- 1 root disk  7,  13 Oct 25 22:03 /dev/loop13
brw-rw---- 1 root disk  7,  14 Sep  9 00:08 /dev/loop14
brw-rw---- 1 root disk  7,  15 Sep  9 00:08 /dev/loop15
brw-rw---- 1 root disk  7,  16 Oct 31 23:06 /dev/loop16
brw-rw---- 1 root disk  7,  17 Oct 31 23:06 /dev/loop17
brw-rw---- 1 root disk  7,  18 Oct 31 23:06 /dev/loop18
brw-rw---- 1 root disk  7,  19 Oct 31 23:06 /dev/loop19
brw-rw---- 1 root disk  7,   2 Sep  9 00:08 /dev/loop2
brw-rw---- 1 root disk  7,   3 Sep  9 00:08 /dev/loop3
brw-rw---- 1 root disk  7,   4 Oct 25 22:03 /dev/loop4
brw-rw---- 1 root disk  7,   5 Sep  9 00:08 /dev/loop5
brw-rw---- 1 root disk  7,   6 Oct 24 22:33 /dev/loop6
brw-rw---- 1 root disk  7,   7 Nov 12 23:59 /dev/loop7
brw-rw---- 1 root disk  7,   8 Nov 12 23:59 /dev/loop8
brw-rw---- 1 root disk  7,   9 Oct 31 23:06 /dev/loop9
crw-rw---- 1 root disk 10, 237 Sep  9 00:08 /dev/loop-control

2 解决办法

2.1 使用命令 sudo losetup -f 自动查找空闲的设备

  • 原理说明, 使用命令 sudo losetup -f 自动查找空闲的设备loop16, 如下:
$ sudo losetup -f
/dev/loop16

2.2 重新设置到找出的空闲设备上

  • 这次很顺利, 没有报错, 执行成功了;
neo@neo-hw-matebookxpro:~/neo-practice/neos/lesson10
$ sudo losetup /dev/loop16 hd.img 
neo@neo-hw-matebookxpro:~/neo-practice/neos/lesson10

2.3 后续研究一下设备被占用的原因

  • todo

3 参考资料

  • losetup 使用帮助:
$ losetup --help

Usage:
 losetup [options] [<loopdev>]
 losetup [options] -f | <loopdev> <file>

Set up and control loop devices.

Options:
 -a, --all                     list all used devices
 -d, --detach <loopdev>...     detach one or more devices
 -D, --detach-all              detach all used devices
 -f, --find                    find first unused device
 -c, --set-capacity <loopdev>  resize the device
 -j, --associated <file>       list all devices associated with <file>
 -L, --nooverlap               avoid possible conflict between devices

 -o, --offset <num>            start at offset <num> into file
     --sizelimit <num>         device is limited to <num> bytes of the file
 -b, --sector-size <num>       set the logical sector size to <num>
 -P, --partscan                create a partitioned loop device
 -r, --read-only               set up a read-only loop device
     --direct-io[=<on|off>]    open backing file with O_DIRECT
     --show                    print device name after setup (with -f)
 -v, --verbose                 verbose mode

 -J, --json                    use JSON --list output format
 -l, --list                    list info about all or specified (default)
 -n, --noheadings              don't print headings for --list output
 -O, --output <cols>           specify columns to output for --list
     --output-all              output all columns
     --raw                     use raw --list output format

 -h, --help                    display this help
 -V, --version                 display version

Available output columns:
         NAME  loop device name
    AUTOCLEAR  autoclear flag set
    BACK-FILE  device backing file
     BACK-INO  backing file inode number
 BACK-MAJ:MIN  backing file major:minor device number
      MAJ:MIN  loop device major:minor number
       OFFSET  offset from the beginning
     PARTSCAN  partscan flag set
           RO  read-only device
    SIZELIMIT  size limit of the file in bytes
          DIO  access backing file with direct-io
      LOG-SEC  logical sector size in bytes

For more details see losetup(8).
 类似资料: