Add UUID to your /etc/fstab for device consistency on VM reboot

Including UUID in the /etc/fstab has benefits and avoids issues around device naming and consistency across reboots for Virtual Machines across most, if not all clouds. Feel free to skip to the end where I am sharing a script to add this to your system, but first some backstory and rational of how it came to this if you need to understand why you also need to configure this for your system.

I have found in the systems I was managing that rebooting by issue a reboot command in Linux OS as well as the Azure portal that disks were not coming up in correct order and files were disappearing on us. With Virtual Machines in the Cloud, your system needs to be able to withstand a reboot. I have seen several physical host failure occurrences on Azure which resulted in Azure having to migrate our Virtual Machine and the Virtual Machines were automatically rebooted in the process.

This seems to impact other clouds too, including (but not an exhaustive list):

  • Amazon Web Service (AWS)
  • Alibaba Cloud
  • Google Cloud Platform (GCP)
  • Microsoft Azure
  • Oracle Cloud Infrastructure (OCI)

All major cloud providers are affected similarly by issues related to device attachment and detachment. In cloud environments, devices such as virtual disks or volumes are often dynamically attached, detached, resized, or reconfigured, leading to potential changes in device names. Azure did write an article about it too but didn't provide an easy way to implement this:?https://learn.microsoft.com/en-us/azure/virtual-machines/linux/attach-disk-portal#mount-the-disk

To ensure that the drive is remounted automatically after a reboot, it must be added to the?/etc/fstab?file. It’s also highly recommended that the UUID (Universally Unique Identifier) is used in?/etc/fstab?to refer to the drive rather than just the device name (such as,?/dev/sdc1). If the OS detects a disk error during boot, using the UUID avoids the incorrect disk being mounted to a given location.

Problems Avoided by Using UUIDs

  1. Mounting Wrong Partitions: Prevents the system from mounting the wrong partition, which could lead to data corruption or system instability.
  2. Boot Failures: Avoids scenarios where the system fails to boot because it cannot find the specified device names in?/etc/fstab.
  3. Data Loss and Corruption: Prevents data loss and corruption by ensuring that the correct filesystems are always mounted, preserving data integrity.

Benefits of Including UUID in?/etc/fstab

  1. Device Consistency: UUIDs (Universally Unique Identifiers)?are unique and persistent across reboots, ensuring that the correct partitions are always mounted regardless of changes in device names.Traditional device names (like?/dev/sda1) can change, especially if new drives are added, removed, or if the order of devices changes. UUIDs prevent this issue by always pointing to the correct device.
  2. Avoiding Device Name Conflicts: In dynamic environments where drives are frequently added or removed, such as in cloud or virtualized systems, device names can change. Using UUIDs avoids potential conflicts and ensures that the right filesystem is mounted.
  3. Stability in Boot Process: During the boot process, the system relies on?/etc/fstab?to mount filesystems. Using UUIDs ensures that the system can reliably find and mount the necessary partitions, avoiding boot failures caused by device name changes.

Script to Auto-magically Add UUID to your /etc/fstab

I wasn't able to find an easy script that would do this out in the wild. So in the spirit of sharing to honour all those whom I have greatly benefited from by sharing their own knowledge, this is my script.

Please create a file on your system for the script and give it executable permission. Run this with sudo root rights. How to do this is consciously exempted in this article because I expect you to know how to do this if you have the rights to root on your system already. If you don’t, please be careful and work with someone who does know. If you require assistance in implementation, please send me a message and I might be able to help recommend professional services to help you.

Please use this script at your own discretion. I do not warranty or take liability of you using it on your own system. It was written for and has only been tested on the systems I am responsible for. This script has been generalized for the internet as I have checked in a company specific version in the internal git repo. By sharing this script, I hope that it gives you guidance on help you roll it out across the systems under your stewardship.

#!/bin/bash
# Author: Melissa Tan <https://www.dhirubhai.net/in/mwtan/>

# Backup the current fstab file with date and time
backup_file="/etc/fstab.bak.$(date +%F_%H-%M-%S)"
cp /etc/fstab $backup_file

# Function to get UUID and add to fstab
add_to_fstab() {
	local device=$1
	local mount_point=$2
	local filesystem=$3
	local options=$4
	local dump=$5
	local pass=$6

	# Get the UUID of the device
	local uuid=$(blkid -s UUID -o value $device)

	if [ -z "$uuid" ]; then
		echo "UUID for $device not found. Skipping..."
		return
	fi

	# Check if the UUID is already in fstab for the correct mount point
	if grep -q "UUID=$uuid $mount_point" /etc/fstab; then
		echo "UUID for $device already exists in fstab for $mount_point. Skipping..."
		return
	fi

	# Check if the UUID is already in fstab with any mount point
	if grep -q "UUID=$uuid" /etc/fstab; then
		echo "UUID=$uuid already exists in fstab. Skipping..."
		return
	fi

	# Add the entry to fstab
	echo "UUID=$uuid $mount_point $filesystem $options $dump $pass" >> /etc/fstab.new
	echo "Added UUID=$uuid for $device to fstab"
}

# Get the list of currently mounted block devices using df
mounted_devices=$(df -hT | grep '^/dev/' | awk '{print $1,$7,$2}')

# Copy the original fstab to a new file
cp /etc/fstab /etc/fstab.new

# Add a section for new UUID entries if needed
echo -e "\n# New UUID entries" >> /etc/fstab.new

# Loop through mounted devices and add them to fstab
while read -r device mount_point filesystem; do
	if [ ! -z "$mount_point" ] && [ "$mount_point" != "none" ] && [ ! -z "$filesystem" ]; then
		add_to_fstab "$device" "$mount_point" "$filesystem" "defaults" 0 2
	fi
done <<< "$mounted_devices"

# Preserve the exact whitespace formatting
mv /etc/fstab.new /etc/fstab

# Notify the user of completion
echo "fstab update complete. Please review /etc/fstab to ensure correctness."

# Reload fstab after modifications
systemctl daemon-reload        

要查看或添加评论,请登录

社区洞察

其他会员也浏览了