Adding Disks to Linux
At some stage you'll be faced with the task of installing an additional hard drive into your Linux server. Perhaps an existing device failed, or maybe you ran out of available space. To provide more space, this section will cover adding a hard disk with only one partition and will then explain how to migrate data from the full disk to the new one.
Scenario
Things are getting crowded on bigboy: Even after you removed all unwanted data, the /var partition would still be over 95% full. You need to add a new hard drive to the system. You can verify this situation with the df -k command's output, which also shows that the other partitions are too full to accept any more data:
[root@bigboy tmp]# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hda3 505636 118224 361307 25% /
/dev/hda1 101089 14281 81589 15% /boot
none 63028 0 63028 0% /dev/shm
/dev/hda5 248895 6613 229432 3% /tmp
/dev/hda7 3304768 2720332 416560 87% /usr
/dev/hda2 3304768 3300536 4232 99% /var
[root@bigboy tmp]#
A new hard disk was added according to the manufacturer's instructions, but you now need to know how to proceed.
Determining the Disk Types
Linux stores the names of all known disk partitions in the /proc/partitions file. The entire hard disk is represented by an entry with a minor number of 0, and all the partitions on the drive are sequentially numbered after that. In the example, the system has two hard disks; disk /dev/hda has been partitioned, but the new disk (/dev/hdb) needs to be prepared to accept data.
[root@bigboy tmp]# cat /proc/partitions
major minor #blocks name
3 0 7334145 hda
3 1 104391 hda1
3 2 1052257 hda2
3 3 2040255 hda3
3 4 1 hda4
3 5 3582463 hda5
3 6 554211 hda6
22 0 78150744 hdb
[root@bigboy tmp]#
|
Linux hard disk device names follow a specific standard. SCSI disks all start with sd and IDE disks with hd. After this comes a letter that identifies the unit number of the disk, so for example, the first disk would be a, the second would be b, the third would be c, and so on. Finally, a two-digit number defines the partition number. Using this convention the fifth partition on the fourth IDE drive would be /dev/hdd5.
|
Preparing Partitions on New Disks
Linux partition preparation is very similar to that in a Windows environment, because both operating systems share the fdisk partitioning utility. The steps are:
1. | The first Linux step in adding a new disk is to partition it in preparation of adding a filesystem to it. Type the fdisk command followed by the name of the disk. You want to run fdisk on the /dev/hdb disk, so the command is
[root@bigboy tmp]# fdisk /dev/hdb
The number of cylinders for this disk is set to 9729.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help):
| 2. | Just to make sure you're on the correct device, issue the p command to print all the known partitions on the disk. In this case, there are none, which is good:
Command (m for help): p
Disk /dev/hdb: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
Command (m for help):
| 3. | The fdisk m command prints a small help manual of valid commands. You will see that n is the command to add a new partition. Add a new primary partition, number 1, and use the defaults to make the partition occupy the entire disk:
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-9729, default 1):<RETURN>
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-9729, default 9729):
| 4. | Run the print ( p) command to confirm that you successfully created the partition.
Command (m for help): p
Disk /dev/hdb: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/hdb1 1 9726 78148161 83 Linux
Command (m for help):
|
If you make a mistake, you can use the d command to delete the partition and start over. The t command enables you to change partition type from the default of 83 for regular Linux partitions to something else, such as 82 for swap space. In most cases, this won't be necessary, the default value is sufficient.
|
|
When you created the new partition, you may have noticed that fdisk queried you as to whether it was going to be a primary or secondary partition. Linux allows only four primary partitions; if you need more, you can convert one of the primary ones into an extended one. Here is an example of a partition table that includes an extended partition followed by two regular partitions within it:
Command (m for help): p
Disk /dev/hda: 7510 MB, 7510164480 bytes
255 heads, 63 sectors/track, 913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/hda1 * 1 13 104391 83 Linux
/dev/hda2 14 144 1052257+ 83 Linux
/dev/hda3 145 398 2040255 82 Linux swap
/dev/hda4 399 913 4136737+ 5 Extended
/dev/hda5 399 844 3582463+ 83 Linux
/dev/hda6 845 913 554211 83 Linux
Command (m for help):
Adding more partitions is just a question of repeating the previous steps the required number of times, while remembering that at some stage, you may need to add an extended partition.
|
| 5. | Changes won't be made to the disk's partition table until you use the w command to write, or save, the changes. Do that now, and, when finished, exit with the q command.
Command (m for help): w
Command (m for help): q
After this is complete, you'll need to verify your work and start migrating your data to the new disk. These steps will be covered next.
|
Verifying Your New Partition
You can take a look at the /proc/partitions file or use the fdisk -l command to see the changes to the disk partition structure of your system:
[root@bigboy tmp]# cat /proc/partitions
major minor #blocks name
...
...
...
22 0 78150744 hdb
22 1 78150744 hdb1
[root@bigboy tmp]#
[root@bigboy tmp]# fdisk -l
...
...
...
Disk /dev/hdb: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/hdb1 1 9729 76051710 83 Linux
[root@bigboy tmp]#
Putting a Directory Structure on Your New Partition
You now need to format the partition, giving it a new directory structure by using the mkfs command. The Fedora installation procedure defaults to an ext3 type, which is what you should use here:
[root@bigboy tmp]# mkfs -t ext3 /dev/hdb1
Next, you must create a special mount point directory, to which the new partition will be attached. Create directory /mnt/hdb1 for this purpose:
[root@bigboy tmp]# mkdir /mnt/hdb1
When Linux boots, it searches the /etc/fstab file for a list of all partitions and their mounting characteristics, and then it mounts the partitions automatically. You'll have to add an entry for your new partition that looks like this:
#
# File: /etc/fstab
#
/dev/hdb1 /mnt/hdb1 ext3 default 1 2
The first entry is the name of the partition followed by the mount point directory and the filesystem type. The fourth entry defines the mounting options, which need be only default for most scenarios. The fifth entry governs whether the dump filesystem backup command can be used for the filesystem. A value of 0 means no, and 1 means yes. The final entry defines the order in which a filesystem check is done at boot time. The check is done twice. The root (or master) filesystem has a value of 1 and is checked on the first pass, all other filesystems should have a value of 2. If you are not familiar with the /etc/fstab file use the man fstab command to get a full explanation of its various options.
You don't have to wait for a reboot to mount your partition. You can use the mount command with the -a option to read the /etc/fstab file for new entries:
[root@bigboy tmp]# mount -a
You are now able to access your new partition as device /mnt/hdb1.
Migrating Data to Your New Partition
As you remember from investigating with the df -k command, the /var partition is almost full.
[root@bigboy tmp]# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hda3 505636 118224 361307 25% /
/dev/hda1 101089 14281 81589 15% /boot
none 63028 0 63028 0% /dev/shm
/dev/hda5 248895 6613 229432 3% /tmp
/dev/hda7 3304768 2720332 416560 87% /usr
/dev/hda2 3304768 3300536 4232 99% /var
[root@bigboy tmp]#
The du -sk * command shows the disk usage of all subdirectories in a directory. You can recursively use the command by using the cd command to step down through all the subdirectories until you discover the one with the greatest file usage. In this case, you only had to go to the /var directory to see that the /var/transactions directory was the culprit.
[root@bigboy tmp]# cd /var
[root@bigboy var]# du -sk *
2036 cache
4 db
8 empty
...
...
133784 transactions
...
...
[root@bigboy var]#
As a solution, the /var partition will be expanded to the new /dev/hdb1 partition mounted on the /mnt/hdb1 directory mount point. To migrate the data, use these steps:
1. | Back up the data on the partition you are about to work on.
| 2. | Use the who command to see who's logged in. If other users are present, send a message with the wall command informing them that the system is about to shutdown.
[root@bigboy tmp]# who
root pts/0 Nov 6 14:46 (192-168-1-242.my-web-site.org)
bob pts/0 Nov 6 12:01 (192-168-1-248.my-web-site.org)
bunny pts/0 Nov 6 16:25 (192-168-1-250.my-web-site.org)
[root@bigboy tmp]# wall The system is shutting down now!
Broadcast message from root (pts/0) (Sun Nov 7 15:04:27 2004):
The system is shutting down now!
[root@bigboy tmp]#
| 3. | Log into the VGA console, and enter single-user mode:
[root@bigboy tmp]# init 1
| 4. | Rename the /var/transactions directory /var/transactions-save to make sure you have an easy to restore backup of the data, not just the tapes:
sh-2.05b# mv /var/transactions /var/transactions-save
| 5. | Create a new, empty /var/transactions directory; this will later act as a mount point:
sh-2.05b# mkdir /var/transactions
| 6. | Copy the contents of the /var/transactions-save directory to the root directory of /dev/hdb1, which is actually /mnt/hdb1:
sh-2.05b# cp -r /var/transactions-save /mnt/hdb1
| 7. | Unmount the new /dev/hdb1 partition:
sh-2.05b# umount /mnt/hdb1
| 8. | Edit the /etc/fstab file, removing our previous entry for /dev/hdb1 replacing it with one using the new mount point.
#
# File: /etc/fstab
#
#/dev/hdb1 /mnt/hdb1 ext3 default 1 2
/dev/hdb1 /var/transactions ext3 default 1 2
| 9. | Remount /dev/hdb1 on the new mount point using the mount -a command, which reads /etc/fstab and automatically mounts any entries that are not mounted already:
sh-2.05b# mount -a
| 10. | Test to make sure that the contents of the new /var/transactions directory is identical to /var/transactions-save.
| 11. | Return to multi-user mode by typing exit. The system will return to its default runlevel.
sh-2.05b# exit
| 12. | Make sure your applications are working correctly and delete both the /var/transactions-save directory and the /mnt/hdb1 mount point directory at some later date.
|
This exercise showed you how to migrate the entire contents of a subdirectory to a new disk. Linux also allows you to merge partitions together in order to create a larger combined one. The reasons and steps for doing so will be explained next.
 |