Home arrow Guides arrow Move Linux to a New Hard Drive
Move Linux to a New Hard Drive PDF  
Digg Reddit Stumble Upon Facebook Twitter Google Technorati Bookmark
Saturday, 23 July 2011
Let's say our hard drive is old and we want to move our Linux installation to a new hard drive.

There are multiple ways of moving your installed Linux to a new drive, we will show you only two of them. The simplest one that can be used if your new drive is the same size as your old drive would be using dd:

  dd if=/dev/sda of=/dev/sdb bs=1M

where sda is our old drive and sdb is our new drive. This command will make an identic copy of your old drive.

But what to do if our newer drive is bigger? Well, a nice solution is to create same number of partitions as old drive, but using bigger partition sizes, and then we will move data from every partition using dump and restore utilities.

Remember that /dev/sda is our old drive and /dev/sdb is our new drive.

We will look how  many partitions are on /dev/sda with fdisk

  fdisk /dev/sda

Then press p (print partitions on the screen).

We will get something like this:
fdisk /dev/sda
Command (m for help): p

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000f3ddf

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          32      248832   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              32        2611    20719617    5  Extended
/dev/sda5              32        2611    20719616   8e  Linux LVM

Then we will go to our new drive and will create three partitions, sdb1, sdb2 and sdb5.

Press n (to create new partition), choose primary and choose then partition number 1.

Use default value for First cylinder, pressing enter.

For Last Cylinder type +228M (will create a 228 MB partition for boot).

Then we will create second partition that could be extended or it coud be primary.

So we will repeat the process of creating a Linux partition, described above. Use default value for First Cylinder and default value for last cylinder (which is the end of the disk).

We will get something like this:

Command (m for help): p
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          30      24094383  Linux
/dev/sdb2              31        2610    20723850   83  Linux

We notice second partition is Linux type. We can change the type by pressing t and choosing partition number, which is 2 in our case. Then we type 8e to change partition type to Linux LVM.

To save changes and create partitions we now type w (write to disk).

Next we will format both partitions.

  mkfs -t ext2 -L boot -v /dev/sdb1
  mkfs -t ext4 -L / -v /dev/sdb2

We notice boot partition is ext2 and / partition is ext4.
-L label is used for labeling the partition.

Now we are ready to move the data using dump and restore:

We install dump package (Ubuntu and Debian):

  apt-get install dump

And last step is to move data:

  sudo -s
  mkdir /mnt/boot
  mkdir /mnt/root
  mount /dev/sdb1 /mnt/boot
  mount /dev/sdb2 /mnt/root


  (dump -0f - /dev/sda1) | ( cd /mnt/boot; restore -rf - )
  (dump -0f - /) | ( cd /mnt/root; restore -rf - )


And let's not forget to restore boot sector (MBR):

  dd if=/dev/sda of=/root/boot_sector bs=512 count=1
  dd if=/root/boot_sector of=/dev/sdb bs=512 count=1

If we want to restore grub we can use:

  grub-install /dev/sdb

That way there will be no need to restore boot sector.

That's it. Try not to boot from new hard drive.

Note
------
The best way to succesfully do a dump/restore is to put both hard drives in a different linux system so you will not do dump of a live system.

Last Updated ( Saturday, 23 July 2011 )
 
< Prev   Next >

Misc

Linux Tips

Polls

What is your favorite Linux Distribution ? (we just added Mint and reset the pool data)