fix vpn host naming
[distro-setup] / distro-begin
1 #!/bin/bash -l
2 # Copyright (C) 2016 Ian Kelling
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # for setting up a new machine
17 # usage: $0 [-r] HOSTNAME
18
19 # tips:
20 # run any sudo command first so your pass is cached
21 # set the scrollback to unlimited in case something goes wrong
22
23
24 ####### begin setup environment #######
25
26
27 ### make ssh interactive shell run better. for when running line interactively line by line
28 sudo bash -c 'source /a/c/.bashrc && source /a/exe/ssh-emacs-setup'
29
30
31 ##### setup error handling
32 interactive=true # set this to false to force set -x
33 [[ $- == *i* ]] || interactive=false
34 if ! $interactive; then
35 set -x
36 fi
37 source /a/bin/errhandle/err
38
39 ### setup logging
40 exec &> >(sudo tee -a /var/log/distro-begin)
41 echo "$0: $(date): starting now)"
42
43
44 ### sanity checking
45 if [[ $EUID == 0 ]]; then
46 if getent passwd iank || getent passwd ian ; then
47 echo "$0: error: running as root. unprivileged user exists. use it."
48 exit 1
49 else
50 echo "$0: warning: running as root. I will setup users then exit"
51 fi
52 fi
53
54
55 ### arg parsing
56 recompile=false
57 while [[ $1 == -* ]]; do
58 case $1 in
59 -r) recompile=true; shift ;;
60 esac
61 done
62 if [[ $1 ]]; then
63 export HOSTNAME=$1
64 fi
65
66
67 ##### variables/env setup
68 script_dir="$(readlink -f "${BASH_SOURCE[@]}")"; script_dir=${script_dir%/*}
69 # shellcheck source=./pkgs
70 source $script_dir/pkgs
71 set +x
72 source /a/bin/distro-functions/src/identify-distros
73 $interactive || set -x
74 for f in iank-dev htpc kd x2 x3 frodo tp li lj demohost kw fz; do
75 eval "$f() { [[ $HOSTNAME == $f ]]; }"
76 done
77 codename=$(debian-codename)
78 has_wayland() { [[ $codename == buster ]]; }
79 has_x() { [[ $codename != buster ]]; }
80 has_monitor() { ! linode ; }
81 linode() { lj || li; }
82 has_btrfs() { ! linode; }
83 home_network() { ! linode; }
84 encrypted() { has_p; }
85 shopt -s extglob
86 export GLOBIGNORE="*/.:*/.."
87 umask 022
88 PATH="/a/exe:$PATH"
89 sed="sed --follow-symlinks"
90
91 ####### end setup environment #######
92
93
94
95
96 ##### begin setup encryption scripts ######
97 if encrypted; then
98 # I tried making a service which was dependent on reboot.target,
99 # but it happened too late in the shutdown process.
100 sudo dd of=/etc/systemd/system/keyscripton.service <<'EOF'
101 [Unit]
102 Description=Turn on automatic decryption of drives on boot
103 # tried using graphical.target, but it made my display manager restart before rebooting.
104 # generally, I don't think targets order shutdown like they do startup.
105 # So, I did systemd-analyze plot > something.svg, and picked a reliably started
106 # service that happens late in the game.
107 After=ntp.service
108 DefaultDependencies=no
109 # not sure if needed, makes sure we shut down before reboot.target
110 Conflicts=reboot.target
111
112 [Service]
113 Type=oneshot
114 RemainAfterExit=yes
115 ExecStart=/bin/true
116 ExecStop=/a/exe/keyscript-on
117
118 [Install]
119 WantedBy=keyscriptoff.service
120 EOF
121 sudo systemctl daemon-reload # needed if the file was already there
122 sudo systemctl stop keyscripton.service
123 # sudo systemctl start keyscripton.service
124 sudo systemctl enable keyscripton.service
125
126 sudo dd of=/etc/systemd/system/keyscriptoff.service <<'EOF'
127 [Unit]
128 Description=Turn off automatic decryption of drives on boot
129
130 [Service]
131 Type=oneshot
132 ExecStart=/a/exe/keyscript-off
133
134 [Install]
135 WantedBy=multi-user.target
136 EOF
137 sudo systemctl daemon-reload # needed if the file was already there
138 sudo systemctl enable keyscriptoff.service
139 sudo systemctl start keyscriptoff.service
140
141 # from /usr/share/doc/dropbear-initramfs/README.initramfs.gz
142 while read -r m _; do /sbin/modinfo -F filename "$m"; done </proc/modules | \
143 sed -nr "s@^/lib/modules/$(uname -r)/kernel/drivers/net(/.*)?/([^/]+)\.ko\$@\2@p" \
144 | sudo dd of=/etc/initramfs-tools/modules
145 sudo apt-get -y install initramfs-tools-core
146 # initram auth keys get setup with rootsshsync
147 # then for remote unlock, ssh and do this once per crypt disk:
148 # echo -n PASS >/lib/cryptsetup/passfifo
149 fi
150 ##### end setup encryption scripts ######
151
152
153 # disabled until its fixed up
154 # install-myqueue
155
156 # todo, it would be nice to cut down on some of the output
157
158 ##### fedora prereq/fundamental settings
159 if isfedora; then
160 # comment out line disallowing calling sudo in scripts
161 sudo $sed -i 's/^Defaults *requiretty/#\0 # ian commented/' /etc/sudoers
162 # turn on magic sysrq commands
163 echo 1 > sudo dd of=/proc/sys/kernel/sysrq
164 echo "kernel.sysrq = 1" > /etc/sysctl.d/90-sysrq.conf
165 # selinux is not user friendly. Like, you enable samba, but you haven't run the magic selinux commands so it doesn't work
166 # and you have no idea why.
167 sudo $sed -i 's/^\(SELINUX=\).*/\1disabled/' /etc/selinux/config
168 selinuxenabled && sudo setenforce 0
169 fi
170
171
172 #### rerun my fai-time scripts
173 # already ran for pxe installs, but used for vps & updates
174 distro=$(distro-name)
175 case $distro in
176 ubuntu|debian|trisquel)
177 sudo bash -c ". /a/bin/fai/fai-wrapper && /a/bin/fai/fai/config/scripts/GRUB_PC/11-iank"
178 ;;
179 *)
180 sudo bash -c ". /a/bin/fai/fai-wrapper &&
181 /a/bin/fai/fai/config/distro-install-common/end"
182 ;;
183 esac
184
185 ###### setup hostname
186 sudo $sed -i '/^127\.0\.1\.1/d' /etc/hosts
187 echo "127.0.1.1 $HOSTNAME.b8.nz $HOSTNAME" | sudo tee -a /etc/hosts
188
189
190 ##### exit first stage if running as root
191 if [[ $EUID == 0 ]]; then
192 echo "$0: running as root. exiting now that users are setup"
193 exit 0
194 fi
195
196
197 #### setup bash for root
198 for x in /a/c/{.bashrc,brc,.bash_profile,.profile,.inputrc,path_add_function}; do
199 sudo -i <<EOF
200 PATH="/a/exe:$PATH"
201 lnf $x /root
202 EOF
203 done
204
205 ###### do conflink
206 # li needs the bind group before conflink
207 if [[ $HOSTNAME == li ]]; then
208 getent group bind &>/dev/null || sudo groupadd -r bind
209 fi
210 # this needs to be before installing pacserve so we have gpg conf.
211 conflink
212
213 ###### bash environment setup
214 set +x
215 err-allow
216 source /etc/profile.d/environment.sh
217 # shellcheck source=./.bashrc
218 source ~/.bashrc
219 err-catch
220 $interactive || set -x
221
222
223 #### setup passwordless sudo
224 tu /etc/sudoers <<EOF
225 $USER ALL=(ALL) NOPASSWD: ALL
226 Defaults env_keep += SUDOD
227 # makes ubuntu be like debian
228 # https://unix.stackexchange.com/a/91572
229 Defaults always_set_home
230 # default setting is to have minimum umask of 0022
231 # This lets us have user-specific umasks which are more permissive.
232 # I did this for transmission and set it's umask gecos on install,
233 # see there for more info.
234 Defaults !umask
235 EOF
236
237
238 #### setup firefox backport
239 ## ian: disabled. backports are not being published atm due to rust packaging issue
240 # if isdeb; then
241 # codename=$(debian-codename)
242 # if isdebian-stable && has_x; then
243 # s dd of=/etc/apt/sources.list.d/mozilla-iceweasel.list <<EOF
244 # deb http://mozilla.debian.net/ $codename-backports firefox-release
245 # deb-src http://mozilla.debian.net/ $codename-backports firefox-release
246 # EOF
247 # p update
248 # # take care of mozilla signing errors in previous command
249 # pi pkg-mozilla-archive-keyring
250 # fi
251 # p update
252 # fi
253
254
255 ###### arch aur wrapper setup
256 if isarch; then
257 #https://wiki.archlinux.org/index.php/Arch_User_Repository#Installing_packages
258 sudo pacman -S --noconfirm --needed base-devel jq
259 # pacaur seems to be the best, although it + cower has a few minor bugs,
260 # its design goals seem good, so, going for it.
261
262 aurpi() {
263 for p in "$@"; do
264 tempdir=$(mktemp -d)
265 pushd $tempdir
266 aurex "$p"
267 makepkg -sri --skippgpcheck --noconfirm
268 popd
269 rm -rf $tempdir
270 done
271 }
272 aurpi cower pacaur
273
274 pi pacserve
275
276 x=$(mktemp); /usr/bin/pacman.conf-insert_pacserve >$x
277 sudo dd of=/etc/pacman.conf if=$x; rm $x
278 sudo systemctl enable pacserve.service
279 sudo systemctl start pacserve.service
280
281 fi
282
283 #### update all packages
284 pup
285
286
287 ###### p1 packages install ######
288 if isarch; then
289 # requirement for trash-cli.
290 # background: strange error if just installing trash-cli: "pyalpm requires python",
291 # so I see that it requires python2, and installing that manually fixes it.
292 # I didn\'t see this on earlier installation, main thing which changed was
293 # pacserve, so not sure if it\'s related.
294 pi python2
295 fi
296 pi ${p1[@]}
297
298
299 ######## fix evbug bug ######
300 case $distro in
301 trisquel|ubuntu)
302 # noticed in flidas.
303 #https://bugs.launchpad.net/ubuntu/+source/module-init-tools/+bug/240553
304 #https://wiki.debian.org/KernelModuleBlacklisting
305 #common advice when searching is to use /etc/modprobe.d/blacklist.conf,
306 #but that file won't work and will get automatically reverted
307 sudo rmmod evbug ||: # might not be loaded yet
308 file=/etc/modprobe.d/evbug.conf
309 line="blacklist evbug"
310 if ! grep -xFq "$line" $file; then
311 sudo dd of=$file 2>/dev/null <<<"$line"
312 sudo depmod -a
313 sudo update-initramfs -u
314 fi
315 ;;
316 esac
317
318
319 ###### link files
320 # convenient to just do all file linking in one place
321 s lnf -T /a/bin /b
322 s lnf -T /nocow/t /t
323 if has_p; then
324 lnf -T /p/News ~/News
325 fi
326 s lnf /q/root/.editor-backups /q/root/.undo-tree-history \
327 /a/opt /a/c/.emacs.d $HOME/mw_vars /k/backup /root
328 pi rsync # needed for rootsshsync
329 /a/bin/ds/install-my-scripts # needed for rootsshsync
330 rootsshsync
331 s lnf /a/c/.vim /a/c/.vimrc /a/c/.gvimrc /root
332 if has_p; then
333 # for dovecot
334 lnf -T /i/k/mboxes ~/mail
335 fi
336
337
338
339 ##### install xinput
340 if has_x; then
341 case $(distro-name) in
342 trisquel|ubuntu|debian)
343 pi xinput
344 ;;
345 fedora)
346 pi xinput_calibrator
347 ;;
348 arch)
349 pi xorg-xinput
350 ;;
351 esac
352
353 #### install redshift
354 case $(distro-name) in
355 trisquel|ubuntu|debian)
356 # recommends gets us geoclue (for darkening automatically at night i assume),
357 # which recommends modemmanager, which is annoying to fix for the model01 keyboard.
358 pi --no-install-recommends gtk-redshift
359 ;;&
360 fedora)
361 pi redshift-gtk
362 ;;&
363 arch)
364 pi redshift
365 ;;&
366 esac
367 fi
368
369 if has_wayland; then
370 pi sway xwayland
371 # originally used xkbcomp, documented in input-setup.sh, this doesnt
372 # work under wayland, but its still useful for creating the config,
373 # then modifying the system files.
374 s sed -i.orig '/key *<KPMU> *{/,/}/s/KP_Multiply/underscore/g' /usr/share/X11/xkb/symbols/keypad
375 fi
376
377
378
379 #### arch specific early packages
380 case $(distro-name) in
381 arch)
382 # pkgfile is like apt-cache
383 pi pkgfile
384 s pkgfile --update
385 ;;
386 esac
387
388 #### fedora specific packages
389 case $(distro-name) in
390 fedora)
391 # todo, this could probably come later
392 p -y groupinstall development-tools c-development books admin-tools
393 pi man-pages
394 ;;
395 # other distros unknown
396 esac
397
398 #### enable trim
399 # enable trim for volume delete, other rare commands
400 sudo $sed -ri 's/( *issue_discards\b).*/\1 = 1/' /etc/lvm/lvm.conf
401 if encrypted; then
402 # flidas or so, these units arent built-in
403 if isdeb && ! systemctl list-unit-files | grep -xF fstrim.timer &>/dev/null ; then
404 sudo cp /usr/share/doc/util-linux/examples/fstrim.{service,timer} /etc/systemd/system
405 fi
406 # does weekly trim
407 sudo systemctl enable fstrim.timer
408 fi
409
410 ##### make extra dirs
411 dirs=(/mnt/{1,2,3,4,5,6,7,8,9} /nocow/t)
412 s mkdir -p "${dirs[@]}"
413 s chown $USER:$USER "${dirs[@]}"
414
415 ###### setup /i
416 tu /etc/fstab <<'EOF'
417 /i/w /w none bind,noauto 0 0
418 /i/k /k none bind,noauto 0 0
419 EOF
420 if ! mountpoint /kr; then
421 s mkdir -p /kr
422 s chown $USER:user2 /kr
423 fi
424 if home_network; then
425 if [[ $HOSTNAME == frodo ]]; then
426 tu /etc/fstab <<'EOF'
427 /k /kr none bind,noauto 0 0
428 EOF
429 else
430 tu /etc/fstab <<'EOF'
431 frodo:/k /kr nfs noauto 0 0
432 EOF
433 fi
434 fi
435 s mkdir -p /q /i/{w,k}
436 for dir in /{i,w,k}; do
437 if mountpoint $dir; then continue; fi # already mounted
438 s mkdir -p $dir
439 s chown $USER:$USER $dir
440 done
441 # not needed for all hosts, but rather just keep it uniform
442 s mkdir -p /mnt/iroot
443 # debian auto mounting of multi-disk encrypted btrfs is busted. It is
444 # in jessie, and in stretch as of 11/26/2016 I have 4 disks in cryptab,
445 # based on 3 of those, it creates .device units for /dev/mapper/dev...
446 # then waits endlessly for them on bootup, after the /dev/mapper disks
447 # have already been created and exist. todo: create a simple repro
448 # for this in a vm and report it upstream.
449 if has_btrfs || home_network; then
450 pi nfs-common
451 s dd of=/root/imount <<'EOF'
452 #!/bin/bash
453 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
454 set -eE -o pipefail
455 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
456 for dir in /i /mnt/iroot /k /kr /w; do
457 if ! mountpoint $dir &>/dev/null && \
458 awk '{print $2}' /etc/fstab | grep -xF $dir &>/dev/null; then
459 if awk '{print $3}' /etc/fstab | grep -xF nfs &>/dev/null; then
460 mount $dir || echo "warning: failed to mount nfs on $dir"
461 else
462 mount $dir
463 fi
464 fi
465 done
466 EOF
467 s chmod +x /root/imount
468
469 s dd of=/etc/systemd/system/imount.service <<EOF
470 [Unit]
471 Description=Mount /i and related mountpoints
472 Before=syncthing@$USER.service
473
474 [Service]
475 Type=oneshot
476 ExecStart=/root/imount
477
478 [Install]
479 RequiredBy=syncthing@$USER.service
480 # note /kr needs networking, this target is the simplest way to
481 # time it when the network should be up, but not do something
482 # dumb like delay startup until the network is up. It happens
483 # at some time after network.target
484 WantedBy=multi-user.target
485 EOF
486 sudo systemctl daemon-reload # needed if the file was already there
487 sudo systemctl enable imount.service
488 sudo systemctl start imount.service
489 fi
490
491 ##### setup /nocow.
492 # a nocow dir that is common to multiple distros installed on the same system
493 dir=/nocow
494 if has_btrfs; then
495 if ! mountpoint $dir; then
496 subvol=/mnt/root/nocow
497 if [[ ! -e $subvol ]]; then
498 s btrfs subvolume create $subvol
499 s chown root:1000 $subvol
500 s chattr +C $subvol
501 fi
502
503 first_root_crypt=$(awk '$2 == "/" {print $1}' /etc/mtab)
504 tu /etc/fstab <<EOF
505 $first_root_crypt /nocow btrfs noatime,subvol=nocow 0 0
506 EOF
507 s mkdir -p $dir
508 s chown $USER:$USER $dir
509 s mount $dir
510 fi
511 else
512 sudo mkdir -p $dir
513 fi
514
515
516 ##### setup email
517 mail-setup
518
519 #### ubuntu nicety
520 if isubuntu; then
521 # disable crash report annoying dialogs.
522 s dd of=/etc/default/apport <<<'enabled=0'
523 fi
524
525
526 ##### install emacs
527 if isarch; then
528 # emacs git build was broken last time i checked,
529 x=$(mktemp -d)
530 pushd $x
531 aurex emacs-git
532 makepkg -si --noconfirm
533 popd
534 rm -rf $x
535 pi hunspell hunspell-en
536 else
537 if $recompile; then
538 /a/bin/buildscripts/emacs
539 else
540 /a/bin/buildscripts/emacs --no-r || /a/bin/buildscripts/emacs
541 fi
542 fi
543
544 ##### install laptop hardware packages
545 if tp || x2 || x3; then
546 case $distro in
547 debian)
548 pi task-laptop
549 ;;
550 ubuntu|trisquel)
551 # the exact packages that task-laptop would install, since ubuntu
552 # doesn\'t have this virtual in practice package.
553 pi avahi-autoipd bluetooth powertop iw wireless-tools wpasupplicant
554 ;;
555 # todo: other distros unknown
556 esac
557 fi
558
559
560 ##### install x stuff
561 if has_monitor; then
562 pi ${p2[@]}
563 if has_x; then
564 pi i3
565 if isarch; then
566 # xorg-xmessage for displaying error messages.
567 # optional dependency in arch, standard elsewhere.
568 pi xorg-server xorg-xmessage xorg-xsetroot xorg-xinit
569 fi
570 fi
571 fi
572
573
574 ##### setup X autostart
575 if has_x; then
576 if isarch; then
577 # https://wiki.archlinux.org/index.php/Xinitrc
578 for homedir in /home/*; do
579 cp /etc/X11/xinit/xinitrc $homedir/.xinitrc
580 # shellcheck disable=SC2016
581 $sed -ri '/^ *twm\b/,$d' $homedir/.xinitrc
582 tee -a $homedir/.xinitrc <<'EOF'
583 /a/bin/desktop-20-autostart.sh
584 xsetroot -cursor_name left_ptr
585 exec xmonad
586 EOF
587 done
588 else
589 # todo, figure this out for arch if we ever try out gnome.
590 # install for multiple display managers in case we use one
591 if isdeb; then
592 dir=/etc/gdm3
593 elif isfedora; then
594 # fedora didn\'t have the 3.
595 dir=/etc/gdm
596 fi
597 s mkdir -p $dir/PostLogin
598 s command cp /a/bin/distro-setup/desktop-20-autostart.sh $dir/PostLogin/Default
599 s mkdir /etc/lightdm/lightdm.conf.d
600 s dd of=/etc/lightdm/lightdm.conf.d/12-iank.conf <<'EOF'
601 [SeatDefaults]
602 session-setup-script=/a/bin/distro-setup/desktop-20-autostart.sh
603 EOF
604 fi
605 fi
606
607
608 #### refix interactive ssh terminal
609 # the first pup command can kill off our /etc/ mod, so rerun this
610 /a/exe/ssh-emacs-setup
611
612
613 echo "$0: $(date): ending now"
614 exit 0