Adding a hard drive in Linux


When a new drive is added in Linux, it will not automatically appear and be usable. It will need to be partitioned and formatted for proper use first.

You do need to know what type of drive you have added to the machine, whether it is a SCSI, SATA or IDE drive. Each type will appear different to your Linux system.

SCSI and SATA appear as sd*

IDE appears as hd*

The asterisks represents the drives as letters a-z

So, if you had two separate hard drives in your system, they would be named as below.

sda, sdb

If the drives have multiple partitions they will include additional information. For instance if you had a total of five partitions on a drive it would appear as such.

sda1, sda2, sda3, sda4, sda5

You can see what drives your system sees with the following command. (This assumes you only have SATA/SCSI drives)

[jhudgins@web ~]$ ls /dev/sd*
  /dev/sda /dev/sda2 /dev/sda4 /dev/sdb
  /dev/sda1 /dev/sda3 /dev/sda5

From this I can see I have two separate drives /dev/sda and /dev/sdb and that /dev/sda is partioned in five partitions and /dev/sdb does not contain any partions.

I need to make /dev/sdb into a usable drive under Linux.

Creating Partitions

I first need to partion the drive for computer use. Linux uses fdisk though with different features than the old DOS systems version.

From the command prompt enter the following command which will display as follows.

[jhudgins@centos ~]$ sudo fdisk /dev/sdb
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):

Let’s get started by displaying the drive partition information.

Command (m for help): p
Disk /dev/sdb: 50.0 GB, 50019202560 bytes
  255 heads, 63 sectors/track, 6081 cylinders
  Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
Command (m for help):

Now we wish to add a partition. In this example we will make the partition the entire drive.

Command (m for help): n
  Command action
  e extended
  p primary partition (1-4)
  Partition number (1-4): 1
  First cylinder (1-6081, default 1): 1
  Last cylinder or +size or +sizeM or +sizeK (1-6081, default 6081): 6081
Command (m for help):

If I had wished, I could have selected a portion of the entire disk for this partition by selecting the number of cylinders, megabytes or kilobytes.

As shown in my example, the entire drive was used to create the partition using the commands below.

n to create a new partition
p to create a primary partition
1 to denote the partition as the first partition of /dev/sdb creating /dev/sdb1

Verify you have partitioned the drive as you desired.

Command (m for help): p
Disk /dev/sdb: 50.0 GB, 50019202560 bytes
  255 heads, 63 sectors/track, 6081 cylinders
  Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
  /dev/sdb1 1 6081 48845601 83 Linux
Command (m for help):

Once verified you will need to execute your changes with the w command.

Command (m for help): w
  The partition table has been altered!
Calling ioctl() to re-read partition table.
  Syncing disks.
  [jhudgins@centos ~]$

Formatting the drive

Once the drive has been partitioned it can be formatted with the file system of your choice. In this case I use ext4.

[jhudgins@centos ~]$ sudo mkfs -t ext4 /dev/sdb1
  mke2fs 1.32 (09-Nov-2002)
  Filesystem label=
  OS type: Linux
  Block size=4096 (log=2)
  Fragment size=4096 (log=2)
  6111232 inodes, 12211400 blocks
  610570 blocks (5.00%) reserved for the super user
  First data block=0
  373 block groups
  32768 blocks per group, 32768 fragments per group
  16384 inodes per group
  Superblock backups stored on blocks:
  32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
  4096000, 7962624, 11239424
Writing inode tables: done
  Creating journal (8192 blocks): done
  Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 38 mounts or
  180 days, whichever comes first. Use tune2fs -c or -i to override.

Mounting your new drive

Last you will want to mount your new drive.

Start by creating a mount point. A mount point is simply the location you wish to have the drive mounted. In this case I am going to create a mount point off of the root or “/” directory called “storage”.

[jhudgins@centos ~]$ cd /
  [jhudgins@centos ~]$ sudo mkdir storage

Make /storage available to all.

[jhudgins@centos ~]$ sudo chmod 777 storage/

You will want the drive to automatically mount when the computer starts which will require you to make an entry into /etc/fstab.

[jhudgins@centos ~]$ sudo vim /etc/fstab

Now add your mount point for /dev/sdb1 as shown.

LABEL=/ / ext4 defaults 1 1
  LABEL=/home /home ext3 defaults 1 2
  LABEL=/boot /boot ext3 defaults 1 2
  /dev/sdb1 /storage ext3 defaults 1 2
  tmpfs /dev/shm tmpfs defaults 0 0
  devpts /dev/pts devpts gid=5,mode=620 0 0
  sysfs /sys sysfs defaults 0 0
  proc /proc proc defaults 0 0
  LABEL=SWAP-sda3 swap swap defaults 0 0
  # Beginning of the block added by the VMware software
  .host:/ /mnt/hgfs vmhgfs defaults,ttl=5 0 0
  # End of the block added by the VMware software

Once saved, a reboot will automatically mount ”’/storage”’ making it available for use or you can enter the following command to avoid the reboot.

[jhudgins@centos ~]$ sudo mount /storage

And finally, the mount command will display your new drive properly mounted.

[jhudgins@centos ~]$ sudo mount
  /dev/sda2 on / type ext4 (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/sda5 on /home type ext4 (rw)
  /dev/sda1 on /boot type ext4 (rw)
  tmpfs on /dev/shm type tmpfs (rw)
  none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
  /dev/sdb1 on /storage type ext3 (rw)