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 codename
=$
(debian-codename
)
77 has_wayland
() { [[ $codename == buster
]]; }
78 has_x
() { [[ $codename != buster
]]; }
79 has_monitor
() { ! linode
; }
80 linode
() { lj || li
; }
81 has_btrfs
() { ! linode
; }
82 home_network
() { ! linode
; }
83 encrypted
() { has_p
; }
85 export GLOBIGNORE
=*/.
:*/..
88 sed="sed --follow-symlinks"
90 ####### end setup environment #######
95 ##### begin setup encryption scripts ######
97 # I tried making a service which was dependent on reboot.target,
98 # but it happened too late in the shutdown process.
99 sudo
dd of
=/etc
/systemd
/system
/keyscripton.service
<<'EOF'
101 Description=Turn on automatic decryption of drives on boot
102 # tried using graphical.target, but it made my display manager restart before rebooting.
103 # generally, I don't think targets order shutdown like they do startup.
104 # So, I did systemd-analyze plot > something.svg, and picked a reliably started
105 # service that happens late in the game.
107 DefaultDependencies=no
108 # not sure if needed, makes sure we shut down before reboot.target
109 Conflicts=reboot.target
115 ExecStop=/a/exe/keyscript-on
118 WantedBy=keyscriptoff.service
120 sudo systemctl daemon-reload
# needed if the file was already there
121 sudo systemctl stop keyscripton.service
122 # sudo systemctl start keyscripton.service
123 sudo systemctl
enable keyscripton.service
125 sudo
dd of
=/etc
/systemd
/system
/keyscriptoff.service
<<'EOF'
127 Description=Turn off automatic decryption of drives on boot
131 ExecStart=/a/exe/keyscript-off
134 WantedBy=multi-user.target
136 sudo systemctl daemon-reload
# needed if the file was already there
137 sudo systemctl
enable keyscriptoff.service
138 sudo systemctl start keyscriptoff.service
140 # from /usr/share/doc/dropbear-initramfs/README.initramfs.gz
141 while read m _
; do /sbin
/modinfo
-F filename
"$m"; done </proc
/modules | \
142 sed -nr "s@^/lib/modules/`uname -r`/kernel/drivers/net(/.*)?/([^/]+)\.ko\$@\2@p" \
143 | sudo
dd of
=/etc
/initramfs-tools
/modules
144 sudo apt-get
-y install initramfs-tools-core
145 # initram auth keys get setup with rootsshsync
146 # then for remote unlock, ssh and do this once per crypt disk:
147 # echo -n PASS >/lib/cryptsetup/passfifo
149 ##### end setup encryption scripts ######
152 # disabled until its fixed up
155 # todo, it would be nice to cut down on some of the output
157 ##### fedora prereq/fundamental settings
159 # comment out line disallowing calling sudo in scripts
160 sudo
$sed -i 's/^Defaults *requiretty/#\0 # ian commented/' /etc
/sudoers
161 # turn on magic sysrq commands
162 echo 1 > sudo
dd of
=/proc
/sys
/kernel
/sysrq
163 echo "kernel.sysrq = 1" > /etc
/sysctl.d
/90-sysrq.conf
164 # selinux is not user friendly. Like, you enable samba, but you haven't run the magic selinux commands so it doesn't work
165 # and you have no idea why.
166 sudo
$sed -i 's/^\(SELINUX=\).*/\1disabled/' /etc
/selinux
/config
167 selinuxenabled
&& sudo setenforce
0
171 #### rerun my fai-time scripts
172 # already ran for pxe installs, but used for vps & updates
173 distro
=$
(distro-name
)
175 ubuntu|debian|trisquel
)
176 sudo bash
-c ". /a/bin/fai/fai-wrapper && /a/bin/fai/fai/config/scripts/GRUB_PC/11-iank"
179 sudo bash
-c ". /a/bin/fai/fai-wrapper &&
180 /a/bin/fai/fai/config/distro-install-common/end"
184 ###### setup hostname
185 sudo
$sed -i '/^127\.0\.1\.1/d' /etc
/hosts
186 echo "127.0.1.1 $HOSTNAME.b8.nz $HOSTNAME" | sudo
tee -a /etc
/hosts
189 ##### exit first stage if running as root
190 if [[ $EUID == 0 ]]; then
191 echo "$0: running as root. exiting now that users are setup"
196 #### setup bash for root
197 for x
in /a
/c
/{.bashrc
,brc
,.bash_profile
,.profile
,.inputrc
,path_add_function
}; do
205 # li needs the bind group before conflink
206 if [[ $HOSTNAME == li
]]; then
207 getent group
bind &>/dev
/null || sudo groupadd
-r bind
209 # this needs to be before installing pacserve so we have gpg conf.
212 ###### bash environment setup
215 source /etc
/profile.d
/environment.sh
218 $interactive ||
set -x
221 #### setup passwordless sudo
222 tu
/etc
/sudoers
<<EOF
223 $USER ALL=(ALL) NOPASSWD: ALL
224 Defaults env_keep += SUDOD
225 # makes ubuntu be like debian
226 # https://unix.stackexchange.com/a/91572
227 Defaults always_set_home
228 # default setting is to have minimum umask of 0022
229 # This lets us have user-specific umasks which are more permissive.
230 # I did this for transmission and set it's umask gecos on install,
231 # see there for more info.
236 #### setup firefox backport
237 ## ian: disabled. backports are not being published atm due to rust packaging issue
239 # codename=$(debian-codename)
240 # if isdebian-stable && has_x; then
241 # s dd of=/etc/apt/sources.list.d/mozilla-iceweasel.list <<EOF
242 # deb http://mozilla.debian.net/ $codename-backports firefox-release
243 # deb-src http://mozilla.debian.net/ $codename-backports firefox-release
246 # # take care of mozilla signing errors in previous command
247 # pi pkg-mozilla-archive-keyring
253 ###### arch aur wrapper setup
255 #https://wiki.archlinux.org/index.php/Arch_User_Repository#Installing_packages
256 sudo pacman
-S --noconfirm --needed base-devel jq
257 # pacaur seems to be the best, although it + cower has a few minor bugs,
258 # its design goals seem good, so, going for it.
265 makepkg
-sri --skippgpcheck --noconfirm
274 x
=$
(mktemp
); /usr
/bin
/pacman.conf-insert_pacserve
>$x
275 sudo
dd of
=/etc
/pacman.conf
if=$x; rm $x
276 sudo systemctl
enable pacserve.service
277 sudo systemctl start pacserve.service
281 #### update all packages
285 ###### p1 packages install ######
287 # requirement for trash-cli.
288 # background: strange error if just installing trash-cli: "pyalpm requires python",
289 # so I see that it requires python2, and installing that manually fixes it.
290 # I didn\'t see this on earlier installation, main thing which changed was
291 # pacserve, so not sure if it\'s related.
297 ######## fix evbug bug ######
301 #https://bugs.launchpad.net/ubuntu/+source/module-init-tools/+bug/240553
302 #https://wiki.debian.org/KernelModuleBlacklisting
303 #common advice when searching is to use /etc/modprobe.d/blacklist.conf,
304 #but that file won't work and will get automatically reverted
305 sudo rmmod evbug ||
: # might not be loaded yet
306 file=/etc
/modprobe.d
/evbug.conf
307 line
="blacklist evbug"
308 if ! grep -xFq "$line" $file; then
309 sudo
dd of
=$file 2>/dev
/null
<<<"$line"
311 sudo update-initramfs
-u
318 # convenient to just do all file linking in one place
322 lnf
-T /p
/News ~
/News
324 s lnf
/q
/root
/.editor-backups
/q
/root
/.undo-tree-history \
325 /a
/opt
/a
/c
/.emacs.d
$HOME/mw_vars
/k
/backup
/root
326 pi rsync
# needed for rootsshsync
327 /a
/bin
/ds
/install-my-scripts
# needed for rootsshsync
329 s lnf
/a
/c
/.vim
/a
/c
/.vimrc
/a
/c
/.gvimrc
/root
332 lnf
-T /i
/k
/mboxes ~
/mail
339 case $
(distro-name
) in
340 trisquel|ubuntu|debian
)
351 #### install redshift
352 case $
(distro-name
) in
353 trisquel|ubuntu|debian
)
354 # recommends gets us geoclue (for darkening automatically at night i assume),
355 # which recommends modemmanager, which is annoying to fix for the model01 keyboard.
356 pi
--no-install-recommends gtk-redshift
369 # originally used xkbcomp, documented in input-setup.sh, this doesnt
370 # work under wayland, but its still useful for creating the config,
371 # then modifying the system files.
372 s
sed -i.orig
'/key *<KPMU> *{/,/}/s/KP_Multiply/underscore/g' /usr
/share
/X11
/xkb
/symbols
/keypad
377 #### arch specific early packages
378 case $
(distro-name
) in
380 # pkgfile is like apt-cache
386 #### fedora specific packages
387 case $
(distro-name
) in
389 # todo, this could probably come later
390 p
-y groupinstall development-tools c-development books admin-tools
393 # other distros unknown
397 # enable trim for volume delete, other rare commands
398 sudo
$sed -ri 's/( *issue_discards\b).*/\1 = 1/' /etc
/lvm
/lvm.conf
400 # flidas or so, these units arent built-in
401 if isdeb
&& ! systemctl list-unit-files |
grep -xF fstrim.timer
&>/dev
/null
; then
402 sudo
cp /usr
/share
/doc
/util-linux
/examples
/fstrim.
{service
,timer
} /etc
/systemd
/system
405 sudo systemctl
enable fstrim.timer
408 ##### make extra dirs
409 dirs=(/mnt
/{1,2,3,4,5,6,7,8,9} /nocow
/t
)
410 s mkdir
-p "${dirs[@]}"
411 s chown
$USER:$USER "${dirs[@]}"
414 tu
/etc
/fstab
<<'EOF'
415 /i/w /w none bind,noauto 0 0
416 /i/k /k none bind,noauto 0 0
418 if ! mountpoint
/kr
; then
420 s chown
$USER:user2
/kr
422 if home_network
; then
423 if [[ $HOSTNAME == frodo
]]; then
424 tu
/etc
/fstab
<<'EOF'
425 /k /kr none bind,noauto 0 0
428 tu
/etc
/fstab
<<'EOF'
429 frodo:/k /kr nfs noauto 0 0
433 s mkdir
-p /q
/i
/{w
,k
}
434 for dir
in /{i
,w
,k
}; do
435 if mountpoint
$dir; then continue; fi # already mounted
437 s chown
$USER:$USER $dir
439 # not needed for all hosts, but rather just keep it uniform
440 s mkdir
-p /mnt
/iroot
441 # debian auto mounting of multi-disk encrypted btrfs is busted. It is
442 # in jessie, and in stretch as of 11/26/2016 I have 4 disks in cryptab,
443 # based on 3 of those, it creates .device units for /dev/mapper/dev...
444 # then waits endlessly for them on bootup, after the /dev/mapper disks
445 # have already been created and exist. todo: create a simple repro
446 # for this in a vm and report it upstream.
447 if has_btrfs || home_network
; then
449 s
dd of
=/root
/imount
<<'EOF'
451 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
453 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
454 for dir in /i /mnt/iroot /k /kr /w; do
455 if ! mountpoint $dir &>/dev/null && \
456 awk '{print $2}' /etc/fstab | grep -xF $dir &>/dev/null; then
457 if awk '{print $3}' /etc/fstab | grep -xF nfs &>/dev/null; then
458 mount $dir || echo "warning: failed to mount nfs on $dir"
465 s
chmod +x
/root
/imount
467 s
dd of
=/etc
/systemd
/system
/imount.service
<<EOF
469 Description=Mount /i and related mountpoints
470 Before=syncthing@$USER.service
474 ExecStart=/root/imount
477 RequiredBy=syncthing@$USER.service
478 # note /kr needs networking, this target is the simplest way to
479 # time it when the network should be up, but not do something
480 # dumb like delay startup until the network is up. It happens
481 # at some time after network.target
482 WantedBy=multi-user.target
484 sudo systemctl daemon-reload
# needed if the file was already there
485 sudo systemctl
enable imount.service
486 sudo systemctl start imount.service
490 # a nocow dir that is common to multiple distros installed on the same system
493 if ! mountpoint
$dir; then
494 subvol
=/mnt
/root
/nocow
495 if [[ ! -e $subvol ]]; then
496 s btrfs subvolume create
$subvol
497 s chown root
:1000 $subvol
501 first_root_crypt
=$
(awk '$2 == "/" {print $1}' /etc
/mtab
)
503 $first_root_crypt /nocow btrfs noatime,subvol=nocow 0 0
506 s chown
$USER:$USER $dir
519 # disable crash report annoying dialogs.
520 s
dd of
=/etc
/default
/apport
<<<'enabled=0'
526 # emacs git build was broken last time i checked,
530 makepkg -si --noconfirm
533 pi hunspell hunspell-en
536 /a/bin/buildscripts/emacs
538 /a/bin/buildscripts/emacs --no-r || /a/bin/buildscripts/emacs
542 ##### install laptop hardware packages
543 if tp || x2 || x3; then
549 # the exact packages that task-laptop would install, since ubuntu
550 # doesn\'t have this virtual in practice package.
551 pi avahi-autoipd bluetooth powertop iw wireless-tools wpasupplicant
553 # todo: other distros unknown
558 ##### install x stuff
564 # xorg-xmessage for displaying error messages.
565 # optional dependency in arch, standard elsewhere.
566 pi xorg-server xorg-xmessage xorg-xsetroot xorg-xinit
572 ##### setup X autostart
575 # https://wiki.archlinux.org/index.php/Xinitrc
576 for homedir in /home/*; do
577 cp /etc/X11/xinit/xinitrc $homedir/.xinitrc
578 $sed -ri '/^ *twm\b/,$d' $homedir/.xinitrc
579 tee -a $homedir/.xinitrc <<'EOF'
580 /a/bin/desktop-20-autostart.sh
581 xsetroot -cursor_name left_ptr
586 # todo, figure this out for arch if we ever try out gnome.
587 # install for multiple display managers in case we use one
591 # fedora didn\'t have the 3.
594 s mkdir -p $dir/PostLogin
595 s command cp /a/bin/distro-setup/desktop-20-autostart.sh $dir/PostLogin/Default
596 s mkdir /etc/lightdm/lightdm.conf.d
597 s dd of=/etc/lightdm/lightdm.conf.d/12-iank.conf <<'EOF'
599 session-setup-script=/a/bin/distro-setup/desktop-20-autostart.sh
605 #### refix interactive ssh terminal
606 # the first pup command can kill off our /etc/ mod, so rerun this
607 /a/exe/ssh-emacs-setup
610 echo "$0: $(date): ending now"