Friday, 12 June 2009

Create Disk Image

Copy Physical Disk

To create a disk image from an actual disk is relatively straightforward - a simple dd command from the physical disk to file. For example from /dev/sda to harddisk.img:

sudo dd if=/dev/sda of=harddisk.img

(Warning: do not mix these up. The reverse is seriously damaging.)

Make New Harddisk Image

However, to create a complete disk image from scratch is less trivial; but of course possible. The following script demonstrates the series of commands which is no more than a batch of simple commands with the added ability to take an argument for the size in MB of the size of disk required (e.g. to create a 100MB disk: ./makediskimage.sh 100). It creates a single bootable FAT32 partition with syslinux. Other configurations are equally possible.

# makediskimage.sh

sizeMB=$1

size=$(echo $(($sizeMB*1024*1024/512)))
# set size of disk
dd if=/dev/zero of=harddisk.img bs=512 count=$size
# equivalent to: qemu-img create -f raw harddisk.img 100M
parted harddisk.img mktable msdos
# create partition table
parted harddisk.img "mkpart p fat32 1 -0"
# make primary partition, type fat32 from 1 to end
parted harddisk.img mkfs y 1 fat32
# make fat32 filesystem on partition 1, without confirmation
parted harddisk.img toggle 1 boot
# make partition 1 bootable
parted harddisk.img unit b print
# list partition table (in bytes)
offset=$(parted harddisk.img unit b print | tail -2 | head -1 | cut -f 1 --delimit="B" | cut -c 9-)
# get offset
# sometimes 512, 16384 or 35226 (512 bytes per unit by 63 cylinders)
sudo syslinux -o $offset harddisk.img
# add boot code to partition 1
dd if=/usr/lib/syslinux/mbr.bin of=harddisk.img conv=notrunc
# copy master boot record to disk

Mount Disk Image

To mount a partition image is straightforward, for example:

sudo mount partition.img /mnt/diskimage

To mount a partition on a complete disk image it only requires an offset to the partition to mount (here on a temporary mount point):

mkdir /tmp/harddisk.img
sudo mount harddisk.img /tmp/harddisk.img -o loop,offset=
16384 # 512*32 cylinders

QEMU

Qemu is the opensource virtualisation software - capable of boot raw disk images and others. To install, include graphical launcher (useful for learning commands):

sudo apt-get install qemu qemu-launcher

To boot harddisk.img with Qemu:

/usr/bin/qemu -boot c -m 512 -hda 'harddisk.img' -net nic,vlan=0 -net user,vlan=0 -localtime

LIVE CD/USB

To add the option of booting into a harddisk image to a live CD/USB boot, for example Clonezilla on an image called clone.img, add to the following lines to syslinux.cfg:

label clonezilla
menu label ^Clonezilla
kernel memdisk
append initrd=/clone.img

(Note: Clonezilla at present needs it's own syslinux and mbr.bin as provided in the utils directory in order to boot properly)

For Syslinux boot disk images can also be compressed with gz to save space:
gzip harddisk.img
mv harddisk.img.gz harddisk.imz

Change the line in syslinux.cfg to:
append initrd=/clone.imz

0 comments:

iantheteacher

Planet ILUG