Google Site SearchFN Site Search FN Blog Login FN Blog Login
Site Navigation:
 
 

HOWTO: Moving a partition.

by Gavin Henry on August 30, 2004.

Introduction:

I am always running out of space in my /home directory, so I am going to share with you the best way to harmlessly move a partition.

Note:
Even if you have only two partitions on your disk, i.e. / and /swap, you can still move /home to another disk.

Planning:

You have two choices, you can either buy a new hard disk for the new partition/s or move some existing ones around. The technique can be applied to both methods, so I will only show you one, installing another disk.

Grab a pen and paper, as this makes life easier later. Run the command:

df -h
and take note of where your /home partition is currently located.

Mine is:
[ghenry@database perl]$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/hda2             2.3G  1.2G  1.1G  53% /
/dev/hda1              92M   16M   72M  18% /boot
none                  126M     0  126M   0% /dev/shm
/dev/hda9             950M   17M  886M   2% /tmp
/dev/hda5             9.2G  5.6G  3.2G  64% /usr
/dev/hda6             4.6G  837M  3.6G  19% /var
/dev/hda8             2.0G  1.2G  759M  61% /home
In my case it is /dev/hda8. I have installed a new disk which is unformatted and unpartitioned at /dev/hdb (doesn't show yet). My plan is to move /home to /dev/hdb1 on to the new disk.

Implementing:

Get the new disk ready by using fdisk. For this howto, I will be using the whole disk, which is only an old 2GB disk.

become root:

[ghenry@database perl]$ su -
Password:
[root@database root]#
Run:
fdisk /dev/hdb
Check that there is nothing on the disk by pressing p:
[root@database root]# fdisk /dev/hdb
 
Command (m for help): p
 
Disk /dev/hdb: 2162 MB, 2162764800 bytes
128 heads, 63 sectors/track, 523 cylinders
Units = cylinders of 8064 * 512 = 4128768 bytes
 
   Device Boot      Start         End      Blocks   Id  System

 
Command (m for help):
Now we can make the new partition. If you are unsure of any commands for fdisk, just press m:
Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
Type n for a new partition, hit p, then 1 (below already has the new partition, so the cylinder value is wrong for 2GB):
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-523, default 523):
Using default value 523
Now type w to write the new partition table.

Once that partition is created, we need to format it. Type:

mkfs.ext3 /dev/hdb1
Once this is done, still as root, type:
mkdir /home.new
We are now ready to copy the existing /home data across to the new partition.
mount /dev/hdb1 /home.new
cp -a /home/* /home.new
Once this is finished, we need to go to runlevel 1, as we are doing some major system changes. We can go to runlevel 1, straight away, but I think a reboot would be best. Type:
reboot
and when the grub menu comes up, press ESC and then e twice to edit the current kernel boot parameters. We want to add a 1 to the end of the kernel line, like so:
root (hd0,0)
kernel /vmlinuz-2.6.8-1.521 ro root=LABEL=/ rhgb selinux=0 1
initrd /initrd-2.6.8-1.521.img
Once done, hit Enter, then b to boot.

Now we are in single user mode, it's time to unmount the old home and mount the new. First, we need to umount home and relabel it, using e2label (man ext3 for info), to something else:

umount /home
e2label /dev/hda8 /home.old
and now make the new partition the new home and remove the mount point, as we will be using /home and not /home.new, which was temporary:
e2label /dev/hdb1 /home
mkdir /home.old
rm -rf /home.new
Once this is done, we need to edit /etc/fstab. It is already setup for home, so we only need to add the new one called /home.old:
LABEL=/                 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
none                    /dev/pts                devpts  gid=5,mode=620  0 0
LABEL=/home             /home                   ext3    defaults        1 2
LABEL=/home.old         /home.old               ext3    defaults        1 2
none                    /proc                   proc    defaults        0 0
none                    /dev/shm                tmpfs   defaults        0 0
LABEL=/tmp              /tmp                    ext3    defaults        1 2
LABEL=/usr              /usr                    ext3    defaults        1 2
LABEL=/var              /var                    ext3    defaults        1 2
/dev/hda7               swap                    swap    defaults        0 0
Now test it by mounting them both:
mount /home
mount /home.old

If that worked OK, we now have successfully completed the move, (if for some reason it hasn't, please go back through the steps above, or drop me an e-mail using the details below) Type:

telinit 5
and login as normal!!!!

Summary:

Not everyone can easily predict how much space they will need for their directory structure. I have shown you one way, by adding a new disk. I hope you can see that this is easily achievable, and the same technique can be used with only one disk. If you have say created a huge /usr directory, and have nowhere near filled it, you can swap it with /home etc.

We did this using LABELS, which is easy, but this becomes much more advanced if we had needed to resize a partition.

Well, that's it for now. For any comments or corrections, please e-mail me.