Add second hard drive to linux

Here is how to add a second disk to linux. In this article, the OS is CentOS 7.

Check to see how many drives are installed: You can do this using the df and fdisk command:


# df -h
Filesystem                     Size  Used Avail Use% Mounted on
/dev/mapper/centos_plesk-root   14G  3.9G   10G  29% /
devtmpfs                       910M     0  910M   0% /dev
tmpfs                          920M  4.0K  920M   1% /dev/shm
tmpfs                          920M  8.6M  912M   1% /run
tmpfs                          920M     0  920M   0% /sys/fs/cgroup
/dev/sda1                      497M  215M  282M  44% /boot
tmpfs                          184M     0  184M   0% /run/user/1000

The above information show the primary disk as sda.

Use fdisk to identify the second drive:


# fdisk -l

Disk /dev/sda: 17.2 GB, 17179869184 bytes, 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000ab095

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    33554431    16264192   8e  Linux LVM

Disk /dev/sdb: 17.2 GB, 17179869184 bytes, 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

The sdb drive is the second drive and it has no information on the mount points.

Now we can partition the drive. To partition the disk – /dev/sdb, enter:


# fdisk /dev/sdb

For help using the partitioner, use 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): 

partitions using the command “p”:


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):

To create a new partition, issue the command “n” and then select “p” for primary and 1-4 depending on which partition on the drive this is (first, second, third, or fourth):

n(creates a new partition)

p(creates a primary partition)

1(the number 1 denotes the partition will be /dev/sdb1)


Command (m for help): n
Command action
  e   extended
  p   primary partition (1-4)
p
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


To save the partition, use the “w” command:


Command (m for help): w


The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Now we can check to see if the partition is there:

# fdisk -l

Disk /dev/sda: 17.2 GB, 17179869184 bytes, 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000ab095

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    33554431    16264192   8e  Linux LVM

Disk /dev/sdb: 17.2 GB, 17179869184 bytes, 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x884e64a3

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    33554431    16776192   83  Linux

Format the new disk using mkfs.ext3 command:

To format Linux partions using ext3fs on the new disk, issue the following command:


# mkfs.ext3 /dev/sdb1

To format Linux partions using ext4fs on the new disk, issue the following command:

# mkfs.ext4 /dev/sdb1

Output (for using ext4):

# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1048576 inodes, 4194048 blocks
209702 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
128 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Mount the new disk using the mount command. First, you’ll need to create a mount point. We’ll use /disk2. This is where we’ll mount /dev/sdb1. Enter the following commands:


# cd /
# mkdir disk2
# mount /dev/sdb1 /disk2
# df -h
Filesystem                     Size  Used Avail Use% Mounted on
/dev/mapper/centos_plesk-root   14G  3.9G   10G  29% /
devtmpfs                       910M     0  910M   0% /dev
tmpfs                          920M  4.0K  920M   1% /dev/shm
tmpfs                          920M  8.6M  912M   1% /run
tmpfs                          920M     0  920M   0% /sys/fs/cgroup
/dev/sdb1                       16G   47M   15G   1% /disk2
/dev/sda1                      497M  215M  282M  44% /boot
tmpfs                          184M     0  184M   0% /run/user/0

Edit /etc/fstab so the new drive will automatically mount to /disk1 on reboot. Add the following:
/dev/sdb1 /disk2 ext4 defaults 0 0

# nano /etc/fstab
# Created by anaconda on Wed Dec  6 19:53:05 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos_plesk-root /                       xfs     defaults        0 0
UUID=f9b133be-f059-45e2-a296-ccddb4cebe06 /boot                   xfs     defaults        0 0
/dev/mapper/centos_plesk-swap swap                    swap    defaults        0 0
/dev/sdb1       /disk2      ext4    defaults        0 0

Mount the disk

# mount -a