I create some loop devices within a docker container.
The container starts by the following command:
docker run --rm --privileged -it ubuntu bash
Then I execute the script inside the container.
#!/bin/bash
for idx in $(seq 3); do
losetup --raw | grep $idx.img | awk '{print $1}' | xargs -I {} losetup -d {}
rm -f $idx.img
fallocate -l 1g $idx.img # create an empty file with 5GB
ls -l $idx.img
loopDev=$(losetup -f) # figure out the loop device
echo "Setup loop device $loopDev on file $idx.img"
strace losetup $loopDev $idx.img
losetup $loopDev
echo "#######################################################"
done
-rw-r--r-- 1 root root 1073741824 Jun 15 14:57 1.img Setup loop device /dev/loop25 on file 1.img /dev/loop25: [66306]:17177356 (/workspace/infrastructure/test-env/rook/1.img) -rw-r--r-- 1 root root 1073741824 Jun 15 14:57 2.img Setup loop device /dev/loop26 on file 2.img /dev/loop26: [66306]:17177357 (/workspace/infrastructure/test-env/rook/2.img) -rw-r--r-- 1 root root 1073741824 Jun 15 14:57 3.img Setup loop device /dev/loop28 on file 3.img losetup: /dev/loop28: failed to set up loop device: No such file or directory losetup: /dev/loop28: failed to use device: No such device
It works for the first two loop devices but after a restart it maybe creates all or just one. I couldn’t figured out the reason why this behavior is so unpredictable.
The strace out put of the command which failed is:
execve("/usr/sbin/losetup", ["losetup", "/dev/loop28", "3.img"], 0x7ffe844f02d0 /* 47 vars */) = 0 arch_prctl(0x3001 /* ARCH_??? */, 0x7fffffd94760) = -1 EINVAL (Invalid argument) mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7e2044058000 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=13419, ...}, AT_EMPTY_PATH) = 0 mmap(NULL, 13419, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7e2044054000 read(3, "177ELF2113>1"..., 832) = 832 newfstatat(3, "", {st_mode=S_IFREG|0755, st_size=2220400, ...}, AT_EMPTY_PATH) = 0 pread64(3, "64@@@"..., 784, 64) = 784 mmap(NULL, 2264656, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7e2043e11000 prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0 newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=3048928, ...}, AT_EMPTY_PATH) = 0 openat(AT_FDCWD, "/dev/loop28", O_RDWR|O_CLOEXEC) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "/usr/share/locale/locale.alias", O_RDONLY|O_CLOEXEC) = 3 newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=2996, ...}, AT_EMPTY_PATH) = 0 openat(AT_FDCWD, "/usr/share/locale/en_US.UTF-8/LC_MESSAGES/util-linux.mo", O_RDONLY) = -1 ENOENT (No such file or directory) write(2, "losetup: ", 9losetup: ) = 9 write(2, "/dev/loop28: failed to set up lo"..., 41/dev/loop28: failed to set up loop device) = 41 openat(AT_FDCWD, "/usr/share/locale/en_US.UTF-8/LC_MESSAGES/libc.mo", O_RDONLY) = -1 ENOENT (No such file or directory) write(2, ": No such file or directoryn", 28: No such file or directory