summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/linhes-live/bin/gen_fstab
blob: db36546a05471d56d7031e101271f051f620011e (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#! /bin/sh
#
# gen_fstab  - make new fstab for larch live system based on detected devices
#
# Author: Michael Towers <gradgrind[at]online[dot]de>
#
# This file is part of the larch project.
#
#    larch is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    larch is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with larch; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#----------------------------------------------------------------------------
# 2007.12.17

# Note that the results are, by default, not copied into place
# They end up here:
DEST="/tmp/fstab"
MNT="/tmp/mnt"

# However, passing '-l' as command-line option will install them.
# The old fstab will then be backed up to fstab~.
LOAD=""
if [ "$1" = "-l" ]; then LOAD="-l"; fi

if [ -n "$( df | grep " ${MNT}" )" ]; then
    echo "ERROR: Mounted filesystem at/within ${MNT}"
    exit 1
fi
mkdir -p ${MNT}
rm -rf ${MNT}/*

tmpfile="/tmp/fstab2"
: >${tmpfile}

echo "# fstab generated by gen_fstab" >${DEST}
echo "#<file system>   <dir>         <type>      <options>    <dump> <pass>" >>${DEST}
echo >>${DEST}

echo "none            /dev/pts      devpts      defaults        0     0" >>${DEST}
echo "none            /dev/shm      tmpfs       defaults        0     0" >>${DEST}
echo >>${DEST}

# Get all other partitions
sfdisk -d | grep "^/dev/" | sed "s|\(.*\):.*Id=\(..\).*|\1 \2|" | \
    while read dev id; do
        # Ignore if id is "Extended" or "LVM", these are not usable partitions
        if [ "${id}" = "5" -o "${id}" = "8e" ]; then continue; fi
        # See if swap
        if [ "${id}" = "82" ]; then
            printf "%-12s %-12s %-8s defaults,noatime 0     0\n" \
                ${dev} swap swap >>${DEST}
            continue
        fi
        removable=""
        part=$( basename ${dev} )
        if [ $( cat /sys/block/${part:0:3}/removable 2>/dev/null ) -ne 0 ]; then
            removable="_rmv"
        fi
        mountdir=${part}${removable}
        printf "%-12s %-12s %-8s user,noauto,noatime 0     0\n" \
                ${dev} /mnt/${mountdir} auto >>${tmpfile}
        mkdir -p ${MNT}/${mountdir}
    done

# LVM
for lvmd in $( ls /dev/mapper 2>/dev/null | grep -v control ); do
    printf "%-30s %-22s %-8s user,noauto,noatime 0     0\n" \
           /dev/mapper/${lvmd} /mnt/${lvmd} auto >>${tmpfile}
    mkdir -p ${MNT}/${lvmd}
done

echo >>${DEST}
cat ${tmpfile} >>${DEST}
rm ${tmpfile}
echo >>${DEST}

# CD devices
for dev in $( cat /proc/sys/dev/cdrom/info 2>/dev/null | head -n 3 | \
        tail -n 1 | cut -d ":" -f 2 ); do
    mountdir="${dev}_cd"
    mkdir ${MNT}/${mountdir}
    printf "%-12s %-12s %-8s user,noauto,exec,unhide 0     0\n" \
            /dev/${dev} /mnt/${mountdir} auto >>${DEST}
done

echo >>${DEST}
echo "# This would do for a floppy" >>${DEST}
echo "#/dev/fd0        /mnt/floppy    vfat,ext2 rw,user,noauto    0     0" >>${DEST}
echo "#    +   mkdir /mnt/floppy" >>${DEST}
echo >>${DEST}
echo "# E.g. for USB storage:" >>${DEST}
echo "#/dev/sdb1        /mnt/usb      auto      rw,user,noauto   0     0" >>${DEST}
echo "#    +   mkdir /mnt/usb" >>${DEST}

if [ -n "${LOAD}" ]; then
    # test if the script is started by root user. If not, exit
    if [ $UID -ne 0 ]; then
        echo "Only root can run ${APP}"; exit 1
    fi

    cp -b ${DEST} /etc/fstab
    # Delete removeable mount points which are not currently mounted
    for m in $( ls /mnt | grep ^[hs]d ); do
        if [ -z "$( df | grep " /mnt/${m}$" )" ]; then
            rmdir /mnt/${m}
        fi
    done
    for m in $( ls ${MNT} ); do
        mkdir -p /mnt/${m}
    done
fi