I want to create an image of some directory tree that is writable directly to a USB drive, just like the images of many Linux distributions. For example, with openSUSE you can download an ISO image that is writable directly the USB using dd.

root@computer# dd if=openSUSE.iso of=/dev/sdb

I tried to create images using mkisofs, but when I ran the dd command above with that image didn't get a partition table, which made windows not recognize the format of the drive and Linux didn't present /dev/sdb1. I also tried to create an empty file, and then create a filesystem in that file using mkfs.vfat.

There seem to be a lot of tutorials on the web on how to write images to USB drives as well as dumping a USB drive to file, but I haven't found anything on creating an image with a partition table.

The problem I'm trying to solve with this is to distribute a pre-formatted USB stick, so there is no need for the stick to be bootable; I would also like to make this process scriptable.


You can use kpartx for this. Here is a way to create a complete disk image.

# create empty image
dd if=/dev/zero of=myvm.img bs=1G count=0 seek=100

# partition the image file with fdisk/gdisk or any other tool
gdisk myvm.img

# make the partitions in the image file available as individual devices
kpartx -a myvm.img

# work with the partitions
./someprogram /dev/mapper/loop0p1

# close the partitions
kpartx -d myvm.img

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.