Persistent LVM Device with Loopback Devices Using fstab

loop-devicelvm

I like to create a LVM device where physical volumes are loopback devices.

I have read lot of documents and tutorials, like this. Unfortunately all of them are based on the losetup command, which loses its configuration at next reboot.

I would make the LVM settings using FSTAB in place of the RC.LOCAL (where the losetup maybe scripted) in order to get my LVM running before some services startup, but I don't know how to reproduce into the FSTAB the command: "losetup /dev/loop0 /opt/my-data-file-0" and so on…

How could I accomplish this?

Best Answer

I have found a convenient way to do this: two SystemD services:

/mnt/systemd/system/loops-setup.service

[Unit]
Description=Setup loopback devices

DefaultDependencies=no
Conflicts=umount.target

Requires=lvm2-lvmetad.service mnt-host.mount
Before=local-fs.target umount.target
After=lvm2-lvmetad.service mnt-host.mount

[Service]
ExecStart=/sbin/losetup /dev/loop0 <LOOPBACK_FILE>
ExecStop=/sbin/losetup -d /dev/loop0

RemainAfterExit=yes
Type=oneshot

[Install]
WantedBy=local-fs-pre.target

/mnt/systemd/system/loops-fsck.service

[Unit]
Description=Loopback devices filesystem check

DefaultDependencies=no
Conflicts=umount.target

Requires=loops-setup.service
Before=local-fs.target umount.target mnt-loops-loop0.mount
After=loops-setup.service

[Service]
ExecStart=/sbin/fsck -pfv /dev/loop0

Type=oneshot

[Install]
WantedBy=local-fs-pre.target
Related Question