Copy Linux System

Here you can find tutorials and notes for server-side maintenance/configuration

Copy Linux System

Postby lik » Thu Feb 05, 2009 7:10 am

* Linux distribution: RedHat/Fedora
* Bootloader: grub
* The current running Linux system harddisk will, from now on, be called "old harddisk."


With the arrival of kernel-2.6.20, all the harddisk device names are now /dev/sdx. It used to be /dev/hdx for ide harddisk and /dev/sdx for SCSI. The following is the old naming convention:

EIDE0 primary /dev/hda
EIDE0 secondary /dev/hdb
EIDE1 primary /dev/hdc
EIDE1 secondary /dev/hdd

With the new naming convention, the first harddisk is /dev/sda and the second harddisk (without reference to the cable connection) is /dev/sdb, etc.

You may check all attached harddisk with this command:
Code: Select all
fdisk   -l

Partition of the old 20GB:
sda1 : 50MB /boot partition
sda2 : 19.5GB / (root) partition
sda3 : 512MB swap partition

Partition of the new 40GB:
sda1 : 50MB /boot partition
sda2 : 39.5GB / (root) partition
sda3 : 512MB swap partition

Notice that although it is possible to have different partition structures between the old and new, it is much more complicated. You need to edit the /etc/fstab of the new harddisk to reflect the different partition structure. And the copying procedure and the mount points will be different. The best way is to have the same partition structures. But it is alright to have different partition sizes.

The procedure to copy one Linux system from one harddisk to another is as follows:

1. Power down the computer and then connect the new harddisk.
2. Turn on the computer to boot Linux. Login as root.
This kind of work is rather low level. It is best to do this work in single user mode. Execute the following to enter single user mode.
Code: Select all
init 1

Another one trick is to remount /home partition (for example) to the read-only and remount back to rw, after the HDD copy done:
Code: Select all
mount /home -o remount,ro

3. We will fdisk to partition the new harddisk. Notice that the partition structure of the new harddisk should be the same as the old one. Execute fdisk:
Code: Select all
fdisk /dev/sdb

You will need the following commands to partition the new harddisk:
m: print the menu of commands
p: print the current partition table
n: add a new partition
a: toggle a bootable flag
t: change a partition's system id
w: write partition table to disk and exit
q: quit fdisk

NOTE: don`t forget to set bootable flag for the boot partition for new HDD.
Additionally, you can use following command to copy/clone HDD partitions table.
First you take the original partition table:
Code: Select all
sfdisk -d /dev/sda > partition.txt

# partition table of /dev/sda (for example):
unit: sectors

/dev/sda1 : start= 63, size= 256977, Id=83, bootable
/dev/sda2 : start= 257040, size=102141270, Id=8e
/dev/sda3 : start=102398310, size= 53849880, Id=8e
/dev/sda4 : start= 0, size= 0, Id= 0

Edit the text file to match the other disk (in this example /dev/sdb).

# partition table of /dev/sdb
unit: sectors

/dev/sdb1 : start= 63, size= 256977, Id=83, bootable
/dev/sdb2 : start= 257040, size=102141270, Id=8e
/dev/sdb3 : start=102398310, size= 53849880, Id=8e
/dev/sdb4 : start= 0, size= 0, Id= 0

Then 'install' the new partition table onto the new disk.
Code: Select all
sfdisk /dev/sdb < partition.txt

Beware that the other disk must be the same size or bigger than the first disk.

Now partition the new harddisk as follows:
/dev/sdb1 50MB Linux, partition type: 83
/dev/sdb2 39.5GB Linux, partition type: 83
/dev/sdb3 512MB Linux swap, partition type: 82

4. After partitioning the harddisk, you need to format the partitions before you can use it. We will use ext3 filesystem which is the same as the old harddisk.
Code: Select all
mke2fs -j /dev/sdb1
mke2fs -j /dev/sdb2

5. Format the swap
Code: Select all
mkswap    /dev/sdb3

6. After the formatting, we can now start copying the contents of the old harddisk to the new harddisk. We now build the mount points, and then mount the new harddisk.
Code: Select all
mkdir /new
mount /dev/sdb2 /new
mkdir /new/boot
mount /dev/sdb1 /new/boot

7. The new harddisk has been mounted and we can start the copying. Notice: do not copy the following files and directories:
/proc This is a virtual filesystem
/lost+found Every partition in Linux has its own /lost+found
/new This is the mount point of the new harddisk
/.journal and /SWAP If you have these, do not copy

During boot, Linux will automatically build all the files and directories under /proc. But we need to create the mount point in the new harddisk:
Code: Select all
mkdir /new/proc

If you are using Fedora, starting at Fedora Core 2, we need to also add:
Code: Select all
mkdir /new/selinux
mkdir /new/srv
mkdir /new/sys

The copying of /boot is a bit different because /boot, in reality, is a separate partition (sdb1) and mounted at /new/boot. Since we already have mounted it, we just copy all the files and directories under it.
Code: Select all
cp -a /boot /new/boot/

In the standard RedHat/Fedora installation, the following have to be copied to the new harddisk:
Code: Select all
cp -a /aquota.user /new/
cp -a /bin /new/
cp -a /dev /new/
cp -a /etc /new/
cp -a /home /new/
cp -a /initrd /new/
cp -a /lib /new/
cp -a /mnt /new/
cp -a /root /new/
cp -a /sbin /new/
cp -a /tmp /new/
cp -a /usr /new/
cp -a /var /new/

In your own Linux system, there might be some other files and directories that need to be copied to the new harddisk. Use the command "ls -l /" (without the double quotes) to check.

We use the parameter -a of cp to preserve the permissions and ownerships of the files and directories. In this way, except for the different sizes, the content of the new harddisk is a replica of the old.
8. Since RedHat 7.2, the bootstrap loader used has been changed from LILO to GRUB. In order that the new harddisk can boot, we now need to install GRUB into the MBR (Master Boot Record).
Code: Select all
cd /new
chroot /new
> root (hd1,0)     # hd1: The second harddisk, 0: First partition
> setup (hd1)      # Install GRUB into the second harddisk's MBR
> quit                 # Get out of GRUB
exit                    # Get out of chroot

Most probably, the mount point in your /etc/fstab and /boot/grub/grub.conf is now using LABEL rather than the actual device name. If so, you need to label the partitions in the new harddisk.
Code: Select all
e2label /dev/sda1    --- show current label of sda1
e2label /dev/sdb2 /           --- set label "/" on sdb2
e2label /dev/sdb1 /boot
mkswap -L swap /dev/sdb3

9. Now turn off the computer.
Code: Select all

10. After turning off the power, remove the connector from the old harddisk and connect it to the new harddisk. Now power on the computer.
11. You should now be able to boot the new Linux system. What you have now is a fully functioning Linux system in a much bigger harddisk, but exactly the same as the old harddisk.

HDD could be copied with help of "dd" command like :
Code: Select all
init 1
dd if=/dev/hda of=/dev/hdb
init 5

Use it with caution!
Posts: 497
Joined: Wed Dec 15, 2010 3:21 am

Return to Server Side Actions


  • Related topics
    Last post