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
20 # run any sudo command first so your pass is cached
21 # set the scrollback to unlimited in case something goes wrong
24 ####### begin setup environment #######
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'
31 ##### setup error handling
32 interactive
=true
# set this to false to force set -x
33 [[ $
- == *i
* ]] || interactive
=false
34 if ! $interactive; then
37 source /a
/bin
/errhandle
/err
40 exec &> >(sudo
tee -a /var
/log
/distro-begin
)
41 echo "$0: $(date): starting now)"
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."
50 echo "$0: warning: running as root. I will setup users then exit"
57 while [[ $1 == -* ]]; do
59 -r) recompile
=true
; shift ;;
67 ##### variables/env setup
68 script_dir
="$(readlink -f "${BASH_SOURCE[@]}")"; script_dir
=${script_dir%/*}
69 # shellcheck source=./pkgs
70 source $script_dir/pkgs
72 source /a
/bin
/distro-functions
/src
/identify-distros
73 $interactive ||
set -x
74 for f
in iank-dev htpc kd x2 x3 frodo tp li lj demohost kw fz
; do
75 eval "$f() { [[ $HOSTNAME == $f ]]; }"
77 codename
=$
(debian-codename
)
78 has_wayland
() { [[ $codename == buster
]]; }
79 has_x
() { [[ $codename != buster
]]; }
80 has_monitor
() { ! linode
; }
81 linode
() { lj || li
; }
82 has_btrfs
() { ! linode
; }
83 home_network
() { ! linode
; }
84 encrypted
() { has_p
; }
86 export GLOBIGNORE
="*/.:*/.."
89 sed="sed --follow-symlinks"
91 ####### end setup environment #######
96 ##### begin setup encryption scripts ######
98 # I tried making a service which was dependent on reboot.target,
99 # but it happened too late in the shutdown process.
100 sudo
dd of
=/etc
/systemd
/system
/keyscripton.service
<<'EOF'
102 Description=Turn on automatic decryption of drives on boot
103 # tried using graphical.target, but it made my display manager restart before rebooting.
104 # generally, I don't think targets order shutdown like they do startup.
105 # So, I did systemd-analyze plot > something.svg, and picked a reliably started
106 # service that happens late in the game.
108 DefaultDependencies=no
109 # not sure if needed, makes sure we shut down before reboot.target
110 Conflicts=reboot.target
116 ExecStop=/a/exe/keyscript-on
119 WantedBy=keyscriptoff.service
121 sudo systemctl daemon-reload
# needed if the file was already there
122 sudo systemctl stop keyscripton.service
123 # sudo systemctl start keyscripton.service
124 sudo systemctl
enable keyscripton.service
126 sudo
dd of
=/etc
/systemd
/system
/keyscriptoff.service
<<'EOF'
128 Description=Turn off automatic decryption of drives on boot
132 ExecStart=/a/exe/keyscript-off
135 WantedBy=multi-user.target
137 sudo systemctl daemon-reload
# needed if the file was already there
138 sudo systemctl
enable keyscriptoff.service
139 sudo systemctl start keyscriptoff.service
141 # from /usr/share/doc/dropbear-initramfs/README.initramfs.gz
142 while read -r m _
; do /sbin
/modinfo
-F filename
"$m"; done </proc
/modules | \
143 sed -nr "s@^/lib/modules/$(uname -r)/kernel/drivers/net(/.*)?/([^/]+)\.ko\$@\2@p" \
144 | sudo
dd of
=/etc
/initramfs-tools
/modules
145 sudo apt-get
-y install initramfs-tools-core
146 # initram auth keys get setup with rootsshsync
147 # then for remote unlock, ssh and do this once per crypt disk:
148 # echo -n PASS >/lib/cryptsetup/passfifo
150 ##### end setup encryption scripts ######
153 # disabled until its fixed up
156 # todo, it would be nice to cut down on some of the output
158 ##### fedora prereq/fundamental settings
160 # comment out line disallowing calling sudo in scripts
161 sudo
$sed -i 's/^Defaults *requiretty/#\0 # ian commented/' /etc
/sudoers
162 # turn on magic sysrq commands
163 echo 1 > sudo
dd of
=/proc
/sys
/kernel
/sysrq
164 echo "kernel.sysrq = 1" > /etc
/sysctl.d
/90-sysrq.conf
165 # selinux is not user friendly. Like, you enable samba, but you haven't run the magic selinux commands so it doesn't work
166 # and you have no idea why.
167 sudo
$sed -i 's/^\(SELINUX=\).*/\1disabled/' /etc
/selinux
/config
168 selinuxenabled
&& sudo setenforce
0
172 #### rerun my fai-time scripts
173 # already ran for pxe installs, but used for vps & updates
174 distro
=$
(distro-name
)
176 ubuntu|debian|trisquel
)
177 sudo bash
-c ". /a/bin/fai/fai-wrapper && /a/bin/fai/fai/config/scripts/GRUB_PC/11-iank"
180 sudo bash
-c ". /a/bin/fai/fai-wrapper &&
181 /a/bin/fai/fai/config/distro-install-common/end"
185 ###### setup hostname
186 sudo
$sed -i '/^127\.0\.1\.1/d' /etc
/hosts
187 echo "127.0.1.1 $HOSTNAME.b8.nz $HOSTNAME" | sudo
tee -a /etc
/hosts
190 ##### exit first stage if running as root
191 if [[ $EUID == 0 ]]; then
192 echo "$0: running as root. exiting now that users are setup"
197 #### setup bash for root
198 for x
in /a
/c
/{.bashrc
,brc
,.bash_profile
,.profile
,.inputrc
,path_add_function
}; do
206 # li needs the bind group before conflink
207 if [[ $HOSTNAME == li
]]; then
208 getent group
bind &>/dev
/null || sudo groupadd
-r bind
210 # this needs to be before installing pacserve so we have gpg conf.
213 ###### bash environment setup
216 source /etc
/profile.d
/environment.sh
217 # shellcheck source=./.bashrc
220 $interactive ||
set -x
223 #### setup passwordless sudo
224 tu
/etc
/sudoers
<<EOF
225 $USER ALL=(ALL) NOPASSWD: ALL
226 Defaults env_keep += SUDOD
227 # makes ubuntu be like debian
228 # https://unix.stackexchange.com/a/91572
229 Defaults always_set_home
230 # default setting is to have minimum umask of 0022
231 # This lets us have user-specific umasks which are more permissive.
232 # I did this for transmission and set it's umask gecos on install,
233 # see there for more info.
238 #### setup firefox backport
239 ## ian: disabled. backports are not being published atm due to rust packaging issue
241 # codename=$(debian-codename)
242 # if isdebian-stable && has_x; then
243 # s dd of=/etc/apt/sources.list.d/mozilla-iceweasel.list <<EOF
244 # deb http://mozilla.debian.net/ $codename-backports firefox-release
245 # deb-src http://mozilla.debian.net/ $codename-backports firefox-release
248 # # take care of mozilla signing errors in previous command
249 # pi pkg-mozilla-archive-keyring
255 ###### arch aur wrapper setup
257 #https://wiki.archlinux.org/index.php/Arch_User_Repository#Installing_packages
258 sudo pacman
-S --noconfirm --needed base-devel jq
259 # pacaur seems to be the best, although it + cower has a few minor bugs,
260 # its design goals seem good, so, going for it.
267 makepkg
-sri --skippgpcheck --noconfirm
276 x
=$
(mktemp
); /usr
/bin
/pacman.conf-insert_pacserve
>$x
277 sudo
dd of
=/etc
/pacman.conf
if=$x; rm $x
278 sudo systemctl
enable pacserve.service
279 sudo systemctl start pacserve.service
283 #### update all packages
287 ###### p1 packages install ######
289 # requirement for trash-cli.
290 # background: strange error if just installing trash-cli: "pyalpm requires python",
291 # so I see that it requires python2, and installing that manually fixes it.
292 # I didn\'t see this on earlier installation, main thing which changed was
293 # pacserve, so not sure if it\'s related.
299 ######## fix evbug bug ######
303 #https://bugs.launchpad.net/ubuntu/+source/module-init-tools/+bug/240553
304 #https://wiki.debian.org/KernelModuleBlacklisting
305 #common advice when searching is to use /etc/modprobe.d/blacklist.conf,
306 #but that file won't work and will get automatically reverted
307 sudo rmmod evbug ||
: # might not be loaded yet
308 file=/etc
/modprobe.d
/evbug.conf
309 line
="blacklist evbug"
310 if ! grep -xFq "$line" $file; then
311 sudo
dd of
=$file 2>/dev
/null
<<<"$line"
313 sudo update-initramfs
-u
320 # convenient to just do all file linking in one place
324 lnf
-T /p
/News ~
/News
326 s lnf
/q
/root
/.editor-backups
/q
/root
/.undo-tree-history \
327 /a
/opt
/a
/c
/.emacs.d
$HOME/mw_vars
/k
/backup
/root
328 pi rsync
# needed for rootsshsync
329 /a
/bin
/ds
/install-my-scripts
# needed for rootsshsync
331 s lnf
/a
/c
/.vim
/a
/c
/.vimrc
/a
/c
/.gvimrc
/root
334 lnf
-T /i
/k
/mboxes ~
/mail
341 case $
(distro-name
) in
342 trisquel|ubuntu|debian
)
353 #### install redshift
354 case $
(distro-name
) in
355 trisquel|ubuntu|debian
)
356 # recommends gets us geoclue (for darkening automatically at night i assume),
357 # which recommends modemmanager, which is annoying to fix for the model01 keyboard.
358 pi
--no-install-recommends gtk-redshift
371 # originally used xkbcomp, documented in input-setup.sh, this doesnt
372 # work under wayland, but its still useful for creating the config,
373 # then modifying the system files.
374 s
sed -i.orig
'/key *<KPMU> *{/,/}/s/KP_Multiply/underscore/g' /usr
/share
/X11
/xkb
/symbols
/keypad
379 #### arch specific early packages
380 case $
(distro-name
) in
382 # pkgfile is like apt-cache
388 #### fedora specific packages
389 case $
(distro-name
) in
391 # todo, this could probably come later
392 p
-y groupinstall development-tools c-development books admin-tools
395 # other distros unknown
399 # enable trim for volume delete, other rare commands
400 sudo
$sed -ri 's/( *issue_discards\b).*/\1 = 1/' /etc
/lvm
/lvm.conf
402 # flidas or so, these units arent built-in
403 if isdeb
&& ! systemctl list-unit-files |
grep -xF fstrim.timer
&>/dev
/null
; then
404 sudo
cp /usr
/share
/doc
/util-linux
/examples
/fstrim.
{service
,timer
} /etc
/systemd
/system
407 sudo systemctl
enable fstrim.timer
410 ##### make extra dirs
411 dirs=(/mnt
/{1,2,3,4,5,6,7,8,9} /nocow
/t
)
412 s mkdir
-p "${dirs[@]}"
413 s chown
$USER:$USER "${dirs[@]}"
416 tu
/etc
/fstab
<<'EOF'
417 /i/w /w none bind,noauto 0 0
418 /i/k /k none bind,noauto 0 0
420 if ! mountpoint
/kr
; then
422 s chown
$USER:user2
/kr
424 if home_network
; then
425 if [[ $HOSTNAME == frodo
]]; then
426 tu
/etc
/fstab
<<'EOF'
427 /k /kr none bind,noauto 0 0
430 tu
/etc
/fstab
<<'EOF'
431 frodo:/k /kr nfs noauto 0 0
435 s mkdir
-p /q
/i
/{w
,k
}
436 for dir
in /{i
,w
,k
}; do
437 if mountpoint
$dir; then continue; fi # already mounted
439 s chown
$USER:$USER $dir
441 # not needed for all hosts, but rather just keep it uniform
442 s mkdir
-p /mnt
/iroot
443 # debian auto mounting of multi-disk encrypted btrfs is busted. It is
444 # in jessie, and in stretch as of 11/26/2016 I have 4 disks in cryptab,
445 # based on 3 of those, it creates .device units for /dev/mapper/dev...
446 # then waits endlessly for them on bootup, after the /dev/mapper disks
447 # have already been created and exist. todo: create a simple repro
448 # for this in a vm and report it upstream.
449 if has_btrfs || home_network
; then
451 s
dd of
=/root
/imount
<<'EOF'
453 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
455 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
456 for dir in /i /mnt/iroot /k /kr /w; do
457 if ! mountpoint $dir &>/dev/null && \
458 awk '{print $2}' /etc/fstab | grep -xF $dir &>/dev/null; then
459 if awk '{print $3}' /etc/fstab | grep -xF nfs &>/dev/null; then
460 mount $dir || echo "warning: failed to mount nfs on $dir"
467 s
chmod +x
/root
/imount
469 s
dd of
=/etc
/systemd
/system
/imount.service
<<EOF
471 Description=Mount /i and related mountpoints
472 Before=syncthing@$USER.service
476 ExecStart=/root/imount
479 RequiredBy=syncthing@$USER.service
480 # note /kr needs networking, this target is the simplest way to
481 # time it when the network should be up, but not do something
482 # dumb like delay startup until the network is up. It happens
483 # at some time after network.target
484 WantedBy=multi-user.target
486 sudo systemctl daemon-reload
# needed if the file was already there
487 sudo systemctl
enable imount.service
488 sudo systemctl start imount.service
492 # a nocow dir that is common to multiple distros installed on the same system
495 if ! mountpoint
$dir; then
496 subvol
=/mnt
/root
/nocow
497 if [[ ! -e $subvol ]]; then
498 s btrfs subvolume create
$subvol
499 s chown root
:1000 $subvol
503 first_root_crypt
=$
(awk '$2 == "/" {print $1}' /etc
/mtab
)
505 $first_root_crypt /nocow btrfs noatime,subvol=nocow 0 0
508 s chown
$USER:$USER $dir
521 # disable crash report annoying dialogs.
522 s
dd of
=/etc
/default
/apport
<<<'enabled=0'
528 # emacs git build was broken last time i checked,
532 makepkg -si --noconfirm
535 pi hunspell hunspell-en
538 /a/bin/buildscripts/emacs
540 /a/bin/buildscripts/emacs --no-r || /a/bin/buildscripts/emacs
544 ##### install laptop hardware packages
545 if tp || x2 || x3; then
551 # the exact packages that task-laptop would install, since ubuntu
552 # doesn\'t have this virtual in practice package.
553 pi avahi-autoipd bluetooth powertop iw wireless-tools wpasupplicant
555 # todo: other distros unknown
560 ##### install x stuff
566 # xorg-xmessage for displaying error messages.
567 # optional dependency in arch, standard elsewhere.
568 pi xorg-server xorg-xmessage xorg-xsetroot xorg-xinit
574 ##### setup X autostart
577 # https://wiki.archlinux.org/index.php/Xinitrc
578 for homedir in /home/*; do
579 cp /etc/X11/xinit/xinitrc $homedir/.xinitrc
580 # shellcheck disable=SC2016
581 $sed -ri '/^ *twm\b/,$d' $homedir/.xinitrc
582 tee -a $homedir/.xinitrc <<'EOF'
583 /a/bin/desktop-20-autostart.sh
584 xsetroot -cursor_name left_ptr
589 # todo, figure this out for arch if we ever try out gnome.
590 # install for multiple display managers in case we use one
594 # fedora didn\'t have the 3.
597 s mkdir -p $dir/PostLogin
598 s command cp /a/bin/distro-setup/desktop-20-autostart.sh $dir/PostLogin/Default
599 s mkdir /etc/lightdm/lightdm.conf.d
600 s dd of=/etc/lightdm/lightdm.conf.d/12-iank.conf <<'EOF'
602 session-setup-script=/a/bin/distro-setup/desktop-20-autostart.sh
608 #### refix interactive ssh terminal
609 # the first pup command can kill off our /etc/ mod, so rerun this
610 /a/exe/ssh-emacs-setup
613 echo "$0: $(date): ending now"