Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to create an image of some directory tree that is writable directly to an 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 an 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 preformated USB stick, so there is no need for the stick to be bootable, and I would also like to make this process scriptable.

share|improve this question
up vote 5 down vote accepted

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
share|improve this answer
Worked like a charm, thanks :) – Kotte Aug 1 '12 at 12:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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