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 source $script_dir/pkgs
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 ]]; }"
76 has_p
() { ! linode
; } # when tp is tracis, then not tp either
78 linode
() { lj || li
; }
79 has_btrfs
() { ! linode
; }
80 home_network
() { ! linode
; }
81 encrypted
() { has_p
; }
83 export GLOBIGNORE
=*/.
:*/..
86 sed="sed --follow-symlinks"
88 ####### end setup environment #######
93 ##### begin setup encryption scripts ######
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'
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.
105 DefaultDependencies=no
106 # not sure if needed, makes sure we shut down before reboot.target
107 Conflicts=reboot.target
113 ExecStop=/a/exe/keyscript-on
116 WantedBy=keyscriptoff.service
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
123 sudo
dd of
=/etc
/systemd
/system
/keyscriptoff.service
<<'EOF'
125 Description=Turn off automatic decryption of drives on boot
129 ExecStart=/a/exe/keyscript-off
132 WantedBy=multi-user.target
134 sudo systemctl daemon-reload
# needed if the file was already there
135 sudo systemctl
enable keyscriptoff.service
136 sudo systemctl start keyscriptoff.service
138 ##### end setup encryption scripts ######
141 # disabled until its fixed up
144 # todo, it would be nice to cut down on some of the output
146 ##### fedora prereq/fundamental settings
148 # comment out line disallowing calling sudo in scripts
149 sudo
$sed -i 's/^Defaults *requiretty/#\0 # ian commented/' /etc
/sudoers
150 # turn on magic sysrq commands
151 echo 1 > sudo
dd of
=/proc
/sys
/kernel
/sysrq
152 echo "kernel.sysrq = 1" > /etc
/sysctl.d
/90-sysrq.conf
153 # selinux is not user friendly. Like, you enable samba, but you haven't run the magic selinux commands so it doesn't work
154 # and you have no idea why.
155 sudo
$sed -i 's/^\(SELINUX=\).*/\1disabled/' /etc
/selinux
/config
156 selinuxenabled
&& sudo setenforce
0
160 #### rerun my fai-time scripts
161 # already ran for pxe installs, but used for vps & updates
162 distro
=$
(distro-name
)
164 ubuntu|debian|trisquel
)
165 sudo bash
-c ". /a/bin/fai/fai-wrapper && /a/bin/fai/fai/config/scripts/GRUB_PC/11-iank"
168 sudo bash
-c ". /a/bin/fai/fai-wrapper &&
169 /a/bin/fai/fai/config/distro-install-common/end"
173 ###### setup hostname
174 sudo
$sed -i '/^127\.0\.1\.1/d' /etc
/hosts
175 echo "127.0.1.1 $HOSTNAME.b8.nz $HOSTNAME" | sudo
tee -a /etc
/hosts
178 ##### exit first stage if running as root
179 if [[ $EUID == 0 ]]; then
180 echo "$0: running as root. exiting now that users are setup"
185 #### setup bash for root
186 for x
in /a
/c
/{.bashrc
,brc
,.bash_profile
,.profile
,.inputrc
,path_add_function
}; do
194 # li needs the bind group before conflink
195 if [[ $HOSTNAME == li
]]; then
196 getent group
bind &>/dev
/null || sudo groupadd
-r bind
198 # this needs to be before installing pacserve so we have gpg conf.
201 ###### bash environment setup
204 source /etc
/profile.d
/environment.sh
207 $interactive ||
set -x
210 #### setup passwordless sudo
211 tu
/etc
/sudoers
<<EOF
212 $USER ALL=(ALL) NOPASSWD: ALL
213 Defaults env_keep += SUDOD
214 # makes ubuntu be like debian
215 # https://unix.stackexchange.com/a/91572
216 Defaults always_set_home
217 # default setting is to have minimum umask of 0022
218 # This lets us have user-specific umasks which are more permissive.
219 # I did this for transmission and set it's umask gecos on install,
220 # see there for more info.
225 #### setup firefox backport
226 ## ian: disabled. backports are not being published atm due to rust packaging issue
228 # codename=$(debian-codename)
229 # if isdebian-stable && has_x; then
230 # s dd of=/etc/apt/sources.list.d/mozilla-iceweasel.list <<EOF
231 # deb http://mozilla.debian.net/ $codename-backports firefox-release
232 # deb-src http://mozilla.debian.net/ $codename-backports firefox-release
235 # # take care of mozilla signing errors in previous command
236 # pi pkg-mozilla-archive-keyring
242 ###### arch aur wrapper setup
244 #https://wiki.archlinux.org/index.php/Arch_User_Repository#Installing_packages
245 sudo pacman
-S --noconfirm --needed base-devel jq
246 # pacaur seems to be the best, although it + cower has a few minor bugs,
247 # its design goals seem good, so, going for it.
254 makepkg
-sri --skippgpcheck --noconfirm
263 x
=$
(mktemp
); /usr
/bin
/pacman.conf-insert_pacserve
>$x
264 sudo
dd of
=/etc
/pacman.conf
if=$x; rm $x
265 sudo systemctl
enable pacserve.service
266 sudo systemctl start pacserve.service
270 #### update all packages
274 ###### p1 packages install ######
276 # requirement for trash-cli.
277 # background: strange error if just installing trash-cli: "pyalpm requires python",
278 # so I see that it requires python2, and installing that manually fixes it.
279 # I didn\'t see this on earlier installation, main thing which changed was
280 # pacserve, so not sure if it\'s related.
286 ######## fix evbug bug ######
290 #https://bugs.launchpad.net/ubuntu/+source/module-init-tools/+bug/240553
291 #https://wiki.debian.org/KernelModuleBlacklisting
292 #common advice when searching is to use /etc/modprobe.d/blacklist.conf,
293 #but that file won't work and will get automatically reverted
294 sudo rmmod evbug ||
: # might not be loaded yet
295 file=/etc
/modprobe.d
/evbug.conf
296 line
="blacklist evbug"
297 if ! grep -xFq "$line" $file; then
298 sudo
dd of
=$file 2>/dev
/null
<<<"$line"
300 sudo update-initramfs
-u
307 # convenient to just do all file linking in one place
311 lnf
-T /p
/News ~
/News
313 s lnf
/q
/root
/.editor-backups
/q
/root
/.undo-tree-history \
314 /a
/opt
/a
/c
/.emacs.d
$HOME/mw_vars
/k
/backup
/root
315 pi rsync
# needed for rootsshsync
317 s lnf
/a
/c
/.vim
/a
/c
/.vimrc
/a
/c
/.gvimrc
/root
320 lnf
-T /i
/k
/mboxes ~
/mail
328 case $
(distro-name
) in
329 trisquel|ubuntu|debian
)
340 #### install redshift
341 case $
(distro-name
) in
342 trisquel|ubuntu|debian
)
343 # recommends gets us geoclue (for darkening automatically at night i assume),
344 # which recommends modemmanager, which is annoying to fix for the model01 keyboard.
345 pi
--no-install-recommends gtk-redshift
357 #### arch specific early packages
358 case $
(distro-name
) in
360 # pkgfile is like apt-cache
366 #### fedora specific packages
367 case $
(distro-name
) in
369 # todo, this could probably come later
370 p
-y groupinstall development-tools c-development books admin-tools
373 # other distros unknown
377 # enable trim for volume delete, other rare commands
378 sudo
$sed -ri 's/( *issue_discards\b).*/\1 = 1/' /etc
/lvm
/lvm.conf
381 sudo
cp /usr
/share
/doc
/util-linux
/examples
/fstrim.
{service
,timer
} /etc
/systemd
/system
384 sudo systemctl
enable fstrim.timer
387 ##### make extra dirs
388 dirs=(/mnt
/{1,2,3,4,5,6,7,8,9} /nocow
/t
)
389 s mkdir
-p "${dirs[@]}"
390 s chown
$USER:$USER "${dirs[@]}"
393 tu
/etc
/fstab
<<'EOF'
394 /i/w /w none bind,noauto 0 0
395 /i/k /k none bind,noauto 0 0
397 if ! mountpoint
/kr
; then
399 s chown
$USER:traci
/kr
401 if home_network
; then
402 if [[ $HOSTNAME == frodo
]]; then
403 tu
/etc
/fstab
<<'EOF'
404 /k /kr none bind,noauto 0 0
407 tu
/etc
/fstab
<<'EOF'
408 frodo:/k /kr nfs noauto 0 0
412 s mkdir
-p /q
/i
/{w
,k
}
413 for dir
in /{i
,w
,k
}; do
414 if mountpoint
$dir; then continue; fi # already mounted
416 s chown
$USER:$USER $dir
418 # not needed for all hosts, but rather just keep it uniform
419 s mkdir
-p /mnt
/iroot
420 # debian auto mounting of multi-disk encrypted btrfs is busted. It is
421 # in jessie, and in stretch as of 11/26/2016 I have 4 disks in cryptab,
422 # based on 3 of those, it creates .device units for /dev/mapper/dev...
423 # then waits endlessly for them on bootup, after the /dev/mapper disks
424 # have already been created and exist. todo: create a simple repro
425 # for this in a vm and report it upstream.
426 if has_btrfs || home_network
; then
428 s
dd of
=/root
/imount
<<'EOF'
430 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
432 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
433 for dir in /i /mnt/iroot /k /kr /w; do
434 if ! mountpoint $dir &>/dev/null && \
435 awk '{print $2}' /etc/fstab | grep -xF $dir &>/dev/null; then
436 if awk '{print $3}' /etc/fstab | grep -xF nfs &>/dev/null; then
437 mount $dir || echo "warning: failed to mount nfs on $dir"
444 s
chmod +x
/root
/imount
446 s
dd of
=/etc
/systemd
/system
/imount.service
<<EOF
448 Description=Mount /i and related mountpoints
449 Before=syncthing@$USER.service
453 ExecStart=/root/imount
456 RequiredBy=syncthing@$USER.service
457 # note /kr needs networking, this target is the simplest way to
458 # time it when the network should be up, but not do something
459 # dumb like delay startup until the network is up. It happens
460 # at some time after network.target
461 WantedBy=multi-user.target
463 sudo systemctl daemon-reload
# needed if the file was already there
464 sudo systemctl
enable imount.service
465 sudo systemctl start imount.service
469 # a nocow dir that is common to multiple distros installed on the same system
472 if ! mountpoint
$dir; then
473 subvol
=/mnt
/root
/nocow
474 if [[ ! -e $subvol ]]; then
475 s btrfs subvolume create
$subvol
476 s chown root
:1000 $subvol
480 first_root_crypt
=$
(awk '$2 == "/" {print $1}' /etc
/mtab
)
482 $first_root_crypt /nocow btrfs noatime,subvol=nocow 0 0
485 s chown
$USER:$USER $dir
493 ###### fix mouse on jessie
494 # it comes with stretch and arch, but not jessie.
495 # propogate /etc/udev/hwdb.d
496 if which systemd-hwdb
; then
497 s systemd-hwdb update
498 ser restart systemd-udev-trigger
506 # todo: probably broken
512 # disable crash report annoying dialogs.
513 s
dd of
=/etc
/default
/apport
<<<'enabled=0'
516 ###### setup time zone
517 # fai sets this an old way that doesn't work for stretch.
518 # no harm in setting it universally here.
519 # using debconf-set-selection, the area gets reset to ETC
520 # on my linode test machine after doing a dpkg-reconfigure, or a reinstall,
521 # so we are using expect :(
522 # I got a random error when running this, so I added a sleep
523 # rather than trying to write a whole detect and wait loop.
524 # E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
525 # E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
527 # todo: this is not idempotent, it fails when running twice, due to prepopulated values.
528 # check into unsetting them using debconf-set-selection.
529 s apt-get -y install --no-install-recommends expect
531 set force_conservative 0
532 spawn dpkg-reconfigure tzdata -freadline
533 expect -nocase timeout {exit 1} "Geographic area:"
535 expect -nocase timeout {exit 1} "Time zone:"
545 # emacs git build was broken last time i checked,
549 makepkg -si --noconfirm
552 pi hunspell hunspell-en
555 /a/bin/buildscripts/emacs
557 /a/bin/buildscripts/emacs --no-r || /a/bin/buildscripts/emacs
562 ##### install laptop hardware packages
563 if tp || x2 || x3; then
569 # the exact packages that task-laptop would install, since ubuntu
570 # doesn\'t have this virtual in practice package.
571 pi avahi-autoipd bluetooth powertop iw wireless-tools wpasupplicant
573 # todo: other distros unknown
581 # note: on older ubuntu I used cabal xmonad + xfce, using cabal versin
582 # see /w/archive/programming/xmonad-cabal.sh
584 # xorg-xmessage for displaying error messages.
585 # optional dependency in arch, standard elsewhere.
586 pi xorg-server xorg-xmessage xmonad-contrib xorg-xsetroot xorg-xinit
593 ##### setup X autostart
596 # https://wiki.archlinux.org/index.php/Xinitrc
597 for homedir in /home/*; do
598 cp /etc/X11/xinit/xinitrc $homedir/.xinitrc
599 $sed -ri '/^ *twm\b/,$d' $homedir/.xinitrc
600 tee -a $homedir/.xinitrc <<'EOF'
601 /a/bin/desktop-20-autostart.sh
602 xsetroot -cursor_name left_ptr
607 # todo, figure this out for arch if we ever try out gnome.
608 # install for multiple display managers in case we use one
612 # fedora didn\'t have the 3.
615 s mkdir -p $dir/PostLogin
616 s command cp /a/bin/distro-setup/desktop-20-autostart.sh $dir/PostLogin/Default
617 s mkdir /etc/lightdm/lightdm.conf.d
618 s dd of=/etc/lightdm/lightdm.conf.d/12-iank.conf <<'EOF'
620 session-setup-script=/a/bin/distro-setup/desktop-20-autostart.sh
626 #### refix interactive ssh terminal
627 # the first pup command can kill off our /etc/ mod, so rerun this
628 /a/exe/ssh-emacs-setup
631 echo "$0: $(date): ending now"