-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathgenfstab.in
144 lines (119 loc) · 2.73 KB
/
genfstab.in
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
shopt -s extglob
m4_include(common)
write_source() {
local tag= spec= label= uuid= comment=()
label=$(lsblk -rno LABEL "$1" 2>/dev/null)
uuid=$(lsblk -rno UUID "$1" 2>/dev/null)
if (( bylabel )); then
tag=LABEL
spec=$label
comment=("$source" "UUID=$uuid")
elif (( byuuid )); then
tag=UUID
spec=$uuid
comment=("$source")
if [[ $label ]]; then
comment+=("LABEL=$label")
fi
else
[[ $uuid ]] && comment+=("UUID=$uuid")
[[ $label ]] && comment+=("LABEL=$label")
fi
[[ $comment ]] && printf '# %s\n' "${comment[*]}"
if [[ $spec ]]; then
printf '%-20s' "$tag=$spec"
else
printf '%-20s' "$(mangle "$source")"
fi
}
root=/mnt
usage() {
cat <<EOF
usage: ${0##*/} [options]
Options:
-L Use labels for source identifiers
-p Avoid printing pseudofs mounts
-r root Generate based on 'root' (default: /mnt)
-U Use UUIDs for source identifiers
EOF
}
if [[ -z $1 || $1 = @(-h|--help) ]]; then
usage
exit $(( $# ? 0 : 1 ))
fi
while getopts ':Lpr:U' flag; do
case $flag in
L)
bylabel=1
;;
U)
byuuid=1
;;
p)
nopseudofs=1
;;
r)
# trim trailing slashes
root=${OPTARG%%+(/)}
;;
:)
die '%s: option requires an argument -- '\''%s'\' "${0##*/}" "$OPTARG"
;;
?)
die '%s: invalid option -- '\''%s'\' "${0##*/}" "$OPTARG"
;;
esac
done
shift $(( OPTIND - 1 ))
if (( bylabel && byuuid )); then
die "cannot specify both -U and -L"
fi
# handle block devices
findmnt -Recruno SOURCE,TARGET,FSTYPE,OPTIONS "$root" |
while read -r source target fstype opts; do
# default 5th and 6th columns
dump=0 pass=2
source=$(unmangle "$source")
target=$(unmangle "$target")
# this is root
target=${target#$root}
if [[ $target = ?(/) ]]; then
target=/
pass=1
fi
# we don't fsck pseudofs
if fstype_is_pseudofs "$fstype"; then
(( nopseudofs )) && continue
pass=0
fi
if [[ $source =~ ^(/.+)\[(.+)\]$ ]]; then
# it's a bind mount
source=$(findmnt -funcevo TARGET "${BASH_REMATCH[1]}")${BASH_REMATCH[2]}
fstype=bind
pass=0
fi
# write one line
write_source "$source"
printf '\t%-10s' "$(mangle "$target")" "$fstype" "$opts"
printf '\t%s %s' "$dump" "$pass"
printf '\n\n'
done
# handle swaps devices
{
# ignore header
read
while read -r device type _ _ prio; do
options=defaults
if [[ $prio != -1 ]]; then
options+=,pri=$prio
fi
if [[ $type = file ]]; then
printf '%-20s' "$device"
else
write_source "$(unmangle "$device")"
fi
printf '\t%-10s\t%-10s\t%-10s\t0 0\n\n' 'none' 'swap' "$options"
done
} </proc/swaps
# vim: et ts=2 sw=2 ft=sh: