Permanent loop device?

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?


Solution 1:

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.

Solution 2:

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.