| 1 |
#! /bin/bash |
| 2 |
|
| 3 |
# Copyright (c) 2002-2025 by Thomas Lange |
| 4 |
|
| 5 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 6 |
fstab_mount() { |
| 7 |
|
| 8 |
if [ $fstabcount -eq 1 ]; then |
| 9 |
|
| 10 |
# save fstab |
| 11 |
cp $FAI_ROOT/$fstabpart/etc/fstab /tmp |
| 12 |
umount_local_disks |
| 13 |
# mount according to fstab saved |
| 14 |
mount2dir $FAI_ROOT /tmp/fstab 0 $mountoption |
| 15 |
fi |
| 16 |
[ $fstabcount -eq 0 ] && echo "No /etc/fstab found" |
| 17 |
[ $fstabcount -ge 2 ] && echo -e "Found multiple /etc/fstab files in : $fstablist\nUse mount2dir for mounting." |
| 18 |
} |
| 19 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 20 |
mount_local_disks() { |
| 21 |
|
| 22 |
# try to mount all local disk partitions containing a file system |
| 23 |
local disk dev devname type |
| 24 |
fstabcount=0 |
| 25 |
[ "$1" = "rw" ] && mountoption=$1 |
| 26 |
|
| 27 |
for disk in /dev/disk/by-uuid/*; do |
| 28 |
type=$(blkid -sTYPE $disk) |
| 29 |
[[ "$type" =~ "swap" ]] && continue |
| 30 |
[[ "$type" =~ "zfs_member" ]] && continue |
| 31 |
dev=$(readlink -e $disk) |
| 32 |
devname=${dev##*/} |
| 33 |
if [ -n "$FAI_BOOTSTICK" ]; then |
| 34 |
[[ "$devname" =~ "$FAI_BOOTSTICK" ]] && continue # skip USB stick device |
| 35 |
fi |
| 36 |
mkdir -p $FAI_ROOT/$devname |
| 37 |
mount -o $mountoption $dev $FAI_ROOT/$devname |
| 38 |
|
| 39 |
# look for btrfs subvolume |
| 40 |
if [[ "$type" =~ "btrfs" ]]; then |
| 41 |
subvol=$(btrfs sub list -a $FAI_ROOT/$devname|head -1) |
| 42 |
subvol=${subvol#*path } |
| 43 |
umount $FAI_ROOT/$devname |
| 44 |
mount $dev -osubvol=$subvol $FAI_ROOT/$devname |
| 45 |
fi |
| 46 |
|
| 47 |
# \ && echo $partition mounted successfully |
| 48 |
if [ -s $FAI_ROOT/$devname/etc/fstab ]; then |
| 49 |
echo "/etc/fstab found in $dev" |
| 50 |
fstabpart=$devname # used in fstab_mount |
| 51 |
fstablist="$fstablist $devname" |
| 52 |
(( fstabcount += 1 )) |
| 53 |
fi |
| 54 |
done |
| 55 |
mount | grep $FAI_ROOT |
| 56 |
} |
| 57 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 58 |
umount_local_disks() { |
| 59 |
|
| 60 |
# can be an extern script |
| 61 |
local part |
| 62 |
test -d $FAI_ROOT || return |
| 63 |
for part in $(grep $FAI_ROOT /proc/mounts | cut -d ' ' -f 2| sort -r); do |
| 64 |
umount $part |
| 65 |
done |
| 66 |
test -d $FAI_ROOT/ida && rmdir $FAI_ROOT/ida/* |
| 67 |
test -d $FAI_ROOT/rd && rmdir $FAI_ROOT/rd/* |
| 68 |
rmdir $FAI_ROOT/* 2>/dev/null |
| 69 |
mountpoint -q $FAI_ROOT && umount $FAI_ROOT |
| 70 |
} |
| 71 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 72 |
|
| 73 |
# main program |
| 74 |
|
| 75 |
mountoption=ro |
| 76 |
dofstab=0 |
| 77 |
|
| 78 |
while getopts ufrw opt ; do |
| 79 |
case "$opt" in |
| 80 |
u) umount_local_disks ; exit ;; |
| 81 |
w) mountoption=rw ;; |
| 82 |
r) mountoption=ro ;; |
| 83 |
f) dofstab=1 ;; |
| 84 |
esac |
| 85 |
done |
| 86 |
|
| 87 |
set_bootstick |
| 88 |
|
| 89 |
mount_local_disks |
| 90 |
if [ $dofstab -eq 1 ]; then |
| 91 |
fstab_mount |
| 92 |
fi |