2

I have two files diskimage.part1 and diskimage.part2 on an external slow (USB 2) hard drive and I need to do the equivalent of

cat diskimage.part{1,2} > diskimage
mount -o ro,loop diskimage /tmp/mountpoint

but the cat will take hours to complete and the disk image does not fit anywhere because of its size.

Is there a "lazy version of cat" that I could use with this disk image? Something like

losetup --readonly /dev/loopX --concatenate diskimage.part*
mount -o ro /dev/loopX /tmp/mountpoint

(But losetup --concatenate doesn't exist)

0

3 Answers 3

2

May be you want to use mdadm

$ losetup --readonly /dev/loop1 diskimage.part1 
$ losetup --readonly /dev/loop2 diskimage.part2 
$ mdadm --create /dev/md0 --level=linear --raid-devices=2 /dev/loop1 /dev/loop2
$ mount -o ro /dev/md0 /tmp/mountpoint
0

In case there's at least enough free space for diskimage and diskimage2 you could append the latter to the former file

cat diskimage.part2 >> diskimage.part1 && mv diskimage.part1 diskimage

It will also be faster than copying both because you need not move that many data. But using another fast device for the concatenation is preferable because of speed considerations.

In case you have not enough free space just use any other (maybe external) device to create the diskimage file.

1
  • Thank you! I have to work around serious space and speed limitations, which is a PITA, and I'm not eager to change diskimage.part1 (even though appending something can be undone easily). I think that mdadm, proposed by miline, will work for me. Mar 11, 2015 at 12:33
0

mdadm --create wanted to write on the loop devices, and I was nowhere brave enough to see if it would work(probably not), so I found (what I think is) a better solution, using dmsetup, which makes sense, LVMs seem to be a better fit for the job:

losetup --read-only /dev/loop1 diskimage.part1 # For this example, 5120 bytes
losetup --read-only /dev/loop2 diskimage.part2 # For this example, 2560 bytes
echo -e "0 10 linear /dev/loop1 0 \n10 5 linear /dev/loop2 0" | dmsetup create merge0
mount -o ro /dev/mapper/merge0 /tmp/mountpoint

Clarifying: dmsetup is the command for managing LVMs. when creating a new mapped device, you use dmsetup create <dev_name>, and it also needs a table describing the mapping. By default, the table comes from standard input.

The table consists of lines of the form start_sector number_sectors target_type [target_type_arguments]. Sectors are made of 512 bytes. In this example, we use the linear mapping, which gets as arguments the file to map to, and the offset of the file to map, also in sectors. In this example, we assume we don't want skip anything in the files, so they are set to zero.

The command then means that for the new device, merge0, the first 10 sectors will map to /dev/loop1, and the next 5 will map to /dev/loop2.

The new device will be available at /dev/mapper/merge0. Remember to run partprobe on it to read partitions, if your images contain any.

Further information can be found here (and specially in the section TABLE_FORMAT), and some REALLY helpfull examples can be found here.

Some thanks to chatGPT for pointing me the right way, although it did give me a completely broken command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .