2 # Copyright (C) 2016 Ian Kelling
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 # for setting up a new machine
17 # usage: $0 [-r] [HOSTNAME]
18 # HOSTNAME changes the machine's hostname
21 # run any sudo command first so your pass is cached
22 # set the scrollback to unlimited in case something goes wrong
25 # send to registrar, glue records:
28 # ns1.iankelling.org 72.14.176.105
29 # ns1.iankelling.org 2600:3c00::f03c:91ff:fe6d:baf8
30 # ns2.iankelling.org 172.105.84.95
31 # ns2.iankelling.org 2a01:7e01::f03c:91ff:feb5:baec
35 # ns1.zroe.org 72.14.176.105
36 # ns1.zroe.org 2600:3c00::f03c:91ff:fe6d:baf8
37 # ns2.zroe.org 172.105.84.95
38 # ns2.zroe.org 2a01:7e01::f03c:91ff:feb5:baec
43 ####### begin setup environment #######
46 ### make ssh interactive shell run better. for when running line interactively line by line
47 sudo bash
-c '/a/exe/ssh-emacs-setup'
49 if [[ -s ~
/.bashrc
]];then . ~
/.bashrc
;fi
50 ##### setup error handling
51 interactive
=true
# set this to false to force set -x
52 [[ $
- == *i
* ]] || interactive
=false
53 if ! $interactive; then
56 source /a
/bin
/errhandle
/err
59 echo 1 >~
/.local
/distro-begin
62 source /a
/bin
/distro-functions
/src
/package-manager-abstractions
65 echo "$0: $(date): starting now)"
69 if [[ $EUID == 0 ]]; then
70 if getent passwd iank || getent passwd ian
; then
71 echo "$0: error: running as root. unprivileged user exists. use it."
74 echo "$0: warning: running as root. I will setup users then exit"
82 while [[ $1 == -* ]]; do
84 -r) recompile
=true
; shift ;;
85 -e) emacs
=false
; shift ;;
93 ##### variables/env setup
94 script_dir
="$(readlink -f "${BASH_SOURCE[@]}")"; script_dir
=${script_dir%/*}
95 # shellcheck source=./pkgs
96 source $script_dir/pkgs
98 source /a
/bin
/distro-functions
/src
/identify-distros
99 $interactive ||
set -x
100 for f
in kd x2 x3 frodo tp li l2 demohost kw
; do
101 eval "$f() { [[ $HOSTNAME == $f ]]; }"
103 codename
=$
(debian-codename
)
104 has_wayland
() { has_monitor
&& [[ $codename == buster
]]; }
105 has_x
() { has_monitor
&& [[ $codename != buster
]]; }
106 has_monitor
() { ! linode
; }
107 linode
() { l2 || li
; }
108 # linode actually has btrfs now, but we dont do anything with it.
109 has_btrfs
() { ! linode
; }
110 home_network
() { ! linode
&& ! kw
; }
111 has_p
() { ! linode
; }
112 encrypted
() { true
; }
114 export GLOBIGNORE
="*/.:*/.."
117 sed="sed --follow-symlinks"
119 ####### end setup environment #######
124 ##### begin setup encryption scripts ######
126 # I tried making a service which was dependent on reboot.target,
127 # but it happened too late in the shutdown process.
128 sudo
dd of
=/etc
/systemd
/system
/keyscripton.service
<<'EOF'
130 Description=Turn on automatic decryption of drives on boot
131 # This is triggered by reboot and when keyscriptoff stops.
133 # tried using graphical.target, but it made my display manager restart before rebooting.
134 # generally, I don't think targets order shutdown like they do startup.
135 # So, I did systemd-analyze plot > something.svg, and picked a reliably started
136 # service that happens late in the game.
138 DefaultDependencies=no
139 # not sure if needed, makes sure we shut down before reboot.target
140 Conflicts=reboot.target
146 ExecStop=/a/exe/keyscript-on
149 WantedBy=keyscriptoff.service
151 sudo systemctl daemon-reload
# needed if the file was already there
152 sudo systemctl
enable keyscripton.service
154 sudo
dd of
=/etc
/systemd
/system
/keyscriptoff.service
<<'EOF'
156 Description=Turn off automatic decryption of drives on boot
160 ExecStart=/a/exe/keyscript-off
163 WantedBy=multi-user.target
165 sudo systemctl daemon-reload
# needed if the file was already there
166 sudo systemctl
enable keyscriptoff.service
167 sudo systemctl start keyscriptoff.service
171 # from /usr/share/doc/dropbear-initramfs/README.initramfs.gz
173 while read -r m _
; do /sbin
/modinfo
-F filename
"$m"; done </proc
/modules | \
174 sed -nr "s@^/lib/modules/$(uname -r)/kernel/drivers/net(/.*)?/([^/]+)\.ko\$@\2@p" \
176 if ! diff -q /etc
/initramfs-tools
/modules
$tmp &>/dev
/null
; then
177 sudo
dd if=$tmp of
=/etc
/initramfs-tools
/modules
178 sudo
/usr
/sbin
/update-initramfs
-u -k all
180 # initram auth keys get setup with rootsshsync
181 $script_dir/rootsshsync
182 # then for remote unlock, ssh and do this once per crypt disk:
183 # echo -n PASS >/lib/cryptsetup/passfifo
188 ##### end setup encryption scripts ######
191 # disabled until its fixed up
194 # todo, it would be nice to cut down on some of the output
197 #### rerun my fai-time scripts
198 # already ran for pxe installs, but used for vps & updates
199 distro
=$
(distro-name
)
201 ubuntu|debian|trisquel
)
202 sudo bash
-c ". /a/bin/fai/fai-wrapper && /a/bin/fai/fai/config/scripts/GRUB_PC/11-iank"
205 sudo bash
-c ". /a/bin/fai/fai-wrapper &&
206 /a/bin/fai/fai/config/distro-install-common/end"
210 ###### setup hostname
211 if [[ $HOSTNAME != $
(cat /etc
/hostname
) ]]; then
212 echo $HOSTNAME > /etc
/hostname
213 hostname
-F /etc
/hostname
215 sudo
sed -i --follow-symlinks -f - /etc
/hosts
<<EOF
216 \$a 127.0.1.1 $HOSTNAME.b8.nz $HOSTNAME
221 ##### exit first stage if running as root
222 if [[ $EUID == 0 ]]; then
223 echo "$0: running as root. exiting now that users are setup"
228 #### setup bash for root
229 for x
in /a
/c
/{.bashrc
,brc
,brc2
,.bash_profile
,.profile
,.inputrc
,path_add_function
}; do
237 # linode needs bind group before conflink
241 # this needs to be before installing pacserve so we have gpg conf.
244 ###### bash environment setup
247 source /etc
/profile.d
/environment.sh
249 # shellcheck source=./.bashrc
252 $interactive ||
set -x
255 #### setup passwordless sudo
258 # makes ubuntu be like debian
259 # https://unix.stackexchange.com/a/91572
261 # umask: default setting is to have minimum umask of 0022
262 # This lets us have user-specific umasks which are more permissive.
263 # I did this for transmission and set it's umask gecos on install,
264 # see there for more info.
266 tu
/etc
/sudoers
<<EOF
267 $USER ALL=(ALL) NOPASSWD: ALL
268 Defaults env_keep += SUDOD
269 Defaults always_set_home
274 #### setup firefox backport
275 ## ian: disabled. backports are not being published atm due to rust packaging issue
277 # codename=$(debian-codename)
278 # if isdebian-stable && has_x; then
279 # s dd of=/etc/apt/sources.list.d/mozilla-iceweasel.list <<EOF
280 # deb http://mozilla.debian.net/ $codename-backports firefox-release
281 # deb-src http://mozilla.debian.net/ $codename-backports firefox-release
284 # # take care of mozilla signing errors in previous command
285 # pi pkg-mozilla-archive-keyring
291 ###### arch aur wrapper setup
293 #https://wiki.archlinux.org/index.php/Arch_User_Repository#Installing_packages
294 sudo pacman
-S --noconfirm --needed base-devel jq
295 # pacaur seems to be the best, although it + cower has a few minor bugs,
296 # its design goals seem good, so, going for it.
303 makepkg
-sri --skippgpcheck --noconfirm
312 x
=$
(mktemp
); /usr
/bin
/pacman.conf-insert_pacserve
>$x
313 sudo
dd of
=/etc
/pacman.conf
if=$x; rm $x
314 sudo systemctl
enable pacserve.service
315 sudo systemctl start pacserve.service
319 #### update all packages
323 ###### p1 packages install ######
327 ######## fix evbug bug ######
328 case $
(debian-codename-compat
) in
330 # noticed in flidas. dunno if it affects any others
331 #https://bugs.launchpad.net/ubuntu/+source/module-init-tools/+bug/240553
332 #https://wiki.debian.org/KernelModuleBlacklisting
333 #common advice when searching is to use /etc/modprobe.d/blacklist.conf,
334 #but that file won't work and will get automatically reverted
335 sudo rmmod evbug ||
: # might not be loaded yet
336 file=/etc
/modprobe.d
/evbug.conf
337 line
="blacklist evbug"
338 if [[ $
(cat $file) != $line ]]; then
339 sudo
dd of
=$file 2>/dev
/null
<<<"$line"
341 sudo update-initramfs
-u
348 # convenient to just do all file linking in one place
349 sudo
/a
/exe
/lnf
-T /a
/bin
/b
350 sudo
/a
/exe
/lnf
-T /nocow
/t
/t
352 lnf
-T /p
/News ~
/News
354 sudo
/a
/exe
/lnf
/q
/root
/.editor-backups
/q
/root
/.undo-tree-history \
355 /a
/opt
/a
/c
/.emacs.d
$HOME/mw_vars
/k
/backup
/root
356 /a
/bin
/ds
/install-my-scripts
# needed for rootsshsync cronjob
357 sudo
/a
/exe
/lnf
/a
/c
/.vim
/a
/c
/.vimrc
/a
/c
/.gvimrc
/root
363 #### arch specific early packages
364 case $
(distro-name
) in
366 # pkgfile is like apt-cache
368 sudo pkgfile
--update
373 # enable trim for volume delete, other rare commands
374 sudo
$sed -ri 's/( *issue_discards\b).*/\1 = 1/' /etc
/lvm
/lvm.conf
376 # flidas or so, these units arent built-in
377 if isdeb
&& ! systemctl list-unit-files |
grep ^fstrim.timer
&>/dev
/null
; then
378 sudo
cp /usr
/share
/doc
/util-linux
/examples
/fstrim.
{service
,timer
} /etc
/systemd
/system
381 sudo systemctl
enable fstrim.timer
384 ##### make extra dirs
385 dirs=(/mnt
/{1,2,3,4,5,6,7,8,9} /nocow
/t
)
386 sudo mkdir
-p "${dirs[@]}"
387 sudo chown
$USER:$USER "${dirs[@]}"
390 if home_network
; then
391 tu
/etc
/fstab
<<'EOF'
392 /i/w /w none bind,noauto 0 0
393 /i/k /k none bind,noauto 0 0
395 if ! mountpoint
/kr
; then
397 sudo chown
$USER:user2
/kr
399 if [[ $HOSTNAME == frodo
]]; then
400 tu
/etc
/fstab
<<'EOF'
401 /k /kr none bind,noauto 0 0
404 tu
/etc
/fstab
<<'EOF'
405 frodo:/k /kr nfs noauto 0 0
408 sudo mkdir
-p /q
/i
/{w
,k
}
409 for dir
in /{i
,w
,k
}; do
410 if mountpoint
$dir; then continue; fi # already mounted
412 sudo chown
$USER:$USER $dir
414 # not needed for all hosts, but rather just keep it uniform
415 sudo mkdir
-p /mnt
/iroot
416 # debian auto mounting of multi-disk encrypted btrfs is busted. It is
417 # in jessie, and in stretch as of 11/26/2016 I have 4 disks in cryptab,
418 # based on 3 of those, it creates .device units for /dev/mapper/dev...
419 # then waits endlessly for them on bootup, after the /dev/mapper disks
420 # have already been created and exist. todo: create a simple repro
421 # for this in a vm and report it upstream.
423 sudo
dd of
=/root
/imount
<<'EOF'
425 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
427 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
428 for dir in /i /mnt/iroot /k /kr /w; do
429 if ! mountpoint $dir &>/dev/null && \
430 awk '{print $2}' /etc/fstab | grep -xF $dir &>/dev/null; then
431 if awk '{print $3}' /etc/fstab | grep -xF nfs &>/dev/null; then
432 mount $dir || echo "warning: failed to mount nfs on $dir"
439 sudo
chmod +x
/root
/imount
440 sudo
dd of
=/etc
/systemd
/system
/imount.service
<<EOF
442 Description=Mount /i and related mountpoints
443 Before=syncthing@$USER.service
447 ExecStart=/root/imount
450 RequiredBy=syncthing@$USER.service
451 # note /kr needs networking, this target is the simplest way to
452 # time it when the network should be up, but not do something
453 # dumb like delay startup until the network is up. It happens
454 # at some time after network.target
455 WantedBy=multi-user.target
457 sudo systemctl daemon-reload
# needed if the file was already there
458 sudo systemctl
enable imount.service
459 sudo systemctl start imount.service
464 # a nocow dir that is common to multiple distros installed on the same system
467 if ! mountpoint
$dir; then
468 subvol
=/mnt
/root
/nocow
469 if [[ ! -e $subvol ]]; then
470 sudo btrfs subvolume create
$subvol
471 sudo chown root
:1000 $subvol
472 sudo
chattr +C
$subvol
475 first_root_crypt
=$
(awk '$2 == "/" {print $1}' /etc
/mtab
)
477 $first_root_crypt /nocow btrfs noatime,subvol=nocow 0 0
480 sudo chown
$USER:$USER $dir
493 # disable crash report annoying dialogs.
494 sudo
dd of
=/etc
/default
/apport
<<<'enabled=0'
499 ##### install laptop hardware packages
500 if tp || x2 || x3; then
506 # the exact packages that task-laptop would install, since ubuntu
507 # doesn\'t have this virtual in practice package.
508 pi avahi-autoipd bluetooth powertop iw wireless-tools wpasupplicant
510 # todo: other distros unknown
518 # xorg-xmessage for displaying error messages.
519 # optional dependency in arch, standard elsewhere.
520 pi xorg-server xorg-xmessage xorg-xsetroot xorg-xinit
524 case $(distro-name) in
525 trisquel|ubuntu|debian)
533 #### install redshift
534 case $(distro-name) in
535 trisquel|ubuntu|debian)
536 # recommends gets us geoclue (for darkening automatically at night i assume),
537 # which recommends modemmanager, which is annoying to fix for the model01 keyboard.
538 pi --no-install-recommends gtk-redshift
545 ##### setup X autostart
546 # todo, figure this out for arch if we ever try out gnome.
547 # install for multiple display managers in case we use one
549 sudo mkdir -p $dir/PostLogin
550 sudo cp /a/bin/distro-setup/desktop-20-autostart.sh $dir/PostLogin/Default
551 sudo mkdir -p /etc/lightdm/lightdm.conf.d
552 sudo dd of=/etc/lightdm/lightdm.conf.d/12-iank.conf <<'EOF'
554 session-setup-script=/a/bin/distro-setup/desktop-20-autostart.sh
558 ### install and configure wayland
561 # originally used xkbcomp, documented in input-setup.sh, this doesnt
562 # work under wayland, but its still useful for creating the config,
563 # then modifying the system files.
564 sudo sed -i.orig '/key *<KPMU> *{/,/}/s/KP_Multiply/underscore/g' /usr/share/X11/xkb/symbols/keypad
567 ##### basic graphical packages
569 pi konsole suckless-tools
576 # emacs git build was broken last time i checked,
580 makepkg -si --noconfirm
583 pi hunspell hunspell-en
586 /a/bin/buildscripts/emacs
588 /a/bin/buildscripts/emacs --no-r
591 # the first pup command can kill off our /etc/ mod, so rerun this
592 /a/exe/ssh-emacs-setup
596 echo 0 >~/.local/distro-begin
597 echo "$0: $(date): ending now"
598 echo "exiting with status 0"