summaryrefslogtreecommitdiff
path: root/setup-alpine.in
blob: d7bcce280cfd72b2e6b7ae288d9ece9465dbefdf (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/sh

PROGRAM=setup-alpine
VERSION=@VERSION@

PREFIX=@PREFIX@
: ${LIBDIR=$PREFIX/lib}
. "$LIBDIR/libalpine.sh"

is_kvm_clock() {
	grep -q "kvm-clock"  "$ROOT"sys/devices/system/clocksource/clocksource0/current_clocksource 2>/dev/null
}

is_virtual_console() {
	case "$(readlink "$ROOT"/proc/self/fd/0)" in
		/dev/tty[0-9]*) return 0;;
	esac
	return 1
}

usage() {
	cat <<-__EOF__
		usage: setup-alpine [-ahq] [-c FILE | -f FILE]

		Setup Alpine Linux

		options:
		 -a  Create Alpine Linux overlay file
		 -c  Create answer file (do not install anything)
		 -e  Empty root password
		 -f  Answer file to use installation
		 -h  Show this help
		 -q  Quick mode. Ask fewer questions.
	__EOF__
	exit $1
}

while getopts "aef:c:hq" opt ; do
	case $opt in
		a) ARCHIVE=yes;;
		f) USEANSWERFILE="$OPTARG";;
		c) CREATEANSWERFILE="$OPTARG";;
		e) empty_root_password=1;;
		h) usage 0;;
		q) empty_root_password=1; quick=1; APKREPOSOPTS="-1"; HOSTNAMEOPTS="alpine";;
		'?') usage "1" >&2;;
	esac
done
shift $(expr $OPTIND - 1)

rc_sys=$(openrc --sys)
# mount xenfs so we can detect xen dom0
if [ "$rc_sys" = "XENU" ] && ! grep -q '^xenfs' /proc/mounts; then
	modprobe xenfs
	mount -t xenfs xenfs /proc/xen
fi

case "$USEANSWERFILE" in
	http*://*|ftp://*)
		# dynamically download answer file from URL (supports HTTP(S) and FTP)
		# ensure the network is up, otherwise setup a temporary interface config
		if ! rc-service networking --quiet status; then
			setup-interfaces -ar
		fi

		temp="$(mktemp)"
		wget -qO "$temp" "$USEANSWERFILE" || die "Failed to download '$USEANSWERFILE'"
		USEANSWERFILE="$temp"
		;;
	*)
		[ -n "$USEANSWERFILE" ] && USEANSWERFILE=$(realpath "$USEANSWERFILE")
		;;
esac
if [ -n "$USEANSWERFILE" ] && [ -e "$USEANSWERFILE" ]; then
	. "$USEANSWERFILE"
fi

if [ -n "$CREATEANSWERFILE" ]; then
	touch "$CREATEANSWERFILE" || echo "Cannot touch file $CREATEANSWERFILE"
	cat > "$CREATEANSWERFILE" <<-__EOF__
		# Example answer file for setup-alpine script
		# If you don't want to use a certain option, then comment it out

		# Use US layout with US variant
		# KEYMAPOPTS="us us"
		KEYMAPOPTS=none

		# Set hostname to 'alpine'
		HOSTNAMEOPTS=alpine

		# Set device manager to mdev
		DEVDOPTS=mdev

		# Contents of /etc/network/interfaces
		INTERFACESOPTS="auto lo
		iface lo inet loopback

		auto eth0
		iface eth0 inet dhcp
		    hostname alpine-test
		"

		# Search domain of example.com, Google public nameserver
		# DNSOPTS="-d example.com 8.8.8.8"

		# Set timezone to UTC
		#TIMEZONEOPTS="UTC"
		TIMEZONEOPTS=none

		# set http/ftp proxy
		#PROXYOPTS="http://webproxy:8080"
		PROXYOPTS=none

		# Add first mirror (CDN)
		APKREPOSOPTS="-1"

		# Create admin user
		USEROPTS="-a -u -g audio,video,netdev juser"
		#USERSSHKEY="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOIiHcbg/7ytfLFHUNLRgEAubFz/13SwXBOM/05GNZe4 juser@example.com"
		#USERSSHKEY="https://example.com/juser.keys"

		# Install Openssh
		SSHDOPTS=openssh
		#ROOTSSHKEY="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOIiHcbg/7ytfLFHUNLRgEAubFz/13SwXBOM/05GNZe4 juser@example.com"
		#ROOTSSHKEY="https://example.com/juser.keys"

		# Use openntpd
		# NTPOPTS="openntpd"
		NTPOPTS=none

		# Use /dev/sda as a sys disk
		# DISKOPTS="-m sys /dev/sda"
		DISKOPTS=none

		# Setup storage with label APKOVL for config storage
		#LBUOPTS="LABEL=APKOVL"
		LBUOPTS=none

		#APKCACHEOPTS="/media/LABEL=APKOVL/cache"
		APKCACHEOPTS=none

	__EOF__
	echo "Answer file $CREATEANSWERFILE has been created.  Please add or remove options as desired in that file"
	exit 0
