| 1 |
#! /bin/bash |
| 2 |
|
| 3 |
#********************************************************************* |
| 4 |
# |
| 5 |
# mount2dir -- mount partitions to a directory using a fstab file |
| 6 |
# |
| 7 |
# This script is part of FAI (Fully Automatic Installation) |
| 8 |
# (c) 2001-2020 by Thomas Lange, [email protected] |
| 9 |
# Universitaet zu Koeln |
| 10 |
# |
| 11 |
#********************************************************************* |
| 12 |
# This program is free software; you can redistribute it and/or modify |
| 13 |
# it under the terms of the GNU General Public License as published by |
| 14 |
# the Free Software Foundation; either version 2 of the License, or |
| 15 |
# (at your option) any later version. |
| 16 |
# |
| 17 |
# This program is distributed in the hope that it will be useful, but |
| 18 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 |
# General Public License for more details. |
| 21 |
# |
| 22 |
# A copy of the GNU General Public License is available as |
| 23 |
# `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution |
| 24 |
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You |
| 25 |
# can also obtain it by writing to the Free Software Foundation, Inc., |
| 26 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 27 |
#********************************************************************* |
| 28 |
|
| 29 |
# examples: |
| 30 |
# mount new created partitions to /target |
| 31 |
# mount2dir /target /tmp/fstab 1 |
| 32 |
# |
| 33 |
# root filesystem already mounted. Found a fstab inside. Now mount |
| 34 |
# the remaining partitons |
| 35 |
# mount2dir $target $target/etc/fstab 0 ro |
| 36 |
# |
| 37 |
# TODO: option parsing with getopts, option for also mounting nfs directories |
| 38 |
# matching a certain server name |
| 39 |
|
| 40 |
target=$1 # where to mount |
| 41 |
fstab=$2 |
| 42 |
fscheck=$3 # make fsck if 1 |
| 43 |
moptions=$4 # if set, use only this mount option (mostly -r) |
| 44 |
|
| 45 |
# exit if too few parameters given |
| 46 |
[ "$fstab" ] || { |
| 47 |
echo "Too few parameters." |
| 48 |
echo "Usage: mount2dir targetdir fstab [ fscheck mopt ]" |
| 49 |
exit 1 |
| 50 |
} |
| 51 |
|
| 52 |
[ -f $fstab ] || { |
| 53 |
echo "No fstab file $fstab found." |
| 54 |
exit 2 |
| 55 |
} |
| 56 |
|
| 57 |
( |
| 58 |
while read device mountpoint fstype mopt dummy; do |
| 59 |
|
| 60 |
case $mopt in |
| 61 |
*noauto*) continue ;; |
| 62 |
esac |
| 63 |
|
| 64 |
[ "$mountpoint" == "none" ] && continue |
| 65 |
[ "$fstype" == "nfs" ] && continue |
| 66 |
[ "$fstype" == "nfs4" ] && continue |
| 67 |
[ "$fstype" == "ceph" ] && continue |
| 68 |
[ "$fstype" == "swap" ] && continue |
| 69 |
[ "$fstype" == "proc" ] && continue |
| 70 |
[ "$fstype" == "usbfs" ] && continue |
| 71 |
[ "$fstype" == "sysfs" ] && continue |
| 72 |
[ "$fstype" == "tmpfs" ] && continue |
| 73 |
[ "$fstype" == "devpts" ] && continue |
| 74 |
|
| 75 |
case $device in |
| 76 |
""|\#*) continue ;; |
| 77 |
|
| 78 |
/dev/*|LABEL=*|UUID=*|PARTLABEL=*|PARTUUID=*|*:*) |
| 79 |
|
| 80 |
# overwrite mount options |
| 81 |
mopt="-o $mopt -o noatime" |
| 82 |
[ "$moptions" ] && mopt+=" -o $moptions" |
| 83 |
|
| 84 |
mkdir -p $target$mountpoint |
| 85 |
if [ $debug ]; then |
| 86 |
echo "Mounting $device to $target$mountpoint type: $fstype opt: $mopt" |
| 87 |
else |
| 88 |
[ X$verbose = X1 ] && echo "Mounting $device to $target$mountpoint" |
| 89 |
fi |
| 90 |
# should we fsck the partition first? |
| 91 |
[ "$fscheck" = 1 ] && fsck -nt $fstype $device |
| 92 |
[ $debug ] && echo "$BASH_SOURCE: calling 'mount -t $fstype $mopt $device $target$mountpoint'" |
| 93 |
mount -t $fstype $mopt $device $target$mountpoint || exit 4 |
| 94 |
esac |
| 95 |
done |
| 96 |
|
| 97 |
) < $fstab |