-
Notifications
You must be signed in to change notification settings - Fork 182
/
hook_postinstall_all.sample.sh
261 lines (189 loc) · 9.83 KB
/
hook_postinstall_all.sample.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
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
#!/bin/bash
#
# Sample global post-install hook for the WSL distribution switcher.
# Supports Arch Linux, APT (Debian, Ubuntu..) and RPM-based (Fedora, CentOS..) distributions.
# Remove ".sample" from file name for install.py to run it automatically after installation.
#
# Accepts the following environmental variables:
# REGULARUSER -- name of your regular user; sent by install.py automatically.
# WINVER -- build version of your Windows; sent by install.py automatically.
# ROOTPASSWD -- root password to set; password is not reset if nothing is set.
# SUDONOPASSWD -- if set to "1", regular user will be added to sudoers with NOPASSWD.
# WITHOUTPACAUR -- if set to "1", AUR helper pacaur will not be installed under Arch.
#
# Installs the following packages:
# locale, apt-utils, dialog -- Debian only, to fix apt/dpkg warnings
# passwd, sudo -- Both, changes root password and adds regular user to sudoers
# fakeroot, pacaur, base-devel -- Arch Linux only, patched fakeroot to use TCP instead of Unix sockets
# tar, gzip, bzip2, xz, squashfs-tools, vim, git, tmux, curl, wget -- Both, but not crucial per-se
#
logerr () { echo -e "\e[91m[!]\e[39m $@" 1>&2; }
logwrn () { echo -e "\e[93m[!]\e[39m $@"; }
log () { echo -e "\e[92m[*]\e[39m $@"; }
# detect package manager
log "Detecting operating system..."
DEB=$(test -f /usr/bin/dpkg && /usr/bin/dpkg --search /usr/bin/dpkg >> /dev/null 2>&1; test "$?" != "0"; echo "$?")
RPM=$(test -f /usr/bin/rpm && /usr/bin/rpm -q -f /usr/bin/rpm >> /dev/null 2>&1; test "$?" != "0"; echo "$?")
PAC=$(test -f /usr/bin/pacman && /usr/bin/pacman -Qo /usr/bin/pacman >> /dev/null 2>&1; test "$?" != "0"; echo "$?")
if [[ "${DEB}" == 1 ]]; then
export DEBIAN_FRONTEND=noninteractive
mgr="apt-get -y"
mgrinst="${mgr} install"
echo -e 'Dpkg::Progress-Fancy "1";\nAPT::Color "1";' >> /etc/apt/apt.conf.d/99progressbar
elif [[ "${RPM}" == 1 ]]; then
if type -P dnf >/dev/null; then
mgr="dnf -y"
echo -e "\ncolor=always" >> /etc/dnf/dnf.conf
#echo -e "\nfastestmirror=true" >> /etc/dnf/dnf.conf
else
mgr="yum -y"
echo -e "\ncolor=always" >> /etc/yum.conf
#echo -e "\nfastestmirror=true" >> /etc/yum.conf
fi
mgrinst="${mgr} install"
elif [[ "${PAC}" == 1 ]]; then
mgr="pacman --noconfirm"
mgrinst="${mgr} --needed -S"
sed -i 's/^#Color$/Color/' /etc/pacman.conf
if [[ "${WINVER}" -lt 14936 ]]; then
# fake chroot() for pacman in order to prevent installation failures during initial update
echo '/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4Bf/Bd1dAD+RRYRoPYmm2orhgzJO2Qbq2d9uE6E+KKoGTNXlICBW5N7Tnzp0H1h/VMBCQjKEQ+/SpKVIi7FPS0yuYJZBuh35lkeCGXFsgHGoGUSCdrCEBNxPWFy/ZHBCunBrpaXpcUTrEnNcjDMaxnz3x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qZYFZEcFwYQVBTTiuFGf0bIwVRbZONgvR2mGeRQTBrAqPp4xlGQjsWBHjIr11s076LyDjUDDU1PvGsss69+83un/tKIDIhmx8nLKi3qQGQjjFsKMuqS99d8J2Z6+9tx7V48GMB4dCjdSsVZmoKsbBtvd0UnYU0QVv1D12VXxjEkfNxHtoHU1R5PXCslrlsZEGq3BpndujrcAXjEkYG4/k3HkiRLO/tpYwVkM+ejhRKA4heTNr6qTvbHiDRkQitAimSx8ou+VTy7ht4yxF0D6C17Et7Q9zZtnRXmBLbgIBxBXz6DwKq2I2Q53MAF+ZbgFKYpxoK0jq4LrV9LXItUeypjr1dG/SMeXcTB2tEAXMVp8mkJIp6/SHUq5XFiQI6Dz83c52pU2mz3HF/cIHl6OR7Ztxvw+Y7RBAoSxUlxaSycaFpiJGxQWHiZC8S6oNUCCIe5xEOVW1zUAcH3yOvl/a+cDqZhKPz8W/1yzH0A+a4GfZV3cOmuUl6ip12mF2y6QT5uyS6OG0nZqPaTufSneiU/S3Oq2bFvehV5RuGfnsGTuqCF8CwDkKh5NOCUJ7uplLAMnp4k/en5+/Cwq5/pIJg2QjQgFKP6r2OBtjIUCCRrBp9umQj62lZ94NObu4Vt+nvwO+XpmDP4y+ubHa/4dwL7mez2gmxSf4MAGaR4BfXxoQcKyEW8I11D/z7Oa7s+FZ48DfNeCBtkF99B6egn+3rWR40nheG+NPMOYP2O3RqQPtYbHjZIQJxr9UkrzvjcpgcUghdBkJygAAAAAAmaFzKeD+DBsAAfkLgDAAALb+Kk+xxGf7AgAAAAAEWVo=' | base64 -d | xz -d > /lib64/libmockchroot.so
echo '/lib64/libmockchroot.so' > /etc/ld.so.preload
# it's shipped as a binary blob, since otherwise it would require gcc to compile,
# which is not available before pacman is set up. to recreate blob above, run:
# wget https://gist.githubusercontent.com/RoliSoft/84813cc353caec614dee8bf74c1b09ef/raw/409978e320084e645b60d86dff7775dc06e9bc1e/libmockchroot.c -O libmockchroot.c
# gcc libmockchroot.c -shared -fPIC -ldl -o libmockchroot.so
# strip -s libmockchroot.so
# cat libmockchroot.so | xz | base64
fi
else
echo "Unsupported operating system." 1>&2; exit 1
fi
# update to prevent installation failures later on
log "Initiating system upgrade..."
if [[ "${DEB}" == 1 ]]; then
${mgr} update && ${mgr} dist-upgrade && ${mgr} install apt-utils dialog locales
elif [[ "${RPM}" == 1 ]]; then
${mgr} upgrade
elif [[ "${PAC}" == 1 ]]; then
# update db and install super-essentials
${mgr} -Sy
${mgrinst} archlinux-keyring
if [[ "${WINVER}" -lt 14936 ]]; then
# switch primitive chroot mocker which always returns true,
# to fakechroot which actually rewrites the paths
${mgrinst} fakechroot
echo '/usr/lib/libfakeroot/fakechroot/libfakechroot.so' > /etc/ld.so.preload
fi
# continue with system upgrade
${mgr} -Su
pacman-db-upgrade
${mgr} -S ca-certificates-mozilla gawk
fi
# fix locale warnings
if [[ "${DEB}" == 1 || "${PAC}" == 1 ]]; then
log "Fixing locale warnings..."
if [[ -f /usr/share/i18n/charmaps/UTF-8.gz ]]; then
if ! type -P gzip >/dev/null; then
${mgrinst} gzip
fi
gzip -d /usr/share/i18n/charmaps/UTF-8.gz
fi
if ! grep -q -E "^\s*en_US\.UTF-8" /etc/locale.gen; then
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
fi
locale-gen
fi
# fix root login by resetting the password
# this works on most distributions I tried, on others it might require --stdin or chpasswd
if [[ ! -z "${ROOTPASSWD}" ]]; then
log "Resetting root password..."
if ! type -P passwd >/dev/null; then
${mgrinst} passwd
fi
echo -e "$ROOTPASSWD\n$ROOTPASSWD" | passwd
fi
# install sudo and edit sudoers
log "Setting up sudo..."
if ! type -P sudo >/dev/null; then
${mgrinst} sudo
fi
if [[ ! -z "${REGULARUSER}" ]]; then
if [[ "${SUDONOPASSWD}" == "1" ]]; then
if ! grep -q -E "${REGULARUSER}.*?ALL.*?NOPASSWD.*?ALL" /etc/sudoers; then
echo -e "\n${REGULARUSER} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
fi
else
group="sudo"
umpkg="passwd"
if [[ "${DEB}" != 1 ]]; then
group="wheel"
umpkg="shadow-utils"
if ! grep -q -E "^\s*%wheel" /etc/sudoers && grep -q -E "^\s*#\s*%wheel" /etc/sudoers; then
sed -i '0,/%wheel/{s/^\s*#\s*//}' /etc/sudoers
fi
fi
if ! type -P usermod >/dev/null; then
${mgrinst} ${umpkg}
fi
usermod -aG "${group}" "${REGULARUSER}"
fi
fi
# Make resolution work
echo "# This file was automatically generated by WSL. To stop automatic generation of this file, remove this line." > /etc/resolv.conf
# fix sudo hostname resolution warning
if ! grep -q "${HOSTNAME}" /etc/hosts; then
log "Fixing hostname resolution warnings with sudo..."
echo -e "\n127.0.1.1 ${HOSTNAME}.localdomain ${HOSTNAME}" >> /etc/hosts
fi
# install TCP version of fakeroot for Arch
if [[ "${PAC}" == 1 ]]; then
log "Installing patched fakeroot..."
# install original fakeroot and dependencies for makepkg
${mgrinst} base-devel git po4a
# overwrite standard fakeroot with a temporary pass-through script
# since we are root at this point, the bypass script has no side-effect
echo -e '#!/bin/bash\nif [[ "$1" == "-v" ]]; then\n\techo 1.0; exit\nfi\nexport FAKEROOTKEY=1\nexec "$@"\nunset FAKEROOTKEY' > /usr/bin/fakeroot
chmod +x /usr/bin/fakeroot
export PATH="/usr/bin/core_perl:/usr/bin/vendor_perl:$PATH"
# download and patch the fakeroot package from ABS
pushd $(mktemp -d)
git clone git://git.archlinux.org/svntogit/packages.git --depth=1 --branch=packages/fakeroot && cd packages/trunk
sed -i 's/--with-ipc=sysv$/--with-ipc=tcp/' PKGBUILD
# patch mkpkg to run as root temporarily and build package
sed -i 's/EUID\s*==\s*0/EUID == 99999/' /usr/bin/makepkg
makepkg -si --noconfirm
sed -i 's/EUID\s*==\s*99999/EUID == 0/' /usr/bin/makepkg
# update pacman config to ignore fakeroot updates, as those use --with-ipc=sysv
sed -i -e 's/IgnorePkg\s*=/IgnorePkg = fakeroot /' -e 's/^\s*#\s*IgnorePkg/IgnorePkg/' /etc/pacman.conf
# cleanup
cd ../.. && rm -rf packages
popd
fi
# install pacaur for Arch
if [[ "${PAC}" == 1 && "${WITHOUTPACAUR}" != "1" ]]; then
log "Installing pacaur..."
# preinstall pacaur dependencies
${mgrinst} yajl expac
gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 487EACC08557AD082088DABA1EB2638FF56C0C53
pushd $(mktemp -d)
sed -i 's/EUID\s*==\s*0/EUID == 99999/' /usr/bin/makepkg
# download and compile cower
git clone https://aur.archlinux.org/cower.git --depth=1 && cd cower
makepkg -si --noconfirm
cd .. && rm -rf cower
# download and compile pacaur
git clone https://aur.archlinux.org/pacaur.git --depth=1 && cd pacaur
makepkg -si --noconfirm
cd .. && rm -rf pacaur
sed -i 's/EUID\s*==\s*99999/EUID == 0/' /usr/bin/makepkg
popd
fi
# install some basics
log "Installing tools..."
if [[ "${DEB}" == 1 ]]; then
distspec="xz-utils"
elif [[ "${RPM}" == 1 || "${PAC}" == 1 ]]; then
distspec="xz"
fi
${mgrinst} vim tmux git curl wget tar gzip bzip2 squashfs-tools ${distspec}