Table of Contents

  • Mounting Disks in Linux
  • Storing Mounts Persistently
  • Expanding Disks
  • Using growpart and resize2fs
  • Troubleshooting
  • Advanced Tips & Iterations
  • fstab vs. parted

Mounting Disks in Linux

Identifying the Disk

# Before you can mount a disk, you need to identify it.
lsblk
# or
sudo fdisk -l

Mounting the Disk

To manually mount a disk:

sudo mount /dev/sdb1 /mnt/mydisk

Storing Mounts Persistently

Using /etc/fstab

Edit the /etc/fstab file:

sudo nano /etc/fstab

Add a line like:

/dev/sdb1 /mnt/mydisk ext4 defaults 0 0
  1. /dev/sdb1: This is the device or partition to be mounted. In this case, it’s the first partition (sdb1) on the second disk (sdb).

  2. /mnt/mydisk: This is the mount point where the device will be mounted. Essentially, once mounted, you can access the contents of /dev/sdb1 from /mnt/mydisk.

  3. ext4: This is the type of the filesystem on the device or partition. ext4 is a common filesystem type for Linux systems, but there are many others like xfs, btrfs, ntfs, etc.

  4. defaults: These are the mount options. The term “defaults” means to use the default options, which are rw, suid, dev, exec, auto, nouser, and async. Each of these options has a specific meaning:

    • rw: Mount the filesystem as read-write.
    • suid: Allow the execution of setuid programs.
    • dev: Interpret character/block special devices on the filesystem.
    • exec: Allow the execution of binaries.
    • auto: Mount automatically at boot.
    • nouser: Only allow root to mount the filesystem.
    • async: Use asynchronous I/O.
  5. 0: This is the dump flag. If it’s set to 1, the dump program will backup the filesystem. 0 means it won’t.

  6. 0: This is the pass number, which controls the order in which filesystem checks are done at reboot. The root filesystem should have the highest priority, typically 1. Other filesystems can be 2 or 0 (to disable checking).

  7. Mounting an NTFS drive with read-only permissions:

    /dev/sdb1 /mnt/mydisk ntfs ro,umask=0222 0 0
    
    • ro: Mount the filesystem as read-only.
    • umask=0222: Set the mask to make all files and directories non-writable.
  8. Mounting a network disk using NFS:

    server:/path/on/server /mnt/mydisk nfs defaults 0 0
    
    • server:/path/on/server: The NFS server’s address and the exported path.
    • nfs: Specifies that the filesystem type is Network File System (NFS).
  9. Mounting with user authentication for cifs (Windows share):

    //server/share /mnt/mydisk cifs username=myuser,password=mypassword 0 0
    
    • //server/share: The location of the CIFS/Windows share.
    • username=myuser,password=mypassword: Authentication details to access the share.
  10. Mounting a tmpfs (RAM disk):

    tmpfs /mnt/mydisk tmpfs defaults,size=1G 0 0
    
    • tmpfs: Specifies that it’s a RAM disk.
    • size=1G: Limit the RAM disk to 1 GB in size.

Using systemd

Create a mount unit file:

sudo nano /etc/systemd/system/mnt-mydisk.mount

With content:

[Unit]
Description=Mount My Disk

[Mount]
What=/dev/sdb1
Where=/mnt/mydisk
Type=ext4
Options=defaults

[Install]
WantedBy=multi-user.target

Enable and start it:

sudo systemctl enable mnt-mydisk.mount
sudo systemctl start mnt-mydisk.mount

While /etc/fstab and systemd’s .mount units serve the same primary purpose, which is to define mount points and their properties, the way you specify options and their syntax differ. systemd can translate /etc/fstab into native mount units on-the-fly, but if you’re writing native systemd mount files, here’s how you do it:

Translation details:

  • What: This corresponds to the device or partition to be mounted, similar to the first field in the fstab entry.

  • Where: This specifies the mount point, analogous to the second field in the fstab entry.

  • Type: This is the filesystem type, corresponding to the third field.

  • Options: This is where you would specify mount options. They’re similar to fstab options but may have subtle differences.

  1. NTFS with read-only:
[Mount]
What=/dev/sdb1
Where=/mnt/mydisk
Type=ntfs
Options=ro,umask=0222
  1. Network disk using NFS:
[Mount]
What=server:/path/on/server
Where=/mnt/mydisk
Type=nfs
Options=defaults
  1. CIFS with authentication:

In systemd:

[Mount]
What=//server/share
Where=/mnt/mydisk
Type=cifs
Options=username=myuser,password=mypassword

Remember, unlike fstab, systemd .mount units aren’t automatically enabled. To have them mount at boot, you’d need to enable the unit:

systemctl enable mnt-mydisk.mount

Always test new mount units with systemctl start unit-name before enabling them to ensure they function as expected.

For mount units, especially those that rely on network services, this is crucial. For example, a mount that depends on a network location needs the network to be fully operational before attempting to mount.

Using After and Requires or Wants:

  • After: This directive ensures that the current unit is only started after the specified unit(s).
  • Requires: If the units listed here aren’t started successfully, the current unit won’t start either.
  • Wants: This is similar to Requires, but it’s less strict. If the unit listed in a Wants directive fails, the current unit will still try to start.
  1. Ensure Network is Up before Mounting:

For mounting a network location, like an NFS share, it’s crucial that the network is up and operational:

