Working with disk images

From Sidvind
Jump to: navigation, search

Creating disk image with partition-table from scratch[edit]

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.

Mounting disk image with partition-table[edit]

Using kpartx[edit]

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[edit]

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

Cloning disk[edit]

(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[edit]

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

Install grub onto disk image[edit]

legacy, 0.97[edit]

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)

VirtualBox conversion[edit]

From VirtualBox (VDI)[edit]

 VBoxManage clonehd MACHINE.vdi IMAGE.img --format RAW

alt

 VBoxManage internalcommands converttoraw MACHINE.vdi IMAGE.img

To VirtualBox (VDI)[edit]

 VBoxManage convertfromraw IMAGE.img MACHINE.vdi --format vdi