[CentOS] resize an image file

William L. Maltby CentOS4Bill at triad.rr.com
Thu Nov 22 15:08:08 UTC 2007

On Wed, 2007-11-21 at 13:26 -0500, Jerry Geis wrote:
> I have a 100G disk on an old redhat 7.3 system.
> Filesystem            Size  Used Avail Use% Mounted on
> /dev/hda1             9.6G  2.4G  6.7G  27% /
> /dev/hda3              99G  6.1G   88G   7% /home
> hda2 is 2G swap
> I am trying to back that complete image up on my centos 5 system.
> I can do the dd if=/dev/hda bs=1M | ssh root at machine 'cat > disk.img'
> which gets me the whole 100G.
> As you can see most of the disk is unused.
> Is there a way to trim the resulting image to only be 10G instead of 100G?

Presuming that it is the data you really want and the file system meta-
data and overhead are irrelevant...

One of the respondents suggested tar, but I'm partial to cpio. Other
than that, it's pretty much as was suggested.

On the target machine, make a 10G file using dd with input of /dev/zero
and output of your file name. Then use the losetup command to associate
a loopback device with it. Then mke2fs on it, mount it and transfer the
files in, using a cpio via ssh instead of tar.

As another thought, what about resizing the target HD partition itself?
If resize2fs can be used, hda3 can then be reduced (just don't move the
starting point), an extended partition can be added and new logical
partitions installed (hda4 might have hda5, hda6, etc.).

This would have the advantage of no handstands, loopback files, etc. If
the target system has LVM, things get easier.

> Thanks,
> Jerry
> <snip sig stuff>


More information about the CentOS mailing list