1. Home
  2. Tutorials
  3. Add a Drive
Yolinux.com Tutorial

Add an Additional Disk Drive to Your Linux Computer

Prerequisites and Drive Types:

Prerequisite: This tutorial covers adding a new disk drive to your linux computer. First it is assumed that the hard drive was physically added to your system.

SATA drives are connected via a dedicated cable of seven conductors of which there are two pairs dedicated to data with the remaining 3 being ground. SATA drives represent the predominat and current technology.
Linux SATA naming convention: /dev/sda, /dev/sdb, ...

IDE based systems, can support two drives on each ribbon cable. The cable is attached to either the Primary or Secondary IDE controller. A "jumper" is pressed onto two pins (thus connecting the two pins) on the drive to define the drive as a "Master" or a "Slave" drive. Each cable can support one master and one slave drive. Typically new desktop systems have one hard drive connected as a Master on the Primary controller and one CD-Rom on the second cable configured as a master.
Linux IDE naming convention: /dev/hda, /dev/hdb, ...

SCSI drives will have jumpers positioned to assign a SCSI device ID number typically numbered 1-8. A sticker on the top of the drive will often show a diagram of jumper placement for drive assignment.
Linux SCSI naming convention: /dev/sda, /dev/sdb, ...

Linux Hard Drive Naming Convention:

IDE drives are referred to as hda for the first drive, hdb for the second etc...IDE uses separate ribbon cables for primary and secondary drives. The partitions on each drive are referred numerically. The first partition on the first drive is referred to as hda1, the second as hda2, the third as hda3 etc ...

Linux IDE naming conventions:

Device Description Configuration
/dev/hda 1st (Primary) IDE controller Master
/dev/hdb 1st (Primary) IDE controller Slave
/dev/hdc 2nd (Secondary) IDE controller Master
/dev/hdd 2nd (Secondary) IDE controller Slave

Note: SCSI disks are labeled /dev/sda, /dev/sdb, /dev/sdc etc... to represent the first, second, third,... SCSI hard drive devices but not the SCSI ID. SCSI hard drive partitions are represented by an additional number. i.e. First drive first partition, /dev/sda1, second partition, /dev/sda2,... Other SCSI devices such as tape backup are labeled /dev/st0 for the first, /dev/st1 for the second and so forth. See YoLinux SCSI tutorial for more info.

Disk Partition Notes:

  • Partitions are defined and generated with fdisk
  • Each hard drive may only have a maximum of four primary partitions (MBR limit: 1-4). One can add more partitions using extended partitions. Multiple logical partitions can then be added to each extended partition (5-20).
  • Extended partitions allow one to place up to 24 partitions on a single drive.
  • One may only boot an OS from a primary partition. A computer system may have multiple drives with primary partitions but only one primary partition may be active on one drive only. The active primary partition is used for booting the system and is referenced by the Master Boot Record (MBR).
  • Creating a primary partition:
    • [root]$ fdisk /dev/sda
    • n (add a new partition)
    • p (new partition will be a primary partition. Options are e or p)
    • 1 (define partition number. 4 primary partitions allowed)
  • Creating and extended partition containing logical partitions
    • [root]$ fdisk /dev/sda
    • n (add a new partition)
    • e (new partition will be an extended partition. Options are e or p)
    • w (Write and save partition table)
    • n (add a new partition)
    • l (new partition will be a logical partition. Options are l or p)
    • Define sector or accept default (first free sector)
    • Define last sector or sector size in (K)ilobyte (M)egabytes, (G)igabytes or accept default to use up the remaining space on drive.
    • w (Write and save partition table)
Command and Response Dialog of Adding a New SATA Drive:

As root perform the following: (as highlighted in bold)

[root]# fdisk /dev/sdb
Command (m for help): m     (Enter the letter "m" to get list of commands)
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): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-9729, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-9729, default 9729):
Using default value 9729

Command (m for help): w    (Write and save partition table)

[root]# mkfs.ext4 -L disk2 /dev/sdb1
Example: RHEL6

Note:
  • Partition size: K = Kilobyte M = Megabyte, G= Gigabyte
    Example, Last cylinder expressed in size: +500M
  • If replacing a failed drive you will probably be in single user mode with a read only drive and may want to match the previous UUID so it boots seamlessly:
    mkfs.ext4 -L disk2 -U <uuid> /dev/sdb1
    where the the UUID is obtained by looking in /etc/fstab
    This will allow the replacement drive to mount just as if it was the old drive it is replacing.
    Example /etc/fstab entry with a UUID:
    UUID=7cb6e598-f639-488e-85c3-2a09d1440008 /share ext4 defaults 1 1

[Potential Pitfall]: If you get the following mkfs ext4 formatting error:
mkfs.ext4: inode_size (128) * inodes_count (0) too big for a  
    filesystem with 0 blocks, specify higher inode_ratio (-i)  
    or lower inode count (-N) 
This occurs when you try and format an extended partition directly rather than a primary partition. Unlike the IDE example below with ext3 (RHEL5), this SATA ext4 configuration requires a primary partition. The extended partition contains its own partition table and is a container for other logical partitions. To format the extended partition, add logical partitions to the extended partition and format these logical partitions.

Command and Response Dialog of Adding a New IDE Drive:

As root perform the following: (as highlighted in bold)

[root]# fdisk /dev/hdb
Command (m for help): m     (Enter the letter "m" to get list of commands)
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): n
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4): 1
First cylinder (1-2654, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-2654, default 2654):
Using default value 2654

Command (m for help): p

Disk /dev/hdb: 240 heads, 63 sectors, 2654 cylinders
Units = cylinders of 15120 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hdb1             1      2654  20064208+   5  Extended

Command (m for help): w    (Write and save partition table)

[root]# mkfs -t ext3 /dev/hdb1
mke2fs 1.27 (8-Mar-2002)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
2508352 inodes, 5016052 blocks
250802 blocks (5.00%) reserved for the super user
First data block=0
154 block groups
32768 blocks per group, 32768 fragments per group
16288 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root]# mkdir /opt2
[root]# mount -t ext3 /dev/hdb1 /opt2
Example: RHEL5

The above example shows the addition of a drive as one whole extended partition used to extend the storage space of the system. It was not created to hold additional operating systems as this would require a primary partition. Primary partitions can be used to extend the storage space of the system as well. It is not precluded from such a function but it will then limit you to four partitions for that hard drive.

File: /etc/fstab

Enter the drive into the fstab file so that it is recognized and mounted upon system boot.

File: /etc/fstab Red Hat 8.0

LABEL=/                 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
none                    /dev/pts                devpts  gid=5,mode=620  0 0
none                    /proc                   proc    defaults        0 0
none                    /dev/shm                tmpfs   defaults        0 0
/dev/hda2               swap                    swap    defaults        0 0
/dev/hdb1               /opt2                   ext3    defaults        1 2
/dev/cdrom              /mnt/cdrom              iso9660 noauto,owner,kudzu,ro 0 0
/dev/fd0                /mnt/floppy             auto    noauto,owner,kudzu 0 0
      
The digits "1 2" refer to whether the mount point should be backed up when the dump command is used and disk integrity checks using fsck. The "1" states that it should be backed up when the dump command is issued (0=no). The "2" refers to the order in which "fsck" should check the mount points. The digit "1" identifies the root ("/") of the filesystem. All others should be "2". (0=no check)

Also see: fstab man page for a description of all options.

Links

SCSI Drives: