13

Are loop devices on Linux that are created with losetup supposed to be permanent? This does not seem to be the case for me. Whenever I restart my computer I see that the loop device is gone. I am using it to create loopback file drive.

How do I make sure that a loopback device is permanently stored?

3 Answers 3

13

No they are not supposed to be permanent. You can configure a script to recreate them at boot.

If you are going to mount the loopback device then probably the best idea would be to put the device on fstab file. The syntax is:

/path/to/device  /path/to/mountpoint   filesystemtype   options

In your case it becomes

/path/to/file    /path/to/mountpoint   filesystemtype   loop

note that device where your file is must be mounted before that: in some system it is sufficient to put the loopback filesystem line after the containing filesystem, but in other system this won't work because devices are mounted in parallel.

2
  • pqnet, thanks for the idea. So my loopback is mounted as btrfs. I will actually use this for another mount. My question mainly deals with luks crypt setup where one has to have a loopback device first to be able to open the encrypted container as luks device. Sorry i did not articulate it well in my question. So scripting sounds like a better idea for my particular situation
    – yarun can
    Aug 17, 2014 at 18:17
  • @yaruncan if you think about it, normal filesystems too aren't persistent between reboots: the system initialization scripts will just mount them again at boot
    – pqnet
    Aug 17, 2014 at 18:23
8

Can't add as comment to pqnet's answer due to reputation system. Thanks for the suggestion.

I added this into /etc/fstab for one of my servers,

/path/to/file /path/to/mount ext4 loop 0 0

Then auto mount all in fstab by mount -a

I can use the file like an fs now:

$ df
Filesystem                   1K-blocks       Used  Available Use%  Mounted on
/mnt/diskimg/data-u02.img    154818540     191944  153053732   1%  /u02

@yaruncan,

Don't know your OS, but one way would be to put your script on a run-level that starts before what depends on it next, i.e. whatever needs to see a loopback device.

0

If you’d like your loop device to be remounted after a reboot, then add this line line to rc.local. rc.local is obsolete but is kept for compatibility and still works in RHEL 8.

First, switch to the root user and move your loopback file to /root

run: vim /etc/rc.local

add the line: losetup /dev/loop0 /root/<diskimage.iso> save the file.

then run: chmod +x /etc/rc.d/rc.local.

Reboot and your loop image should be mounted.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .