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