Chinaunix首页 | 论坛 | 博客

qsh

  • 博客访问: 4056216
  • 博文数量: 1015
  • 博客积分: 15904
  • 博客等级: 上将
  • 技术积分: 8572
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-04 19:16
文章分类

全部博文(1015)

文章存档

2019年(1)

2017年(1)

2016年(19)

2015年(27)

2014年(30)

2013年(95)

2012年(199)

2011年(72)

2010年(109)

2009年(166)

2008年(296)

分类:

2010-01-01 14:49:07

Copy Linux System

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

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

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.

    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

  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:

    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
  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.
    mke2fs    -j    /dev/sdb1
    mke2fs    -j    /dev/sdb2
  5. Format the swap
    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.
    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. 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:
      mkdir    /new/proc
      If you are using Fedora, starting at Fedora Core 2, we need to also add:
      mkdir    /new/selinux
      mkdir    /new/srv
      mkdir    /new/sys
    • /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

    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.

  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).
    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
  9. Now turn off the computer.
    halt
  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.

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

阅读(939) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~