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