Linux: Mount and Manage Storage Devices

In Linux, all storage devices must be attached to the filesystem hierarchy at a specific location called a mount point. Unlike Windows with drive letters (C:, D:), Linux integrates all storage into a single directory tree.

Listing Connected Storage Devices

Before mounting anything, let’s see what storage devices are connected to your system:

lsblk

Sample output:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 465.8G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
├─sda2   8:2    0     1G  0 part /boot
└─sda3   8:3    0 464.3G  0 part 
  └─centos-root 253:0    0 464.3G  0 lvm  /
sdb      8:16   0 931.5G  0 disk 
sr0     11:0    1  1024M  0 rom

To see detailed information about block devices:

sudo blkid

Sample output:

/dev/sda1: UUID="1234-5678" TYPE="vfat" PARTUUID="abcd1234-01"
/dev/sda2: UUID="98765432-1234-5678-9012-123456789012" TYPE="xfs" PARTUUID="abcd1234-02"
/dev/sda3: UUID="11111111-2222-3333-4444-555555555555" TYPE="LVM2_member" PARTUUID="abcd1234-03"
/dev/sdb1: UUID="abcdef12-3456-7890-abcd-ef1234567890" TYPE="ext4" PARTUUID="defg5678-01"

Mounting Filesystems

Temporary Mount

To mount a filesystem temporarily until the next reboot:

# Create a mount point
sudo mkdir /mnt/mydisk

# Mount the device
sudo mount /dev/sdb1 /mnt/mydisk

# Verify the mount
df -h /mnt/mydisk

Sample output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       916G   77M  870G   1% /mnt/mydisk

Permanent Mount with /etc/fstab

For permanent mounting across reboots, edit /etc/fstab:

# Backup fstab first
sudo cp /etc/fstab /etc/fstab.backup

# Edit fstab
sudo nano /etc/fstab

Add a line like this:

UUID=abcdef12-3456-7890-abcd-ef1234567890 /mnt/mydisk ext4 defaults 0 2

Then mount all filesystems defined in fstab:

sudo mount -a

Working with Different Filesystems

Creating Filesystems

Before mounting a new disk, you might need to create a filesystem on it:

# Create an ext4 filesystem
sudo mkfs.ext4 /dev/sdc1

# Create an XFS filesystem (if xfsprogs is installed)
sudo mkfs.xfs /dev/sdc2

Sample output for ext4 creation:

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
61056000 inodes, 244189696 blocks
12209484 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2988441600
7453 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, 7962624, 11239424, 20480000, 23887872

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

Mounting with Options

Mount with specific options for performance or security:

# Mount with no execution permissions
sudo mount -o noexec /dev/sdb1 /mnt/mydisk

# Mount read-only
sudo mount -o ro /dev/sdb1 /mnt/mydisk

# Mount with specific user permissions
sudo mount -o uid=1000,gid=1000 /dev/sdb1 /mnt/mydisk

Managing Mounts

Unmounting Devices

Before removing a device, always unmount it:

# Unmount by mount point
sudo umount /mnt/mydisk

# Unmount by device
sudo umount /dev/sdb1

If the device is busy, find what’s using it:

lsof /mnt/mydisk

Sample output:

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
bash    12345 user  cwd    DIR   8,17     4096 524289 /mnt/mydisk

Checking Mount Status

View all currently mounted filesystems:

mount | grep sdb

Sample output:

/dev/sdb1 on /mnt/mydisk type ext4 (rw,relatime,data=ordered)

Or use the more readable findmnt command:

findmnt /mnt/mydisk

Sample output:

TARGET      SOURCE    FSTYPE OPTIONS
/mnt/mydisk /dev/sdb1 ext4   rw,relatime,data=ordered

Working with Special Mounts

Mounting ISO Files

Mount an ISO file as a loop device:

sudo mkdir /mnt/iso
sudo mount -o loop myimage.iso /mnt/iso
ls /mnt/iso

Mounting Network Filesystems

Mount an NFS share:

sudo mount -t nfs server:/path/to/share /mnt/nfs

Mount a CIFS/SMB share:

sudo mount -t cifs //server/share /mnt/cifs -o username=myuser

Troubleshooting Common Issues

Device Not Found

If you get “mount: special device /dev/sdX does not exist”:

# Rescan SCSI bus
sudo rescan-scsi-bus.sh

# Or manually trigger a rescan
echo "- - -" | sudo tee /sys/class/scsi_host/host*/scan

Permission Denied

When mounting, if you get permission errors:

# Check if you're using sudo
sudo mount /dev/sdb1 /mnt/mydisk

# Check if the device is already mounted
mount | grep sdb1

Filesystem Errors

Check and repair filesystems:

# Check ext filesystem
sudo fsck /dev/sdb1

# Check XFS filesystem
sudo xfs_repair /dev/sdb1

Automating Mount Management

Create a script to manage mounts:

#!/bin/bash
# mount-manager.sh

DEVICE="/dev/sdb1"
MOUNTPOINT="/mnt/mydisk"

case "$1" in
    mount)
        if mountpoint -q "$MOUNTPOINT"; then
            echo "Already mounted"
        else
            sudo mount "$DEVICE" "$MOUNTPOINT"
            echo "Mounted $DEVICE at $MOUNTPOINT"
        fi
        ;;
    unmount)
        if mountpoint -q "$MOUNTPOINT"; then
            sudo umount "$MOUNTPOINT"
            echo "Unmounted $MOUNTPOINT"
        else
            echo "Not mounted"
        fi
        ;;
    *)
        echo "Usage: $0 {mount|unmount}"
        exit 1
        ;;
esac

Make it executable and use:

chmod +x mount-manager.sh
./mount-manager.sh mount

Conclusion

Mastering storage device management in Linux is crucial for system administration. With these commands and examples, you can confidently handle mounting, unmounting, and managing filesystems. Remember to always backup critical data before making changes to storage configurations.

Regular practice with these commands will make storage management second nature. Always verify your mounts and ensure proper permissions to maintain system stability and security.


Linux storage management mount filesystem umount fstab blkid lsblk disk management sysadmin ext4 xfs nfs cifs device mounting