I just want to add a disk

  1. Install the disk

    • Drives that communicate through a USB port can simply be powered on and plugged in.
    • SATA and SAS drives rarely are hot-addable. Reboot the system.
  2. Locate the drive’s device file

    We just need to talk to the disk, linux uses device files as it’s api endpoint, so lets find that so we can move forward.

    • The drive will expose it’s interface in a device file, as with other devices it’s an easy to route traffic to the driver.
    • Typically under /dev/sd* where sd stands for SCSI disk (this applies to SATA, USB etc.) and the letter is the order in which the drives were detected.
    • You can use lsblk to find it as well, even better use lsblk -o +MODEL,SERIAL.
  3. Install a partition

    Now we’ve found the way to talk to the drive, we need a partition table installed.

    Partitioning was added as a way to divide the space of a disk, into multiple logical disks, at the time it was to boot multiple OS’s on a single disk. Now it used for the same purposes as well as to logically gaurd partitions for specific uses and ensure they don’t overflow into other partitions, like too many logs not completely filling the entire disk.

    fdisk is the most commonly used and available tool for paritioning a disk, parted following it.

    bash
    # Typical fdisk process (interactive)
    sudo fdisk /dev/sdb
    g        # new GPT table
    n        # new partition
    <Enter>  # partition number (default)
    <Enter>  # first sector (default)
    <Enter>  # last sector (default = whole disk)
    w        # write and exit

    You should now have a new device file to target the partition e.g., /dev/sdb1

  4. Create a file system in the partition

    Use the mkfs (make file system) to create the file system on the partition

    Bash
    sudo mkfs -t ext4 /dev/sdb1
    sudo mkfs -t ext4 -L spare /dev/sdb1 # Optionally you can label the fs
  5. Create a mount point and mount the new file system

    Quick blerb about why we mount in linux.

    A filesystem is just way to organize data on a storage device, until you mount it, linux has no way to route to it.

    When you mount it you’re saying “take the root of this file system and make it appear at this directory”. Linux is just connecting your new file system to your root file system, since linux has one unified tree starting at /

    Windows does this differently, they opt to mount each file system a new letter C:\, D:\, E:\ instead of how linux just has one root /

    Question

    Why does linux do this? I find windows implementation simplier.

    Answer

    It gives you much more modularity and flexability.

    Imagine your /home drive fills up, in windows there’s no great way to just extend this folder, but with linux’s interface you can install another drive and mount it at /home. This is just a better seperation of layers.

    Now let’s actually mount it.

    bash
    sudo mkdir /mnt/spare
    sudo mount /dev/sdb1 /mnt/spare 
    sudo mount LABEL=spare /mnt/spare # Or using the label

    The convention for mounting folders is as follows typically.

    Use caseMount location
    Temporary/manual mount/mnt/<name>
    Removable media/media/<label>
    Permanent filesystemMount where it belongs semantically (e.g. /home, /var, /srv)
  6. Setup persistence for the mount (optional)

    To have the filesystem automatically mounted at boot time:

    • Edit the /etc/fstab file and duplicate one of the existing entries.
    • Change the device name and mount point to match those shown in the mount above
    bash
    /dev/sdb1  /mnt/spare  ext4  defaults  0  2
    
    # Breakdown
    /dev/sdb1  # which file system to mount
    /mnt/spare # where to mount it
    ext4       # file system type
    defaults   # mount options (rw, r)
    0          # don't dump (legacy)
    2          # check the file system with fsck

    It’s best practice to use the UUID of the file system, it’s generated when you run mkfs and it’s locked to the file system, so you can ensure there’s no collision or services changes to the drives config.

    Bash
    sudo blkid /dev/sdb1
    # UUID="8a0d8e4d-7e87-4c77-9d67-a5f0c4c9d0d8"

    Then you fstab becomes:

    UUID=8a0d8e4d-7e87-4c77-9d67-a5f0c4c9d0d8  /mnt/spare  ext4  defaults  0  2