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
39 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
40 for x
in /a
/bin
/errhandle
/*-function; do
44 $interactive || errcatch
47 exec &> >(sudo
tee -a /var
/log
/distro-begin
)
48 echo "$0: $(date): starting now)"
52 if [[ $EUID == 0 ]]; then
53 if getent passwd iank || getent passwd ian
; then
54 echo "$0: error: running as root. unprivileged user exists. use it."
57 echo "$0: warning: running as root. I will setup users then exit"
64 # for copying to a new data fs
65 bootstrapfs
=false
# old flag, needs new look before using.
66 while [[ $1 == -* ]]; do
68 -r) recompile
=true
; shift ;;
76 ##### variables/env setup
77 script_dir
="$(readlink -f "$BASH_SOURCE")"
78 source $script_dir/pkgs
80 source /a
/bin
/distro-functions
/src
/identify-distros
81 $interactive ||
set -x
82 for f
in iank-dev htpc treetowl x2 frodo tp li lj demohost kw fz
; do
83 eval "$f() { [[ $HOSTNAME == $f ]]; }"
85 has_p
() { ! linode
; } # when tp is tracis, then not tp either
87 linode
() { lj || li
; }
88 has_btrfs
() { ! linode
; }
89 home_network
() { ! linode
; }
90 encrypted
() { has_p
; }
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
147 ##### end setup encryption scripts ######
150 # disabled until its fixed up
153 # todo, it would be nice to cut down on some of the output
155 ##### fedora prereq/fundamental settings
157 # comment out line disallowing calling sudo in scripts
158 sudo
$sed -i 's/^Defaults *requiretty/#\0 # ian commented/' /etc
/sudoers
159 # turn on magic sysrq commands
160 echo 1 > sudo
dd of
=/proc
/sys
/kernel
/sysrq
161 echo "kernel.sysrq = 1" > /etc
/sysctl.d
/90-sysrq.conf
162 # selinux is not user friendly. Like, you enable samba, but you haven't run the magic selinux commands so it doesn't work
163 # and you have no idea why.
164 sudo
$sed -i 's/^\(SELINUX=\).*/\1disabled/' /etc
/selinux
/config
165 selinuxenabled
&& sudo setenforce
0
169 #### rerun my fai-time scripts
170 # already ran for pxe installs, but used for vps & updates
171 distro
=$
(distro-name
)
173 ubuntu|debian|trisquel
)
174 sudo bash
-c ". /a/bin/fai/fai-wrapper && /a/bin/fai/fai/config/scripts/GRUB_PC/11-iank"
177 sudo bash
-c ". /a/bin/fai/fai-wrapper &&
178 /a/bin/fai/fai/config/distro-install-common/end"
182 ###### setup hostname
183 sudo
$sed -i '/^127\.0\.1\.1/d' /etc
/hosts
184 echo "127.0.1.1 $HOSTNAME.b8.nz $HOSTNAME" | sudo
tee -a /etc
/hosts
187 ##### exit first stage if running as root
188 if [[ $EUID == 0 ]]; then
189 echo "$0: running as root. exiting now that users are setup"
194 #### setup bash for root
195 for x
in /a
/c
/{.bashrc
,brc
,.bash_profile
,.profile
,.inputrc
,path_add_function
}; do
203 # li needs the bind group before conflink
204 if [[ $HOSTNAME == li
]]; then
205 getent group
bind &>/dev
/null || sudo groupadd
-r bind
207 # this needs to be before installing pacserve so we have gpg conf.
210 ###### bash environment setup
213 source /etc
/profile.d
/environment.sh
215 $interactive || errcatch
216 $interactive ||
set -x
219 #### setup passwordless sudo
220 tu
/etc
/sudoers
<<EOF
221 $USER ALL=(ALL) NOPASSWD: ALL
222 Defaults env_keep += SUDOD
223 # makes ubuntu be like debian
224 # https://unix.stackexchange.com/a/91572
225 Defaults always_set_home
229 #### setup firefox backport
230 ## ian: disabled. backports are not being published atm due to rust packaging issue
232 # codename=$(debian-codename)
233 # if isdebian-stable && has_x; then
234 # s dd of=/etc/apt/sources.list.d/mozilla-iceweasel.list <<EOF
235 # deb http://mozilla.debian.net/ $codename-backports firefox-release
236 # deb-src http://mozilla.debian.net/ $codename-backports firefox-release
239 # # take care of mozilla signing errors in previous command
240 # pi pkg-mozilla-archive-keyring
246 ###### arch aur wrapper setup
248 #https://wiki.archlinux.org/index.php/Arch_User_Repository#Installing_packages
249 sudo pacman
-S --noconfirm --needed base-devel jq
250 # pacaur seems to be the best, although it + cower has a few minor bugs,
251 # its design goals seem good, so, going for it.
258 makepkg
-sri --skippgpcheck --noconfirm
267 x
=$
(mktemp
); /usr
/bin
/pacman.conf-insert_pacserve
>$x
268 sudo
dd of
=/etc
/pacman.conf
if=$x; rm $x
269 sudo systemctl
enable pacserve.service
270 sudo systemctl start pacserve.service
274 #### update all packages
278 ###### p1 packages install ######
280 # requirement for trash-cli.
281 # background: strange error if just installing trash-cli: "pyalpm requires python",
282 # so I see that it requires python2, and installing that manually fixes it.
283 # I didn\'t see this on earlier installation, main thing which changed was
284 # pacserve, so not sure if it\'s related.
290 ######## fix evbug bug ######
294 #https://bugs.launchpad.net/ubuntu/+source/module-init-tools/+bug/240553
295 #https://wiki.debian.org/KernelModuleBlacklisting
296 #common advice when searching is to use /etc/modprobe.d/blacklist.conf,
297 #but that file won't work and will get automatically reverted
298 sudo rmmod evbug ||
: # might not be loaded yet
299 file=/etc
/modprobe.d
/evbug.conf
300 line
="blacklist evbug"
301 if ! grep -xFq "$line" $file; then
302 sudo
dd of
=$file 2>/dev
/null
<<<"$line"
304 sudo update-initramfs
-u
311 # convenient to just do all file linking in one place
315 lnf
-T /p
/News ~
/News
317 s lnf
/q
/root
/.editor-backups
/q
/root
/.undo-tree-history \
318 /a
/opt
/a
/c
/.emacs.d
$HOME/mw_vars
/k
/backup
/root
320 s lnf
/a
/c
/.vim
/a
/c
/.vimrc
/a
/c
/.gvimrc
/root
323 lnf
-T /i
/k
/mboxes ~
/mail
326 ### begin setup for keyboard and redshift ###
327 case $
(distro-name
) in
328 trisquel|ubuntu|debian
)
331 # recommends gets us geoclue (for darkening automatically at night i assume),
332 # which recommends modemmanager, which is annoying to fix for the model01 keyboard.
333 pi
--no-install-recommends gtk-redshift
337 p
-y groupinstall development-tools c-development books admin-tools
348 pi xorg-server redshift xorg-xinput
352 ### end setup for keyboard and redshift ###
354 # enables trim for volume delete, other rare commands.
355 sudo
$sed -ri 's/( *issue_discards\b).*/\1 = 1/' /etc
/lvm
/lvm.conf
359 sudo
cp /usr
/share
/doc
/util-linux
/examples
/fstrim.
{service
,timer
} /etc
/systemd
/system
362 sudo systemctl
enable fstrim.timer
365 dirs=(/mnt
/{1,2,3,4,5,6,7,8,9} /nocow
/t
)
366 s mkdir
-p "${dirs[@]}"
367 s chown
$USER:$USER "${dirs[@]}"
370 tu
/etc
/fstab
<<'EOF'
371 /i/w /w none bind,noauto 0 0
372 /i/k /k none bind,noauto 0 0
376 if ! mountpoint
/kr
; then
378 s chown
$USER:traci
/kr
381 if home_network
; then
382 if [[ $HOSTNAME == frodo
]]; then
383 tu
/etc
/fstab
<<'EOF'
384 /k /kr none bind,noauto 0 0
387 tu
/etc
/fstab
<<'EOF'
388 frodo:/k /kr nfs noauto 0 0
393 s mkdir
-p /q
/i
/{w
,k
}
394 for dir
in /{i
,w
,k
}; do
395 if mountpoint
$dir; then continue; fi # already mounted
397 s chown
$USER:$USER $dir
400 # not needed for all hosts, but rather just keep it uniform
401 s mkdir
-p /mnt
/iroot
403 # debian auto mounting of multi-disk encrypted btrfs is busted. It is
404 # in jessie, and in stretch as of 11/26/2016 I have 4 disks in cryptab,
405 # based on 3 of those, it creates .device units for /dev/mapper/dev...
406 # then waits endlessly for them on bootup, after the /dev/mapper disks
407 # have already been created and exist. todo: create a simple repro
408 # for this in a vm and report it upstream.
409 if has_btrfs || home_network
; then
411 s
dd of
=/root
/imount
<<'EOF'
413 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
415 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
416 for dir in /i /mnt/iroot /k /kr /w; do
417 if ! mountpoint $dir &>/dev/null && \
418 awk '{print $2}' /etc/fstab | grep -xF $dir &>/dev/null; then
419 if awk '{print $3}' /etc/fstab | grep -xF nfs &>/dev/null; then
420 mount $dir || echo "warning: failed to mount nfs on $dir"
427 s
chmod +x
/root
/imount
429 s
dd of
=/etc
/systemd
/system
/imount.service
<<EOF
431 Description=Mount /i and related mountpoints
432 Before=syncthing@$USER.service
436 ExecStart=/root/imount
439 RequiredBy=syncthing@$USER.service
440 # note /kr needs networking, this target is the simplest way to
441 # time it when the network should be up, but not do something
442 # dumb like delay startup until the network is up. It happens
443 # at some time after network.target
444 WantedBy=multi-user.target
446 sudo systemctl daemon-reload
# needed if the file was already there
447 sudo systemctl
enable imount.service
448 sudo systemctl start imount.service
453 if ! mountpoint
$dir; then
454 subvol
=/mnt
/root
/nocow
455 if [[ ! -e $subvol ]]; then
456 s btrfs subvolume create
$subvol
457 s chown root
:1000 $subvol
461 first_root_crypt
=$
(awk '$2 == "/" {print $1}' /etc
/mtab
)
463 $first_root_crypt /nocow btrfs noatime,subvol=nocow 0 0
466 s chown
$USER:$USER $dir
473 # ssh and probably some other things care about parent directory
474 # ownership, and ssh doesn\'t allow any group writable parent
475 # directories, so we are forced to use a directory structure similar
476 # to home directories
477 s chown root
:$USER /q
481 # it comes with stretch and arch, but not jessie.
482 # propogate /etc/udev/hwdb.d
483 if which systemd-hwdb
; then
484 s systemd-hwdb update
485 ser restart systemd-udev-trigger
490 # I\'ve had problems with postfix on debian:
491 # on stretch, a startup ordering issue caused all mail to fail.
492 # postfix changed defaults to only use ipv6 dns, causing all my mail to fail.
493 # exim4 is default on debian, so I assume it would
494 # be packaged better to avoid these types of things.
495 # I haven\'t gotten around to getting a non-debian exim
503 # disable crash report annoying dialogs.
504 s
dd of
=/etc
/default
/apport
<<<'enabled=0'
507 # fai sets this an old way that doesn't work for stretch.
508 # no harm in setting it universally here.
509 # using debconf-set-selection, the area gets reset to ETC
510 # on my linode test machine after doing a dpkg-reconfigure, or a reinstall,
511 # so we are using expect :(
512 # I got a random error when running this, so I added a sleep
513 # rather than trying to write a whole detect and wait loop.
514 # E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
515 # E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?
517 # todo: this is not idempotent, it fails when running twice, due to prepopulated values.
518 # check into unsetting them using debconf-set-selection.
519 s apt-get -y install --no-install-recommends expect
521 set force_conservative 0
522 spawn dpkg-reconfigure tzdata -freadline
523 expect -nocase timeout {exit 1} "Geographic area:"
525 expect -nocase timeout {exit 1} "Time zone:"
534 # install so it's build dependencies don't get removed.
536 # emacs git build is currently broken
541 makepkg -si --noconfirm
547 pi hunspell hunspell-en
550 /a/bin/buildscripts/emacs
552 /a/bin/buildscripts/emacs --no-r || /a/bin/buildscripts/emacs
556 # todo, figure this out for arch if we ever try out gnome.
558 # install for multiple display managers in case we use one
562 # fedora didn\'t have the 3.
565 s mkdir -p $dir/PostLogin
566 s command cp /a/bin/distro-setup/desktop-20-autostart.sh $dir/PostLogin/Default
567 s mkdir /etc/lightdm/lightdm.conf.d
568 s dd of=/etc/lightdm/lightdm.conf.d/12-iank.conf <<'EOF'
570 session-setup-script=/a/bin/distro-setup/desktop-20-autostart.sh
576 # todo, also note for work comp, scp opt/org-mode bin/build-scripts
578 # use the package manger version to install the cabal version
580 PATH="$PATH:$HOME/.cabal/bin"
582 # todo, on older ubuntu I used cabal xmonad + xfce,
583 # see /a/bin/old-unused/xmonad-cabal.sh
586 # for displaying error messages.
587 # optional dependency in arch, standard elsewhere.
588 pi xorg-xmessage xmonad-contrib xorg-xsetroot xorg-xinit
590 # https://wiki.archlinux.org/index.php/Xinitrc
591 for homedir in /home/*; do
592 cp /etc/X11/xinit/xinitrc $homedir/.xinitrc
593 $sed -ri '/^ *twm\b/,$d' $homedir/.xinitrc
594 tee -a $homedir/.xinitrc <<'EOF'
595 /a/bin/desktop-20-autostart.sh
596 xsetroot -cursor_name left_ptr
611 # the exact packages that task-laptop would install, since ubuntu
612 # doesn\'t have this virtual in practice package.
613 pi avahi-autoipd bluetooth powertop iw wireless-tools wpasupplicant
615 # todo: other distros unknown
620 # the first pup command can kill off our /etc/ mod, so rerun this
621 /a/exe/ssh-emacs-setup
622 echo "$0: $(date): ending now"