Copy Linux System

繁體中文版


If you want to copy a whole disk, I would recommend using clonezilla-live to do this job. Just copy the image of the whole disk to a backup device, e.g., thumb drive, and then you can copy the image from the thumb drive to another disk. It is a complete clone of the original disk.

If you just want to expand or reduce a partition, I would recommend using gparted-live (gnome partition editor). Although gparted is very stable and safe, I still would recommend that you use clonezilla to backup the whole disk, just in case. If something goes wrong you can always restore the image back to this disk.

Just recently I have used both programs and I am very impressed. I have 4 Fedora 20 servers. So many years ago, I allocated 100M for the /boot partition and thought that would be enough. But with the size of the Linux kernel increasing all the time and then the change from grub to grub2 boot program, 100M is not enough anymore. I hesitated to do what is called a clean install of Fedora 20 as it would entail a lot of hard work. But I was afraid of manipulating partitions as this is a very dangerous work. If you are not careful enough, you could destroy all the data in the disk. But without much choice, I decided to use clonezilla and gparted. After backing up the whole disk with clonezilla, I am a little consoled that even if something happens with gparted, I could still restore the whole disk with clonezilla.

My disk is divided into 3 partitions: /dev/sda1 is /boot, /dev/sda2 is / (root) and /dev/sda3 is swap. So the first thing I did was to reduce the / (root) partition so that there is some space to expand /boot. Then I expanded the /boot partition. gparted took a lot of time to do the whole job. But when the work was done, the tense waiting period was all worth it.

2014/07/08


Two years ago you used a 20GB harddisk to install a Linux server. At that time, you felt that 20GB was huge and that you could use this for at least 10 years without any problem.

Within these 2 years of using the Linux server, due to actual needs, you have continuously updated the programs and installed new server softwares that your users required. The personnels in your company have increased and therefore you have to keeping on adding new user accounts. Two years ago, you allocated 20MB of disk space to every user thinking that 20MB for personal data was more than enough. But 2 years later you found out 20MB was not enough.

You have realized that presently 20GB of harddisk space is not enough to go around. You might be thinking of changing to a much bigger harddisk. But how do you do that?

You might be thinking that one solution to the problem is to allocate the whole 20GB harddisk to the server. Then add a new and much bigger harddisk (40GB or more) exclusively for the users. This should solve the problem. The way to do it is to move all the user accounts to the new harddisk. Moving the /home directory to the new harddisk is easy enough to do.

What you have not considered is that you might need to continue to add more server services due to [5~users' need. You still need to continuously update your existing programs. And after two years, will your 20GB harddisk be enough? Thinking over it, you might come to the conclusion that moving the whole system to a bigger harddisk is the best solution to the problem.

But how to move?

You first reaction might be to install a new Linux server in the new 40 GB harddisk. And afterwards, just move all user accounts and data from the old harddisk to the new. This should solve the problem nicely. In this way, you do not have to recreate the user accounts and data.

This way of thinking is not quite complete. True, installing a new Linux server is simple enough. And it does not take so much time. Maybe about 20 minutes is all it takes to install a new Linux system. And copying the user accounts and data from the old harddisk to the new will take just about a few minutes.

But you have missed one very important thing. Within these two years, due to bugs and security concerns, how many times have you updated your server programs? How many times have you updated the kernel? May be three or four times?

When you install a new Linux server, what you have installed is the old versions of all the programs. You need to update all of these programs. But do you remember which programs you have updated? If you have updated a certain program four times, can you now just update to the recent one directly? Most probably not. It is possible that on the second update, some configurations have changed and that the third and fourth updates presuppose these changes. If you directly update to the latest version, it is possible that problems will arise. In other words, you have to one by one update from the first, then to the second, third and fourth. You can not jump from the first to the fourth.

This, of course, takes a lot of time. If you have recorded all the changes and updates you have made, it does help a lot. But to one by one updating all the programs still takes an enormous amount of time.

Is not your present Linux system very stable? Are you not satisfied with this present system? If it is possible just to copy the whole Linux system to the new harddisk, would it not be just wonderful?

If it is Microsoft Windows system, I am not sure. But if it is a Linux system I can absolutely say that you can copy (you are right, copy) the whole system from one harddisk to another of different sizes.

I am now going to talk about this subject matter. But before doing so, I need to list some presumptions.


Note:

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:

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.
  3. 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.

    init 1

  4. 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:
  5. 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

    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
  6. 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.
  7. mke2fs    -j    /dev/sdb1
    mke2fs    -j    /dev/sdb2
  8. Format the swap
  9. mkswap    /dev/sdb3
  10. 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.
  11. mkdir    /new
    mount    /dev/sdb2   /new
    mkdir    /new/boot
    mount    /dev/sdb1   /new/boot
  12. The new harddisk has been mounted and we can start the copying. Notice: do not copy the following files and directories:
  13. 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.

    cp    -a    /boot/*    /new/boot/

    In the standard RedHat/Fedora installation, the following have to be copied to the new harddisk:

    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.

  14. 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).
  15. cd    /new
    chroot    /new
    grub
    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.

    e2label     /dev/sdb2     /
    e2label     /dev/sdb1     /boot
    mkswap   -L   swap   /dev/sdb3
  16. Now turn off the computer.
  17. halt
  18. After turning off the power, remove the connector from the old harddisk and connect it to the new harddisk. Now power on the computer.
  19. 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.

Fr. Visminlu Vicente L. Chua, S.J.
2005/03/19
Updated: 2008/12/04