Change location of /etc/fstab

1.4k views Asked by At

I have written a script which requires to read a few entries in /etc/fstab. I have tested the script by manually adding some entries in /etc/fstab and then restored the file to its original contents, also manually. Now I would like to automate those tests and run them as a seperate script. I do, however, not feel comfortable with the idea of changing /etc/fstab altered. I was thinking of making a backup copy of /etc/fstab, then altering it and finally restoring the original file after the tests are done. I would prefer it if I could temporarily alter the location of fstab.

Is there a way to alter the location of fstab to, say, /usr/local/etc/fstab so that when mount -a is run from within a script only the entries in /usr/local/etc/fstab are processed?

UPDATE:

I used bishop's solution by setting LIBMOUNT_FSTAB=/usr/local/etc/fstab. I have skimmed the man page of mount on several occasions in the past but I never noticed this variable. I am not sure if this variable has always been there and I simply overlooked it or if it had been added at some point. I am using mount from util-linux 2.27.1 and at least in this version LIBMOUNT_FSTAB is available and documented in the man-page. It is in the ENVIRONMENT section at the end. This will make my automated tests a lot safer in the future.

UPDATE2:

Since there has been some discussion whether this is an appropriate programming question or not, I have decided to write a small script which demonstrates the usage of LIBMOUNT_FSTAB.

#!/bin/bash

libmount=libmount_fstab
tmpdir="/tmp/test_${libmount}_folder" # temporary test folder
mntdir="$tmpdir/test_${libmount}_mountfolder" # mount folder for loop device
img="$tmpdir/loop.img" # dummy image for loop device
faketab="$tmpdir/alternate_fstab" # temporary, alternative fstab

# get first free loop device
loopdev=$(losetup -f)

# verify there is a free loop device
if [[ -z "$loopdev" ]];then
    echo "Error: No free loop device" >&2
    exit 1
fi

# check that loop device is not managed by default /etc/fstab
if grep "^$loopdev" /etc/fstab ;then
    echo "Error: $loopdev already managed by /etc/fstab" >&2
    exit 1
fi

# make temp folders
mkdir -p "$tmpdir"
mkdir -p "$mntdir"

# create temporary, alternative fstab
echo "$loopdev $mntdir ext2 errors=remount-ro 0 1" > "$faketab"

# create dummy image for loop device
dd if=/dev/zero of="$img" bs=1M count=5 &>/dev/null

# setup loop device with dummy image
losetup "$loopdev" "$img" &>/dev/null

# format loop device so it can be mounted
mke2fs "$loopdev" &>/dev/null

# alter location for fstab
export LIBMOUNT_FSTAB="$faketab"

# mount loop device by using alternative fstab
mount "$loopdev" &>/dev/null

# verify loop device was successfully mounted
if mount | grep "^$loopdev" &>/dev/null;then
    echo "Successfully used alternative fstab: $faketab"
else
    echo "Failed to use alternative fstab: $faketab"
fi

# clean up
umount "$loopdev" &>/dev/null
losetup -d "$loopdev"
rm -rf "$tmpdir"

exit 0

My script primarily manages external devices which are not attached most of the time. I use loop-devices to simulate external devices to test the functionality of my script. This saves a lot of time since I do not have to attach/reattach several physical devices. I think this proves that being able to use an alternative fstab is a very useful feature and allows for scripting safe test scenarios whenever parsing/altering of fstab is required. In fact, I have decided to partially rewrite my script so that it can also use an alternative fstab. Since most of the external devices are hardly ever attached to the system their corresponding entries are just cluttering up /etc/fstab.

1

There are 1 answers

12
bishop On BEST ANSWER

Refactor your code that modifies fstab contents into a single function, then test that function correctly modifies the dummy fstab files you provide it. Then you can confidently use that function as part of your mount pipeline.

function change_fstab {
    local fstab_path=${1:?Supply a path to the fstab file}
    # ... etc
}

change_fstab /etc/fstab && mount ...

Alternatively, set LIBMOUNT_FSTAB per the libmount docs:

LIBMOUNT_FSTAB=/path/to/fake/fstab mount ...