Three computer hard disk drives side by side with the top cover removed with a multi colored circuit pattern background

How to Add New Disk Drive to a Linux System

  • Adam Douglas

Today I was asked, “how do I add another disk drive to a Linux system?”. So I thought this would be a good post for today (smile). There is not a lot of discussion on how to do this, and I can understand why. You see, this is not a light topic to write about. It would be more of a series of posts since there is a lot of technology knowledge one should know such as, how file systems work, what file system to use, how disk drives are configured, how the mounting system works, etc. As you can see there is more to it than one would think. I believe though I can make some assumptions and present a general idea of how a disk drive would be added to a Linux system.

Assumptions

Adding and configuring a drive can be done in multitude of ways, therefore I will make the following assumptions.

  • General understanding of using a Linux terminal (command-line interface)
  • Steps prefixed with a “$” (dollar sign) represents the CLI prompt
  • Steps prefixed with a “#” (number sign) represents the CLI prompt with elevated user permissions (e.g. root)
  • The text after the “$” or “#” is to be entered at the CLI
  • A new internal disk drive (hard disk drive, solid state drive, etc.) is already installed into the computer system
  • The new disk drive will be represented by the device name of /dev/sdb or UUID of “234d9f5b-bfbd-4f46-a37b-420ed6940690”
  • The new disk drive will be configured to have one primary partition with ext4 as the file system
  • The new disk drive will be automatically mounted at each boot using the /etc/fstab
  • The new disk drive will have a mount point of “/mnt/data”

Danger

The instructions outlined below are done at your own risk. Data loss may occur if not done carefully.

Locate New Disk Drive

Use one of the following commands, lsblk or fdisk to get device name for the new disk drive.

$ lsblk
sda      8:32   0 238.5G  0 disk
├─sda1   8:33   0   499M  0 part /boot
└─sda2   8:34   0   238G  0 part /
sdb      8:48   0   1.8T  0 disk
# fdisk -l
Disk /dev/sda: 238.47 GiB, 256060514304 bytes, 500118192 sectors
Disk model: Samsung SSD 850
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 286D3D8A-D1C8-44FC-8277-73403A448BF8

Device       Start       End   Sectors  Size Type
/dev/sda1     2048   1048576   1046529  511M EFI System
/dev/sda2  1050624 500118158 499067535  238G Linux filesystem

Disk /dev/sdb: 1.82 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: WDC WD2003FZEX-0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Create Partition

  1. Create new partition.

     # fdisk /dev/sdb
    
     Command (m for help): n
     Partition type
        p   primary (0 primary, 0 extended, 4 free)
        e   extended (container for logical partitions)
     Select (default p): p
     Partition number (1-4, default 1): 1
     First sector (2048-3907029167, default 2048): <press-enter>
     Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-3907029167, default 3907029167): <press-enter>
    
     Created a new partition 1 of type 'Linux' and of size 1.8 TiB.
    
  2. Verify new partition.

     Command (m for help): p
    
     Disk /dev/sdb: 1.82 TiB, 2000398934016 bytes, 3907029168 sectors
     Disk model: WDC WD2003FZEX-0
     Units: sectors of 1 * 512 = 512 bytes
     Sector size (logical/physical): 512 bytes / 512 bytes
     I/O size (minimum/optimal): 512 bytes / 512 bytes
     Disklabel type: dos
     Disk identifier: 0x20a1e5f0
    
     Device     Boot Start        End    Sectors  Size Id Type
     /dev/sdb1        2048 3907029167 3907027120  1.8T 83 Linux
    
  3. Save changes and exit.

     Command (m for help): w
    
     Calling ioctl() to re-read partition table.
     Syncing disks.
    

Format Partition

Format the drive partition using the file system ext4 and set the volume partition label to “new-volume-label”. The value of “new-volume-label” can be replaced with whatever name one desires.

# mkfs.ext4 -L new-volume-label /dev/sb1
-L  Set the volume label for the file system. The maximum length of the volume label is 16 characters.

Mount Drive

  1. Create a directory for the mount point.

    The directory created will be used to mount (attach) the drive to the current file system.

     # mkdir /mnt/data
    
  2. Get the UUID of the disk drive.

    The use of the UUID or Universally unique identifier is used instead of the device name, because device names often change due to hardware detection order, and can change when other disks are added or removed.

     $ lsblk -f
    
     NAME   FSTYPE FSVER LABEL          UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
     sda
     ├─sda1 vfat   FAT32                5352-AC6C                             250.5M    50% /boot
     └─sda2 ext4         ArchLinux      cce03871-dc3b-4418-90f9-87ca427d4223   29.4G    87% /
     sdb
     └─sdb1 ext4   1.0                  234d9f5b-bfbd-4f46-a37b-420ed6940690
    
     -f  Output info about filesystems.
    
  3. Add disk drive to fstab.

    From the previous step copy the UUID of the sdb1 device and enter it into the fstab configuration file. This will allow for the system to automatically mount the drive at each time the system is turned on or rebooted. The last line shown below is our new disk drive.

     # nano /etc/fstab
    
     # <file system> <dir>   <type>  <options>   <dump>  <pass>
     # /dev/sda1 LABEL=ArchLinux
     UUID=cce03871-dc3b-4418-90f9-87ca427d4223   /           ext4    defaults,errors=remount-ro 0 1
     # /dev/sdb1 LABEL=new-volume-label
     UUID=234d9f5b-bfbd-4f46-a37b-420ed6940690   /mnt/data   ext4    defaults,errors=remount-ro 0 2
    
  4. Mount disk drive.

     # mount -a
    
     -a  Mount all filesystems (of the given types) mentioned in fstab (except for those whose line contains the noauto keyword).
    
  5. Verify drive is mounted.

    To verify the drive has been mounted, use one of the following commands, lsblk or mount. The output will show the drive mounted with the mount point of “/mnt/data”.

     $ lsblk
    
     NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
     sda      8:32   0 238.5G  0 disk
     ├─sda1   8:33   0   499M  0 part /boot
     └─sda2   8:34   0   238G  0 part /
     sdb      8:48   0   1.8T  0 disk
     └─sdb1   8:34   0   1.8T  0 part /mnt/data
    
     $ mount
    
     /dev/sda1 on /boot type vfat (rw,relatime,errors=remount-ro)
     /dev/sda2 on / type ext4 (rw,relatime,errors=remount-ro)
     /dev/sdb1 on /mnt/data type ext4 (rw,relatime,errors=remount-ro)
    

Set Permissions

The file system of a drive can be configured in many ways, therefore in this example I will assume the drive is being used by one person. By default, a drive will have user and group ownership (group) set to “root”. Let’s change this to grant the primary user, “adam” access to the entire drive.

# chown -R adam:adam /mnt/data

This will change the user and group ownership as follows.

$ ls -al /mnt
drwxr-xr-x  1 root root  208 Nov 30 14:23 ..
drwxr-xr-x 12 adam adam 4096 Dec  7 19:05 data

Accessing the Drive

The new disk drive can be accessed by browsing to the path, “/mnt/data”.

This is post 46 of 100, and is round 2 of the 100 Days To Offload challenge.

    • change topic
    • add tag linux
    • change topic
    • add tag
    • change 100DaysToOffload message