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