Tutorial: Adding Additional Hard Drives in Linux
1 The Theory Behind Hard Drive Management
2 Adding A Hard Drive with Existing Data
3 Adding A New (Empty) Hard Drive
4 Formatting a Drive and Wrapping Up
1。 The Theory Behind Hard Drive Management
Monday, June 3, 2002 10:36:33 AM
"How can I add one more hard disk?", "What should I do if my collegue brings a new hard disk and I have to copy my files there?", "When I was using Windows, one could see the new disk immediately and Linux is too complex!" These are some of the common questions and concerns of new Linux users, who are facing the problem of adding of a new hard drive. Some of them are so accustomed to Windows that they can not think any other way, so they come up with such statements as "I have used fdisk and format programs, but there are no such ones in Linux!"
In reality, the procedure of adding of new hard drive in Linux is not that hard and sometimes you can spend less time then you would spend to do the same thing in Windows. To illustrate this, I’ll start from the very begining to explain this procedure in detail.
First of all, I need to mention that before a users tries to add any new hard disk, he should learn the basics of what "partitioning" and "file systems" are and what is the difference between Primary, Extended, and logical partitions. You should know how to add a new disk to the computer physically (which is typically covered in the hard drive’s documentation). I’m going to assume you know these basics, because the focus of this article will be how to get Linux to behave with your new drive.
I’m going to start from the theory of the process.
There’s not much sense in diggging into the terminology and principles of adding of a new devices in UNIX systems–there are too many different UNIX versions to try that approach. We’ll just go-over Linux-specific issues.
All hard disk drives in the Linux operation system have special names, which consist of three parts, two of which are listed here:
- Two symbols "hd" or "sd" for the IDE and SCSI disk drives
- One symbol somewhere in the range from "a" to "h" for the IDE disk drives, or the range from "a" to "p" for SCSI ones.
(I have to mention that for different Linux distributions, this range could differ. For example, Red Hat Linux 7.2 uses the range "a" to "l" for the IDE disk drives, and a range from "a" to "az" (two symbol combination!) for the SCSI drives).
This second symbol defines the number of device. SCSI disk drives use sorted numbering, which depends on the ID of device. The IDE scheme differs a bit. Let’s
look at the scheme:
- a and b – Master and Slave disks at the Primary interface of the 1st IDE controller
- c and d – Master and Slave disks at the Secondary interface of the 1st IDE controller
- e and f – Master and Slave disks at the Primary interface of the 2nd IDE controller
- g and h – Master and Slave disks at the Secondary interface of the 2nd IDE controller
…and so on.
When you’re connecting a new IDE disk to the motherboard you should always be ready to remember how was it connected: which interface of what controller is being used and how was it assigned: master or slave.
When you don’t have a motherboard with four IDE controllers, or your server configuration has no additional IDE controllers, you should not have to use device names starting with "hde."
The third section of the naming scheme is:
- Number of slices (partitions) – This is a number in a range from 1 to 16 for IDE hard drives and from 1 to 15 for SCSI hard drives.
All partitions are numbered next way: 1-4 – primary and extended, 5-16 (15) – logical. So, in one system we can have not more than five primary partitions and not more than 11 (10) logical partitions on one physical hard disk.
If this number is absent, this means that this name represents the entire hard drive.
To get list of disk names in a system we need to enter this command as ‘root’ superuser for IDE drives:
# ls /dev/hd*
# ls /dev/sd*
for SCSI drives.
If you are very new to all of this, remember that this will not generate a list of connected (attached) disks but a rather a list of disk names. With this list we wil be able to determine when and what were the last disks attached. So, don’t be wondering if you get a very long list.
It’s time to do some practice now. All actions should be executed with superuser ‘root’ priveleges. Warning: you should be very accurate so as not to damage the data on your hard drives. Backing up everything would not be a bad idea, either.
Let’s take a look at some typical and usual situations.
2. Adding A Hard Drive with Existing Data
For example, your friend gave you a hard drive which was formatted in MS Windows system or even Linux, and there are some important files on this drive you want to keep.
First of all, obviously, we need to connect the hard drive physically to our PC, which for this example will be Master on Secondary interface. In this case, the disk name in the system will be hdc. After starting Linux, it is necessary to check whether Linux found the new device. For this, you can use the following command:
# ls /proc/ide
The result will be some string like:
drivers hda@ hdc@ ide0/ ide1/ piix
So, we have found device hdc in our list. Everything seems to be okay. If you did not see the device in this list, you would need to check again all steps of physically connecting the hard drive to the PC.
Let’s now take a look at what partitions we have available on the drive:
# fdisk -l /dev/hdc Disk /dev/hdc: 64 heads, 63 sectors, 787 cylinders Units = cylinders of 4032 * 512 bytes Device Boot Start End Blocks Id System /dev/hdc1 * 1 610 1229728+ 83 Linux /dev/hdc2 611 787 356832 5 Extended /dev/hdc5 611 787 356800+ c Win95 FAT32 (LBA)
It’s easy to understand that in this example we have three partitions on our hard drive, and two of them (the first and fifth) are filled with some sort of data. Also, the first partition is a primary partition of the Linux system, and the fifth is a logical disk of Windows FAT32. The first partition is a bootable (as shown be the ‘*’ symbol in the Boot column).
Now we need to attach these partitions to our file system. Such an operation of connecting partitions (to be more exact – connecting file systems) in Linux is called mounting and, as such, we use the command ‘mount’ to handle this. There is also a reverse operation known as unmounting, which we can start with the command ‘umount’.
When you mount a partition in Linux, you need to associate it with a directory somethere in the file system. This is called creating a mount point. Usually you will want to create these mount point directories for each partition, such as:
# mkdir /mnt/hdc1 # mkdir /mnt/hdc5
Of course, you can use pre-existing directories. We just need to remember a couple of rules:
- In the Linux system, it is a usual (though not necessary) practice to group all mount points in one place, except in the cases when mounting system partitions (like /usr, /home, /var, etc.);
- The directory used as the mount point should be empty, or else its contents will be unavailable until the partition is unmounted.
Now, let’s use the command ‘mount’ and attach some partitions:
# mount /dev/hdc1 /mnt/hdc1 # mount /dev/hdc5 /mnt/hdc5
Here /dev/hdc1 and /dev/hdc5 are the device names, and /mnt/hdc1 and /mnt/hdc5 are the mount points. (It goes without saying, that the directory names for the mount points don’t have to mirror the partition names exactly.
Now, when changing to the directory to either /mnt/hdc1 or /mnt/hdc5, we can see contents of partitions.
If you are mounting a partition that uses a different filesystem than the one you’re working in, it’s necessary to mention the type of file system using the key -t of the mount command. If you have already mounted the partition, do not forget to unmount the partition with the command ‘umount’ before using the -t key to establish the type of filesytem used on the "foreign" partition:
# umount /mnt/hdc5 # mount -t vfat /dev/hdc5 /mnt/hdc5
The most common values of key -t are:
- msdos – FAT12 and FAT16
- vfat – FAT16 with long file name support, FAT32
- ext2 – primary Linux file system;
- ext3 – compataible with ext2 file system with log support
- reiserfs – new Linux file system with log support
A full list of supported file systems (more than 30) can be found in the man pages of the mount command.
Some readers, after following all of the above operations, may note that instead of certain lagnage symbols appearing in the file names of a FAT file-system partition, all they get are question marks. A prime example of this are f.e. Russian symbols within filenames on a FAT file system.
This doesn’t mean that Linux won’t support the Russian language in file names. Actually, Linux supports a lot of national code pages–we just need to establish exactly which one we want to use. This is done with the key -o.
After unmounting the partition in question, use these commands. For FAT partitions created with Windows 98 and/or Windows NT:
# mount -t vfat -o iocharset=koi8-r,codepage=866 /dev/hdc5 /mnt/hdc5
For FAT partitions, created with Windows ME and/or Windows 2000/XP:
# mount -t vfat -o iocharset=koi8-r,codepage=866,uni_xlate=1 /dev/hdc5 /mnt/hdc5
The difference here is in the additional argument uni_xlate. We need that argument because Windows ME/2000/XP uses Unicode in file names.
So, now we have everything set up for Russian file names. You can use the iocharset variable for your own language needs with the preceeding commands.
For FAT partitions it is also nice to use the arguments uid and quiet. This is very important because FAT doesn’t have any services to separate or deny access to files.
The argument uid=user defines the owner of a mounted file system (user) who will have all rights to write/create new files on disk. The argument quiet hides all error messages, which will occur because it’s impossible to change file owner and file access rights once a file is copied onto a FAT partition.
3. Adding A New (Empty) Hard Drive
Let’s try to add an absolutely new hard drive in this next example.
The only difference from the previous methodology is that before mounting the new drive, we need to divide that hard drive into partitions first and format them to whatever filesystem we want to use.
To keep things simple, we’ll assume that this new hard drive is connected as the Master at the Secondary interface (hdc), just as in the prior example.
Dividing into partitions is done with the help of ‘fdisk’ utility:
# fdisk /dev/hdc Command (m for help): _
For a short manual we need to enter the ‘m’ command:
Command (m for help): m Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only) Command (m for help):
We need to know just few necessary commands: d,p,n,q,w. Don’t be afraid to use them–all your changes will be done only in memory and will only be written to the hard after the ‘w’ command is entered. If we have an error, we just need to cancel all changes with the command ‘q’.
First of all, let’s make ourself sure that disk is empty:
Command (m for help): p Disk /dev/hdc: 64 heads, 63 sectors, 787 cylinders Units = cylinders of 4032 * 512 bytes Device Boot Start End Blocks Id System Command (m for help):
Often sellers of PCs and hardware will pre-divide disks into partitions. In such cases we can use command ‘d’ (described below) to delete everything what we do not need.
So, let’s create a new parititon (with command ‘n’), answering all the questions as we go:
Command (m for help): n Command action e extended p primary partition (1-4)
Choosing the partition type– primary or extended–we will enter P for primary.
In choosing number of the partition in this example, we will assign all of the all free space to one partition. Because this will be the first and only existing partition, we will assign the number 1:
Partition number (1-4): 1
When setting the number of the first cylinder of the partition, we can just use the default value, which is recorded when we just press Enter:
First cylinder (1-787, default 1): Using default value 1
Setting the number of the last cylinder of the partition is easy in this example, since we are filling the entire drive with one partition. Again, just press Enter for the default (instead of the number of the cylinder, we can set size of partition in bytes, kbytes, or megabytes):
Last cylinder or +size or +sizeM or +sizeK (1-787, default 787): Using default value 787 Command (m for help):
Now, partition is created. We can take a look of new table of partitions with the help of the ‘p’ command:
Command (m for help): p Disk /dev/hdc: 64 heads, 63 sectors, 787 cylinders Units = cylinders of 4032 * 512 bytes Device Boot Start End Blocks Id System /dev/hdc1 1 787 1586560+ 83 Linux Command (m for help):
If we don’t like (for some reason) the location or number of the partition, we can delete it using ‘d’ command:
Command (m for help): d Partition number (1-4): 1 Command (m for help):
If you want to divide the disk into a few partitions, you will need to repeat all these operations a few times. One thing that is important to remember is that if the amount of partitions is greater than four, you’ll need to create not primary, but an extended partition across the whole available disk space. Inside this partition you can create as many logical parts as needed.
Remember, please, that logical partitions always should be numbered from 5 upwards; it doesn’t matter how many primary partitions were created before (one, three, or none at all).
The type of the newly created partition on a new hard drive will always be set to 83 (Linux type). The ‘fdisk’ utility supports control and creation of numerous partition types, though. We can get a list of them using the command ‘l’. We also can change the type of partition using the command ‘t’. I absolutely do not recommend beginners should use this ability in a Linux system; better they should use only two types — 83 (Linux) and 82 (Linux swap).
After all manipulations with ‘fdisk,’ we will get needed configuration. Now we should quit ‘fdisk’ and save all results using the command ‘w’.
We do not need to reboot our PC. Sometimes, to tell the truth, ‘fdisk’ is unable to update the table of partitions in memory (such as in, for example, the latest versions of Red Hat Linux), but it will report about such problem. If this happens, then you will have to reboot the PC.
Of course, before using a partition, you need to format it. Keep in mind that, since we need to work with partitions on hard disk, we be working with with the device /dev/hdc, but we will format the partition /dev/hdc1.
We can accomplish this formatting with the ‘mkfs’ utility. Usually for Linux partitions, we will use one of three file systems: ext2, ext3, or reiserfs. We
also have the option to use several others, like ext, XFS, JFS. But ext is too old, and is not in much use anymore, and XFS and JFS installation requires deep knowledge of setup and configuring the Linux operation system.
We can execute the following command to format the partition with the ext2 file system:
# mkfs -t ext2 /dev/hdc1
(key ‘-t’ points to the type of file system). To format as ext3:
# mkfs -t ext2 -j /dev/hdc1
This is not a typo, by the way. We are setting the type to ext2. But since ext3 is an updated "child" of ext2, it has forward and backward compatibility with its "parent".
To format a partition with ReiserFS:
# mkfs -t reiserfs /dev/hdc1
Once ‘mkfs’ has finished its work, our partition is ready to be mounted.
All of the methods described above have one rather unfortunate drawback: after each reboot of the system, you need manually mount partitions again as root. Ideally, you will want automated mounting on PC startup.
This is easily done. In the system configuration directory /etc there is a file named ‘fstab’. Inside this file is a list of connected partitions, which looks something like this:
# cat /etc/fstab /dev/hda1 / reiserfs notail,usrquota,grpquota 1 1 none /dev/pts devpts mode=0620 0 0 none /dev/shm tmpfs defaults 0 0 /dev/hdb /mnt/cdrom auto user,iocharset=koi8-r,umask=0,exec,codepage=866,ro,noauto 0 0 /dev/fd0 /mnt/floppy auto user,iocharset=koi8-r,umask=0,sync,exec,codepage=866,noauto 0 0 none /proc proc defaults 0 0 /dev/hda5 swap swap defaults 0 0
Each string of this file keeps a description of each partition for mounting and has six columns (fields):
- Name of device
- Mount point
- Type of file system
- Additional arguments of mount. Some of them are common for everything, some of them are specific for this or that file system
- ID number in query to checkup of integrity of file system on PC startup. Such checking works periodicaly. If this id number is 0, no checking will be done
- Backup value (0 or 1). This argument is used by some backup software utilities.
Let’s come back to our example. Specifically, partition hdc1 was formatted as ext3 and it needs to be mounted on the /mnt/hdc1 mount point during PC startup. To accomplish this, we will add the following string to the end of the file /etc/fstab:
/dev/hdc1 /mnt/hdc1 ext3 default 2 1
This is very important: do not forget to add a "carrier return" (break line, enter, etc.) at the end of /etc/fstab file. Otherwise, the last string in the file will be ignored! (Actually, this doesn’t matter in the latest versions of Linux. But get in the habit anyway, to avoid problems on PCs with other Unix software or older versions of Linux).
Now, we need make sure: did we edit everything right in fstab?
# mount /mnt/hdc1
If we don’t get any error messages that means this partition will be automatically mounted upon every startup.
Notice, by the way, this other (short) syntax of the ‘mount’ command. When we are using only one argument (mount point) all the needed information for ‘mount’ was derived from /etc/fstab.
In this article, we examined a few variations and rules for connecting new hard drives in Linux. Note that we did not cover a lot of the specialized fine-tuning that can be done in some of the more advanced operations, such as tuning file systems with log support, defining access rights to mounted partitions, and the like. Hopefully you will take from this article is a clearer understanding of the principles behind adding a hard drive to a Linux system.