Table of Contents
- Mounting Disks in Linux
- Storing Mounts Persistently
- Expanding Disks
- Using
growpartandresize2fs - Troubleshooting
- Advanced Tips & Iterations
fstabvs.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
/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)./mnt/mydisk: This is the mount point where the device will be mounted. Essentially, once mounted, you can access the contents of
/dev/sdb1from/mnt/mydisk.ext4: This is the type of the filesystem on the device or partition.
ext4is a common filesystem type for Linux systems, but there are many others likexfs,btrfs,ntfs, etc.defaults: These are the mount options. The term “defaults” means to use the default options, which are
rw,suid,dev,exec,auto,nouser, andasync. Each of these options has a specific meaning:rw: Mount the filesystem as read-write.suid: Allow the execution ofsetuidprograms.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.
0: This is the dump flag. If it’s set to
1, thedumpprogram will backup the filesystem.0means it won’t.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 be2or0(to disable checking).Mounting an NTFS drive with read-only permissions:
/dev/sdb1 /mnt/mydisk ntfs ro,umask=0222 0 0ro: Mount the filesystem as read-only.umask=0222: Set the mask to make all files and directories non-writable.
Mounting a network disk using NFS:
server:/path/on/server /mnt/mydisk nfs defaults 0 0server:/path/on/server: The NFS server’s address and the exported path.nfs: Specifies that the filesystem type is Network File System (NFS).
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.
Mounting a tmpfs (RAM disk):
tmpfs /mnt/mydisk tmpfs defaults,size=1G 0 0tmpfs: 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 thefstabentry.Where: This specifies the mount point, analogous to the second field in thefstabentry.Type: This is the filesystem type, corresponding to the third field.Options: This is where you would specify mount options. They’re similar tofstaboptions but may have subtle differences.
- NTFS with read-only:
[Mount]
What=/dev/sdb1
Where=/mnt/mydisk
Type=ntfs
Options=ro,umask=0222
- Network disk using NFS:
[Mount]
What=server:/path/on/server
Where=/mnt/mydisk
Type=nfs
Options=defaults
- 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 toRequires, but it’s less strict. If the unit listed in aWantsdirective fails, the current unit will still try to start.
- 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
- 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
In the AWS Management Console, navigate to EC2 and choose the instance.
Modify the EBS volume size.
SSH into the instance:
Using
growpartandresize2fs# Expands a partition. sudo growpart /dev/sdb 1 # Resizes the filesystem. sudo resize2fs /dev/sdb1Mounting 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
- Disk not recognized: Ensure all connections are secure.
- Permission denied: Use
sudo. - Mount point does not exist: Ensure the directory exists.
- Filesystem errors during
resize2fs:
e2fsck -f /dev/sdb1
Unable to resize in AWS: Ensure the EBS volume has been resized.
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: mountedRun 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 -hCheck 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 testdiskFollow on-screen instructions to recover partitions or files.
Buy Me a Coffee