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