moving/resizing partitions between drives
Posted on 2007-07-26
I've got an 80GB drive with the following partitions.
Disk /dev/hda: 81.9 GB, 81964302336 bytes
255 heads, 63 sectors/track, 9964 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/hda1 * 1 13 104391 83 Linux
/dev/hda2 14 5112 40957717+ 83 Linux
/dev/hda3 5113 5239 1020127+ 82 Linux swap / Solaris
/dev/hda1 is boot, /dev/hda2 is everything else. This box is used as a CCTV server (using Zoneminder - highly recommended btw), and the images it records are all written to a directory on /dev/hda1. This doesn't cause the disk to fill up because Zoneminder auto deletes when the free space gets below a certain percentage, but I want to move the images to a separate partition and add a much larger disk at the same time.
I think I can copy the existing partitions to a new larger disk using the following (or equivalent - here hda is the existing disk, hdb is the new larger disk - obviously I'll have to boot from a rescue cd to do this):
dd if=/dev/hda of=/dev/hdb
and then add a new partition at the end, but what I really want to do is also resize /dev/hda2 down to about 10Gb (it's currently ~40Gb) to maximise the amount of space available. I'll delete all the image files from /dev/hda2, which should bring the spaced used down to about 7Gb.
I think I can create the partitions on the new disk with the desired size using fdisk and then copy them individually. So /dev/hdb1 would be exactly the same as /dev/hda1, and /dev/hdb2 would be about 10Gb. I could then do
dd if=/dev/hda1 of=/dev/hdb1
which should work fine because the partitions are identically sized, but if I try
dd if=/dev/hda2 of=/dev/hdb2
then this will fail as hda2 is bigger than hdb2. I guess I could add conv=noerror but how do I guarantee that I'll get all the files off hda2. The total space used should be less than the size of the new partition, but is there the possibility that some files might be missed because they're located higher up the partition that the 10Gb point (remember that I've deleted all the images by this point so it's just important files left). Or is ext3 cleverer than this and will move all the files towards the start of the partition when I delete the images files. Or is there a totally different and better way to do this?