Ubuntu – Create loop device for ext4 filesystem

filesystemloop-devicemount

I created and formatted a file as an ext4 file system and I am trying to mount it as a loop device. Initially, there were no loopX entries in /dev, so I created one like this:

root:~# mknod -m640 /dev/loop0 b 7 0
root:~# chown root:disk /dev/loop0

The node was created:

root:~# ls -l /dev/loop0
brw-r----- 1 root disk 7, 0 Aug 26 14:32 /dev/loop0

If I try to bind the loop device to the file, an error occurs:

root:~# losetup /dev/loop0 /root/virtual.ext4
/dev/loop0: Operation not permitted

I tried to set the permissions to o777, but it does not solve the problem.

What could cause this problem, and how can I solve it?

Best Answer

You are kind of going about this in the wrong order.

Try this:

First create the loop device:

sudo losetup /dev/loop0 /root/virtual.ext4

Second, create a mount point for the device, replacing <newdir> with an appropriate directory name

sudo mkdir /mnt/<newdir>

Thirdly, mount the device. You will need to use appropriate options if you want to use this as a RW device.

sudo mount /dev/loop0 /mnt/<newdir>

Reverse the process to remove the device:

sudo umount /mnt/<newdir>
sudo rm -r /mnt/<newdir>
sudo losetup -d /dev/loop0

copied almost verbatim from http://www.walkernews.net/2007/07/01/create-linux-loopback-file-system-on-disk-file/

Related Question