File: fai-mount-disk

package info (click to toggle)
fai 6.5.1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,084 kB
  • sloc: sh: 6,774; perl: 5,665; makefile: 138
file content (92 lines) | stat: -rwxr-xr-x 2,715 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#! /bin/bash

# Copyright (c) 2002-2025 by Thomas Lange

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
fstab_mount() {

    if [ $fstabcount -eq 1 ]; then

        # save fstab
        cp $FAI_ROOT/$fstabpart/etc/fstab /tmp
        umount_local_disks
        # mount according to fstab saved
        mount2dir $FAI_ROOT /tmp/fstab 0 $mountoption
    fi
    [ $fstabcount -eq 0 ] && echo "No /etc/fstab found"
    [ $fstabcount -ge 2 ] && echo -e "Found multiple /etc/fstab files in : $fstablist\nUse mount2dir for mounting."
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
mount_local_disks() {

    # try to mount all local disk partitions containing a file system
    local disk dev devname type
    fstabcount=0
    [ "$1" = "rw" ] && mountoption=$1

    for disk in /dev/disk/by-uuid/*; do
        type=$(blkid -sTYPE $disk)
        [[ "$type" =~ "swap" ]] && continue
        [[ "$type" =~ "zfs_member" ]] && continue
        dev=$(readlink -e $disk)
        devname=${dev##*/}
        if [ -n "$FAI_BOOTSTICK" ]; then
            [[ "$devname" =~ "$FAI_BOOTSTICK" ]] && continue # skip USB stick device
        fi
        mkdir -p $FAI_ROOT/$devname
        mount -o $mountoption $dev $FAI_ROOT/$devname

        # look for btrfs subvolume
        if [[ "$type" =~ "btrfs" ]]; then
            subvol=$(btrfs sub list -a $FAI_ROOT/$devname|head -1)
            subvol=${subvol#*path }
            umount $FAI_ROOT/$devname
            mount $dev -osubvol=$subvol $FAI_ROOT/$devname
        fi

        # \ && echo $partition mounted successfully
        if [ -s $FAI_ROOT/$devname/etc/fstab ]; then
            echo "/etc/fstab found in $dev"
            fstabpart=$devname   # used in fstab_mount
            fstablist="$fstablist $devname"
            (( fstabcount += 1 ))
        fi
    done
    mount | grep $FAI_ROOT
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
umount_local_disks() {

    # can be an extern script
    local part
    test -d $FAI_ROOT || return
    for part in $(grep $FAI_ROOT /proc/mounts | cut -d ' ' -f 2| sort -r); do
        umount $part
    done
    test -d $FAI_ROOT/ida && rmdir $FAI_ROOT/ida/*
    test -d $FAI_ROOT/rd && rmdir $FAI_ROOT/rd/*
    rmdir $FAI_ROOT/* 2>/dev/null
    mountpoint -q $FAI_ROOT && umount $FAI_ROOT
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# main program

mountoption=ro
dofstab=0

while getopts ufrw opt ; do
      case "$opt" in
        u) umount_local_disks ; exit ;;
        w) mountoption=rw ;;
        r) mountoption=ro ;;
        f) dofstab=1 ;;
      esac
done

set_bootstick

mount_local_disks
if [ $dofstab -eq 1 ]; then
    fstab_mount
fi