You are not logged in.

#1 2012-09-25 10:52:52

hungerfish
Member
Registered: 2009-09-13
Posts: 254

losetup and mount during boot

I want to use losetup to map a disk image file to a loopback device (dev/loop0) during boot.
I would also want to mount this device as my home partition.
The disk image is not located on the root partition, but on another separate partition.

I can do this manually by using following commands:

losetup --find --show /mnt/data/home-flat.vmdk
losetup -o 32256 /dev/loop1 /dev/loop0
mount /dev/loop1 /home

The reason I need this, is that I have a dualboot setup (windows,archlinux) but also want access to an arch system via a virtual machine from within windows.
So I basically have two arch-installations, which share a home partition(the disk image above).
I cannot just map a real partition for the vm, because my disk has a gpt partition map and vmware currently does not support that.

I've looked at creating a custom hook for this, but I am at a loss, especially because the disk image is itself located on a (ntfs)partition,
which itself needs to be mounted before executing the losetup commands.

Any ideas?


Beetles and bacteria are vastly more successful than humans in terms of survival.

Offline

#2 2012-09-28 20:57:55

johni
Member
Registered: 2012-02-03
Posts: 102

Re: losetup and mount during boot

Are you using the init scripts, or systemd?

If you are using initscripts, you can add an early hook into the init bootstrap.

Create a script in /etc/rc.d/functions.d

Call the script something like "doloops"

The script should look something like this:

# Loop device hook

do_loop()
{
    mount /dev/your_ntfs_partition /mnt/data
    losetup --find --show /mnt/data/home-flat.vmdk
    losetup -o 32256 /dev/loop1 /dev/loop0
}

add_hook sysinit_premount do_loop

Then just add the /dev/loop1 /home mount to /etc/fstab and let the system mount it as normal.

Note: If /mnt/data is being mounted in /etc/fstab, comment it out since we are having to mount it early to setup loop devices.


If you are using systemd, you can take the same script, and put it somewhere else like /usr/lib/systemd/scripts, and remove the line reading "add_hook sysinit_premount do_loop", and just change it to "do_loop".

Then add a service file to call it early in the boot process.  Make it something like /usr/lib/systemd/system/doloops.service

[Unit]
Description=Setup loop devices
DefaultDependencies=false
ConditionFileIsExecutable=/usr/lib/systemd/scripts/doloops
Before=local-fs.target
After=systemd-udev-settle.service
Required=systemd-udev-settle.service

