Working with disk images

Creating disk image with partition-table from scratch
A sparse image can be created using dd:

# dd if=/dev/zero of=/PATH/TO/IMAGE bs=1024k seek=NUM_MB count=0

Create partition-table and partitions:

# fdisk /PATH/TO/IMAGE

To create filsystems use kpartx (see below regarding mounting) to setup loopback devices for the partitions. It is possible using losetup but it requires passing the size to mkfs and is generally much more difficult.

Using kpartx
kpartx is part of multipath-tools.

# kpartx -av /PATH/TO/IMAGE add map loop0p1 (253:8): 0 204800 linear /dev/loop0 2048 add map loop0p2 (253:9): 0 20273152 linear /dev/loop0 206848

Partitions can now be accessed using /dev/mapper/loop0p1 etc.

# mount /dev/mapper/loop0p1 /mnt/...

Detatching devices.

# kpartx -dv /PATH/TO/IMAGE

Using losetup manually
# losetup -f /dev/loopN # losetup -o $((512*SECTOR)) /dev/loopN /PATH/TO/IMAGE # mount /dev/loopN /mnt/...

Cloning disk
(adapt blocksize to fit drive)

# fdisk -l > /PATH/TO/IMAGE.fdisk # dd if=/dev/DISK of=/PATH/TO/IMAGE bs=4096 conv=noerror,sync

Over network
On receiver

# nc -l -p 4711 | dd of=/PATH/TO/IMAGE bs=4096

On sender

# dd if=/dev/DISK bs=4096 conv=noerror,sync | nc IP 4711

legacy, 0.97
Make sure /boot/grub contains all required files (including stages)

# grub --no-floppy grub> device (hd0) /PATH/TO/IMAGE grub> geometry (hd0) 4096 16 63 grub> root (hd0,0) grub> setup (hd0)

From VirtualBox (VDI)
VBoxManage clonehd MACHINE.vdi IMAGE.img --format RAW

alt

VBoxManage internalcommands converttoraw MACHINE.vdi IMAGE.img

To VirtualBox (VDI)
VBoxManage convertfromraw IMAGE.img MACHINE.vdi --format vdi