forked from kennwhite/digitalocean-alpine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigitalocean-alpine.sh
executable file
·185 lines (150 loc) · 5.18 KB
/
digitalocean-alpine.sh
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
#!/bin/sh
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
logfile="/tmp/digitalocean-alpine.log"
if [ "$1" = "--step-chroot" ]; then
printf "" > "$logfile"
printf " Installing packages..." >&2
cat <<EOF > /etc/apk/keys/layeh.com-5b313ebb.rsa.pub
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8OZrGEUMGjd2oAkYb+qu
rIT7k5FFS5zP6v/YwOOmbT4iQMHlkEP/Aj1PhKZt4FiirFm3fpVKjJa9uPeWRVC4
eqZ+o9e5xm+2Sb8+Ljn617y2Yzb5kxRyE+1pOuA9WfROZdE+VNvkgcReql6tu19F
qHj+hwCf3vNsTFeDiiyFKH4UAATR6eolKHGRqk66L3nbRlHvZbODqUcyOeUEXKp7
ntnM2l6VIxMDHCxbZ9/cu4o/KjW2iT3802D4EWxPT3eksdZERgSVPTJrKskMzey+
5rqLXTu2NU+V7E+UjQ6hlavc2139CQb3y4smmdpzQTnmUfg281kb9Be0KIDfcOdR
VwIDAQAB
-----END PUBLIC KEY-----
EOF
echo "https://cdn.layeh.com/alpine/3.9/" >> /etc/apk/repositories
if ! apk add --no-cache alpine-base linux-virt syslinux grub grub-bios e2fsprogs eudev openssh rng-tools rng-tools-openrc digitalocean-alpine docker >>"$logfile" 2>>"$logfile"; then
echo
exit 1
fi
echo " Done" >&2
printf " Configuring services..." >&2
rc-update add --quiet hostname boot
rc-update add --quiet networking boot
rc-update add --quiet urandom boot
rc-update add --quiet crond default
rc-update add --quiet swap boot
rc-update add --quiet udev sysinit
rc-update add --quiet udev-trigger sysinit
rc-update add --quiet sshd default
rc-update add --quiet digitalocean boot
rc-update add --quiet rngd boot
rc-update add --quiet docker boot
adduser -SDHs /sbin/nologin dockremap
addgroup -S dockremap
echo dockremap:$(cat /etc/passwd|grep dockremap|cut -d: -f3):65536 >> /etc/subuid
echo dockremap:$(cat /etc/passwd|grep dockremap|cut -d: -f4):65536 >> /etc/subgid
mkdir -p /etc/docker
cat <<EOF > /etc/docker/daemon.json
{
"userns-remap": "dockremap",
"experimental": false,
"live-restore": true,
"ipv6": false,
"icc": false,
"no-new-privileges": false
}
EOF
chmod 0644 -R /etc/docker
sed -i -r -e 's/^(tty[2-6]:)/#\1/' /etc/inittab
echo "/dev/vdb /media/cdrom iso9660 ro 0 0" >> /etc/fstab
echo " Done" >&2
printf " Installing bootloader..." >&2
if ! grub-install /dev/vda >>"$logfile" 2>>"$logfile"; then
echo
exit 1
fi
if ! grub-mkconfig -o /boot/grub/grub.cfg >>"$logfile" 2>>"$logfile"; then
echo
exit 1
fi
sync
echo " Done" >&2
rm -f "$0"
exit 0
fi
if [ "$1" != "--rebuild" ]; then
echo "usage: digitalocean-alpine --rebuild" >&2
echo " Rebuild the current droplet with Alpine Linux" >&2
echo >&2
echo " WARNING: This is a destructive operation. You will lose your data." >&2
echo " This script has only been tested with Debian 9.7 x64 droplets." >&2
exit 1
fi
if [ -f /etc/alpine-release ]; then
echo "digitalocean-alpine: Alpine Linux already installed" >&2
exit 0
fi
if [ "$(id -u)" -ne "0" ]; then
echo "digitalocean-alpine: script must be run as root" >&2
exit 1
fi
SCRIPTPATH="$(realpath "$0")"
if [ ! -x "$SCRIPTPATH" ]; then
echo "digitalocean-alpine: script must be executable" >&2
exit 1
fi
printf "Downloading Alpine 3.9.1..." >&2
if ! wget -q -O /tmp/rootfs.tar.gz http://dl-cdn.alpinelinux.org/alpine/v3.9/releases/x86_64/alpine-minirootfs-3.9.1-x86_64.tar.gz; then
echo " Failed!" >&2
exit 1
fi
echo " Done" >&2
printf "Verifying SHA256 checksum..." >&2
if ! echo "0278fcf10a12dce14c0be7d57b2daf8fef90449b7c388a49e52f7e843c38b3b3 /tmp/rootfs.tar.gz" | sha256sum -c >/dev/null 2>&1; then
echo " Failed!" >&2
exit 1
fi
echo " Done" >&2
printf "Creating mount points..." >&2
umount -a >/dev/null 2>&1
mount -o rw,remount --make-rprivate /dev/vda1 /
mkdir /tmp/tmpalpine
mount none /tmp/tmpalpine -t tmpfs
echo " Done" >&2
printf "Extracting Alpine..." >&2
tar xzf /tmp/rootfs.tar.gz -C /tmp/tmpalpine
cp "$SCRIPTPATH" /tmp/tmpalpine/tmp/digitalocean-alpine.sh
echo " Done" >&2
printf "Copying existing droplet configuration..." >&2
cp /etc/fstab /tmp/tmpalpine/etc
cp /etc/hostname /tmp/tmpalpine/etc
cp /etc/resolv.conf /tmp/tmpalpine/etc
grep -v ^root: /tmp/tmpalpine/etc/shadow > /tmp/tmpalpine/etc/shadow.bak
mv /tmp/tmpalpine/etc/shadow.bak /tmp/tmpalpine/etc/shadow
grep ^root: /etc/shadow >> /tmp/tmpalpine/etc/shadow
mkdir -p /tmp/tmpalpine/etc/ssh
cp -r /etc/ssh/ssh_host_* /tmp/tmpalpine/etc/ssh
cp -r /root/.ssh /tmp/tmpalpine/root
echo " Done" >&2
printf "Changing to new root..." >&2
mkdir /tmp/tmpalpine/oldroot
pivot_root /tmp/tmpalpine /tmp/tmpalpine/oldroot
cd / || exit 1
echo " Done" >&2
printf "Rebuilding file systems..." >&2
mount --move /oldroot/dev /dev
mount --move /oldroot/proc /proc
mount --move /oldroot/sys /sys
mount --move /oldroot/run /run
rm -rf /oldroot/*
cp -r /bin /etc /home /lib/ /media /mnt/ /root /sbin /srv /tmp /usr /var /oldroot
mkdir /oldroot/dev /oldroot/proc /oldroot/sys /oldroot/run
mount -t proc proc /oldroot/proc
mount -t sysfs sys /oldroot/sys
mount -o bind /dev /oldroot/dev
echo " Done" >&2
echo "chroot configuration..." >&2
if ! chroot /oldroot /bin/ash /tmp/digitalocean-alpine.sh --step-chroot; then
echo "ERROR: could not install Alpine Linux. See /oldroot$logfile" >&2
exit 1
fi
echo "Rebooting system. You should be able to reconnect shortly." >&2
reboot
sleep 1
reboot