-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·323 lines (270 loc) · 8.08 KB
/
setup.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#! /usr/bin/env bash
# inherit traps in subfunctions
set -E
main() {
if [[ $1 == userconf ]]; then
# helper to run configuration steps as desired user
xfce_conf
dotfiles
exit
elif [[ -n $1 ]]; then
local user="$1"
fi
if [[ $EUID -ne 0 ]]; then
sudo "$0" "$@"
exit
fi
local virt="$(vcheck)"
local user="$(getuser)"
echo "Running setup on $(vtype "$virt") host '$HOSTNAME' for user '$user'"
read -p "Press enter to continue..."
if [ "$virt" -eq 0 ]; then
vsetup
fi
setup "$user"
grubconf
grubtheme
userconf "$user"
read -t 10 -n 1 -p "Finished setup, rebooting in 10s - Press any key to continue..." || true
echo "Rebooting now"
sleep 1
reboot
}
getuser() {
if [[ -n $user ]]; then
echo "$user"
elif [[ $EUID -ne 0 ]]; then
echo "$USER"
elif [[ -n $SUDO_USER ]]; then
echo "$SUDO_USER"
elif [[ -n $LOGNAME ]]; then
echo "$LOGNAME"
else
echo "Failed to get username!"
return 1
fi
}
vcheck() {
# check if inside VM (setup virtualbox)
grep -q "^flags.*\ hypervisor" /proc/cpuinfo
echo $?
}
vtype() {
if [ "$1" -eq 0 ]; then
echo -n "virtual"
elif [ "$1" -eq 1 ]; then
echo -n "physical"
else
echo "ERROR: Unknown return code \"$1\" from vcheck"
fi
}
modconf() {
# set option if file if not existing yet, otherwise modify line
file="$1"
option="${2%%=*}"
value="${2#*=}"
if grep -q "^$option" "$file"; then
sed -i "s/^$option.*/$option=$value/" "$file"
else
echo "$option=$value" >> "$file"
fi
}
vsetup() {
vboxadd="/media/*/VBOXADDITIONS_*/autorun.sh"
vboxadd2="/media/*/VBox_GAs_*/autorun.sh"
while ! [ -f $vboxadd ] && ! [ -f $vboxadd2 ]; do
echo "Could not find vbox additions DVD under path '$vboxadd' or '$vboxadd2'!"
read -p "Press enter to retry..."
done
if ! [ -f $vboxadd ]; then
vboxadd="$vboxadd2"
fi
# build essentials for rebuilding the kernel (required)
sudo apt install -y build-essential gcc make perl dkms
eval $vboxadd
if [ -b /dev/cdrom ]; then
eject /dev/cdrom
elif [ -b /dev/sr0 ]; then
eject /dev/sr0
elif [ -b /dev/dvd ]; then
eject /dev/dvd
else
read -p "Cannot eject DVD, please remove disk manually, and press enter to continue..."
fi
}
setup() {
local user="$1"
# Swap optimization
modconf "/etc/sysctl.conf" "vm.swappiness=20"
# modconf "/etc/sysctl.conf" "vm.vfs_cache_pressure=50"
modconf "/etc/sysctl.conf" "zswap.enabled=1"
modconf "/etc/sysctl.conf" "zswap.max_pool_percent=25"
# Update system
apt -y update
apt -y upgrade
apt -y full-upgrade
# Init package list
pkgs=""
addpkg(){ pkgs+=" $@";}
# dependencies
addpkg default-jre # java
# wireshark
# for unattended setup:
DEBIAN_FRONTEND=noninteractive apt-get -y install wireshark
echo "wireshark-common wireshark-common/install-setuid boolean true" | debconf-set-selections
DEBIAN_FRONTEND=noninteractive dpkg-reconfigure wireshark-common
# regular setup: apt install wireshark
usermod -a -G wireshark "$user"
# multimedia
snap install spotify
# quick cheatsheets
snap install cheat
# tools
addpkg tmux
addpkg vim-gtk3
# system
addpkg gparted # Main partition tool
addpkg partitionmanager # KDE Partition Manager (supports LVM)
# remote access
addpkg openssh-server # ssh-server
# utils
addpkg htop # Better process manager
addpkg expect # Expect tcl scripting language
addpkg curl # Fetch text from URL easily
addpkg stow # Symlink tool (used for dotfiles)
addpkg tig # ncurses-based terminal git-frontend
addpkg zstd # zstd compression library
# addpkg git-crypt # encrypted file storage in git
addpkg tree # simple tool to show directory-structure as tree
addpkg ripgrep # fast grep alternative
addpkg fzf # fuzzy find tool
addpkg ruby # used for tmuxinator
addpkg fonts-hack-ttf # nice font (e.g. for vim)
addpkg powerline # fonts pached for powerline
# themes
apt-add-repository -y ppa:numix/ppa
addpkg numix-gtk-theme
addpkg numix-icon-theme-circle
# veracrypt
#apt-add-repository -y ppa:unit193/encryption
#addpkg veracrypt
# update new repositories first
apt update
apt install -y $pkgs
# disable ssh-server (not configured yet)
systemctl stop ssh
systemctl disable ssh
# tmuxinator requires ruby
gem install tmuxinator
# Cleanup
apt clean
apt autoremove -y
}
grubconf() {
# make os selectable before reboot via "grub-reboot <os-num>"
# modconf "/etc/default/grub" "GRUB_DEFAULT=saved"
# always show grub, even if only one os was found
# modconf "/etc/default/grub" "GRUB_TIMEOUT_STYLE=menu"
# modconf "/etc/default/grub" "GRUB_TIMEOUT=3"
# patch recordfail timeout from 30s to 3s
# workaround for bug on systems with LVM to always trigger this
# http://bugs.launchpad.net/ubuntu/+source/grub2/+bug/1815002
modconf "/etc/default/grub" "GRUB_RECORDFAIL_TIMEOUT=3"
sudo update-grub
# grub-set-default 0
}
grubtheme() {
if ! [ -d grub2-themes ]; then
git clone --depth 1 https://github.com/vinceliuice/grub2-themes
fi
cd grub2-themes
git pull
sudo ./install.sh -b -v
cd ..
}
xfce_conf(){
xfconf-query -c xfwm4 -p /general/theme -nt string -s Numix
xfconf-query -c xsettings -p /Net/ThemeName -nt string -s Numix
xfconf-query -c xsettings -p /Gtk/MonospaceFontName -nt string -s "Hack 9"
xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/power-button-action -nt uint -s 4
if [ "$(vcheck)" -eq 0 ]; then
xfconf-query -c xfce4-screensaver -p /saver/enabled -nt bool -s false
xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/dpms-enabled -nt bool -s false
xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/blank-on-ac -nt int -s 0
fi
}
xfce_conf_work() {
# disable automount
xfconf-query -c thunar-volman -p /automount-drives/enabled -nt bool -s false
xfconf-query -c thunar-volman -p /automount-media/enabled -nt bool -s false
xfconf-query -c thunar-volman -p /autobrowse/enabled -nt bool -s false
}
xfce_terminal_conf() {
local termcfg="~/.config/xfce4/terminal/terminalrc"
if [ -f "$termcfg" ]; then
modconf "$termcfg" "ColorForeground=#839496"
modconf "$termcfg" "TabActivityColor=#dc322f"
modconf "$termcfg" "ColorForeground=#839496"
modconf "$termcfg" "ColorBackground=#002b36"
modconf "$termcfg" "ColorCursor=#93a1a1"
modconf "$termcfg" "ColorBold=#93a1a1"
modconf "$termcfg" "ColorPalette=#073642;#dc322f;#859900;#b58900;#268bd2;#d33682;#2aa198;#eee8d5;#002b36;#cb4b16;#586e75;#657b83;#839496;#6c71c4;#93a1a1;#fdf6e3"
modconf "$termcfg" "FontName=DejaVu Sans Mono 8"
modconf "$termcfg" "ShortcutsNoMenukey=TRUE"
modconf "$termcfg" "ScrollingLines=20000"
modconf "$termcfg" "MiscScrollAlternateScreen=TRUE"
modconf "$termcfg" "ShortcutsNoMnemonics=TRUE"
modconf "$termcfg" "FontUseSystem=TRUE"
modconf "$termcfg" "MiscShowUnsafePasteDialog=FALSE"
modconf "$termcfg" "TabActivityColor=#dc322f"
else
cp "dotfiles/xfce/.config/xfce4/terminal/terminalrc" "$termcfg"
fi
}
setup_virt() {
sudo apt-get install docker.io
sudo apt-get install virt-manager
sudo apt-get install ovmf #uefi support
sudo usermod -a -G libvirt $user
sudo usermod -a -G docker $user
}
# @todo Call for server
setup_server() {
sudo snap install canonical-livepatch
sudo canonical-livepatch enable <secret>
}
enable_iommu() {
modconf "/etc/default/grub" 'RUB_CMDLINE_LINUX_DEFAULT="quiet splash intel_iommu=on iommu=pt"'
modconf "/etc/default/grub" 'RUB_CMDLINE_LINUX_DEFAULT="quiet splash amd_iommu=on iommu=pt"'
update-grub
# fetch groups:
# for g in /sys/kernel/iommu_groups/*; do
# echo "IOMMU Group ${g##*/}:"
# for d in $g/devices/*; do
# echo -e "\t$(lspci -nns ${d##*/})"
# done;
# done;
}
userconf() {
echo "initializing dotfiles..."
su $1 -c "'$0' userconf"
}
dotfiles() {
echo "dotfiles for $USER"
cd "$(dirname "$0")"
git submodule update --init
cd dotfiles
git checkout master
echo "Linking dotfiles"
./link.sh
echo "Running post-config script"
./postconfig.sh
}
errorhandler() {
echo "Error on line $1 - Command failed: '$2'"
read -p "Press enter to continue..."
}
# capture last command for errorhandler
trap 'last_command=$BASH_COMMAND' DEBUG
trap 'errorhandler "$LINENO" "$last_command"' ERR
main "$@"