I'm setting up a loop device for VM raw image back-file, with this command:
sudo losetup /dev/loop0 "/home/asus/VirtualBox VMs/Windows RAW/Windows 10.img"
And it works fine.
$ losetup -l
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO LOG-SEC
/dev/loop0 0 0 0 0 /home/asus/VirtualBox VMs/Windows RAW/Windows 10.img 0 512
But I want to make this change permanent, so I created this .sevice
to run at startup:
$ cat /etc/systemd/system/loops-setup.service
[Unit]
Description=Setup loop devices
DefaultDependencies=no
Conflicts=umount.target
Before=local-fs.target
After=systemd-udev-settle.service
Required=systemd-udev-settle.service
[Service]
Type=oneshot
ExecStart=/sbin/losetup /dev/loop0 "/home/asus/VirtualBox VMs/Windows RAW/Windows 10.img"
ExecStop=/sbin/losetup -d /dev/loop0
TimeoutSec=60
RemainAfterExit=yes
[Install]
WantedBy=local-fs.target
Also=systemd-udev-settle.service
Also this service starts well manually:
sudo systemctl start loops-setup
But it failed to start at startup, and shows this error:
May 11 19:57:17 ubuntu losetup[308]: losetup: /dev/loop0: failed to set up loop device: No such file or directory
I guess its because of hierarchy of services, and I should change Before
and After
options but I don't know how to do this.
Thanks in advance.
This issue is caused by two reasons:
1. As @CharlesGreen mentioned
systemd-udev-settle.service
can not be activated:So this service stays inactive:
2. The second problem is
losetup
command needs to accesshome
partition to set upWindows 10.img
. And this partition is not mounted at startup.Therefore change the service in this way, resolved problem: