Follow Us!

Share Us!

Find Us!

Click to open a larger map


HowTo :: Moving Your Linux System To A New Drive

This is a how-to for any linux user with moderate console skills who needs to move a linux system to a new (or different) hard drive. I’ve been meaning to blog on this for a while since I’ve re-written the instructions dozens of times over the years. I should point out that being able to move a system using the method I describe is just another reason why I like linux so much….it’s portable! This same procedure is also great for cloning lots of identical systems because it can be easily scripted.

Okay, so you have a running linux system and the drive is filling up. You have a new larger drive and you want to move your entire setup to the new drive as painlessly as possible. No problem! For the purpose of the guide we’ll assume the system is using IDE drives, the old drive is /dev/hda and the new larger drive is already installed as /dev/hdb (temporarily). We’ll also assume the partition scheme on /dev/hda includes only one ext3 formatted partition holding everything. And finally the system is Slackware (my fav!) using the more recent 2.6 kernel.

## Prepare the system to be copied and shut down any file-writing services like mysql, samba, apache and the like.

/etc/rc.d/rc.mysqld stop && /etc/rc.d/rc.samba stop && /etc/rc.d/rc.httpd stop

## Using fdisk, re-create the /dev/hda partition scheme on /dev/hdb.

fdisk -l /dev/hda # show the partition scheme on hda

fdisk /dev/hdb # create partition scheme on hdb. don’t forget to toggle /dev/hdb1 as bootable!

## Format the new partition as ext3

mkfs.ext3 /dev/hdb1

## Mount the new partition

mkdir /mnt/hdb1

mount /dev/hdb1 /mnt/hdb1

## Change directory to the root of /dev/hda1 and copy the file system using tar, excluding certain directories. Using tar preserves all file and group ownership attributes. Be patient….it will take a while depending on how much data is being copied.

cd /

tar -cvf – –exclude=/proc –exclude=/sys –exclude=/media –exclude=/mnt/hdb1 / | tar -C /mnt/hdb1 -xvf –

## Re-create the excluded directories on the new drive

cd /mnt/hdb1

mkdir proc && mkdir sys

chmod 555 ./proc && chmod 555 ./sys

## Direct Lilo to write the master boot record info to the new drive.

lilo -M /dev/hdb mbr

## Shut down the system, re-cable (or jumper-pin) your IDE drives to make the new drive /dev/hda and boot up. You should now have your system running on the new drive.

Systems can also be copied over-the-network using the same tar line above with ssh in the middle.

## Over-the-network ssh system copy. The host and remote systems are both using /dev/hda1 as the system partition.

tar -cvf – –exclude=/proc –exclude=/sys –exclude=/media –exclude=/mnt/hdb1 / | ssh -l remote_user_name remote_system_ip_address “tar -C /mnt/hda1 -xvf – ”

##

If you run into any trouble booting with the newly imaged drive there are a couple options: 1) move the drives back to the previous configuration and carefully do the procedure again or 2) boot the system to a knoppix live-cd with the new drive installed as /dev/hda, mount then chroot to the /dev/hda1 partition and re-run lilo.

As always, make backups of your critical data first and use extra caution when using disk-level tools like fdisk and mkfs. Enjoy!