fi

if [ "$ARCHIVE" ] ; then
	echo "Creating an Alpine overlay"
	init_tmpdir ROOT
else
	PKGADD="apk add"
fi

if [ "$rc_sys" != LXC ]; then
	if is_virtual_console || [ -n "$KEYMAPOPTS" ]; then
		setup-keymap ${KEYMAPOPTS}
	fi
	setup-hostname ${HOSTNAMEOPTS} && [ -z "$SSH_CONNECTION" ] && rc-service hostname --quiet restart
	setup-devd -C mdev # just to bootstrap
fi

[ -z "$SSH_CONNECTION" ] && rst_if=1
if [ -n "$INTERFACESOPTS" ]; then
	if [ "$INTERFACESOPTS" != none ]; then
		printf "$INTERFACESOPTS" | setup-interfaces -i ${rst_if:+-r}
	fi
else
	setup-interfaces ${quick:+-a} ${rst_if:+-r}
fi

# setup up dns if no dhcp was configured
if [ -f "$ROOT"/etc/network/interfaces ] && ! grep -q '^iface.*dhcp' "$ROOT"/etc/network/interfaces; then
	setup-dns ${DNSOPTS}
fi

# set root password
if [ -z "$empty_root_password" ]; then
	while ! $MOCK passwd ; do
		echo "Please retry."
	done
fi

if [ -z "$quick" ]; then
	# pick timezone
	setup-timezone ${TIMEZONEOPTS}
fi

rc-update --quiet add networking boot
rc-update --quiet add seedrng boot || rc-update --quiet add urandom boot
svc_list="cron crond"
if [ -e /dev/input/event0 ]; then
	# Only enable acpid for systems with input events entries
	# https://gitlab.alpinelinux.org/alpine/aports/-/issues/12290
	svc_list="$svc_list acpid"
fi
for svc in $svc_list; do
	if rc-service --exists $svc; then
		rc-update --quiet add $svc
	fi
done

# start up the services
$MOCK openrc ${SSH_CONNECTION:+-n} boot
$MOCK openrc ${SSH_CONNECTION:+-n} default

# update /etc/hosts - after we have got dhcp address
# Get default fully qualified domain name from *first* domain
# given on *last* search or domain statement.
_dn=$(sed -n \
-e '/^domain[[:space:]][[:space:]]*/{s///;s/\([^[:space:]]*\).*$/\1/;h;}' \
-e '/^search[[:space:]][[:space:]]*/{s///;s/\([^[:space:]]*\).*$/\1/;h;}' \
-e '${g;p;}' "$ROOT"/etc/resolv.conf 2>/dev/null)

_hn=$(hostname)
_hn=${_hn%%.*}

sed -i -e "s/^127\.0\.0\.1.*/127.0.0.1\t${_hn}.${_dn:-$(get_fqdn my.domain)} ${_hn} localhost.localdomain localhost/" \
	"$ROOT"/etc/hosts 2>/dev/null

if [ -z "$quick" ]; then
	setup-proxy -q ${PROXYOPTS}
fi
# activate the proxy if configured
if [ -r "$ROOT/etc/profile" ]; then
	. "$ROOT/etc/profile"
fi

if ! is_kvm_clock && [ "$rc_sys" != "LXC" ] && [ "$quick" != 1 ]; then
	setup-ntp ${NTPOPTS}
fi

setup-apkrepos ${APKREPOSOPTS}

# Now that network and apk are operational we can install another device manager
if [ "$rc_sys" != LXC ] && [ -n "$DEVDOPTS" -a "$DEVDOPTS" != mdev ]; then
	setup-devd ${DEVDOPTS}
fi

# lets stop here if in "quick mode"
if [ "$quick" = 1 ]; then
	exit 0
fi

setup-user ${USERSSHKEY+-k "$USERSSHKEY"} ${USEROPTS:--a -g 'audio video netdev'}
for i in "$ROOT"home/*; do
	if [ -d "$i" ]; then
		lbu add $i
	fi
done

setup-sshd ${ROOTSSHKEY+-k "$ROOTSSHKEY"} ${SSHDOPTS}
root_keys="$ROOT"/root/.ssh/authorized_keys
if [ -f "$root_keys" ]; then
	lbu add "$ROOT"/root
fi

if is_xen_dom0; then
	setup-xen-dom0
fi

if [ "$rc_sys" = "LXC" ]; then
	exit 0
fi

DEFAULT_DISK=none \
	setup-disk -w /tmp/alpine-install-diskmode.out -q ${DISKOPTS} || exit

diskmode=$(cat /tmp/alpine-install-diskmode.out 2>/dev/null)

# setup lbu and apk cache unless installed sys on disk
if [ "$diskmode" != "sys" ]; then
	setup-lbu ${LBUOPTS}
	setup-apkcache ${APKCACHEOPTS}
	if [ -L "$ROOT"/etc/apk/cache ]; then
		apk cache sync
	fi
fi