How to mount a Loop Device in a Container

I referred to the solution here, but it didn’t work.

What I did

incus init images:ubuntu/noble c1
incus config device add c1 loop-control unix-char path=/dev/loop-control
incus config device add c1 loop1 unix-block path=/dev/loop1
incus config device add c1 loop2 unix-block path=/dev/loop2

incus config set c1 \
    security.syscalls.intercept.mount=true \
    security.syscalls.intercept.mount.allowed=ext4

incus start c1
incus exec c1 -- truncate -s 1GiB loop.img
incus exec c1 -- mkfs.ext4 loop.img
incus exec c1 -- mount -t ext4 -o loop loop.img /mnt

the error

mount: /mnt: failed to setup loop device for /root/loop.img

I tried enabling privileged mode with incus config set c1 security.privileged=true, but it didn’t work.

Can you show the losetup -a output?

root@c1:~# ls -l
total 33424
-rw-r--r-- 1 root root 1073741824 Apr 10 05:35 loop.img
root@c1:~# mount -t ext4 -o loop loop.img /mnt
mount: /mnt: failed to setup loop device for /root/loop.img.
root@c1:~# ll /dev/loop*
crw-rw---- 1 root disk 10, 237 Apr 10 07:22 /dev/loop-control
brw-rw---- 1 root root  7,   1 Apr 10 07:22 /dev/loop1
brw-rw---- 1 root root  7,   2 Apr 10 07:22 /dev/loop2
root@c1:~# losetup -a
root@c1:~# 

nothing

I found that this issue does not occur if the loop0 device is present within the container.

incus config device add c1 loop0 unix-block path=/dev/loop0

Ah yeah, it would start from loop0 and then go up from there.