Resizing a Linux disk image file
Here’s a quick how-to on creating a larger Linux disk image file and transferring the contents of an existing image to it. For example, most of the Xen images on this site are 1 GB – but you probably would want them to be much larger if you were going to actually *use* them. Here’s what to do:
- Create a new disk image fileUsing the dd command, create a blank image file of whatever size you need (I’ll use 5 GB as an example here):
dd if=/dev/zero of=new_image.img bs=1 count=1 seek=5G
- Format the disk image
In this case, I’ll use the ext4 filesystem. You’ll get a warning because you are formatting something that is not a block device (disk). Just say yes when it asks you to proceed anyway.
mkfs.ext4 new_image.img
mke2fs 1.41.10 (10-Feb-2009)
new_image.img is not a block special device.
Proceed anyway? (y,n) y
warning: Unable to get device geometry for new_image.img
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override. - Create mount points for both the new and old images
In order to read from or write to these images, you’re going to need to mount them somewhere on the filesystem. I usually just make two directories called “old” and “new”.
mkdir old new - Mount the new and old disk images
Since we want to mount the image files as though they were actual disks, the “loop” option is needed.
mount -o loop old_image.img old/
mount -o loop new_image.img new/ - Copy the data from the old image to the new
You need to add the -r switch to the cp command in order to recursively traverse all the directories. I also use the -v (verbose) switch, so I can see the files fly by the screen as they are copied.
cp -rv old/* ./new/ - Unmount the images from the filesystem
umount old new

You can also just do this (adding 1GB):
dd if=/dev/zero bs=1G count=1 >> image.img
e2fsck -f image.img
resize2fs image.img
[Reply]
The correct cp parameter is -a (archive) which keeps all attributes of the files: time-stamp, ownership, permission, etc. Just -r does not guarantee to do all that. Also, use “old/*” may miss hidden files under “old/”.
cp -av old/. new/
[Reply]