fix btrbk service
[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 has_p() { ! linode; } # when tp is tracis, then not tp either
77 has_x() { ! linode; }
78 linode() { lj || li; }
79 has_btrfs() { ! linode; }
80 home_network() { ! linode; }
81 encrypted() { has_p; }
82 shopt -s extglob
83 export GLOBIGNORE=*/.:*/..
84 umask 022
85 PATH="/a/exe:$PATH"
86 sed="sed --follow-symlinks"
87
88 ####### end setup environment #######
89
90
91
92
93 ##### begin setup encryption scripts ######
94 if encrypted; then
95 # I tried making a service which was dependent on reboot.target,
96 # but it happened too late in the shutdown process.
97 sudo dd of=/etc/systemd/system/keyscripton.service <<'EOF'
98 [Unit]
99 Description=Turn on automatic decryption of drives on boot
100 # tried using graphical.target, but it made my display manager restart before rebooting.
101 # generally, I don't think targets order shutdown like they do startup.
102 # So, I did systemd-analyze plot > something.svg, and picked a reliably started
103 # service that happens late in the game.
104 After=ntp.service
105 DefaultDependencies=no
106 # not sure if needed, makes sure we shut down before reboot.target
107 Conflicts=reboot.target
108
109 [Service]
110 Type=oneshot
111 RemainAfterExit=yes
112 ExecStart=/bin/true
113 ExecStop=/a/exe/keyscript-on
114
115 [Install]
116 WantedBy=keyscriptoff.service
117 EOF
118 sudo systemctl daemon-reload # needed if the file was already there
119 sudo systemctl stop keyscripton.service
120 # sudo systemctl start keyscripton.service
121 sudo systemctl enable keyscripton.service
122
123 sudo dd of=/etc/systemd/system/keyscriptoff.service <<'EOF'
124 [Unit]
125 Description=Turn off automatic decryption of drives on boot
126
127 [Service]
128 Type=oneshot
129 ExecStart=/a/exe/keyscript-off
130
131 [Install]
132 WantedBy=multi-user.target
133 EOF
134 sudo systemctl daemon-reload # needed if the file was already there
135 sudo systemctl enable keyscriptoff.service
136 sudo systemctl start keyscriptoff.service
137
138 # from /usr/share/doc/dropbear-initramfs/README.initramfs.gz
139 while read m _; do /sbin/modinfo -F filename "$m"; done </proc/modules | \
140 sed -nr "s@^/lib/modules/`uname -r`/kernel/drivers/net(/.*)?/([^/]+)\.ko\$@\2@p" \
141 | sudo dd of=/etc/initramfs-tools/modules
142 sudo apt-get -y install initramfs-tools-core
143 # initram auth keys get setup with rootsshsync
144 # then for remote unlock, ssh and do this once per crypt disk:
145 # echo -n PASS >/lib/cryptsetup/passfifo
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 err-allow
213 source /etc/profile.d/environment.sh
214 source ~/.bashrc
215 err-catch
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 # default setting is to have minimum umask of 0022
227 # This lets us have user-specific umasks which are more permissive.
228 # I did this for transmission and set it's umask gecos on install,
229 # see there for more info.
230 Defaults !umask
231 EOF
232
233
234 #### setup firefox backport
235 ## ian: disabled. backports are not being published atm due to rust packaging issue
236 # if isdeb; then
237 # codename=$(debian-codename)
238 # if isdebian-stable && has_x; then
239 # s dd of=/etc/apt/sources.list.d/mozilla-iceweasel.list <<EOF
240 # deb http://mozilla.debian.net/ $codename-backports firefox-release
241 # deb-src http://mozilla.debian.net/ $codename-backports firefox-release
242 # EOF
243 # p update
244 # # take care of mozilla signing errors in previous command
245 # pi pkg-mozilla-archive-keyring
246 # fi
247 # p update
248 # fi
249
250
251 ###### arch aur wrapper setup
252 if isarch; then
253 #https://wiki.archlinux.org/index.php/Arch_User_Repository#Installing_packages
254 sudo pacman -S --noconfirm --needed base-devel jq
255 # pacaur seems to be the best, although it + cower has a few minor bugs,
256 # its design goals seem good, so, going for it.
257
258 aurpi() {
259 for p in "$@"; do
260 tempdir=$(mktemp -d)
261 pushd $tempdir
262 aurex "$p"
263 makepkg -sri --skippgpcheck --noconfirm
264 popd
265 rm -rf $tempdir
266 done
267 }
268 aurpi cower pacaur
269
270 pi pacserve
271
272 x=$(mktemp); /usr/bin/pacman.conf-insert_pacserve >$x
273 sudo dd of=/etc/pacman.conf if=$x; rm $x
274 sudo systemctl enable pacserve.service
275 sudo systemctl start pacserve.service
276
277 fi
278
279 #### update all packages
280 pup
281
282
283 ###### p1 packages install ######
284 if isarch; then
285 # requirement for trash-cli.
286 # background: strange error if just installing trash-cli: "pyalpm requires python",
287 # so I see that it requires python2, and installing that manually fixes it.
288 # I didn\'t see this on earlier installation, main thing which changed was
289 # pacserve, so not sure if it\'s related.
290 pi python2
291 fi
292 pi ${p1[@]}
293
294
295 ######## fix evbug bug ######
296 case $distro in
297 trisquel|ubuntu)
298 # noticed in flidas.
299 #https://bugs.launchpad.net/ubuntu/+source/module-init-tools/+bug/240553
300 #https://wiki.debian.org/KernelModuleBlacklisting
301 #common advice when searching is to use /etc/modprobe.d/blacklist.conf,
302 #but that file won't work and will get automatically reverted
303 sudo rmmod evbug ||: # might not be loaded yet
304 file=/etc/modprobe.d/evbug.conf
305 line="blacklist evbug"
306 if ! grep -xFq "$line" $file; then
307 sudo dd of=$file 2>/dev/null <<<"$line"
308 sudo depmod -a
309 sudo update-initramfs -u
310 fi
311 ;;
312 esac
313
314
315 ###### link files
316 # convenient to just do all file linking in one place
317 s lnf -T /a/bin /b
318 s lnf -T /nocow/t /t
319 if has_p; then
320 lnf -T /p/News ~/News
321 fi
322 s lnf /q/root/.editor-backups /q/root/.undo-tree-history \
323 /a/opt /a/c/.emacs.d $HOME/mw_vars /k/backup /root
324 pi rsync # needed for rootsshsync
325 /a/bin/ds/install-my-scripts # needed for rootsshsync
326 rootsshsync
327 s lnf /a/c/.vim /a/c/.vimrc /a/c/.gvimrc /root
328 if has_p; then
329 # for dovecot
330 lnf -T /i/k/mboxes ~/mail
331 fi
332
333
334
335
336 ##### install xinput
337 if has_x; then
338 case $(distro-name) in
339 trisquel|ubuntu|debian)
340 pi xinput
341 ;;
342 fedora)
343 pi xinput_calibrator
344 ;;
345 arch)
346 pi xorg-xinput
347 ;;
348 esac
349
350 #### install redshift
351 case $(distro-name) in
352 trisquel|ubuntu|debian)
353 # recommends gets us geoclue (for darkening automatically at night i assume),
354 # which recommends modemmanager, which is annoying to fix for the model01 keyboard.
355 pi --no-install-recommends gtk-redshift
356 ;;&
357 fedora)
358 pi redshift-gtk
359 ;;&
360 arch)
361 pi redshift
362 ;;&
363 esac
364 fi
365
366
367 #### arch specific early packages
368 case $(distro-name) in
369 arch)
370 # pkgfile is like apt-cache
371 pi pkgfile
372 s pkgfile --update
373 ;;
374 esac
375
376 #### fedora specific packages
377 case $(distro-name) in
378 fedora)
379 # todo, this could probably come later
380 p -y groupinstall development-tools c-development books admin-tools
381 pi man-pages
382 ;;
383 # other distros unknown
384 esac
385
386 #### enable trim
387 # enable trim for volume delete, other rare commands
388 sudo $sed -ri 's/( *issue_discards\b).*/\1 = 1/' /etc/lvm/lvm.conf
389 if encrypted; then
390 if isdeb; then
391 sudo cp /usr/share/doc/util-linux/examples/fstrim.{service,timer} /etc/systemd/system
392 fi
393 # does weekly trim
394 sudo systemctl enable fstrim.timer
395 fi
396
397 ##### make extra dirs
398 dirs=(/mnt/{1,2,3,4,5,6,7,8,9} /nocow/t)
399 s mkdir -p "${dirs[@]}"
400 s chown $USER:$USER "${dirs[@]}"
401
402 ###### setup /i
403 tu /etc/fstab <<'EOF'
404 /i/w /w none bind,noauto 0 0
405 /i/k /k none bind,noauto 0 0
406 EOF
407 if ! mountpoint /kr; then
408 s mkdir -p /kr
409 s chown $USER:traci /kr
410 fi
411 if home_network; then
412 if [[ $HOSTNAME == frodo ]]; then
413 tu /etc/fstab <<'EOF'
414 /k /kr none bind,noauto 0 0
415 EOF
416 else
417 tu /etc/fstab <<'EOF'
418 frodo:/k /kr nfs noauto 0 0
419 EOF
420 fi
421 fi
422 s mkdir -p /q /i/{w,k}
423 for dir in /{i,w,k}; do
424 if mountpoint $dir; then continue; fi # already mounted
425 s mkdir -p $dir
426 s chown $USER:$USER $dir
427 done
428 # not needed for all hosts, but rather just keep it uniform
429 s mkdir -p /mnt/iroot
430 # debian auto mounting of multi-disk encrypted btrfs is busted. It is
431 # in jessie, and in stretch as of 11/26/2016 I have 4 disks in cryptab,
432 # based on 3 of those, it creates .device units for /dev/mapper/dev...
433 # then waits endlessly for them on bootup, after the /dev/mapper disks
434 # have already been created and exist. todo: create a simple repro
435 # for this in a vm and report it upstream.
436 if has_btrfs || home_network; then
437 pi nfs-common
438 s dd of=/root/imount <<'EOF'
439 #!/bin/bash
440 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
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 s chmod +x /root/imount
455
456 s dd of=/etc/systemd/system/imount.service <<EOF
457 [Unit]
458 Description=Mount /i and related mountpoints
459 Before=syncthing@$USER.service
460
461 [Service]
462 Type=oneshot
463 ExecStart=/root/imount
464
465 [Install]
466 RequiredBy=syncthing@$USER.service
467 # note /kr needs networking, this target is the simplest way to
468 # time it when the network should be up, but not do something
469 # dumb like delay startup until the network is up. It happens
470 # at some time after network.target
471 WantedBy=multi-user.target
472 EOF
473 sudo systemctl daemon-reload # needed if the file was already there
474 sudo systemctl enable imount.service
475 sudo systemctl start imount.service
476 fi
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 s btrfs subvolume create $subvol
486 s chown root:1000 $subvol
487 s 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 s mkdir -p $dir
495 s chown $USER:$USER $dir
496 s mount $dir
497 fi
498 else
499 sudo mkdir -p $dir
500 fi
501
502
503 ###### fix mouse on jessie
504 # it comes with stretch and arch, but not jessie.
505 # propogate /etc/udev/hwdb.d
506 if which systemd-hwdb; then
507 s systemd-hwdb update
508 ser restart systemd-udev-trigger
509 fi
510
511
512 ##### setup email
513 if isdeb; then
514 mail-setup exim4
515 else
516 # todo: probably broken
517 mail-setup postfix
518 fi
519
520 #### ubuntu nicety
521 if isubuntu; then
522 # disable crash report annoying dialogs.
523 s dd of=/etc/default/apport <<<'enabled=0'
524 fi
525
526 ###### setup time zone
527 # fai sets this an old way that doesn't work for stretch.
528 # no harm in setting it universally here.
529 # using debconf-set-selection, the area gets reset to ETC
530 # on my linode test machine after doing a dpkg-reconfigure, or a reinstall,
531 # so we are using expect :(
532 # I got a random error when running this, so I added a sleep
533 # rather than trying to write a whole detect and wait loop.
534 # E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
535 # E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
536 sleep 1
537 # todo: this is not idempotent, it fails when running twice, due to prepopulated values.
538 # check into unsetting them using debconf-set-selection.
539 s apt-get -y install --no-install-recommends expect
540 s expect <<EOF ||:
541 set force_conservative 0
542 spawn dpkg-reconfigure tzdata -freadline
543 expect -nocase timeout {exit 1} "Geographic area:"
544 send "\02512\r"
545 expect -nocase timeout {exit 1} "Time zone:"
546 send "\0255\r"
547 expect eof
548 exit
549 EOF
550
551
552 ##### install emacs
553 if has_x; then
554 if isarch; then
555 # emacs git build was broken last time i checked,
556 x=$(mktemp -d)
557 pushd $x
558 aurex emacs-git
559 makepkg -si --noconfirm
560 popd
561 rm -rf $x
562 pi hunspell hunspell-en
563 else
564 if $recompile; then
565 /a/bin/buildscripts/emacs
566 else
567 /a/bin/buildscripts/emacs --no-r || /a/bin/buildscripts/emacs
568 fi
569 fi
570 fi
571
572 ##### install laptop hardware packages
573 if tp || x2 || x3; then
574 case $distro in
575 debian)
576 pi task-laptop
577 ;;
578 ubuntu|trisquel)
579 # the exact packages that task-laptop would install, since ubuntu
580 # doesn\'t have this virtual in practice package.
581 pi avahi-autoipd bluetooth powertop iw wireless-tools wpasupplicant
582 ;;
583 # todo: other distros unknown
584 esac
585 fi
586
587
588 ##### install xmonad
589 if has_x; then
590 pi ${p2[@]}
591 # note: on older ubuntu I used cabal xmonad + xfce, using cabal versin
592 # see /w/archive/programming/xmonad-cabal.sh
593 if isarch; then
594 # xorg-xmessage for displaying error messages.
595 # optional dependency in arch, standard elsewhere.
596 pi xorg-server xorg-xmessage xmonad-contrib xorg-xsetroot xorg-xinit
597 else
598 pi suckless-tools
599 fi
600 fi
601
602
603 ##### setup X autostart
604 if has_x; then
605 if isarch; then
606 # https://wiki.archlinux.org/index.php/Xinitrc
607 for homedir in /home/*; do
608 cp /etc/X11/xinit/xinitrc $homedir/.xinitrc
609 $sed -ri '/^ *twm\b/,$d' $homedir/.xinitrc
610 tee -a $homedir/.xinitrc <<'EOF'
611 /a/bin/desktop-20-autostart.sh
612 xsetroot -cursor_name left_ptr
613 exec xmonad
614 EOF
615 done
616 else
617 # todo, figure this out for arch if we ever try out gnome.
618 # install for multiple display managers in case we use one
619 if isdeb; then
620 dir=/etc/gdm3
621 elif isfedora; then
622 # fedora didn\'t have the 3.
623 dir=/etc/gdm
624 fi
625 s mkdir -p $dir/PostLogin
626 s command cp /a/bin/distro-setup/desktop-20-autostart.sh $dir/PostLogin/Default
627 s mkdir /etc/lightdm/lightdm.conf.d
628 s dd of=/etc/lightdm/lightdm.conf.d/12-iank.conf <<'EOF'
629 [SeatDefaults]
630 session-setup-script=/a/bin/distro-setup/desktop-20-autostart.sh
631 EOF
632 fi
633 fi
634
635
636 #### refix interactive ssh terminal
637 # the first pup command can kill off our /etc/ mod, so rerun this
638 /a/exe/ssh-emacs-setup
639
640
641 echo "$0: $(date): ending now"
642 exit 0