How to Reconstruct Linux fstab File from Current Mount Configuration: A Step-by-Step Recovery Guide


2 views

When working with Linux system administration, we've all had those "oops" moments. In this case, you've accidentally wiped your /etc/fstab while performing backups on Ubuntu 10.04. The good news is that since the system hasn't been rebooted, we can reconstruct the file using current mount information.

We have two crucial command outputs to work with:

$ sudo blkid
/dev/sda1: UUID="3fc55e0f-a9b3-4229-9e76-ca95b4825a40" TYPE="ext4" 
/dev/sda5: UUID="718e611d-b8a3-4f02-a0cc-b3025d8db54d" TYPE="swap" 
/dev/sdb1: LABEL="Files_Server_Int" UUID="02fc2eda-d9fb-47fb-9e60-5fe3073e5b55" TYPE="ext4" 
/dev/sdc1: UUID="41e60bc2-2c9c-4104-9649-6b513919df4a" TYPE="ext4" 
/dev/sdd1: LABEL="Expansion Drive" UUID="782042B920427E5E" TYPE="ntfs" 

$ cat /etc/mtab
/dev/sda1 / ext4 rw,errors=remount-ro 0 0
proc /proc proc rw,noexec,nosuid,nodev 0 0
none /sys sysfs rw,noexec,nosuid,nodev 0 0
none /sys/fs/fuse/connections fusectl rw 0 0
/dev/sdc1 /home ext4 rw 0 0
/dev/sdb1 /media/Files_Server ext4 rw 0 0
/dev/sdd1 /media/Expansion\040Drive fuseblk rw,nosuid,nodev,allow_other,blksize=4096,default_permissions 0 0

We'll create a new fstab file by combining information from both commands:

# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>

# Root partition
UUID=3fc55e0f-a9b3-4229-9e76-ca95b4825a40 / ext4 rw,errors=remount-ro 0 1

# Swap partition
UUID=718e611d-b8a3-4f02-a0cc-b3025d8db54d none swap sw 0 0

# Home partition
UUID=41e60bc2-2c9c-4104-9649-6b513919df4a /home ext4 rw,relatime 0 2

# Files Server partition
UUID=02fc2eda-d9fb-47fb-9e60-5fe3073e5b55 /media/Files_Server ext4 rw,relatime 0 2

# NTFS Expansion Drive
UUID=782042B920427E5E /media/Expansion\040Drive ntfs-3g defaults,windows_names,locale=en_US.utf8 0 0

# Special filesystems
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0

For those who prefer an automated approach, here's a bash script that can help generate a basic fstab:

#!/bin/bash

# Backup existing fstab if it exists
[[ -f /etc/fstab ]] && cp /etc/fstab /etc/fstab.bak

# Generate new fstab
{
    echo "# /etc/fstab: static file system information."
    echo "#"
    echo "# <file system> <mount point>   <type>  <options>       <dump>  <pass>"
    echo ""
    
    # Process mounted filesystems
    grep -E '^/dev/' /etc/mtab | while read -r line; do
        device=$(echo "$line" | awk '{print $1}')
        mountpoint=$(echo "$line" | awk '{print $2}')
        fstype=$(echo "$line" | awk '{print $3}')
        options=$(echo "$line" | awk '{print $4}')
        
        # Skip temporary filesystems
        [[ "$mountpoint" == /dev* ]] && continue
        [[ "$mountpoint" == /proc* ]] && continue
        [[ "$mountpoint" == /sys* ]] && continue
        
        # Get UUID if available
        if uuid=$(blkid -o value -s UUID "$device" 2>/dev/null); then
            echo "UUID=$uuid $mountpoint $fstype $options 0 $(get_fsck_pass "$mountpoint"))"
        else
            echo "$device $mountpoint $fstype $options 0 0"
        fi
    done
    
    # Add special filesystems
    echo "proc /proc proc defaults 0 0"
    echo "sysfs /sys sysfs defaults 0 0"
    echo "devpts /dev/pts devpts gid=5,mode=620 0 0"
    echo "tmpfs /dev/shm tmpfs defaults 0 0"
    
} > /etc/fstab

echo "New fstab has been generated. Please verify before rebooting!"

Before rebooting, it's crucial to verify the new fstab:

# Check for syntax errors
sudo mount -a

# Verify all expected filesystems are mounted
mount | grep -E '^/dev/'

1. The errors=remount-ro option for the root partition is critical for system stability
2. NTFS partitions should use ntfs-3g driver with appropriate options
3. The last number in each line (pass) determines filesystem check order (1 for root, 2 for others, 0 for no check)
4. Temporary filesystems (tmpfs, proc, sysfs) should remain as is

Once verified, make the changes permanent:

sudo chmod 644 /etc/fstab
sudo chown root:root /etc/fstab

When working on Ubuntu systems, particularly servers, accidentally wiping the /etc/fstab file can be catastrophic. The current scenario shows:

  • Original fstab entries were deleted during backup operations
  • System remains running with active mounts
  • Need to reconstruct fstab before next reboot

First, let's capture all currently mounted filesystems with their UUIDs:

# Get UUID and filesystem type information
sudo blkid

# View currently mounted filesystems
cat /etc/mtab

The output shows:

/dev/sda1: UUID="3fc55e0f-a9b3-4229-9e76-ca95b4825a40" TYPE="ext4"
/dev/sdc1 /home ext4 rw 0 0

Here's how to translate mtab entries into proper fstab format:

# Example for /home partition
UUID=41e60bc2-2c9c-4104-9649-6b513919df4a /home ext4 defaults 0 2

# Example for swap
UUID=718e611d-b8a3-4f02-a0cc-b3025d8db54d none swap sw 0 0

For a more systematic approach, use this bash script:

#!/bin/bash
{
    # Root filesystem
    echo "UUID=$(blkid -s UUID -o value /dev/sda1) / ext4 errors=remount-ro 0 1"
    
    # Swap partition
    echo "UUID=$(blkid -s UUID -o value /dev/sda5) none swap sw 0 0"
    
    # Home partition
    echo "UUID=$(blkid -s UUID -o value /dev/sdc1) /home ext4 defaults 0 2"
    
    # Additional mounts
    echo "UUID=02fc2eda-d9fb-47fb-9e60-5fe3073e5b55 /media/Files_Server ext4 defaults 0 2"
    echo "UUID=782042B920427E5E /media/Expansion\\040Drive ntfs-3g defaults,windows_names 0 0"
} | sudo tee /etc/fstab
  • NTFS partitions need ntfs-3g as filesystem type
  • Spaces in mount points require \040 escape sequence
  • System partitions should have dump/pass values set properly (root=1, others=2)

Before rebooting, validate the new fstab:

# Test fstab without actually mounting
sudo mount -a --fake

# Verify mount points
findmnt --verify --verbose
  1. Keep backup copies of critical system files before editing
  2. Consider using lsblk -f for alternative partition information
  3. Document custom mount points for future reference