-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgencmdline
executable file
·75 lines (60 loc) · 1.39 KB
/
gencmdline
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
#!/bin/bash
#
# Generate a kernel cmdline string from /etc/cmdline and /etc/cmdline.d/
set -euo pipefail
readonly SELF="${0##*/}"
usage() {
cat <<-EOF
Usage: ${SELF} [OPTIONS]... [ROOT]
Generate a kernel cmdline string from /etc/cmdline and /etc/cmdline.d/
Optionally reads from an alternate root directory at ROOT.
OPTIONS:
-h, --help Print this help message
EOF
exit 0
}
error() {
if [[ $# -gt 0 ]]; then
>&2 echo "${SELF}: $@"
fi
exit 1
}
error_usage() {
if [[ $# -gt 0 ]]; then
>&2 echo "${SELF}: $@"
fi
>&2 echo "Try '${SELF} --help' for more information"
exit 1
}
main() {
# Read arguments
local opts
if ! opts="$(getopt -n "${SELF}" -o h -l help -- "$@")"; then
error_usage
fi
eval set -- "${opts}"
unset opts
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help) usage;;
--) shift 1; break;;
*) error "internal error [1]";;
esac
done
if [[ $# -gt 1 ]]; then
error_usage "invalid operands"
fi
# Normalize ROOT path
local ROOT
if ! ROOT="$(realpath -eq "${1:-/}")" || [[ ! -d "${ROOT}" ]]; then
error "$1: No such file or directory"
else
readonly ROOT
fi
# Generate cmdline string
# Use unquoted echo and subshell to normalize whitespace and concatenate to
# a space delimited single line
echo -E $(cat $ROOT/etc/cmdline $ROOT/etc/cmdline.d/* 2>/dev/null | sed 's/#.*//')
}
main "$@"
# vim: ft=sh sw=0 sts=0 noet