Solved dev/loopX when mounting a regular file

Solved issue

banderas20

Active Member
Joined
Aug 1, 2018
Messages
102
Reaction score
42
Credits
799
Hi!

I have created a filesystem inside a regular file:

1. Created a dummy 2GB file.
2. Created the partition with
Code:
fdisk <file>
3. Formatted it with
Code:
mkfs.ext4 <file>
4. Checked it with
Code:
file <file>
. I got the UUID.
5. Added it to
Code:
/etc/fstab
starting with the UUID.
6. Remounted all with
Code:
mount -a

But I can't find any reference to the file. Neither with
Code:
lsblk
nor
Code:
df -h
. Instead, I see a new /dev/loopX device.

I don't know how to interpret this. Moreover, am I missing something?

Thanks a lot!
 


Sounds like you've created a block device that needs to be mounted as a loop device to work on. To the filesystem it looks like it's just a file, so lsblk won't see it as as a block device since it reads from the sys filesystem. As for mounting it automatically in fstab, you could check out the loop device options in the mount man page which has a section on those, and determine if they are useful in fstab. I guess it depends on what you wish to use the block device you've created for. In my case, I mount it as a loop device when I need to and keep fstab slim for the essentials. YMMV.
 
Yes. It seems so. I don't know what a loop device is, and how it differs from other devices.

BTW, yesterday it worked. I rebooted the machine and it's unmounted. If I run sudo mount -a, I get this error:

Code:
mount: /mnt/extradisk: can't find UUID=595feec0-abc5-4821-b3ec-2acd7e597d4d

Thanks!
 
bansderas20 wrote:
I don't know what a loop device is ...
....
If I run sudo mount -a, I get this error

A loop device is a device that mounts a block device that is not an actual piece of hardware like /dev/sda (a hard disk), or /dev/sr0 (cdrom). You have created a block device in software, but it needs to behave like it's hardware, so the loop device is a means of treating the created block device as hardware, but virtually so it's virtual hardware and made to look to the system like it's hardware. It's software looped back on the system as hardware.

I assume you wanted to mount the block device you created, but mount alone won't mount it. It needs to be mounted as a loop device with a command (as root) such as:
Code:
mount -t auto -o loop=/dev/loop0 filename /mnt
or
Code:
mount filename /mnt -t auto -o loop
where filename is the name of the file which is the block device you created. You can use a full path for the filename such as /home/user/filename. It needs to have a filesystem and be in proper order to be mounted or mount will complain.

Distros differ and some distros will automatically create the loop device /dev/loop0 (and others /dev/loop#), but sometimes the devices have to be created before they are used. Run:
Code:
losetup -f
to see the first available loop device on the system.
 
Hi!

Thanks for the detailed explanation. I manage to mount it. However, I don't know how to write it in fstab to automatically mount it upon reboot.

I add an extra line like this:

Code:
2GBfile /mnt/extradisk  ext4    loop,errors=remount-rw  0       1

But I still get this error:

Code:
wrong fs type, bad option, bad superblock on /dev/loop10, missing codepage or helper program, or other error

Thanks again!
 
Given that you wish to mount your block device at boot, there a few ways to do that other than using fstab. Since you are able to mount the block device with a command, you could consider placing that command in an /etc/rc.local file which you create for the purpose. In writing that file, begin it with #!/bin/sh, include your working command on the next line, make the file executable with something like: chmod 775. In a systemd system that will cause the file to execute its command automatically at the end of the boot process and bring up the mounted device.

Another alternative to actually write a systemd service which you enable, and it will achieve the same result of bringing up the mounted device at boot if the configs instruct it to.

There aren't enough details to say why the fstab mount didn't work. From your config, the mount point /mnt/extradisk, must exist in your filesystem, and the block device must have an ext4 filesystem, but details of its creation aren't present. Unfortunately the error message is rather generalised with its text of "or other error" at the end, so it's not as helpful as it might have been.
 
Last edited:
Given that you wish to mount your block device at boot, there a few ways to do that other than using fstab. Since you are able to mount the block device with a command, you could consider placing that command in an /etc/rc.local file which you create for the purpose. In writing that file, begin it with #!/bin/sh, include your working command on the next line, make the file executable with something like: chmod 775. In a systemd system that will cause the file to execute its command automatically at the end of the boot process and bring up the mounted device.

Another alternative to actually write a systemd service which you enable, and it will achieve the same result of bringing up the mounted device at boot if the configs instruct it to.

There aren't enough details to say why the fstab mount didn't work. From your config, the mount point /mnt/extradisk, must exist in your filesystem, and the block device must have an ext4 filesystem, but details of its creation aren't present. Unfortunately the error message is rather generalised with its text of "or other error" at the end, so it's not as helpful as it might have been.

Indeed, I know the alternatives to mount it at boot time. Thanks ;)

What I wanted to know is the reason why it doesn't work in fstab.
Since it is a regular file, it doesn't show as a block device. The mount point exists and the file is formatted in ext4. In case it might help, I post the entry in the fstab.

Code:
#Regular file test
/home/user/Documents/2GBfile /mnt/extradisk  ext4    loop,errors=remount-rw  0       1

Thanks again for your time and your help!
 
The fstab file is now encouraged by its manpage to create labels or uuid for block devices instead of device names, or in your case filenames. Perhaps consider generating a uuid for your device with the uuidgen command, or a label with the e2label command. You could experiment with some more mount options like auto. The lack of precision in the error message probably calls for more experimentation. As mentioned, in my own case such devices were mounted manually so I don't quite have the experience that you are aiming to achieve and in my brief online search haven't found a resolution yet.
 
Hi osprey.

I added the absolute path after trying to use the UUID. The mount option complained about not finding that UUID. Therefore I tried with the path in FTSAB, without success.

I have other options to achieve the result, so no need to waste more time for now.

Thanks a lot for your support. Much appreciated, osprey!
 

Staff online


Top