forked from JamiKettunen/void-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.sh.in
213 lines (193 loc) · 5.92 KB
/
setup.sh.in
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
#!/usr/bin/env bash
# This script is meant to be ran inside the new Void Linux installation.
#set -x # Debug
# Constants
############
COLOR_GREEN="@COLOR_GREEN@"
COLOR_BLUE="@COLOR_BLUE@"
COLOR_RED="@COLOR_RED@"
COLOR_RESET="@COLOR_RESET@"
DEF_MIRROR="@DEF_MIRROR@"
MIRROR="@MIRROR@"
RM_PKGS="@RM_PKGS@"
RM_FILES="@RM_FILES@"
BASE_PKGS="@BASE_PKGS@"
EXTRA_PKGS="@EXTRA_PKGS@"
ENABLE_SV="@ENABLE_SV@"
DISABLE_SV="@DISABLE_SV@"
HOSTNAME="@HOSTNAME@"
USERNAMES="@USERNAMES@"
USERS_PW_DEFAULT='@USERS_PW_DEFAULT@'
USERS_PW_ENCRYPTION="@USERS_PW_ENCRYPTION@"
USERS_GROUPS_COMMON="@USERS_GROUPS_COMMON@"
USERS_SHELL_DEFAULT="@USERS_SHELL_DEFAULT@"
USERS_SUDO_ASKPASS=@USERS_SUDO_ASKPASS@
PERMIT_ROOT_LOGIN=@PERMIT_ROOT_LOGIN@
# Runtime vars
###############
arch="$(xbps-uhelper arch)" # e.g. "aarch64-musl"
# Functions
############
error() { echo -e "${COLOR_RED}ERROR: $1${COLOR_RESET}" 1>&2; exit 1; }
log() { echo -e "${COLOR_GREEN}(rootfs) ${COLOR_BLUE}>>${COLOR_RESET} $1"; }
enable_sv() { local svd; for sv in $@; do svd=/etc/sv/$sv; [ -d $svd ] && ln -s $svd /etc/runit/runsvdir/default/ || error "Service '$sv' doesn't exist!"; done; }
disable_sv() { local svd; for sv in $@; do svd=/etc/sv/$sv; [ -d $svd ] && touch $svd/down || error "Service '$sv' doesn't exist!"; done; }
runas() { sudo -u $1 -i bash -c "$2"; }
cmd_exists() { command -v $1 >/dev/null; }
mirror_setup() {
[ "$MIRROR" = "$DEF_MIRROR" ] && return
log "Switching default package mirror to '$MIRROR'..."
cp /usr/share/xbps.d/*-repository-*.conf /etc/xbps.d/
sed -i "s|$DEF_MIRROR|$MIRROR|g" /etc/xbps.d/*-repository-*.conf
}
base_setup() {
if [[ "$arch" != *"-musl" ]]; then
log "Generating default locales for glibc..."
xbps-reconfigure -f glibc-locales
fi
log "Upgrading xbps..."
xbps-install -Su xbps -y
if [ "$RM_PKGS" ]; then
log "Removing some unneeded packages..."
xbps-remove -y $RM_PKGS || error "Failed to remove packages '$RM_PKGS'!"
fi
if [ "$RM_FILES" ]; then
log "Removing some unneeded files..."
local rm_patterns=() rm_excludes=() pattern
# FIXME: doesn't support spaces!
for pattern in $RM_FILES; do
if [[ "$pattern" != "!"* ]]; then
rm_patterns+=("$pattern")
else
rm_excludes+=(-not -path "${pattern:1}")
fi
done
find "${rm_patterns[@]}" -maxdepth 0 "${rm_excludes[@]}" -exec rm -rf {} + 2>/dev/null
fi
log "Upgrading all base packages..."
xbps-install -Su -y
log "Finishing base system setup..."
xbps-install base-system -y
xbps-remove -y base-voidstrap
}
stage1_setup() {
mirror_setup
base_setup
}
pkgs_setup() {
[ "$BASE_PKGS$EXTRA_PKGS" ] || return
log "Installing additional configured packages..."
xbps-install -Sy $BASE_PKGS $EXTRA_PKGS || error "Failed to install packages '$BASE_PKGS $EXTRA_PKGS'!"
if [ "$EXTRA_PKGS" ]; then
log "Repository-locking all built extra packages..."
xbps-pkgdb -m repolock $EXTRA_PKGS || error "Failed to repolock packages '$EXTRA_PKGS'!"
fi
}
sv_setup() {
if [ "$ENABLE_SV" ]; then
log "Enabling runit services $ENABLE_SV..."
enable_sv $ENABLE_SV
fi
if [ "$DISABLE_SV" ]; then
log "Disabling runit services $DISABLE_SV..."
disable_sv $DISABLE_SV
fi
}
user_setup() {
log "Performing user setup..."
local pw_conf=""
while IFS="" read line; do
local fields=() user="" pw="$USERS_PW_DEFAULT" id="" groups="$USERS_GROUPS_COMMON" \
shell="$USERS_SHELL_DEFAULT" fullname=""
IFS=':' read -ra fields <<< "$line"
user="${fields[0]}"
[ "${fields[1]}" ] && pw="${fields[1]}"
[ "${fields[2]}" ] && id="${fields[2]}"
[ "$pw" ] && pw_conf+="$user:$pw\n"
[ "${fields[4]}" ] && shell="${fields[4]}"
[ "${fields[5]}" ] && fullname="${fields[5]}"
if [ "$user" = "root" ]; then
[ -z "$pw" ] && passwd -d root
if [[ "$shell" && "$shell" != "/bin/sh" ]]; then
chsh -s $shell || error "Failed to change shell of root user to '$shell'!"
fi
if [ "$fullname" ]; then
chfn -f "$fullname" root || error "Failed to change full name of root user to '$fullname'!"
fi
continue
fi
if [ "${fields[3]}" ]; then
[ "$groups" ] && groups+=",${fields[3]}" || groups="${fields[3]}"
fi
local args=()
[ "$id" ] && args+=(-u $id)
[ "$groups" ] && args+=(-G $groups)
[ "$shell" ] && args+=(-s $shell)
[ "$fullname" ] && args+=(-c "$fullname")
useradd -m "${args[@]}" $user || error "Couldn't create new user $user!"
done <<< "$(</users.conf)"
shred -uz /users.conf
[ "$pw_conf" ] || return
local args=()
[ "$USERS_PW_ENCRYPTION" ] && args+=(-c $USERS_PW_ENCRYPTION) || args+=(-e)
printf "${pw_conf::-2}" | chpasswd ${args[@]}
local nopasswd=""
$USERS_SUDO_ASKPASS || nopasswd=" NOPASSWD:"
echo "%wheel ALL=(ALL)${nopasswd} ALL" > /etc/sudoers.d/wheel
chmod 400 /etc/sudoers.d/wheel
}
root_ssh_setup() {
$PERMIT_ROOT_LOGIN || return
log "Allowing login as root via SSH..."
sed 's/^#PermitRootLogin.*/PermitRootLogin yes/' -i /etc/ssh/sshd_config
}
hostname_setup() {
[ "$HOSTNAME" ] || return
log "Setting system hostname to $HOSTNAME..."
echo "$HOSTNAME" > /etc/hostname
}
fix_root_perms() {
log "Setting /root permissions to 700 (from $(stat -c '%a' /root))..."
chmod 700 /root
}
run_custom_script() {
local custom_script="/mkrootfs.custom.sh"
[ -f "$custom_script" ] || return
log "Running $custom_script..."
. "$custom_script"
rm "$custom_script"
}
do_cleanup() {
log "Removing installed orphan packages..."
xbps-remove -o -y
if [ ! -d /pkgcache ]; then
local cache_size="$(du -sh /var/cache/xbps | awk '{print $1}')"
log "Cleaning $cache_size of cached packages..."
rm /var/cache/xbps/*
fi
}
finalize_setup() {
if cmd_exists updatedb; then
log "Updating mlocate database..."
sudo updatedb
fi
if cmd_exists fake-hwclock; then
log "Saving current time for fake-hwclock to restore on initial boot..."
fake-hwclock save "\$(date +'%Y-%m-%d %H:%M:%S')"
fi
}
stage2_setup() {
pkgs_setup
sv_setup
user_setup
root_ssh_setup
hostname_setup
fix_root_perms
run_custom_script
do_cleanup
finalize_setup
}
# Script
#########
stage="$1"
[ "$stage" ] && ${stage}_setup