automatic fstab & mount -a

  • Beitrags-Autor:
  • Beitrags-Kategorie:IT, Software & Co.
  • Lesedauer:3 min Lesezeit

How to create /etc/fstab automaticly?  ‚How to create /etc/fstab automaticly?‘ I was asked once by my brother (aka „Will“). My answer was very simple (like my english): „By using a Bash script“. But there are no good bash scripts to do it… (Knoppix etc does it too, but not as preciesly as my brother would like to). So he wrote it by himself…  What does it do?

1) It checks what do you have in /etc/fstab
2) It checks which partitions you have (Known types: Linux = auto [kernel driver will decide], Windows = fat32 [vfat], Windows NT partitions = NTFS [using ntfs-g3 driver – you should have it in your system], Linux swap partitions)
3) It makes dirs in /mnt/ (or other -> see options) and mount -a
4) Also swap partitions

How to use it?

1) Put the script in a file like /etc/rd.d/fstab_and_mnt.sh
2) Edit and change options
# Mountpoint:
MountPoint=mnt
#NTFS Driver avalible options: ntfs-3g; fuse; ntfs
NtfsDriver=ntfs-3g
3) chmod file with 755: „chmod 755 /etc/rd.d/fstab_and_mnt.sh“
4) Run: „/etc/rd.d/fstab_and_mnt.sh rebuild“

=================== Script follows ============================

#!/bin/bash

#Of course /mnt, but: mnt, for check in Script. If change to: media; not: /media.
MountPoint=mnt

#NTFS Driver avalible options: ntfs-3g; fuse; ntfs
NtfsDriver=ntfs-3g

path=${path}
if [ -f /usr/bin/gawk ]; then awk=/usr/bin/gawk; fi
if [ -f /usr/bin/awk ]; then awk=/usr/bin/awk; fi

if [ ! -d /$MountPoint ]; then mkdir /$MountPoint; fi

Mout_DRV() {
blkid | while read -r device ; do
dev_name=“`echo „$device“ | $awk ‚{print $1}‘ | cut -d „:“ -f-1`“
dev_mount=“`echo „$dev_name“ | sed „s/dev/$MountPoint/“`“
for i in $device; do if echo $i | grep -q „^TYPE“; then dev_sys=`echo $i | sed „s/“//g“ | cut -d „=“ -f2-`; fi; done
if [ $dev_sys ==  „swap“ ]; then
echo $dev_name swap swap defaults 0 0 >>/etc/fstab
swapon $dev_name>/dev/null 2>&1 & swapon -a>/dev/null 2>&1
continue
fi
if [ $dev_sys == „ntfs“ ]; then dev_sys=$NtfsDriver; fi

if [ ! -d $dev_mount ]; then mkdir $dev_mount; fi

echo -e $dev_name    $dev_mount $dev_sys rw,noauto,user,$1 0 0 >>/etc/fstab

done
}

Mout_DRV exec>/dev/null

Create_CD() {
dev_name=/dev/$1
dev_mount=/$MountPoint/$1
if [ ! -d /$MountPoint/$1 ]; then mkdir /$MountPoint/$1; fi
echo $dev_name    $dev_mount auto ro,noauto,user,exec 0 0 >>/etc/fstab
}

for i in cdrom cdrom1; do if [ -L /dev/$i ]; then Create_CD $i>/dev/null; fi; done

RebuildFstab() {
echo -n>/tmp/fstab.tmp
cat /etc/fstab | while read line; do
line1=“`echo -e $line | $awk ‚{print $1}’`“
line2=“`echo -e $line | $awk ‚{print $2}’`“
line3=“`echo -e $line | $awk ‚{print $3}’`“
if grep -w -q „^$line1“ /tmp/fstab.tmp; then if grep -w -q $line3 /tmp/fstab.tmp; then continue; fi; fi
if [ $(dirname ${line1}) != „/dev“ ]; then
echo $line>>/tmp/fstab.tmp;
else
if [ -e $line1 ]; then
echo $line>>/tmp/fstab.tmp
else
umount $line2>/dev/null 2>&1
rm -fr $line2
fi
fi
done
mv -f /tmp/fstab.tmp /etc/fstab
}
if ! grep -w -q rebuild /proc/cmdline ; then RebuildFstab>/dev/null; fi

exit

=================== Script ends ============================