[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/doloops
TimeoutSec=60
RemainAfterExit=yes

[Install]
WantedBy=local-fs.target
Also=systemd-udev-settle.service

Enable the service in systemd:
systemctl enable doloops.service

Offline

#3 2012-09-29 19:43:03

hungerfish
Member
Registered: 2009-09-13
Posts: 254

Re: losetup and mount during boot

Thanks for the help.
I'm still using the init scripts, but will be migrating soon, so now I know where to look come the time smile

Your solution however doesn't quite work, as I had to add

mount /dev/loop1 /home/

to /etc/rc.local instead of /etc/fstab , because (I assume) losetup hadn't finished by the time fstab gets parsed.
This isn't ideal, but it certainly works, so again thank you for your post!

EDIT:
I've now noticed some warning/error messages during shutdown (on metal) related to the loop device, they however vanish too quickly, and I can't seem to find them in syslog. Are they even stored? Is there a way I can freeze or delay power cycling long enough to be able to read them?
EDIT2:
Ok, I found that hitting scroll-lock pauses during boot/shutdown, so I was able to read:

loop: Write error at byte offset xxx, length 4096
Buffer I/O error on device loop0
Buffer I/O error on device loop1
JBD2 Error -5 detected when updating journal superblock

This pattern repeats a few times, always at different 'offsets'.

Running fsck.ext4 on the image file seems fine though, and I'm not experiencing any data loss symptoms (yet), still any ideas?
EDIT3:
So I just had a really bad crash (running as vm), after which I needed to manually fsck and repair the filesystem on the virtual disk. So I guess I'm missing something... sad

Last edited by hungerfish (2012-09-29 21:39:46)


Beetles and bacteria are vastly more successful than humans in terms of survival.

Offline

#4 2012-09-29 21:51:45

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: losetup and mount during boot

Why isn't this just an fstab entry?

/mnt/data/home-flat.vmdk    /home    auto    loop,offset=32256    0 2

Offline

#5 2012-09-30 09:37:30

hungerfish
Member
Registered: 2009-09-13
Posts: 254

Re: losetup and mount during boot

That would seem much more simple smile
However, it doesn't work. sad

NTFS signature is missing.
Failed to mount '/dev/loop0': Invalid argument
The device '/dev/loop0' doesn't seem to have a valid NTFS.
Maybe the wrong device is used? Or the whole disk instead of a
partition (e.g. /dev/sda, not /dev/sda1)? Or the other way around?

home.flat.vmdk is of course a virtual disk (with one partition), so the error message is spot on in this case.
But now I'm confused. Why does the 'two-stage' approach work, and why is just specifying an offset to mount from not enough?


Beetles and bacteria are vastly more successful than humans in terms of survival.

Offline

#6 2012-10-01 15:09:03

johni
Member
Registered: 2012-02-03
Posts: 102

Re: losetup and mount during boot

Sorry for the delayed response.  I've been away from the computer all weekend.

hungerfish wrote:

Thanks for the help.
I'm still using the init scripts, but will be migrating soon, so now I know where to look come the time smile

Your solution however doesn't quite work, as I had to add

mount /dev/loop1 /home/

to /etc/rc.local instead of /etc/fstab , because (I assume) losetup hadn't finished by the time fstab gets parsed.
This isn't ideal, but it certainly works, so again thank you for your post!

Hmmm.   The "sysinit_premount" parameter to add_hook should have made this happen before
/etc/fstab is even parsed.

Can you show your /etc/fstab entry?

Ok, I found that hitting scroll-lock pauses during boot/shutdown, so I was able to read:

loop: Write error at byte offset xxx, length 4096
Buffer I/O error on device loop0
Buffer I/O error on device loop1
JBD2 Error -5 detected when updating journal superblock

This pattern repeats a few times, always at different 'offsets'.

Maybe since the file is on an NTFS partition (I assume ntfs-3g?)  Perhaps it killed off the
fuse module that kept NTFS volume mounted before unmounting /home, or removing the
loop devices. Just a guess on that one.


I have used the technique I describe before to pre-setup loop devices for old loop-AES volumes, and it
worked for me.

EDIT3:
So I just had a really bad crash (running as vm), after which I needed to manually fsck and repair the filesystem on the virtual disk. So I guess I'm missing something... sad

Is it possible you suspended the VM rather than shutdown before mounting on linux?  That might cause this.

Offline

#7 2012-10-01 15:11:27

johni
Member
Registered: 2012-02-03
Posts: 102

Re: losetup and mount during boot

falconindy wrote:

Why isn't this just an fstab entry?

/mnt/data/home-flat.vmdk    /home    auto    loop,offset=32256    0 2

Falconindy's suggestion is a much easier approach and one I didn't even think of.
Maybe it is trying to mount home before mounting /mnt/data?   Possible just rearranging /etc/fstab could make this work.

Offline

#8 2012-10-01 15:25:00

hungerfish
Member
Registered: 2009-09-13
Posts: 254

Re: losetup and mount during boot

I'll be away from home this week, so I'll have to get back to this...but I think you may be right about the fuse-module thing.
You were definitely right about that I suspended the vm, well actually I didn't, but vmware-player(v5 seems to be quite unstable,imo) crashed and I totally forgot about it doing so.
I did use the right order in fstab though, so it seems there is a difference between just mounting the image at an offset, and 'double-looping' it.


Beetles and bacteria are vastly more successful than humans in terms of survival.

Offline

Board footer

Powered by FluxBB