organize / cleanup
[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 set -e -o pipefail
37 fi
38 set -E
39 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
40 for x in /a/bin/errhandle/*-function; do
41 source $x
42 done
43 set +e
44 $interactive || errcatch
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 # for copying to a new data fs
65 bootstrapfs=false # old flag, needs new look before using.
66 while [[ $1 == -* ]]; do
67 case $1 in
68 -r) recompile=true; 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")"
78 source $script_dir/pkgs
79 set +x
80 source /a/bin/distro-functions/src/identify-distros
81 $interactive || set -x
82 for f in iank-dev htpc treetowl x2 frodo tp li lj demohost kw fz; do
83 eval "$f() { [[ $HOSTNAME == $f ]]; }"
84 done
85 has_p() { ! linode; } # when tp is tracis, then not tp either
86 has_x() { ! linode; }
87 linode() { lj || li; }
88 has_btrfs() { ! linode; }
89 home_network() { ! linode; }
90 encrypted() { has_p; }
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 fi
147 ##### end setup encryption scripts ######
148
149
150 # disabled until its fixed up
151 # install-myqueue
152
153 # todo, it would be nice to cut down on some of the output
154
155 ##### fedora prereq/fundamental settings
156 if isfedora; then
157 # comment out line disallowing calling sudo in scripts
158 sudo $sed -i 's/^Defaults *requiretty/#\0 # ian commented/' /etc/sudoers
159 # turn on magic sysrq commands
160 echo 1 > sudo dd of=/proc/sys/kernel/sysrq
161 echo "kernel.sysrq = 1" > /etc/sysctl.d/90-sysrq.conf
162 # selinux is not user friendly. Like, you enable samba, but you haven't run the magic selinux commands so it doesn't work
163 # and you have no idea why.
164 sudo $sed -i 's/^\(SELINUX=\).*/\1disabled/' /etc/selinux/config
165 selinuxenabled && sudo setenforce 0
166 fi
167
168
169 #### rerun my fai-time scripts
170 # already ran for pxe installs, but used for vps & updates
171 distro=$(distro-name)
172 case $distro in
173 ubuntu|debian|trisquel)
174 sudo bash -c ". /a/bin/fai/fai-wrapper && /a/bin/fai/fai/config/scripts/GRUB_PC/11-iank"
175 ;;
176 *)
177 sudo bash -c ". /a/bin/fai/fai-wrapper &&
178 /a/bin/fai/fai/config/distro-install-common/end"
179 ;;
180 esac
181
182 ###### setup hostname
183 sudo $sed -i '/^127\.0\.1\.1/d' /etc/hosts
184 echo "127.0.1.1 $HOSTNAME.b8.nz $HOSTNAME" | sudo tee -a /etc/hosts
185
186
187 ##### exit first stage if running as root
188 if [[ $EUID == 0 ]]; then
189 echo "$0: running as root. exiting now that users are setup"
190 exit 0
191 fi
192
193
194 #### setup bash for root
195 for x in /a/c/{.bashrc,brc,.bash_profile,.profile,.inputrc,path_add_function}; do
196 sudo -i <<EOF
197 PATH="/a/exe:$PATH"
198 lnf $x /root
199 EOF
200 done
201
202 ###### do conflink
203 # li needs the bind group before conflink
204 if [[ $HOSTNAME == li ]]; then
205 getent group bind &>/dev/null || sudo groupadd -r bind
206 fi
207 # this needs to be before installing pacserve so we have gpg conf.
208 conflink
209
210 ###### bash environment setup
211 set +x
212 errallow
213 source /etc/profile.d/environment.sh
214 source ~/.bashrc
215 $interactive || errcatch
216 $interactive || set -x
217
218
219 #### setup passwordless sudo
220 tu /etc/sudoers <<EOF
221 $USER ALL=(ALL) NOPASSWD: ALL
222 Defaults env_keep += SUDOD
223 # makes ubuntu be like debian
224 # https://unix.stackexchange.com/a/91572
225 Defaults always_set_home
226 EOF
227
228
229 #### setup firefox backport
230 ## ian: disabled. backports are not being published atm due to rust packaging issue
231 # if isdeb; then
232 # codename=$(debian-codename)
233 # if isdebian-stable && has_x; then
234 # s dd of=/etc/apt/sources.list.d/mozilla-iceweasel.list <<EOF
235 # deb http://mozilla.debian.net/ $codename-backports firefox-release
236 # deb-src http://mozilla.debian.net/ $codename-backports firefox-release
237 # EOF
238 # p update
239 # # take care of mozilla signing errors in previous command
240 # pi pkg-mozilla-archive-keyring
241 # fi
242 # p update
243 # fi
244
245
246 ###### arch aur wrapper setup
247 if isarch; then
248 #https://wiki.archlinux.org/index.php/Arch_User_Repository#Installing_packages
249 sudo pacman -S --noconfirm --needed base-devel jq
250 # pacaur seems to be the best, although it + cower has a few minor bugs,
251 # its design goals seem good, so, going for it.
252
253 aurpi() {
254 for p in "$@"; do
255 tempdir=$(mktemp -d)
256 pushd $tempdir
257 aurex "$p"
258 makepkg -sri --skippgpcheck --noconfirm
259 popd
260 rm -rf $tempdir
261 done
262 }
263 aurpi cower pacaur
264
265 pi pacserve
266
267 x=$(mktemp); /usr/bin/pacman.conf-insert_pacserve >$x
268 sudo dd of=/etc/pacman.conf if=$x; rm $x
269 sudo systemctl enable pacserve.service
270 sudo systemctl start pacserve.service
271
272 fi
273
274 #### update all packages
275 pup
276
277
278 ###### p1 packages install ######
279 if isarch; then
280 # requirement for trash-cli.
281 # background: strange error if just installing trash-cli: "pyalpm requires python",
282 # so I see that it requires python2, and installing that manually fixes it.
283 # I didn\'t see this on earlier installation, main thing which changed was
284 # pacserve, so not sure if it\'s related.
285 pi python2
286 fi
287 pi ${p1[@]}
288
289
290 ######## fix evbug bug ######
291 case $distro in
292 trisquel|ubuntu)
293 # noticed in flidas.
294 #https://bugs.launchpad.net/ubuntu/+source/module-init-tools/+bug/240553
295 #https://wiki.debian.org/KernelModuleBlacklisting
296 #common advice when searching is to use /etc/modprobe.d/blacklist.conf,
297 #but that file won't work and will get automatically reverted
298 sudo rmmod evbug ||: # might not be loaded yet
299 file=/etc/modprobe.d/evbug.conf
300 line="blacklist evbug"
301 if ! grep -xFq "$line" $file; then
302 sudo dd of=$file 2>/dev/null <<<"$line"
303 sudo depmod -a
304 sudo update-initramfs -u
305 fi
306 ;;
307 esac
308
309
310 ###### link files
311 # convenient to just do all file linking in one place
312 s lnf -T /a/bin /b
313 s lnf -T /nocow/t /t
314 if has_p; then
315 lnf -T /p/News ~/News
316 fi
317 s lnf /q/root/.editor-backups /q/root/.undo-tree-history \
318 /a/opt /a/c/.emacs.d $HOME/mw_vars /k/backup /root
319 rootsshsync
320 s lnf /a/c/.vim /a/c/.vimrc /a/c/.gvimrc /root
321 if has_p; then
322 # for dovecot
323 lnf -T /i/k/mboxes ~/mail
324 fi
325
326
327
328
329 ##### install xinput
330 if has_x; then
331 case $(distro-name) in
332 trisquel|ubuntu|debian)
333 pi xinput
334 ;;
335 fedora)
336 pi xinput_calibrator
337 ;;
338 arch)
339 pi xorg-xinput
340 ;;
341 esac
342
343 #### install redshift
344 case $(distro-name) in
345 trisquel|ubuntu|debian)
346 # recommends gets us geoclue (for darkening automatically at night i assume),
347 # which recommends modemmanager, which is annoying to fix for the model01 keyboard.
348 pi --no-install-recommends gtk-redshift
349 ;;&
350 fedora)
351 pi redshift-gtk
352 ;;&
353 arch)
354 pi redshift
355 ;;&
356 esac
357 fi
358
359
360 #### arch specific early packages
361 case $(distro-name) in
362 arch)
363 # pkgfile is like apt-cache
364 pi pkgfile
365 s pkgfile --update
366 ;;
367 esac
368
369 #### fedora specific packages
370 case $(distro-name) in
371 fedora)
372 # todo, this could probably come later
373 p -y groupinstall development-tools c-development books admin-tools
374 pi man-pages
375 ;;
376 # other distros unknown
377 esac
378
379 #### enable trim
380 # enable trim for volume delete, other rare commands
381 sudo $sed -ri 's/( *issue_discards\b).*/\1 = 1/' /etc/lvm/lvm.conf
382 if encrypted; then
383 if isdeb; then
384 sudo cp /usr/share/doc/util-linux/examples/fstrim.{service,timer} /etc/systemd/system
385 fi
386 # does weekly trim
387 sudo systemctl enable fstrim.timer
388 fi
389
390 ##### make extra dirs
391 dirs=(/mnt/{1,2,3,4,5,6,7,8,9} /nocow/t)
392 s mkdir -p "${dirs[@]}"
393 s chown $USER:$USER "${dirs[@]}"
394
395 ###### setup /i
396 tu /etc/fstab <<'EOF'
397 /i/w /w none bind,noauto 0 0
398 /i/k /k none bind,noauto 0 0
399 EOF
400 if ! mountpoint /kr; then
401 s mkdir -p /kr
402 s chown $USER:traci /kr
403 fi
404 if home_network; then
405 if [[ $HOSTNAME == frodo ]]; then
406 tu /etc/fstab <<'EOF'
407 /k /kr none bind,noauto 0 0
408 EOF
409 else
410 tu /etc/fstab <<'EOF'
411 frodo:/k /kr nfs noauto 0 0
412 EOF
413 fi
414 fi
415 s mkdir -p /q /i/{w,k}
416 for dir in /{i,w,k}; do
417 if mountpoint $dir; then continue; fi # already mounted
418 s mkdir -p $dir
419 s chown $USER:$USER $dir
420 done
421 # not needed for all hosts, but rather just keep it uniform
422 s mkdir -p /mnt/iroot
423 # debian auto mounting of multi-disk encrypted btrfs is busted. It is
424 # in jessie, and in stretch as of 11/26/2016 I have 4 disks in cryptab,
425 # based on 3 of those, it creates .device units for /dev/mapper/dev...
426 # then waits endlessly for them on bootup, after the /dev/mapper disks
427 # have already been created and exist. todo: create a simple repro
428 # for this in a vm and report it upstream.
429 if has_btrfs || home_network; then
430 pi nfs-common
431 s dd of=/root/imount <<'EOF'
432 #!/bin/bash
433 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
434 set -eE -o pipefail
435 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
436 for dir in /i /mnt/iroot /k /kr /w; do
437 if ! mountpoint $dir &>/dev/null && \
438 awk '{print $2}' /etc/fstab | grep -xF $dir &>/dev/null; then
439 if awk '{print $3}' /etc/fstab | grep -xF nfs &>/dev/null; then
440 mount $dir || echo "warning: failed to mount nfs on $dir"
441 else
442 mount $dir
443 fi
444 fi
445 done
446 EOF
447 s chmod +x /root/imount
448
449 s dd of=/etc/systemd/system/imount.service <<EOF
450 [Unit]
451 Description=Mount /i and related mountpoints
452 Before=syncthing@$USER.service
453
454 [Service]
455 Type=oneshot
456 ExecStart=/root/imount
457
458 [Install]
459 RequiredBy=syncthing@$USER.service
460 # note /kr needs networking, this target is the simplest way to
461 # time it when the network should be up, but not do something
462 # dumb like delay startup until the network is up. It happens
463 # at some time after network.target
464 WantedBy=multi-user.target
465 EOF
466 sudo systemctl daemon-reload # needed if the file was already there
467 sudo systemctl enable imount.service
468 sudo systemctl start imount.service
469 fi
470
471 ##### setup /nocow.
472 # a nocow dir that is common to multiple distros installed onthe same system
473 dir=/nocow
474 if has_btrfs; then
475 if ! mountpoint $dir; then
476 subvol=/mnt/root/nocow
477 if [[ ! -e $subvol ]]; then
478 s btrfs subvolume create $subvol
479 s chown root:1000 $subvol
480 s chattr +C $subvol
481 fi
482
483 first_root_crypt=$(awk '$2 == "/" {print $1}' /etc/mtab)
484 tu /etc/fstab <<EOF
485 $first_root_crypt /nocow btrfs noatime,subvol=nocow 0 0
486 EOF
487 s mkdir -p $dir
488 s chown $USER:$USER $dir
489 s mount $dir
490 fi
491 else
492 sudo mkdir -p $dir
493 fi
494
495
496 ###### fix mouse on jessie
497 # it comes with stretch and arch, but not jessie.
498 # propogate /etc/udev/hwdb.d
499 if which systemd-hwdb; then
500 s systemd-hwdb update
501 ser restart systemd-udev-trigger
502 fi
503
504
505 ##### setup email
506 if isdeb; then
507 mail-setup exim4
508 else
509 # todo: probably broken
510 mail-setup postfix
511 fi
512
513 #### ubuntu nicety
514 if isubuntu; then
515 # disable crash report annoying dialogs.
516 s dd of=/etc/default/apport <<<'enabled=0'
517 fi
518
519 ###### setup time zone
520 # fai sets this an old way that doesn't work for stretch.
521 # no harm in setting it universally here.
522 # using debconf-set-selection, the area gets reset to ETC
523 # on my linode test machine after doing a dpkg-reconfigure, or a reinstall,
524 # so we are using expect :(
525 # I got a random error when running this, so I added a sleep
526 # rather than trying to write a whole detect and wait loop.
527 # E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
528 # E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
529 sleep 1
530 # todo: this is not idempotent, it fails when running twice, due to prepopulated values.
531 # check into unsetting them using debconf-set-selection.
532 s apt-get -y install --no-install-recommends expect
533 s expect <<EOF ||:
534 set force_conservative 0
535 spawn dpkg-reconfigure tzdata -freadline
536 expect -nocase timeout {exit 1} "Geographic area:"
537 send "\02512\r"
538 expect -nocase timeout {exit 1} "Time zone:"
539 send "\0255\r"
540 expect eof
541 exit
542 EOF
543
544
545 ##### install emacs
546 if has_x; then
547 if isarch; then
548 # emacs git build was broken last time i checked,
549 x=$(mktemp -d)
550 pushd $x
551 aurex emacs-git
552 makepkg -si --noconfirm
553 popd
554 rm -rf $x
555 pi hunspell hunspell-en
556 else
557 if $recompile; then
558 /a/bin/buildscripts/emacs
559 else
560 /a/bin/buildscripts/emacs --no-r || /a/bin/buildscripts/emacs
561 fi
562 fi
563 fi
564
565 ##### install laptop hardware packages
566 if tp || x2; then
567 case $distro in
568 debian)
569 pi task-laptop
570 ;;
571 ubuntu|trisquel)
572 # the exact packages that task-laptop would install, since ubuntu
573 # doesn\'t have this virtual in practice package.
574 pi avahi-autoipd bluetooth powertop iw wireless-tools wpasupplicant
575 ;;
576 # todo: other distros unknown
577 esac
578 fi
579
580
581 ##### install xmonad
582 if has_x; then
583 pi ${p2[@]}
584 # note: on older ubuntu I used cabal xmonad + xfce, using cabal versin
585 # see /w/archive/programming/xmonad-cabal.sh
586 if isarch; then
587 # xorg-xmessage for displaying error messages.
588 # optional dependency in arch, standard elsewhere.
589 pi xorg-server xorg-xmessage xmonad-contrib xorg-xsetroot xorg-xinit
590 else
591 pi suckless-tools
592 fi
593 fi
594
595
596 ##### setup X autostart
597 if has_x; then
598 if isarch; then
599 # https://wiki.archlinux.org/index.php/Xinitrc
600 for homedir in /home/*; do
601 cp /etc/X11/xinit/xinitrc $homedir/.xinitrc
602 $sed -ri '/^ *twm\b/,$d' $homedir/.xinitrc
603 tee -a $homedir/.xinitrc <<'EOF'
604 /a/bin/desktop-20-autostart.sh
605 xsetroot -cursor_name left_ptr
606 exec xmonad
607 EOF
608 done
609 else
610 # todo, figure this out for arch if we ever try out gnome.
611 # install for multiple display managers in case we use one
612 if isdeb; then
613 dir=/etc/gdm3
614 elif isfedora; then
615 # fedora didn\'t have the 3.
616 dir=/etc/gdm
617 fi
618 s mkdir -p $dir/PostLogin
619 s command cp /a/bin/distro-setup/desktop-20-autostart.sh $dir/PostLogin/Default
620 s mkdir /etc/lightdm/lightdm.conf.d
621 s dd of=/etc/lightdm/lightdm.conf.d/12-iank.conf <<'EOF'
622 [SeatDefaults]
623 session-setup-script=/a/bin/distro-setup/desktop-20-autostart.sh
624 EOF
625 fi
626 fi
627
628
629 #### refix interactive ssh terminal
630 # the first pup command can kill off our /etc/ mod, so rerun this
631 /a/exe/ssh-emacs-setup
632
633
634 echo "$0: $(date): ending now"
635 exit 0