~2012

creating a harddrive image from a directory

Since I'm experimenting with tools for creating a minimal OS it would be handy to create a harddrive image from a directory. This is possible with a few commands.

First create an empty image using dd:

dd if=/dev/zero of=disk.img bs=1MB count=0 seek=1024

This will create a 1GB image. Just change the seek value to create an image for your size.

Next we need to format the image:

mkfs.ext4 -F disk.img

This will format the image with an ext4 filesystem. Just use mkfs.ext3 if you want an ext3 filesystem. The -F option is the magic trick so don't forget that!

Now you can mount your filesystem!

sudo mount -o loop disk.img /mnt

And put your files on it! For example:

multistrap -f multistrap.conf -d /mnt

Now you could use qemu to boot the harddrive. Unmount the drive before giving qemu control over it!:

sudo umount /mnt
qemu -kernel vmlinuz -initrd initrd -hda disk.img -m 256 -append "root=/dev/sda devtmpfs"

Enjoy