-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_in_one.sh
executable file
·252 lines (204 loc) · 7.22 KB
/
all_in_one.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
#!/usr/bin/env bash
set -e
curp=$(dirname "$0")
export http_proxy=http://10.40.30.80:3128/
# load .env
if [ -f "$curp/.env" ]; then
source "$curp/.env"
else
echo "File $curp/.env does not exist!"
echo "Copy $curp/.env.example as $curp/.env and edit it first!"
exit 1
fi
if [ "$AAI_FORMAT" -eq "1" ]; then
parted "$AAI_DEVICE" --script -- mklabel msdos
parted "$AAI_DEVICE" --script -- mkpart primary 0% 100%
mkfs.ext4 -F "$AAI_PARTITION"
fi
mountpoint -q "$AAI_MNT" || mount $AAI_PARTITION "$AAI_MNT"
if [ "$AAI_MIRROR" == "yes" ]; then
cp "$curp/mirrorlist" /etc/pacman.d/mirrorlist
fi
# fix for older archiso file - update database and keyring first
http_proxy=http://10.40.30.80:3128/ pacman -Syv --noconfirm archlinux-keyring
if [ "$AAI_SKIP_INIT" == "no" ]; then
if [ "$(hostname)" == "archiso" ]; then
# do not use host cache on arch iso
http_proxy=http://10.40.30.80:3128/ pacstrap "$AAI_MNT" base base-devel
else
http_proxy=http://10.40.30.80:3128/ pacstrap -c "$AAI_MNT" base base-devel
fi
fi
pkgs=()
cmds=()
case "${AAI_TYPE}" in
vm)
while read -r pkg; do
pkgs+=("${pkg}")
# install and enable qemu-guest-agent.service
cmds+=("systemctl enable qemu-guest-agent.service")
done < <(cat "$curp"/pkgs/all.txt "$curp"/pkgs/vm.txt | sort | uniq)
;;
desktop)
while read -r pkg; do
pkgs+=("${pkg}")
done < <(cat "$curp"/pkgs/all.txt "$curp"/pkgs/host.txt "$curp"/pkgs/desktop.txt | sort | uniq)
;;
server)
while read -r pkg; do
pkgs+=("${pkg}")
done < <(cat "$curp"/pkgs/all.txt "$curp"/pkgs/host.txt "$curp"/pkgs/server.txt | sort | uniq)
;;
*)
echo "Unknown machine type!!"
exit 1
;;
esac
if [ "${AAI_DOCKER}" == "y" ]; then
pkgs+=("docker" "docker-compose")
cmds+=("usermod -aG docker \"${AAI_USER}\"")
cmds+=("systemctl enable docker.service")
fi
if [ "${AAI_TIME_SYNC}" == "y" ]; then
cmds+=("timedatectl set-ntp true")
cmds+=("systemctl enable systemd-timesyncd.service")
cmds+=("hwclock --systohc")
fi
if [ ! -z "${AAI_SWAPFILE}" ]; then
cmds+=("fallocate -l \"${AAI_SWAPFILE}\" /swapfile")
cmds+=("chmod 600 /swapfile")
cmds+=("mkswap /swapfile")
fi
if [ "${AAI_LIBVIRT}" == "y" ]; then
cmds+=("echo br_netfilter > /etc/modules-load.d/br-netfilter.conf")
cmds+=("echo net.bridge.bridge-nf-call-ip6tables=0 >> /etc/sysctl.d/99-sysctl.conf")
cmds+=("echo net.bridge.bridge-nf-call-iptables=0 >> /etc/sysctl.d/99-sysctl.conf")
cmds+=("echo net.bridge.bridge-nf-call-arptables=0 >> /etc/sysctl.d/99-sysctl.conf")
cmds+=("systemctl enable libvirtd.service")
pkgs+=("libvirt")
pkgs+=("qemu")
pkgs+=("openbsd-netcat")
pkgs+=("bridge-utils")
fi
if [ "${AAI_DHCP}" == "y" ]; then
cmds+=("systemctl enable dhcpcd.service")
fi
genfstab -U "$AAI_MNT" >"$AAI_MNT"/etc/fstab
arch-chroot "$AAI_MNT" <<EOF
set -o errexit
export http_proxy=http://10.40.30.80:3128/
ln -sf /usr/share/zoneinfo/"${AAI_TIMEZONE}" /etc/localtime
# setup locales
echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
echo "bg_BG.UTF-8 UTF-8" > /etc/locale.gen
locale-gen
echo "LANG=en_US.UTF-8" > /etc/locale.conf
echo "LC_COLLATE=C" >> /etc/locale.conf
echo "$AAI_HOSTNAME" > /etc/hostname
echo "127.0.1.1 ${AAI_HOSTNAME}.localdomain ${AAI_HOSTNAME}" >> /etc/hosts
# sysctl customizations - truncate with first command
echo "fs.inotify.max_user_watches=524288" > /etc/sysctl.d/99-sysctl.conf
echo "fs.file-max=131072" >> /etc/sysctl.d/99-sysctl.conf
# pacman
# workaround for conflict between iptables -> iptables-nft
http_proxy=http://10.40.30.80:3128/ pacman -S iptables-nft --noconfirm --ask 4
http_proxy=http://10.40.30.80:3128/ pacman --noconfirm -S $(echo ${pkgs[*]// /|})
if [ ! -d "/home/$AAI_USER" ]; then
useradd --create-home "$AAI_USER" --shell /usr/bin/zsh
groupadd sudo
usermod -aG video,audio,scanner,lp,sudo $AAI_USER
fi
# ssh daemon
systemctl enable sshd.service
# cron service
systemctl enable cronie.service
EOF
for cmd in "${cmds[@]}"; do
arch-chroot "$AAI_MNT" <<EOF
set -o errexit
${cmd}
EOF
done
arch-chroot "$AAI_MNT" <<EOF
echo "${AAI_USER}:${AAI_PASSWORD}" | chpasswd
EOF
# setup sudoers file
cat >"$AAI_MNT"/etc/sudoers <<EOF
root ALL=(ALL) ALL
%sudo ALL=(ALL) ALL
Defaults insults
EOF
# desktop machine - setup trizen and install aur pacakges
if [ "${AAI_TYPE}" == "desktop" ]; then
arch-chroot "$AAI_MNT" <<EOF
set -o errexit
# force sudo to use -A for the install
echo -ne '#!/usr/bin/env bash\necho '"${AAI_PASSWORD}"'\n' > /usr/local/bin/spit_pass.sh && chmod +x /usr/local/bin/spit_pass.sh
echo -ne '#!/bin/bash\n SUDO_ASKPASS=/usr/local/bin/spit_pass.sh /usr/bin/sudo -A "\$@"\n' > /usr/local/bin/sudo && chmod +x /usr/local/bin/sudo
EOF
# sync trizen cache
[ -d "/home/${AAI_USER}/.cache/trizen" ] && rsync -a /home/${AAI_USER}/.cache/trizen "${AAI_MNT}"/home/${AAI_USER}/.cache
arch-chroot "${AAI_MNT}" "mkdir -pv /work && chown ${AAI_USER}:${AAI_USER} /work"
#setup user enviroment
arch-chroot "${AAI_MNT}" su -l "${AAI_USER}" <<EOF
set -o errexit
mkdir -pv /work/dev/personal
[ ! -d /work/dev/personal/i3wmonarch ] && git clone https://github.com/eldiaboloz/i3wmonarch.git /work/dev/personal/i3wmonarch
[ ! -d /work/dev/trizen ] && git clone https://aur.archlinux.org/trizen.git /work/dev/trizen
alias sudo="sudo -A"
cd /work/dev/trizen && makepkg --noconfirm -si
# dummy call so config is created
trizen --help > /dev/null
# persist trizen cache
sed -i 's#/tmp/trizen-$AAI_USER#\$ENV{HOME}/.cache/trizen#' \$HOME/.config/trizen/trizen.conf
# use modifed sudo command
sed -i 's#/usr/bin/sudo#/usr/local/bin/sudo#' \$HOME/.config/trizen/trizen.conf
# install aur packages
trizen --noconfirm -S ttf-font-icons skypeforlinux-stable-bin jetbrains-toolbox
cd /work/dev/personal/i3wmonarch && git submodule update --init --recursive && ./scripts/i3wm/create_symlinks.sh
cd /work/dev/personal/i3wmonarch/github.com/nonpop/xkblayout-state && make
cd /work/dev/personal/i3wmonarch/github.com/powerline/fonts && ./install.sh
EOF
fi
# ssh access
mkdir -pv "$AAI_MNT"/root/.ssh
chmod 0700 "$AAI_MNT"/root/.ssh
[ -f "$curp/root_authorized_keys" ] &&
cp "$curp/root_authorized_keys" "$AAI_MNT"/root/.ssh/authorized_keys
if [ "$AAI_INTEL_BL" == "yes" ]; then
cat >"$AAI_MNT"/etc/udev/rules.d/backlight.rules <<EOF
ACTION=="add", SUBSYSTEM=="backlight", KERNEL=="intel_backlight", RUN+="/bin/chgrp video /sys/class/backlight/%k/brightness"
ACTION=="add", SUBSYSTEM=="backlight", KERNEL=="intel_backlight", RUN+="/bin/chmod g+w /sys/class/backlight/%k/brightness"
EOF
fi
# @TODO: setup trizen
# bootloader - grub
arch-chroot "$AAI_MNT" mkinitcpio -p linux
# add options to restart / shutdown
cat >>"$AAI_MNT"/etc/grub.d/40_custom <<'EOF'
#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries. Simply type the
# menu entries you want to add after this comment. Be careful not to change
# the 'exec tail' line above.
menuentry "System shutdown" {
echo "System shutting down..."
halt
}
menuentry "System restart" {
echo "System rebooting..."
reboot
}
EOF
cat >>"$AAI_MNT"/etc/default/grub <<'EOF'
GRUB_DEFAULT=saved
GRUB_TIMEOUT=5
GRUB_SAVEDEFAULT="true"
GRUB_DISABLE_SUBMENU=y
GRUB_CMDLINE_LINUX_DEFAULT="quiet i915.modeset=0"
EOF
arch-chroot "$AAI_MNT" <<EOF
grub-install --target=i386-pc "$AAI_DEVICE"
grub-mkconfig -o /boot/grub/grub.cfg
echo "root:${AAI_ROOT_PASSWORD}" | chpasswd
EOF