This article is also available as a TechRepublic download.
Your server has tons of data on it. It's running Fedora Core 6. You're either running out of room, or you've noticed an eminent hard drive failure. What do you do? Since you're running Linux, you are in luck.
I ran into a very similar issue a while ago and had complete success transferring every one and zero from one hard drive onto another. Possible? Yes, but it's not for the faint of heart. All it takes is a little know-how and some faith. Here's how it works.
Adding the new drive: The physical
The first step is to connect your new hard drive. Naturally, there's no difference from a hardware perspective. You just install it like normal and boot the OS.
In Fedora Core 6, ATA drives appear as SCSI emulation (i.e., /dev/sdXX where XX is the identifier for the hard drive). Your existing hard drive will probably appear as /dev/sda. When you hook up your second drive, it's likely going to come up as /dev/sdb. In any case, your first order of business is to figure out the device node for your primary hard drive, and the device node for your second hard drive.
One very simple method of finding this information is running the dmesg command; look for any references to sda or sdb. An entry for sdb is most likely your new hard drive.
You now need to boot into single user mode, otherwise known as run level 1. At the grub boot loader, don't press [Enter] to boot. Instead press [E] before time out occurs to edit the boot configuration. Press [E] again to enter the kernel command line and append a space and the numeral one ( 1) to the end of the command line. (Note: There is a space in front of the 1.) Press [B] to boot into run level 1.
Adding the new drive: fdisk
You will use fdisk to format the second hard drive. Please use this with caution. Remember, you now have two drives hooked up. Don't fdisk the old drive, otherwise all the information you wish to transfer won't be there to transfer. So before you begin to use fdisk, verify that you're wiping out the partition table on the correct (new) hard drive.
As a quick reference, the fdisk commands are:
- b -- Enters BSD disk label command line mode
- m -- Displays help
- p -- Displays the current partition table
- d -- Deletes a partition
- n -- Creates a new partition
- w -- Writes the partition table to disk
- t -- Sets the type of partition
- L -- Displays the list of partition types
- q -- Quits fdisk
After formatting the partitions on the new hard drive, mount them under something similar to /mnt/data. You can repartition your system if you find it necessary. The partitions on the new drive do not have to be an exact match to the old drive. If you have a better layout in mind, implement it now. Over the years, I have learned that simplicity and continuity are the best policies. I tend to partition about 100-200MB for /boot and the rest goes to /. So, in my case, I'd end up mounting /mnt/data and /mnt/data/boot.
With the drive mounted, it's time to copy everything from the old drive to the new. Being old-school, I am impartial to tar. Tar tends to be more reliable for copying all the special device files and preserving all the file attributes.
Please be careful: If your files tend to have extended file attributes (if, for example, you are using SELinux), tar will not preserve them. If that is the case, you will need to install star, which is very similar to tar, only it has some extended features (such as the preservation of extended file attributes).
Of course, before doing any of the following, you should make some test copies of files with extended attributes and verify that they get properly copied. In the place of tar, use star (if you know the system needs extended attributes.)
When you're ready to go, change to the / directory, and run the command:
tar cf - bin boot etc home lib lost+found media misc [...] | (cd /mnt/data; tar xvf -)
You will need to manually list all your root directory entries in your / directory, except for /mnt. Don't include the /mnt directory, because that is where the new device will be mounted (/mnt/data), and you'll be recursively copying everything until your second hard drive is full. Instead, manually recreate any additional mount points in /mnt/data/mnt.
Note: The special mount points can be seen by using the mount command without any arguments. If you enter the command:
You'll see a listing such as:
/dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/hda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/hdc on /media/cdrom type iso9660 (ro,nosuid,nodev,user=jlwallen)
From this listing, you know to exclude proc and sys (the special mounts) from your tar copy, but you'll need to manually create an empty mount point directory in their place: /mnt/data/proc, and /mnt/data/sys. You'll also need to exclude /dev/shm and /dev/pts.
You need to copy the rest of your /dev (so it's easier to include /dev in your tar copy), then manually wipe out /mnt/data/dev/shm and /mnt/data/dev/pts, and leave empty mount point directories in their place.
Shut it all down
After you've very carefully copied everything to the new drive, taking extreme care to copy over all critical files/directories, run the shutdown command (shutdown -h now will work just fine). Pull out your existing hard drive, rejumper or reconnect your second drive if necessary, and restart your machine.
You will need to boot the installation CD and type rescue to go into rescue mode. If everything was done correctly, the installer should find the copied install image, and give you a shell prompt with your new system mounted as /.
Finish it up
Ease your mind before you turn this new drive into a production machine. Make sure that everything is in its place. Now run /sbin/grub-install to reinstall bootloader.
You are now ready to reboot. Hopefully, the new system will come up and boot without any issues.
First of all, make note of how your partitions are created now, and what you want to increase. (Don't forget the swap partition.)
Shutdown your system and install the new disk as either the secondary on the same bus as your current disk, or (preferably) as the primary disk on the secondary bus. Reboot your machine into single user mode (see above).
Check that all partitions are mounted by issuing /bin/mount -a. If they are all mounted properly, run /sbin/fdisk /dev/sdX (where X is the correct device label for the new drive.)
On the new drive, create your partitions and make sure you set the type correctly (83 = linux, 82 = linux swap).
You will format your new partitions with /sbin/mkfs -t ext3 /dev/hdcX (Where X is the correct partition number). Now, format your new swap partition with /sbin/mkswap /dev/hdcX (where X is the partition number for swap).
Dealing with partitions
Now you will create your new directories and mount them. I recommend the following:
a) mkdir /mnt/root
b) mount /dev/sdcX /mnt/root
c) mkdir /mnt/root/boot
d) mount /dev/sdcX /mnt/root/boot
e) mkdir /mnt/root/home
f) mount /dev/sdcX /mnt/root/home
Copy the data
With the new partitions created and mounted, copy your partitions -- dump/restore works very well -- but only on an idle filesystem. If you try to use dump/restore on an active filesystem (on that is mounted), it will cause you all kinds of grief (but then, so will programs like tar or star).
To use dump/restore, follow this example:
dump -0f - /dev/sdaX | (cd /mnt/root; restore -rf -)
(where /dev/sdaX is the location of your root filesystem).
dump -0f - /dev/sdaX | (cd /mnt/root/home; restore -rf - )
(where /dev/sdaX is the location of your home filesystem).
Use the above example to copy all of the drive over. Once that is complete, label your new partitions (otherwise, things will break). To do this, use the following example (changing X to the partition number on the new disk):
/sbin/e2label /dev/sdcX /
/sbin/e2label /dev/sdcX /boot
/sbin/e2label /dev/sdcX /home
Boot loader time
Install the boot loader on the new disk using the following command:
--root-directory=/mnt/root --recheck /dev/sdc
Author's Note: If you have a separate boot partition, use /mnt/root/boot.
Grub uses the actual disk name /dev/sdc, not a partition (i.e., NO /dev/sda1).
Shut it down!
Shutdown the system, remove the old disk, install the new disk as primary (on the primary controller), and reboot. You can now remove the file restoresymtable, left behind in the top directory of every filesystem by restore, if desired (not necessary, but sometimes space becomes a priority).
There you have it
You now have two proven, solid methods of dumping the contents of one drive onto another while preserving all the data of the operating system. This is a very typical system for Linux. That's why its flexibility and agility make it one of the soundest solutions for the IT industry.
Jack Wallen is an award-winning writer for Techrepublic and Linux.com. As an avid promoter/user of the Linux OS, Jack tries to convert as many users to open source as possible. His current favorite flavor of Linux is Bodhi Linux (a melding of Ubuntu and Enlightenment). When Jack isn't writing about Linux he is hard at work on his other writing career -- writing about zombies, various killers, super heroes, and just about everything else he can manipulate between the folds of reality. You can find Jack's books on Amazon, Barnes & Noble, and Smashwords. Outnumbered in his house one male to two females and three humans to six felines, Jack maintains his sanity by riding his mountain bike and working on his next books. For more news about Jack Wallen, visit his website Get Jack'd.