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 ####### begin setup environment #######
28 ### make ssh interactive shell run better. for when running line interactively line by line
29 sudo bash
-c 'source /a/c/.bashrc && source /a/exe/ssh-emacs-setup'
32 ##### setup error handling
33 interactive
=true
# set this to false to force set -x
34 [[ $
- == *i
* ]] || interactive
=false
35 if ! $interactive; then
38 source /a
/bin
/errhandle
/err
39 source /a
/bin
/distro-functions
/src
/package-manager-abstractions
42 exec &> >(sudo
tee -a /var
/log
/distro-begin
)
43 echo "$0: $(date): starting now)"
47 if [[ $EUID == 0 ]]; then
48 if getent passwd iank || getent passwd ian
; then
49 echo "$0: error: running as root. unprivileged user exists. use it."
52 echo "$0: warning: running as root. I will setup users then exit"
60 while [[ $1 == -* ]]; do
62 -r) recompile
=true
; shift ;;
63 -e) emacs
=false
; shift ;;
71 ##### variables/env setup
72 script_dir
="$(readlink -f "${BASH_SOURCE[@]}")"; script_dir
=${script_dir%/*}
73 # shellcheck source=./pkgs
74 source $script_dir/pkgs
76 source /a
/bin
/distro-functions
/src
/identify-distros
77 $interactive ||
set -x
78 for f
in kd x2 x3 frodo tp li lj demohost kw
; do
79 eval "$f() { [[ $HOSTNAME == $f ]]; }"
81 codename
=$
(debian-codename
)
82 has_wayland
() { has_monitor
&& [[ $codename == buster
]]; }
83 has_x
() { has_monitor
&& [[ $codename != buster
]]; }
84 has_monitor
() { ! linode
; }
85 linode
() { lj || li
; }
86 # linode actually has btrfs now, but we dont do anything with it.
87 has_btrfs
() { ! linode
; }
88 home_network
() { ! linode
&& ! kw
; }
92 export GLOBIGNORE
="*/.:*/.."
95 sed="sed --follow-symlinks"
97 ####### end setup environment #######
102 ##### begin setup encryption scripts ######
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'
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.
114 DefaultDependencies=no
115 # not sure if needed, makes sure we shut down before reboot.target
116 Conflicts=reboot.target
122 ExecStop=/a/exe/keyscript-on
125 WantedBy=keyscriptoff.service
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
132 sudo
dd of
=/etc
/systemd
/system
/keyscriptoff.service
<<'EOF'
134 Description=Turn off automatic decryption of drives on boot
138 ExecStart=/a/exe/keyscript-off
141 WantedBy=multi-user.target
143 sudo systemctl daemon-reload
# needed if the file was already there
144 sudo systemctl
enable keyscriptoff.service
145 sudo systemctl start keyscriptoff.service
149 # from /usr/share/doc/dropbear-initramfs/README.initramfs.gz
151 while read -r m _
; do /sbin
/modinfo
-F filename
"$m"; done </proc
/modules | \
152 sed -nr "s@^/lib/modules/$(uname -r)/kernel/drivers/net(/.*)?/([^/]+)\.ko\$@\2@p" \
154 if ! diff -q /etc
/initramfs-tools
/modules
$tmp &>/dev
/null
; then
155 sudo
dd if=$tmp of
=/etc
/initramfs-tools
/modules
156 sudo
/usr
/sbin update-initramfs
-u -k all
158 # initram auth keys get setup with rootsshsync
159 $script_dir/rootsshsync
160 # then for remote unlock, ssh and do this once per crypt disk:
161 # echo -n PASS >/lib/cryptsetup/passfifo
166 ##### end setup encryption scripts ######
169 # disabled until its fixed up
172 # todo, it would be nice to cut down on some of the output
175 #### rerun my fai-time scripts
176 # already ran for pxe installs, but used for vps & updates
177 distro
=$
(distro-name
)
179 ubuntu|debian|trisquel
)
180 sudo bash
-c ". /a/bin/fai/fai-wrapper && /a/bin/fai/fai/config/scripts/GRUB_PC/11-iank"
183 sudo bash
-c ". /a/bin/fai/fai-wrapper &&
184 /a/bin/fai/fai/config/distro-install-common/end"
188 ###### setup hostname
189 if [[ $HOSTNAME != $
(cat /etc
/hostname
) ]]; then
190 echo $HOSTNAME > /etc
/hostname
191 hostname
-F /etc
/hostname
193 sudo
$sed -i '/^127\.0\.1\.1/d' /etc
/hosts
194 echo "127.0.1.1 $HOSTNAME.b8.nz $HOSTNAME" | sudo
tee -a /etc
/hosts
197 ##### exit first stage if running as root
198 if [[ $EUID == 0 ]]; then
199 echo "$0: running as root. exiting now that users are setup"
204 #### setup bash for root
205 for x
in /a
/c
/{.bashrc
,brc
,.bash_profile
,.profile
,.inputrc
,path_add_function
}; do
213 # li needs the bind group before conflink
214 if [[ $HOSTNAME == li
]]; then
215 getent group
bind &>/dev
/null || sudo groupadd
-r bind
217 # this needs to be before installing pacserve so we have gpg conf.
220 ###### bash environment setup
223 source /etc
/profile.d
/environment.sh
224 # shellcheck source=./.bashrc
227 $interactive ||
set -x
230 #### setup passwordless sudo
233 # makes ubuntu be like debian
234 # https://unix.stackexchange.com/a/91572
236 # umask: default setting is to have minimum umask of 0022
237 # This lets us have user-specific umasks which are more permissive.
238 # I did this for transmission and set it's umask gecos on install,
239 # see there for more info.
241 tu
/etc
/sudoers
<<EOF
242 $USER ALL=(ALL) NOPASSWD: ALL
243 Defaults env_keep += SUDOD
244 Defaults always_set_home
249 #### setup firefox backport
250 ## ian: disabled. backports are not being published atm due to rust packaging issue
252 # codename=$(debian-codename)
253 # if isdebian-stable && has_x; then
254 # s dd of=/etc/apt/sources.list.d/mozilla-iceweasel.list <<EOF
255 # deb http://mozilla.debian.net/ $codename-backports firefox-release
256 # deb-src http://mozilla.debian.net/ $codename-backports firefox-release
259 # # take care of mozilla signing errors in previous command
260 # pi pkg-mozilla-archive-keyring
266 ###### arch aur wrapper setup
268 #https://wiki.archlinux.org/index.php/Arch_User_Repository#Installing_packages
269 sudo pacman
-S --noconfirm --needed base-devel jq
270 # pacaur seems to be the best, although it + cower has a few minor bugs,
271 # its design goals seem good, so, going for it.
278 makepkg
-sri --skippgpcheck --noconfirm
287 x
=$
(mktemp
); /usr
/bin
/pacman.conf-insert_pacserve
>$x
288 sudo
dd of
=/etc
/pacman.conf
if=$x; rm $x
289 sudo systemctl
enable pacserve.service
290 sudo systemctl start pacserve.service
294 #### update all packages
298 ###### p1 packages install ######
302 ######## fix evbug bug ######
306 #https://bugs.launchpad.net/ubuntu/+source/module-init-tools/+bug/240553
307 #https://wiki.debian.org/KernelModuleBlacklisting
308 #common advice when searching is to use /etc/modprobe.d/blacklist.conf,
309 #but that file won't work and will get automatically reverted
310 sudo rmmod evbug ||
: # might not be loaded yet
311 file=/etc
/modprobe.d
/evbug.conf
312 line
="blacklist evbug"
313 if ! grep -xFq "$line" $file; then
314 sudo
dd of
=$file 2>/dev
/null
<<<"$line"
316 sudo update-initramfs
-u
323 # convenient to just do all file linking in one place
324 s
/a
/exe
/lnf
-T /a
/bin
/b
325 s
/a
/exe
/lnf
-T /nocow
/t
/t
327 lnf
-T /p
/News ~
/News
329 s
/a
/exe
/lnf
/q
/root
/.editor-backups
/q
/root
/.undo-tree-history \
330 /a
/opt
/a
/c
/.emacs.d
$HOME/mw_vars
/k
/backup
/root
331 /a
/bin
/ds
/install-my-scripts
# needed for rootsshsync cronjob
332 s
/a
/exe
/lnf
/a
/c
/.vim
/a
/c
/.vimrc
/a
/c
/.gvimrc
/root
338 #### arch specific early packages
339 case $
(distro-name
) in
341 # pkgfile is like apt-cache
348 # enable trim for volume delete, other rare commands
349 sudo
$sed -ri 's/( *issue_discards\b).*/\1 = 1/' /etc
/lvm
/lvm.conf
351 # flidas or so, these units arent built-in
352 if isdeb
&& ! systemctl list-unit-files |
grep ^fstrim.timer
&>/dev
/null
; then
353 sudo
cp /usr
/share
/doc
/util-linux
/examples
/fstrim.
{service
,timer
} /etc
/systemd
/system
356 sudo systemctl
enable fstrim.timer
359 ##### make extra dirs
360 dirs=(/mnt
/{1,2,3,4,5,6,7,8,9} /nocow
/t
)
361 s mkdir
-p "${dirs[@]}"
362 s chown
$USER:$USER "${dirs[@]}"
365 if home_network
; then
366 tu
/etc
/fstab
<<'EOF'
367 /i/w /w none bind,noauto 0 0
368 /i/k /k none bind,noauto 0 0
370 if ! mountpoint
/kr
; then
372 s chown
$USER:user2
/kr
374 if [[ $HOSTNAME == frodo
]]; then
375 tu
/etc
/fstab
<<'EOF'
376 /k /kr none bind,noauto 0 0
379 tu
/etc
/fstab
<<'EOF'
380 frodo:/k /kr nfs noauto 0 0
383 s mkdir
-p /q
/i
/{w
,k
}
384 for dir
in /{i
,w
,k
}; do
385 if mountpoint
$dir; then continue; fi # already mounted
387 s chown
$USER:$USER $dir
389 # not needed for all hosts, but rather just keep it uniform
390 s mkdir
-p /mnt
/iroot
391 # debian auto mounting of multi-disk encrypted btrfs is busted. It is
392 # in jessie, and in stretch as of 11/26/2016 I have 4 disks in cryptab,
393 # based on 3 of those, it creates .device units for /dev/mapper/dev...
394 # then waits endlessly for them on bootup, after the /dev/mapper disks
395 # have already been created and exist. todo: create a simple repro
396 # for this in a vm and report it upstream.
398 s
dd of
=/root
/imount
<<'EOF'
400 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
402 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
403 for dir in /i /mnt/iroot /k /kr /w; do
404 if ! mountpoint $dir &>/dev/null && \
405 awk '{print $2}' /etc/fstab | grep -xF $dir &>/dev/null; then
406 if awk '{print $3}' /etc/fstab | grep -xF nfs &>/dev/null; then
407 mount $dir || echo "warning: failed to mount nfs on $dir"
414 s
chmod +x
/root
/imount
415 s
dd of
=/etc
/systemd
/system
/imount.service
<<EOF
417 Description=Mount /i and related mountpoints
418 Before=syncthing@$USER.service
422 ExecStart=/root/imount
425 RequiredBy=syncthing@$USER.service
426 # note /kr needs networking, this target is the simplest way to
427 # time it when the network should be up, but not do something
428 # dumb like delay startup until the network is up. It happens
429 # at some time after network.target
430 WantedBy=multi-user.target
432 sudo systemctl daemon-reload
# needed if the file was already there
433 sudo systemctl
enable imount.service
434 sudo systemctl start imount.service
439 # a nocow dir that is common to multiple distros installed on the same system
442 if ! mountpoint
$dir; then
443 subvol
=/mnt
/root
/nocow
444 if [[ ! -e $subvol ]]; then
445 s btrfs subvolume create
$subvol
446 s chown root
:1000 $subvol
450 first_root_crypt
=$
(awk '$2 == "/" {print $1}' /etc
/mtab
)
452 $first_root_crypt /nocow btrfs noatime,subvol=nocow 0 0
455 s chown
$USER:$USER $dir
468 # disable crash report annoying dialogs.
469 s
dd of
=/etc
/default
/apport
<<<'enabled=0'
474 ##### install laptop hardware packages
475 if tp || x2 || x3; then
481 # the exact packages that task-laptop would install, since ubuntu
482 # doesn\'t have this virtual in practice package.
483 pi avahi-autoipd bluetooth powertop iw wireless-tools wpasupplicant
485 # todo: other distros unknown
493 # xorg-xmessage for displaying error messages.
494 # optional dependency in arch, standard elsewhere.
495 pi xorg-server xorg-xmessage xorg-xsetroot xorg-xinit
499 case $(distro-name) in
500 trisquel|ubuntu|debian)
508 #### install redshift
509 case $(distro-name) in
510 trisquel|ubuntu|debian)
511 # recommends gets us geoclue (for darkening automatically at night i assume),
512 # which recommends modemmanager, which is annoying to fix for the model01 keyboard.
513 pi --no-install-recommends gtk-redshift
520 ##### setup X autostart
522 # https://wiki.archlinux.org/index.php/Xinitrc
523 for homedir in /home/*; do
524 cp /etc/X11/xinit/xinitrc $homedir/.xinitrc
525 # shellcheck disable=SC2016
526 $sed -ri '/^ *twm\b/,$d' $homedir/.xinitrc
527 tee -a $homedir/.xinitrc <<'EOF'
528 /a/bin/desktop-20-autostart.sh
529 xsetroot -cursor_name left_ptr
534 # todo, figure this out for arch if we ever try out gnome.
535 # install for multiple display managers in case we use one
537 s mkdir -p $dir/PostLogin
538 s command cp /a/bin/distro-setup/desktop-20-autostart.sh $dir/PostLogin/Default
539 s mkdir /etc/lightdm/lightdm.conf.d
540 s dd of=/etc/lightdm/lightdm.conf.d/12-iank.conf <<'EOF'
542 session-setup-script=/a/bin/distro-setup/desktop-20-autostart.sh
548 ### install and configure wayland
551 # originally used xkbcomp, documented in input-setup.sh, this doesnt
552 # work under wayland, but its still useful for creating the config,
553 # then modifying the system files.
554 s sed -i.orig '/key *<KPMU> *{/,/}/s/KP_Multiply/underscore/g' /usr/share/X11/xkb/symbols/keypad
557 ##### basic graphical packages
559 pi konsole suckless-tools
566 # emacs git build was broken last time i checked,
570 makepkg -si --noconfirm
573 pi hunspell hunspell-en
576 /a/bin/buildscripts/emacs
578 /a/bin/buildscripts/emacs --no-r
581 # the first pup command can kill off our /etc/ mod, so rerun this
582 /a/exe/ssh-emacs-setup
585 echo "$0: $(date): ending now"