2 # Setup Ian's computers
3 # Copyright (C) 2024 Ian Kelling
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # SPDX-License-Identifier: GPL-3.0-or-later
20 #### for setting up a new machine
21 # usage: $0 [-r] [HOSTNAME]
22 # HOSTNAME changes the machine's hostname
24 # Update target_down alerts in
25 # /a/bin/ds/filesystem/etc/prometheus/rules/iank.yml
27 # Update hostnames in /b/ds/check-remote-mailqs
28 # Update hostnames in /b/ds/machine_specific/*.hosts /p/c/machine_specific/*.hosts
29 # Update hostnames in this file
31 ### end new machine setup
34 # run any sudo command first so your pass is cached
35 # set the scrollback to unlimited in case something goes wrong
38 # send to registrar, glue records:
41 # ns1.iankelling.org 72.14.176.105
42 # ns1.iankelling.org 2600:3c00::f03c:91ff:fe6d:baf8
43 # ns2.iankelling.org 172.105.84.95
44 # ns2.iankelling.org 2a01:7e01::f03c:91ff:feb5:baec
48 # ns1.zroe.org 72.14.176.105
49 # ns1.zroe.org 2600:3c00::f03c:91ff:fe6d:baf8
50 # ns2.zroe.org 172.105.84.95
51 # ns2.zroe.org 2a01:7e01::f03c:91ff:feb5:baec
56 ####### begin setup environment #######
58 # shellcheck disable=SC2317 # false positive
60 ### make ssh interactive shell run better. for when running line interactively line by line
61 sudo bash
-c '/a/exe/ssh-emacs-setup' ||
exit $?
63 ##### setup error handling
64 interactive
=true
# set this to false to force set -x
65 [[ $
- == *i
* ]] || interactive
=false
66 if ! $interactive; then
69 source /a
/bin
/bash-bear-trap
/bash-bear
73 echo 1 >~
/.local
/distro-begin
76 source /a
/bin
/distro-functions
/src
/package-manager-abstractions
79 echo "$0: $(date): starting now)"
83 if [[ $EUID == 0 ]]; then
84 if getent passwd iank || getent passwd ian
; then
85 echo "$0: error: running as root. unprivileged user exists. use it."
88 echo "$0: warning: running as root. I will setup users then exit"
96 if [[ -e /a
/opt
/emacs
]]; then
99 while [[ $1 == -* ]]; do
101 -r) recompile
=true
; shift ;;
102 -e) emacs
=false
; shift ;;
110 ##### variables/env setup
111 script_dir
="$(readlink -f "${BASH_SOURCE[@]}")"; script_dir
=${script_dir%/*}
112 # shellcheck source=./pkgs
113 source $script_dir/pkgs
115 source /a
/bin
/distro-functions
/src
/identify-distros
116 $interactive ||
set -x
117 for f
in kd x2 x3 x8 frodo tp li bk je demohost kw sy bo so
; do
118 eval "$f() { [[ $HOSTNAME == $f ]]; }"
120 codename
=$
(debian-codename
)
121 bitfolk
() { je || bk
; }
122 has_wayland
() { has_monitor
&& [[ $codename == buster
]]; }
123 has_x
() { has_monitor
&& [[ $codename != buster
]]; }
124 has_monitor
() { ! vps
; }
125 vps
() { bitfolk || li
; }
126 # linode actually has btrfs now, but we dont do anything with it.
127 has_btrfs
() { ! vps
; }
128 home_network
() { ! vps
&& ! tp
; }
129 has_p
() { ! vps
&& ! tp
; }
130 encrypted
() { ! bitfolk
; }
132 export GLOBIGNORE
="*/.:*/.."
135 sed="sed --follow-symlinks"
137 ####### end setup environment #######
140 ##### begin setup encryption scripts ######
142 # I tried making a service which was dependent on reboot.target,
143 # but it happened too late in the shutdown process.
144 sudo
dd of
=/etc
/systemd
/system
/keyscripton.service
<<'EOF'
146 Description=keyscripton
147 # This is triggered by reboot and when keyscriptoff stops.
149 # tried using graphical.target, but it made my display manager restart before rebooting.
150 # generally, I don't think targets order shutdown like they do startup.
151 # So, I did systemd-analyze plot > something.svg, and picked a reliably started
152 # service that happens late in the game.
154 DefaultDependencies=no
155 # not sure if needed, makes sure we shut down before reboot.target
156 Conflicts=reboot.target
162 ExecStop=/a/exe/keyscript-on
165 WantedBy=keyscriptoff.service
167 sudo systemctl daemon-reload
# needed if the file was already there
168 sudo systemctl
enable keyscripton.service
170 sudo
dd of
=/etc
/systemd
/system
/keyscriptoff.service
<<'EOF'
172 Description=keyscriptoff
176 ExecStart=/a/exe/keyscript-off
179 WantedBy=multi-user.target
181 sudo systemctl daemon-reload
# needed if the file was already there
182 sudo systemctl
enable keyscriptoff.service
183 sudo systemctl start keyscriptoff.service
187 ## /usr/share/doc/dropbear-initramfs/README.initramfs.gz
188 ## claims we need to do this. but it works fine without it.
190 # while read -r m _; do /sbin/modinfo -F filename "$m"; done </proc/modules | \
191 # sed -nr "s@^/lib/modules/$(uname -r)/kernel/drivers/net(/.*)?/([^/]+)\.ko\$@\2@p" \
193 # if ! diff -q /etc/initramfs-tools/modules $tmp &>/dev/null; then
194 # sudo dd if=$tmp of=/etc/initramfs-tools/modules
195 # sudo /usr/sbin/update-initramfs -u -k all
198 ## if we were creating an intall for a different machine needing different modules, we could include them all like this:
199 ## 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
201 # this is here to cleanup the leftover from the comments above. remove it eventually.
202 if [[ -s /etc
/initramfs-tools
/modules
]]; then
203 sudo truncate
-s0 /etc
/initramfs-tools
/modules
204 sudo
/usr
/sbin
/update-initramfs
-u -k all
207 # initram auth keys get setup with rootsshsync later on.
208 $script_dir/rootsshsync
210 ### To do a remote unlock: ssh and do this once per crypt disk:
211 # echo -n PASS >/lib/cryptsetup/passfifo
216 ##### end setup encryption scripts ######
219 # disabled until its fixed up
222 # todo, it would be nice to cut down on some of the output
225 #### rerun my fai-time scripts
226 # already ran for pxe installs, but used for vps & updates
227 distro
=$
(distro-name
)
229 ubuntu|debian|trisquel
)
230 sudo bash
-c ". /a/bin/fai/fai-wrapper && /a/bin/fai/fai/config/scripts/IANK/11-iank"
233 sudo bash
-c ". /a/bin/fai/fai-wrapper &&
234 /a/bin/fai/fai/config/distro-install-common/end"
238 ###### setup hostname
239 if [[ $HOSTNAME != $
(cat /etc
/hostname
) ]]; then
240 echo $HOSTNAME > /etc
/hostname
241 hostname
-F /etc
/hostname
243 # office vpn dhcp adds to /etc/resolv.conf search office.fsf.org which
244 # makes that be #1 priority, which makes dnsmasq resolve that for
245 # unqualified hosts first, which means we skip the hosts file. Ya, its
246 # kinda dumb, but it is what it is. There is a dnsmasq config option to
247 # override it too, but this seems simpler.
248 sudo
sed -i --follow-symlinks -f - /etc
/hosts
<<EOF
249 \$a 127.0.1.1 $HOSTNAME.b8.nz $HOSTNAME.office.fsf.org $HOSTNAME
253 ##### exit first stage if running as root
254 if [[ $EUID == 0 ]]; then
255 if [[ ! -e /home
/iank
/.ssh
/authorized_keys
&& ! -L /home
/iank
/.ssh
/authorized_keys
]]; then
256 sudo
-u iank mkdir
-p /home
/iank
/.
ssh
257 chmod 0700 /home
/iank
/.
ssh
258 sudo
-u iank
ln -sf /p
/c
/machine_specific
/vps
/subdir_files
/.ssh
/authorized_keys
/home
/iank
/.
ssh
260 echo "$0: running as root. exiting now that users are setup"
265 #### setup bash for root
266 for x
in /a
/c
/{.bashrc
,brc
,brc2
,.bash_profile
,.profile
,.inputrc
,path_add_function
}; do
274 # convenient to just do all file linking in one place
275 sudo
/a
/exe
/lnf
-T /a
/bin
/b
276 sudo
/a
/exe
/lnf
-T /a
/f
/f
277 sudo
/a
/exe
/lnf
-T /var
/log
/exim4
/el
278 sudo
/a
/exe
/lnf
-T /a
/f
/ans
/c
279 sudo
/a
/exe
/lnf
-T /nocow
/t
/t
281 lnf
-T /p
/News ~
/News
283 dirs=(/q
/root
/q
/root
/.editor-backups
/q
/root
/.undo-tree-history
)
284 sudo mkdir
-p ${dirs[@]}
285 sudo
chmod 600 ${dirs[@]}
286 sudo
/a
/exe
/lnf
/q
/root
/.editor-backups
/q
/root
/.undo-tree-history \
287 /a
/opt
/a
/c
/.emacs.d
$HOME/mw_vars
/k
/backup
/root
288 /a
/bin
/ds
/install-my-scripts
# needed for rootsshsync cronjob
289 sudo
/a
/exe
/lnf
/a
/c
/.vim
/a
/c
/.vimrc
/a
/c
/.gvimrc
/root
293 # vps needs bind group before conflink
300 # this needs to be before installing pacserve so we have gpg conf.
303 if [[ -e /etc
/rootsudoenv
]]; then
304 source /etc
/rootsudoenv
309 ###### bash environment setup
312 source /etc
/profile.d
/environment.sh
313 export LC_USEBASHRC
=t
314 # shellcheck source=./brc
317 $interactive ||
set -x
321 if dpkg
-s -- nscd
&>/dev
/null
; then
322 sudo apt-get
-y remove
--purge --auto-remove nscd
323 sudo systemctl stop nscd ||
: # fails if already stopped
326 # http://strugglers.net/~andy/blog/2020/12/03/starting-services-only-when-the-network-is-ready-on-debiansystemd/
327 if systemctl
cat ifupdown-wait-online.service
&>/dev
/null
; then
328 sudo systemctl
enable ifupdown-wait-online.service
332 # remove line like this: 85.119.82.128 je.iankelling.org je
333 # it messes with hostname -f.
334 sudo
sed -ri "/^127\./n;/[[:space:]]$HOSTNAME\$/d" /etc
/hosts
337 if isdeb
&& [[ $
(debian-codename
) == aramo
]]; then
338 sudo
dd of
=/etc
/apt
/preferences.d
/aramo-jammy-missing
<<'EOF'
339 Package: linux-libc-dev libmysqlclient21
340 Pin: release n=jammy,o=Ubuntu
345 # libfdk just has some patent worries.
346 # https://www.gnu.org/licenses/license-list.en.html#fdk
347 if isdeb
&& [[ $
(debian-codename
) == nabia
]]; then
348 sudo
dd of
=/etc
/apt
/preferences.d
/nabia-focal-missing
<<'EOF'
350 Pin: release n=focal,o=Ubuntu
354 # it has a dependency python-paramiko that should be
355 # python3-paramiko. the backport version is pretty close to the normal
356 # version, so i just set the pin priority low for the backport and it
358 sudo
dd of
=/etc
/apt
/preferences.d
/nabia-backport
<<'EOF'
360 Pin: release a=nabia-backports
367 # to test when these become available in trisquel, copy the package list, remove the * chars, repaplace PACKAGES below
368 # for x in PACKAGES; do hr; e $x; if [[ $x == *- ]]; then s="^$x"; dpkg -l "$x*" | sed '0,/^+++/d'; else s="^$x\$"; fi; aptitude search "~O Trisquel ~n $s"; done
371 #### setup firefox backport
372 ## ian: disabled. backports are not being published atm due to rust packaging issue
374 # codename=$(debian-codename)
375 # if isdebian-stable && has_x; then
376 # s dd of=/etc/apt/sources.list.d/mozilla-iceweasel.list <<EOF
377 # deb http://mozilla.debian.net/ $codename-backports firefox-release
378 # deb-src http://mozilla.debian.net/ $codename-backports firefox-release
381 # # take care of mozilla signing errors in previous command
382 # pi pkg-mozilla-archive-keyring
388 ###### arch aur wrapper setup
390 #https://wiki.archlinux.org/index.php/Arch_User_Repository#Installing_packages
391 sudo pacman
-S --noconfirm --needed base-devel jq
392 # pacaur seems to be the best, although it + cower has a few minor bugs,
393 # its design goals seem good, so, going for it.
400 makepkg
-sri --skippgpcheck --noconfirm
409 x
=$
(mktemp
); /usr
/bin
/pacman.conf-insert_pacserve
>$x
410 sudo
dd of
=/etc
/pacman.conf
if=$x; rm $x
411 sudo systemctl
enable pacserve.service
412 sudo systemctl start pacserve.service
416 #### update all packages
417 # shellcheck disable=SC2119 # obvious
421 ###### p1 packages install ######
425 ######## fix evbug bug ######
426 case $
(debian-codename-compat
) in
427 xenial|bionic|focal|jammy|noble
)
428 # noticed in flidas. dunno if it affects any others
429 #https://bugs.launchpad.net/ubuntu/+source/module-init-tools/+bug/240553
430 #https://wiki.debian.org/KernelModuleBlacklisting
431 #common advice when searching is to use /etc/modprobe.d/blacklist.conf,
432 #but that file won't work and will get automatically reverted
433 sudo rmmod evbug ||
: # might not be loaded yet
434 file=/etc
/modprobe.d
/evbug.conf
435 line
="blacklist evbug"
436 if [[ $
(cat $file) != "$line" ]]; then
437 sudo
dd of
=$file status
=none
<<<"$line"
439 sudo update-initramfs
-u
446 #### arch specific early packages
447 case $
(distro-name
) in
449 # pkgfile is like apt-cache
451 sudo pkgfile
--update
456 # enable trim for volume delete, other rare commands
457 sudo
$sed -ri 's/( *issue_discards\b).*/\1 = 1/' /etc
/lvm
/lvm.conf
459 # flidas or so, these units arent built-in
460 if isdeb
&& ! systemctl list-unit-files |
grep ^fstrim.timer
&>/dev
/null
; then
461 sudo
cp /usr
/share
/doc
/util-linux
/examples
/fstrim.
{service
,timer
} /etc
/systemd
/system
464 sudo systemctl
enable fstrim.timer
467 ##### make extra dirs
469 for dir
in /mnt
/{1,2,3,4,5,6,7,8,9} /nocow
/t
; do
470 [[ -e $dir ]] && continue
473 if (( ${#dirs[@]} )); then
474 # mkdir -p on a mounted directory will fail. so will chown if its an ro mount
475 sudo mkdir
-p "${dirs[@]}"
476 sudo chown
$USER:$USER "${dirs[@]}"
480 # disabled temporarily
482 # if home_network; then
483 # sudo /a/exe/teeu /etc/fstab <<'EOF'
484 # /i/w /w none bind,noauto 0 0
485 # /i/k /k none bind,noauto 0 0
487 # if ! mountpoint /kr; then
489 # sudo chown $USER:user2 /kr
491 # if [[ $HOSTNAME == frodo ]]; then
492 # sudo /a/exe/teeu /etc/fstab <<'EOF'
493 # /k /kr none bind,noauto 0 0
496 # sudo /a/exe/teeu /etc/fstab <<'EOF'
497 # frodo:/k /kr nfs noauto 0 0
500 # sudo mkdir -p /i/{w,k}
501 # for dir in /{i,w,k}; do
502 # if mountpoint $dir; then continue; fi # already mounted
504 # sudo chown $USER:$USER $dir
506 # # debian auto mounting of multi-disk encrypted btrfs is busted. It is
507 # # in jessie, and in stretch as of 11/26/2016 I have 4 disks in cryptab,
508 # # based on 3 of those, it creates .device units for /dev/mapper/dev...
509 # # then waits endlessly for them on bootup, after the /dev/mapper disks
510 # # have already been created and exist. todo: create a simple repro
511 # # for this in a vm and report it upstream.
513 # sudo dd of=/root/imount <<'EOF'
515 # [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
516 # set -eE -o pipefail
517 # trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
518 # for dir in /i /k /kr /w; do
519 # if ! mountpoint $dir &>/dev/null && \
520 # awk '{print $2}' /etc/fstab | grep -xF $dir &>/dev/null; then
521 # if awk '{print $3}' /etc/fstab | grep -xF nfs &>/dev/null; then
522 # mount $dir || echo "warning: failed to mount nfs on $dir"
529 # sudo chmod +x /root/imount
530 # sudo dd of=/etc/systemd/system/imount.service <<EOF
532 # Description=Mount /i and related mountpoints
533 # Before=syncthing@$USER.service
537 # ExecStart=/root/imount
540 # RequiredBy=syncthing@$USER.service
541 # # note /kr needs networking, this target is the simplest way to
542 # # time it when the network should be up, but not do something
543 # # dumb like delay startup until the network is up. It happens
544 # # at some time after network.target
545 # WantedBy=multi-user.target
547 # sudo systemctl daemon-reload # needed if the file was already there
548 # sudo systemctl enable imount.service
549 # sudo systemctl start imount.service
554 # a nocow dir that is common to multiple distros installed on the same system
557 if ! mountpoint
$dir; then
558 subvol
=/mnt
/root
/nocow
559 if [[ ! -e $subvol ]]; then
560 sudo btrfs subvolume create
$subvol
561 sudo chown root
:1000 $subvol
562 sudo
chattr +C
$subvol
565 first_root_crypt
=$
(awk '$2 == "/" {print $1}' /etc
/mtab
)
566 sudo
/a
/exe
/teeu
/etc
/fstab
<<EOF
567 $first_root_crypt /nocow btrfs noatime,subvol=nocow$( (( $(nproc) > 2)) && echo ,compress=zstd ) 0 0
570 sudo chown
$USER:$USER $dir
579 sudo
/a
/exe
/teeu
/etc
/fstab
<<'EOF'
580 /dev/mapper/crypt_dev_ata-Samsung_SSD_870_QVO_8TB_S5VUNG0N900656V-part7 /d btrfs nofail,x-systemd.device-timeout=30s,x-systemd.mount-timeout=30s,noatime,compress=zstd,subvol=d 0 0
581 /d/m /i none bind,compress=zstd 0 0
583 if ! mountpoint
/d
&>/dev
/null
; then
585 if [[ -d /mnt
/r
7/d
]]; then
589 if ! mountpoint
/i
&>/dev
/null
; then
595 sudo
/a
/exe
/teeu
/etc
/fstab
<<'EOF'
596 /dev/mapper/crypt_dev_ata-ata-Hitachi_HDS722020ALA330_JK1121YAG7SXWS-part1 /i btrfs nofail,x-systemd.device-timeout=30s,x-systemd.mount-timeout=30s,noatime,subvol=i 0 0
598 if ! mountpoint
/i
&>/dev
/null
; then
600 if [[ -d /mnt
/i
/i
]]; then
608 sudo systemctl disable systemd-networkd
616 # disable crash report annoying dialogs.
617 sudo
dd of
=/etc
/default
/apport
<<<'enabled=0'
620 ##### install laptop hardware packages
621 if tp || x2 || x3 || bo || sy; then
627 # the exact packages that task-laptop would install, since ubuntu
628 # doesn\'t have this virtual in practice package.
629 pi avahi-autoipd bluetooth powertop iw wireless-tools wpasupplicant
631 # todo: other distros unknown
637 # sway not packaged for t9, not bothering to build it yet since
638 # i3 doesnt seem to tear and stutter on video anymore.
639 if [[ $codename == buster ]]; then
648 case $(distro-name) in
649 trisquel|ubuntu|debian)
654 # recommends gets us geoclue (for darkening automatically at night i assume),
655 # which recommends modemmanager, which is annoying to fix for the model01 keyboard.
656 # commented because I dont use it much, and in nabia its named changed to redshift-gtk
657 #pi --no-install-recommends gtk-redshift
659 ##### setup X autostart
660 # install for multiple display managers in case we use one
661 dir=/etc/X11/xinit/xinitrc.d/
663 sudo cp /a/bin/distro-setup/desktop-20-autostart.sh $dir
665 ## disabled since i'm not using gdm atm
667 # sudo mkdir -p $dir/PostLogin
668 # sudo cp /a/bin/distro-setup/desktop-20-autostart.sh $dir/PostLogin/Default
669 sudo mkdir -p /etc/lightdm/lightdm.conf.d
670 # etiona lightdm.log:
671 # [SeatDefaults] is now called [Seat:*], please update this configuration
672 sudo dd of=/etc/lightdm/lightdm.conf.d/12-iank.conf <<'EOF'
674 # display-setup-script=/a/bin/ds/lightdm-start
675 session-setup-script=/a/bin/distro-setup/desktop-20-autostart.sh
679 # originally used xkbcomp, documented in input-setup.sh, this doesnt
680 # work under wayland, but its still useful for creating the config,
681 # then modifying the system files.
682 sudo sed -i.orig '/key *<KPMU> *{/,/}/s/KP_Multiply/underscore/g' /usr/share/X11/xkb/symbols/keypad
684 ##### basic graphical packages
685 pi konsole suckless-tools ssh-askpass
693 # emacs git build was broken last time i checked,
697 makepkg -si --noconfirm
700 pi hunspell hunspell-en
703 /a/bin/buildscripts/emacs
704 /a/bin/buildscripts/mu4e
706 /a/bin/buildscripts/emacs --no-r
707 /a/bin/buildscripts/mu4e --no-r
710 # the first pup command can kill off our /etc/ mod, so rerun this
711 /a/exe/ssh-emacs-setup
714 if [[ $HOSTNAME == kd ]] && ! mountpoint /d &>/dev/null; then
716 # if this is a fresh reinstall, need to run something like this
719 btrbk archive /mnt/rust1/btrbk /mnt/r7/btrbk
720 btrfs sub snap /mnt/r7/btrbk/LATEST /mnt/r7/d
726 echo 0 >~/.local/distro-begin
727 echo "$0: $(date): ending now"
728 echo "exiting with status 0"