全部博文(1015)
分类:
2010-01-01 14:49:07
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.
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:
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
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
mke2fs -j /dev/sdb1
mke2fs -j /dev/sdb2
mkswap /dev/sdb3
mkdir /new
mount /dev/sdb2 /new
mkdir /new/boot
mount /dev/sdb1 /new/boot
mkdir /new/procIf you are using Fedora, starting at Fedora Core 2, we need to also add:
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.
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.
cd /new
chroot /new
grub
root (hd1,0) # hd1: The second harddisk, 0: First partition.exit # Get out of chroot.
setup (hd1) # Install GRUB into the second harddisk's MBR.
quit # Get out of GRUB.
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
halt
Fr. Visminlu Vicente L. Chua, S.J.
2005/03/19
Updated: 2008/12/04