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