[Unit]
Description=Mount NFS Share
After=network-online.target
Requires=network-online.target

[Mount]
What=server:/path/on/server
Where=/mnt/nfs_share
Type=nfs
Options=defaults

[Install]
WantedBy=multi-user.target

Here, network-online.target is a special systemd target that ensures the network is fully configured and online. If you want the mount to try even if network-online.target fails for some reason, you’d use Wants

Wants=network-online.target
  1. Mount after a Specific Service:

Imagine you have a custom service (custom-service.service) that must run before a specific mount:

[Unit]
Description=Mount after Custom Service
After=custom-service.service
Requires=custom-service.service

[Mount]
What=/dev/sdb1
Where=/mnt/mydisk
Type=ext4
Options=defaults

[Install]
WantedBy=multi-user.target

The mount will only occur after custom-service.service has started.

Expanding Disks

VMware

In VMware, edit the VM and modify the disk size (e.g., from 400GB to 500GB).

# get the id of the disks
run lsscsi or lsblk
# Rescan the SCSI bus or block
sudo tee /sys/block/sdb/device/rescan
echo 1 > /sys/class/scsi_device/32\:0\:0\:0/device/rescan

# Instead of targeting a specific SCSI device, you can trigger a rescan on all SCSI buses
echo "- - -" | sudo tee -a /sys/class/scsi_host/host*/scan


# Resize the filesystem
resize2fs /dev/sdb
resize2fs /dev/nvme2n1p1

AWS

  1. In the AWS Management Console, navigate to EC2 and choose the instance.

  2. Modify the EBS volume size.

  3. SSH into the instance:

    Using growpart and resize2fs

    # Expands a partition.
    sudo growpart  /dev/sdb 1
    # Resizes the filesystem.
    sudo resize2fs  /dev/sdb1
    

    Mounting nfs in aws on a EC2 instance

    sudo apt install nfs-common
    sudo mkdir /efs
    sudo mount -t nfs -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport fs-0a8612345678933c.efs.eu-central-1.amazonaws.com:/ /efs
    

Troubleshooting

  1. Disk not recognized: Ensure all connections are secure.
  2. Permission denied: Use sudo.
  3. Mount point does not exist: Ensure the directory exists.
  4. Filesystem errors during resize2fs:
e2fsck -f /dev/sdb1
  1. Unable to resize in AWS: Ensure the EBS volume has been resized.

  2. Errors after fstab: Double-check the entries. Boot into rescue mode and revert changes if needed.

Advanced Tips & Iterations

Advanced Commands

Parted: A versatile disk partitioning and manipulation tool.

  • Intended Usage: For managing disk partitions, especially with the GPT partition table, or disks larger than 2TB.

    Example:

    To resize a partition with parted:

    sudo parted /dev/sdb
    (parted) resizepart
    Partition number? 1
    End? [500GB]? 600GB
    (parted) quit
    

XFS Filesystem: An alternative to ext4, optimized for parallel I/O.

  • Intended Usage: Filesystem of choice in certain high-performance scenarios.

    Example:

    To grow an XFS filesystem:

    sudo xfs_growfs /mount/point
    

Automation

Ansible: A configuration management tool, ideal for automating tasks.

  • Intended Usage: Automate repetitive tasks, such as disk mounting or resizing across multiple servers.

    Example:

    Ansible playbook to mount a disk:

    ---
    - hosts: servers
      tasks:
        - name: Mount Disk
          mount:
            path: /mnt/mydisk
            src: /dev/sdb1
            fstype: ext4
            state: mounted
    

    Run with:

    ansible-playbook mount_disk.yml
    

Bash Scripts: A way to combine multiple commands into one script.

  • Intended Usage: Scripting repetitive tasks.

    Example:

    Bash script to check and mount a disk:

    #!/bin/bash
    if [ ! -d "/mnt/mydisk" ]; then
      mkdir /mnt/mydisk
    fi
    mount /dev/sdb1 /mnt/mydisk
    

Monitoring

df & du: Tools to monitor disk usage.

  • Intended Usage: Regularly check available disk space or determine which files/directories consume the most space.

    Example:

    Check disk usage:

    df -h
    

    Check directory size:

    du -sh /path/to/directory
    

Optimization

hdparm: Get/set SATA/IDE device parameters.

  • Intended Usage: Optimize drive performance and security.

    Example:

    Check drive’s read speed:

    sudo hdparm -tT /dev/sda
    

iostat: Monitor system I/O.

  • Intended Usage: Identify bottlenecks or heavy I/O operations.

    Example:

    Monitor I/O stats:

    iostat -xz
    

Backup & Recovery

dd: Disk dump - copy and convert data.

  • Intended Usage: Clone disks or create image backups.

    Example:

    Clone a disk:

    sudo dd if=/dev/sda of=/path/to/backup.img bs=4M status=progress
    

rsync: A fast and versatile file copying tool.

  • Intended Usage: Backup data incrementally.

    Example:

    Backup a directory:

    rsync -av /path/to/source/ /path/to/destination/
    

testdisk: A tool to recover deleted partitions and repair boot sectors.

  • Intended Usage: Recover lost partitions or files.

    Example:

    Start testdisk:

    sudo testdisk
    

    Follow on-screen instructions to recover partitions or files.



Buy Me a Coffee