rendered paste body#!/bin/bash
# Calls scanpartitions as root and adds entries to /etc/fstab
PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/sbin/restorefstab"
export PATH
umask 022
TMP="/tmp/fstab.$$.tmp"
NEWENTRIES="# Dynamic entries below"
rm -f "$TMP"
umount -a
if [[ -e "/tmp/ROOT.*.tmp" ]];
then
rm -f /tmp/ROOT.*.tmp
fi
ROOT="/tmp/ROOT.$$.tmp"
df -l / | cut -d" " -f1 | grep / > $ROOT;
{ echo -n "root is "; cat "$ROOT"; }
echo "2nd root is $(<$ROOT)"
'echo "# Static entries" >> "$TMP"'
if [[ $(cat $ROOT) = "/dev/root" ]]
then
echo "$ROOT equals /dev/root"
'echo "line of text" >> "$TMP"'
# rm -f /etc/fstab
# cp /etc/fstab.dr /etc/fstab
# else
# cp /etc/fstab.nodr /etc/fstab
fi
[ ! -e /proc/partitions ] && { echo "$0: /proc not mounted, exiting" >&2; exit 1; }
# Read in boot parameters
CMDLINE="$(cat /proc/cmdline)"
isbootparam(){
case "$CMDLINE" in *$1*) return 0;; esac
return 1
}
if [ -e /var/run/restorefstab.sh.pid ]; then
ps "$(</var/run/restorefstab.sh.pid)" >/dev/null 2>&1 && exit 0
rm -f /var/run/restorefstab.sh.pid
fi
echo "$$" >/var/run/restorefstab.sh.pid
# Search if a line STARTING with string
stringinfile(){
while read line; do
case "$line" in $1*) return 0;; esac
done <"$2"
return 1
}
# Echo lines until a line STARTING with string
staticentries(){
while read line; do
case "$line" in $1*) break ;; esac
echo "$line"
done <"$2"
}
# Strip lines from previous errors
goodentries(){
while read line; do
case "$line" in
*/cdrom*) ;;
*/fd*) ;;
*) echo $line ;;
esac
done <"$1"
}
verbose=""
remove=""
for i in "$@"; do
case "$i" in
-v*) verbose="yes" ;;
-r*) remove="yes" ;;
*) echo "Usage: $0 [-v[erbose]] [-r[emove_old]]" ;;
esac
done
[ -n "$verbose" ] && echo "Scanning partitions..." >&2
cat "$ROOT" >> "$TMP"
stringinfile "$NEWENTRIES" /etc/fstab
if [ $? != 0 ]; then
# there is newentries structure, this must be an old style file, prep it
goodentries /etc/fstab >"$TMP"
echo "$NEWENTRIES" >>"$TMP"
else
# this is a new style structure
if [ -n "$remove" ]; then
# rebuild the dynamic entires
staticentries "$NEWENTRIES" /etc/fstab >"$TMP"
echo "$NEWENTRIES" >>"$TMP"
fi
# otherwise just add new entries to end
fi
##
count=0
while read device mountpoint fstype relax; do
stringinfile "$device " "$TMP" || \
[ "$device " = "/dev/sda " ] || \
{ count="$((count + 1))"
[ -d "$mountpoint" ] || \
[ "$mountpoint" = "swap" ] || \
mkdir -p "$mountpoint" 2>/dev/null
options="auto,users,exec"
case "$fstype" in
ntfs) options="auto,users"
## options="${options},ro,umask=0222"
fstype="ntfs-3g"
;;
vfat) options="${options},umask=000" ;;
swap) if isbootparam "noswap"; then
options="sw,noauto" ;
else
options="sw,pri=1" ;
fi ;;
reiserfs) options="${options},noatime,notail" ;;
esac
echo "$device $mountpoint $fstype $options 0 0";
}
done >>"$TMP" <<EOT
$(scanpartitions)
EOT
[ -n "$verbose" ] && { [ "$count" -gt 0 ] && echo "Adding $count new partitions to /etc/fstab." >&2 || echo "No new partitions found." >&2; }
# cat {"$ROOT / ext3 defaults 0 0"} >> "$TMP"
cp "$TMP" /etc/fstab.new
cp "$TMP" /home/james/Documents/fstab.test.txt
# cat "$TMP" >> /etc/fstab
mv "$TMP" /etc/fstab
rm -f /tmp/ROOT.*.tmp
rm -f /var/run/restorefstab.sh.pid
mount -a
exit 0