Linux Loop Device – Mounting Multiple img Files as Single Loop Device

block-devicelinuxloop-device

Is there a way to take a disk img file that is broken up into parts and mount it as a single loop device?

Best Answer

I don't think you can do it in place but if you have enough space this should work:

# Create the files that will hold your data
dd if=/dev/zero of=part-00 bs=1M count=4k
dd if=/dev/zero of=part-01 bs=1M count=4k

# Create the loop devices
losetup /dev/loop0 part-00
losetup /dev/loop1 part-01

# Create a RAID array
mdadm --create /dev/md0 --level=linear --raid-devices=2 /dev/loop0 /dev/loop1

# Copy the original filesystem
dd if=original-file-00 of=/dev/md0 bs=512
# Look at the records written value
dd if=original-file-01 of=/dev/md0 bs=512 seek=<sum of records written values so far>

# Mount the new filesystem
mount /dev/md0 /mnt

You can't simply create a RAID array from the original files because the RAID disks have a specific header where the number of disks, RAID level, etc is stored. If you do it that part of your original files will be overwritten.

You can use the mdadm --build to create an array without metadata but then you really should make a backup first. Or if read-only mount is enough:

losetup -r /dev/loop0 original-00
losetup -r /dev/loop1 original-11
mdadm --build /dev/md0 --level=linear --raid-devices=2 /dev/loop0 /dev/loop1
mount /dev/md0 /mnt

Why do you want to do this? If your filesystem can't handle >4GB files you should just switch to a sane one.