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