570d077ca68d1fc9f5bdbb666962de4e83506bdb
[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 --follow-symlinks -f - /etc/hosts <<EOF
199 \$a 127.0.1.1 $HOSTNAME.b8.nz $HOSTNAME
200 /^127\.0\.1\.1/d
201 EOF
202
203
204 ##### exit first stage if running as root
205 if [[ $EUID == 0 ]]; then
206 echo "$0: running as root. exiting now that users are setup"
207 exit 0
208 fi
209
210
211 #### setup bash for root
212 for x in /a/c/{.bashrc,brc,brc2,.bash_profile,.profile,.inputrc,path_add_function}; do
213 sudo -i <<EOF
214 PATH="/a/exe:$PATH"
215 lnf $x /root
216 EOF
217 done
218
219 ###### do conflink
220 # li needs the bind group before conflink
221 if [[ $HOSTNAME == li ]]; then
222 getent group bind &>/dev/null || sudo groupadd -r bind
223 fi
224 # this needs to be before installing pacserve so we have gpg conf.
225 conflink
226
227 ###### bash environment setup
228 set +x
229 err-allow
230 source /etc/profile.d/environment.sh
231 # shellcheck source=./.bashrc
232 source ~/.bashrc
233 err-catch
234 $interactive || set -x
235
236
237 #### setup passwordless sudo
238
239 # always_set_home
240 # makes ubuntu be like debian
241 # https://unix.stackexchange.com/a/91572
242
243 # umask: default setting is to have minimum umask of 0022
244 # This lets us have user-specific umasks which are more permissive.
245 # I did this for transmission and set it's umask gecos on install,
246 # see there for more info.
247
248 tu /etc/sudoers <<EOF
249 $USER ALL=(ALL) NOPASSWD: ALL
250 Defaults env_keep += SUDOD
251 Defaults always_set_home
252 Defaults !umask
253 EOF
254
255
256 #### setup firefox backport
257 ## ian: disabled. backports are not being published atm due to rust packaging issue
258 # if isdeb; then
259 # codename=$(debian-codename)
260 # if isdebian-stable && has_x; then
261 # s dd of=/etc/apt/sources.list.d/mozilla-iceweasel.list <<EOF
262 # deb http://mozilla.debian.net/ $codename-backports firefox-release
263 # deb-src http://mozilla.debian.net/ $codename-backports firefox-release
264 # EOF
265 # p update
266 # # take care of mozilla signing errors in previous command
267 # pi pkg-mozilla-archive-keyring
268 # fi
269 # p update
270 # fi
271
272
273 ###### arch aur wrapper setup
274 if isarch; then
275 #https://wiki.archlinux.org/index.php/Arch_User_Repository#Installing_packages
276 sudo pacman -S --noconfirm --needed base-devel jq
277 # pacaur seems to be the best, although it + cower has a few minor bugs,
278 # its design goals seem good, so, going for it.
279
280 aurpi() {
281 for p in "$@"; do
282 tempdir=$(mktemp -d)
283 pushd $tempdir
284 aurex "$p"
285 makepkg -sri --skippgpcheck --noconfirm
286 popd
287 rm -rf $tempdir
288 done
289 }
290 aurpi cower pacaur
291
292 pi pacserve
293
294 x=$(mktemp); /usr/bin/pacman.conf-insert_pacserve >$x
295 sudo dd of=/etc/pacman.conf if=$x; rm $x
296 sudo systemctl enable pacserve.service
297 sudo systemctl start pacserve.service
298
299 fi
300
301 #### update all packages
302 pup
303
304
305 ###### p1 packages install ######
306 pi ${p1[@]}
307
308
309 ######## fix evbug bug ######
310 case $distro in
311 trisquel|ubuntu)
312 # noticed in flidas.
313 #https://bugs.launchpad.net/ubuntu/+source/module-init-tools/+bug/240553
314 #https://wiki.debian.org/KernelModuleBlacklisting
315 #common advice when searching is to use /etc/modprobe.d/blacklist.conf,
316 #but that file won't work and will get automatically reverted
317 sudo rmmod evbug ||: # might not be loaded yet
318 file=/etc/modprobe.d/evbug.conf
319 line="blacklist evbug"
320 if ! grep -xFq "$line" $file; then
321 sudo dd of=$file 2>/dev/null <<<"$line"
322 sudo depmod -a
323 sudo update-initramfs -u
324 fi
325 ;;
326 esac
327
328
329 ###### link files
330 # convenient to just do all file linking in one place
331 s /a/exe/lnf -T /a/bin /b
332 s /a/exe/lnf -T /nocow/t /t
333 if has_p; then
334 lnf -T /p/News ~/News
335 fi
336 s /a/exe/lnf /q/root/.editor-backups /q/root/.undo-tree-history \
337 /a/opt /a/c/.emacs.d $HOME/mw_vars /k/backup /root
338 /a/bin/ds/install-my-scripts # needed for rootsshsync cronjob
339 s /a/exe/lnf /a/c/.vim /a/c/.vimrc /a/c/.gvimrc /root
340
341
342
343
344
345 #### arch specific early packages
346 case $(distro-name) in
347 arch)
348 # pkgfile is like apt-cache
349 pi pkgfile
350 s pkgfile --update
351 ;;
352 esac
353
354 #### enable trim
355 # enable trim for volume delete, other rare commands
356 sudo $sed -ri 's/( *issue_discards\b).*/\1 = 1/' /etc/lvm/lvm.conf
357 if encrypted; then
358 # flidas or so, these units arent built-in
359 if isdeb && ! systemctl list-unit-files | grep ^fstrim.timer &>/dev/null ; then
360 sudo cp /usr/share/doc/util-linux/examples/fstrim.{service,timer} /etc/systemd/system
361 fi
362 # does weekly trim
363 sudo systemctl enable fstrim.timer
364 fi
365
366 ##### make extra dirs
367 dirs=(/mnt/{1,2,3,4,5,6,7,8,9} /nocow/t)
368 s mkdir -p "${dirs[@]}"
369 s chown $USER:$USER "${dirs[@]}"
370
371 ###### setup /i
372 if home_network; then
373 tu /etc/fstab <<'EOF'
374 /i/w /w none bind,noauto 0 0
375 /i/k /k none bind,noauto 0 0
376 EOF
377 if ! mountpoint /kr; then
378 s mkdir -p /kr
379 s chown $USER:user2 /kr
380 fi
381 if [[ $HOSTNAME == frodo ]]; then
382 tu /etc/fstab <<'EOF'
383 /k /kr none bind,noauto 0 0
384 EOF
385 else
386 tu /etc/fstab <<'EOF'
387 frodo:/k /kr nfs noauto 0 0
388 EOF
389 fi
390 s mkdir -p /q /i/{w,k}
391 for dir in /{i,w,k}; do
392 if mountpoint $dir; then continue; fi # already mounted
393 s mkdir -p $dir
394 s chown $USER:$USER $dir
395 done
396 # not needed for all hosts, but rather just keep it uniform
397 s mkdir -p /mnt/iroot
398 # debian auto mounting of multi-disk encrypted btrfs is busted. It is
399 # in jessie, and in stretch as of 11/26/2016 I have 4 disks in cryptab,
400 # based on 3 of those, it creates .device units for /dev/mapper/dev...
401 # then waits endlessly for them on bootup, after the /dev/mapper disks
402 # have already been created and exist. todo: create a simple repro
403 # for this in a vm and report it upstream.
404 pi nfs-common
405 s dd of=/root/imount <<'EOF'
406 #!/bin/bash
407 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
408 set -eE -o pipefail
409 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
410 for dir in /i /mnt/iroot /k /kr /w; do
411 if ! mountpoint $dir &>/dev/null && \
412 awk '{print $2}' /etc/fstab | grep -xF $dir &>/dev/null; then
413 if awk '{print $3}' /etc/fstab | grep -xF nfs &>/dev/null; then
414 mount $dir || echo "warning: failed to mount nfs on $dir"
415 else
416 mount $dir
417 fi
418 fi
419 done
420 EOF
421 s chmod +x /root/imount
422 s dd of=/etc/systemd/system/imount.service <<EOF
423 [Unit]
424 Description=Mount /i and related mountpoints
425 Before=syncthing@$USER.service
426
427 [Service]
428 Type=oneshot
429 ExecStart=/root/imount
430
431 [Install]
432 RequiredBy=syncthing@$USER.service
433 # note /kr needs networking, this target is the simplest way to
434 # time it when the network should be up, but not do something
435 # dumb like delay startup until the network is up. It happens
436 # at some time after network.target
437 WantedBy=multi-user.target
438 EOF
439 sudo systemctl daemon-reload # needed if the file was already there
440 sudo systemctl enable imount.service
441 sudo systemctl start imount.service
442 fi
443 ###### end setup /i
444
445 ##### setup /nocow.
446 # a nocow dir that is common to multiple distros installed on the same system
447 dir=/nocow
448 if has_btrfs; then
449 if ! mountpoint $dir; then
450 subvol=/mnt/root/nocow
451 if [[ ! -e $subvol ]]; then
452 s btrfs subvolume create $subvol
453 s chown root:1000 $subvol
454 s chattr +C $subvol
455 fi
456
457 first_root_crypt=$(awk '$2 == "/" {print $1}' /etc/mtab)
458 tu /etc/fstab <<EOF
459 $first_root_crypt /nocow btrfs noatime,subvol=nocow 0 0
460 EOF
461 s mkdir -p $dir
462 s chown $USER:$USER $dir
463 s mount $dir
464 fi
465 else
466 sudo mkdir -p $dir
467 fi
468
469
470 ##### setup email
471 mail-setup
472
473 #### ubuntu nicety
474 if isubuntu; then
475 # disable crash report annoying dialogs.
476 s dd of=/etc/default/apport <<<'enabled=0'
477 fi
478
479
480
481 ##### install laptop hardware packages
482 if tp || x2 || x3; then
483 case $distro in
484 debian)
485 pi task-laptop
486 ;;
487 ubuntu|trisquel)
488 # the exact packages that task-laptop would install, since ubuntu
489 # doesn\'t have this virtual in practice package.
490 pi avahi-autoipd bluetooth powertop iw wireless-tools wpasupplicant
491 ;;
492 # todo: other distros unknown
493 esac
494 fi
495
496 if has_x; then
497 ###### install X
498 pi i3
499 if isarch; then
500 # xorg-xmessage for displaying error messages.
501 # optional dependency in arch, standard elsewhere.
502 pi xorg-server xorg-xmessage xorg-xsetroot xorg-xinit
503 fi
504
505 ##### install xinput
506 case $(distro-name) in
507 trisquel|ubuntu|debian)
508 pi xinput
509 ;;
510 arch)
511 pi xorg-xinput
512 ;;
513 esac
514
515 #### install redshift
516 case $(distro-name) in
517 trisquel|ubuntu|debian)
518 # recommends gets us geoclue (for darkening automatically at night i assume),
519 # which recommends modemmanager, which is annoying to fix for the model01 keyboard.
520 pi --no-install-recommends gtk-redshift
521 ;;&
522 arch)
523 pi redshift
524 ;;&
525 esac
526
527 ##### setup X autostart
528 if isarch; then
529 # https://wiki.archlinux.org/index.php/Xinitrc
530 for homedir in /home/*; do
531 cp /etc/X11/xinit/xinitrc $homedir/.xinitrc
532 # shellcheck disable=SC2016
533 $sed -ri '/^ *twm\b/,$d' $homedir/.xinitrc
534 tee -a $homedir/.xinitrc <<'EOF'
535 /a/bin/desktop-20-autostart.sh
536 xsetroot -cursor_name left_ptr
537 exec xmonad
538 EOF
539 done
540 else
541 # todo, figure this out for arch if we ever try out gnome.
542 # install for multiple display managers in case we use one
543 dir=/etc/gdm3
544 s mkdir -p $dir/PostLogin
545 s command cp /a/bin/distro-setup/desktop-20-autostart.sh $dir/PostLogin/Default
546 s mkdir /etc/lightdm/lightdm.conf.d
547 s dd of=/etc/lightdm/lightdm.conf.d/12-iank.conf <<'EOF'
548 [SeatDefaults]
549 session-setup-script=/a/bin/distro-setup/desktop-20-autostart.sh
550 EOF
551 fi
552
553 fi
554
555 ### install and configure wayland
556 if has_wayland; then
557 pi sway xwayland
558 # originally used xkbcomp, documented in input-setup.sh, this doesnt
559 # work under wayland, but its still useful for creating the config,
560 # then modifying the system files.
561 s sed -i.orig '/key *<KPMU> *{/,/}/s/KP_Multiply/underscore/g' /usr/share/X11/xkb/symbols/keypad
562 fi
563
564 ##### basic graphical packages
565 if has_monitor; then
566 pi konsole suckless-tools
567 fi
568
569
570 ##### install emacs
571 if $emacs; then
572 if isarch; then
573 # emacs git build was broken last time i checked,
574 x=$(mktemp -d)
575 pushd $x
576 aurex emacs-git
577 makepkg -si --noconfirm
578 popd
579 rm -rf $x
580 pi hunspell hunspell-en
581 else
582 if $recompile; then
583 /a/bin/buildscripts/emacs
584 else
585 /a/bin/buildscripts/emacs --no-r
586 fi
587 fi
588 # the first pup command can kill off our /etc/ mod, so rerun this
589 /a/exe/ssh-emacs-setup
590 fi
591
592
593 echo 0 >~/.local/distro-begin
594 echo "$0: $(date): ending now"
595 exit 0