setup-alpine.in 4.79 KB
Newer Older
1 2 3
#!/bin/sh

PROGRAM=setup-alpine
4
VERSION=@VERSION@
5 6 7 8

PREFIX=
. $PREFIX/lib/libalpine.sh

9 10 11 12 13 14 15 16 17 18
# Extract fully qualified domain name from current hostname. If none is
# currently set, use 'my.domain'.
get_fqdn() {
	local _dn
	_dn=$(hostname -f 2>/dev/null)
	_dn=${_dn#$(hostname -s 2>/dev/null)}
	_dn=${_dn#.}
	echo "${_dn:=my.domain}"
}

19 20 21 22
is_qemu() {
	grep -q "QEMU" /proc/cpuinfo
}

23
usage() {
24 25 26 27 28 29 30 31
	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)
32
		 -e  Empty root password
33 34 35 36
		 -f  Answer file to use installation
		 -h  Show this help
		 -q  Quick mode. Ask fewer questions.
	__EOF__
37
	exit 1
38 39
}

40
while getopts "aef:c:hq" opt ; do
41 42
	case $opt in
		a) ARCHIVE=yes;;
43
		f) USEANSWERFILE="$OPTARG";;
44
		c) CREATEANSWERFILE="$OPTARG";;
45
		e) empty_root_password=1;;
46
		h) usage;;
47
		q) quick=1; APKREPOSOPTS="-1"; HOSTNAMEOPTS="alpine";;
48 49 50
		*) usage;;
	esac
done
51
shift $(expr $OPTIND - 1)
52

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

60 61 62 63 64 65 66 67
if [ "$USEANSWERFILE" != "" ]; then
	if [ -e "$USEANSWERFILE" ]; then
		. "$USEANSWERFILE"
	fi
fi

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

72 73
		# Use US layout with US variant
		KEYMAPOPTS="us us"
74

75 76
		# Set hostname to alpine-test
		HOSTNAMEOPTS="-n alpine-test"
77

78 79 80
		# Contents of /etc/network/interfaces
		INTERFACESOPTS="auto lo
		iface lo inet loopback
81

82 83 84 85
		auto eth0
		iface eth0 inet dhcp
		    hostname alpine-test
		"
86

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

90 91
		# Set timezone to UTC
		TIMEZONEOPTS="-z UTC"
92

93 94
		# set http/ftp proxy
		PROXYOPTS="http://webproxy:8080"
95

96 97
		# Add a random mirror
		APKREPOSOPTS="-r"
98

99 100
		# Install Openssh
		SSHDOPTS="-c openssh"
101

102 103
		# Use openntpd
		NTPOPTS="-c openntpd"
104

105 106
		# Use /dev/sda as a data disk
		DISKOPTS="-m data /dev/sda"
107

108 109 110
		# Setup in /media/sdb1
		LBUOPTS="/media/sdb1"
		APKCACHEOPTS="/media/sdb1/cache"
111

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

117 118 119 120
if [ "$ARCHIVE" ] ; then
	echo "Creating an Alpine overlay"
	init_tmpdir ROOT
else
121
	PKGADD="apk add"
122 123
fi

124 125 126 127
if [ "$rc_sys" != LXC ]; then
	$PREFIX/sbin/setup-keymap ${KEYMAPOPTS}
	$PREFIX/sbin/setup-hostname ${HOSTNAMEOPTS}
fi
128 129 130 131 132 133

if [ -n "$INTERFACESOPTS" ]; then
	printf "$INTERFACESOPTS" | $PREFIX/sbin/setup-interfaces -i
else
	$PREFIX/sbin/setup-interfaces
fi
134 135
# start the networking
/etc/init.d/networking --quiet start >/dev/null
136 137 138

# setup up dns if no dhcp was configured
grep '^iface.*dhcp' $ROOT/etc/network/interfaces > /dev/null ||\
139
	$PREFIX/sbin/setup-dns ${DNSOPTS}
140 141

# set root password
142 143 144 145 146
if [ -z "$NOCOMMIT" ] && [ -z "$empty_root_password" ]; then
	while ! passwd ; do
		echo "Please retry."
	done
fi
147

148 149 150 151
if [ -z "$quick" ]; then
	# pick timezone
	$PREFIX/sbin/setup-timezone ${TIMEZONEOPTS}
fi
152

153 154
rc-update --quiet add networking boot
rc-update --quiet add urandom boot
155 156 157 158 159
for svc in acpid cron crond; do
	if rc-service --exists $svc; then
		rc-update --quiet add $svc
	fi
done
Natanael Copa's avatar
Natanael Copa committed
160

161 162
# enable new hostname
/etc/init.d/hostname --quiet restart
163 164

# start up the services
165 166
openrc boot
openrc default
167

168 169 170 171 172 173 174 175 176 177 178 179 180
# 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;}' /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)} ${_hn} localhost.localdomain localhost/" /etc/hosts

181 182 183
if [ -z "$quick" ]; then
	$PREFIX/sbin/setup-proxy -q ${PROXYOPTS}
fi
184 185 186 187
# activate the proxy if configured
if [ -r "$ROOT/etc/profile" ]; then
	. "$ROOT/etc/profile"
fi
188

189
$PREFIX/sbin/setup-apkrepos ${APKREPOSOPTS}
190

191 192 193 194 195
# lets stop here if in "quick mode"
if [ "$quick" = 1 ]; then
	exit 0
fi

196
$PREFIX/sbin/setup-sshd ${SSHDOPTS}
197

198
if ! is_qemu && [ "$rc_sys" != "LXC" ]; then
199 200
	$PREFIX/sbin/setup-ntp ${NTPOPTS}
fi
201

202 203 204 205
if is_xen_dom0; then
	setup-xen-dom0
fi

206 207 208 209
if [ "$rc_sys" = "LXC" ]; then
	exit 0
fi

210
DEFAULT_DISK=none \
211
	$PREFIX/sbin/setup-disk -q ${DISKOPTS} || exit
212

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

# setup lbu and apk cache unless installed sys on disk
if [ "$diskmode" != "sys" ]; then
217 218
	$PREFIX/sbin/setup-lbu ${LBUOPTS}
	$PREFIX/sbin/setup-apkcache ${APKCACHEOPTS}
219 220 221
	if [ -L /etc/apk/cache ]; then
		apk cache sync
	fi
222
fi