blob: c4f876afc227af52a7b4373fa1dba7a939a51725 (
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
|
#!/bin/sh
PROGRAM=setup-hostname
PREFIX=@PREFIX@
: ${LIBDIR=$PREFIX/lib}
. "$LIBDIR/libalpine.sh"
usage() {
cat <<-__EOF__
usage: setup-hostname [-h] [HOSTNAME]
Sets the system hostname.
options:
-h Show this help
Sets hostname to HOSTNAME or prompt if unspecified.
__EOF__
exit $1
}
# http://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
valid_hostname() {
# check length
if [ $(echo "$1" | wc -c) -gt 255 ]; then
echo "Hostname '$1' is too long." >&2
return 1
fi
# check that it only contains valid chars
if ! [ -z "$(echo $1 | sed 's/[0-9a-z.-]//g')" ]; then
echo "Hostname must only contain letters (a-z), digits (0-9), '.' or '-'" >&2
return 1
fi
# must not start with - or .
case "$1" in
-*) echo "Hostname must not start with a '-'" >&2; return 1;;
.*) echo "Hostname must not start with a '.'" >&2; return 1;;
esac
return 0
}
while getopts "hn:" opt; do
case $opt in
h) usage 0;;
n) name="$OPTARG";;
'?') usage "1" >&2;;
esac
done
shift $(( $OPTIND - 1 ))
if [ -z "$name" ] && [ $# -eq 1 ]; then
name="$1"
fi
# if name is set, then we run non-interactively
if [ -n "$name" ] && ! valid_hostname "$name"; then
exit 1
fi
HOST="$name"
while [ -z "$name" ]; do
HOST=$(hostname)
ask "Enter system hostname (fully qualified form, e.g. 'foo.example.org')" "$HOST"
if valid_hostname "$resp"; then
HOST="$resp"
break
fi
done
mkdir -p "$ROOT/etc"
echo "$HOST" > "$ROOT/etc/hostname"
|