lots of updates
[distro-setup] / distro-begin
1 #!/bin/bash -l
2 # Copyright (C) 2016 Ian Kelling
3 # This program is under GPL v. 3 or later, see <http://www.gnu.org/licenses/>
4
5 # todo. dunno why, but original bootstrap of timezone is not sticking.
6 # fixed manually with:
7 # s dpkg-reconfigure tzdata
8
9
10 # for bootstrapping a new machine
11
12 # to make ssh run better, first run this:
13 sudo bash -c 'source /a/c/repos/bash/.bashrc && source /a/bin/ssh-emacs-setup'
14
15
16 # see t.org for OS installer notes
17
18 # usage: $0 [OPTIONS] HOSTNAME
19
20 # tips:
21 # run any sudo command first so your pass is cached
22 # set the scrollback to unlimited in case something goes wrong
23
24 if [[ $EUID == 0 ]]; then
25 echo "error: do not run as root"
26 exit
27 fi
28
29 interactive=true # set this to true if running by hand in emacs
30 [[ $- == *i* ]] || interactive=false
31
32
33
34 if ! $interactive; then
35 set -x
36 set -e -o pipefail
37 fi
38 set -E
39 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
40
41 # headless=false # unused atm
42 recompile=true
43 # for copying to a new data fs
44 bootstrapfs=false # old flag, needs new look before using.
45 while [[ $1 == -* ]]; do
46 case $1 in
47 # avoid some of the longer compilation steps,
48 # when we need to rerun because we had an error
49 -n) recompile=false; shift ;;
50 esac
51 done
52
53 if [[ $1 ]]; then
54 host=$1
55 else
56 host=$HOSTNAME
57 fi
58
59 for f in iank-dev htpc treetowl x2 frodo tp; do
60 eval "$f() { [[ $host == $f ]]; }"
61 done
62 has_p() { iank-dev || x2 || frodo; }
63 encrypted() { has_p || tp; }
64
65 shopt -s extglob
66 export GLOBIGNORE=*/.:*/..
67 umask 0002
68
69
70 ####### end command line parsing
71
72 if encrypted; then
73 # I tried making a service which was dependent on reboot.target,
74 # but it happened too late in the shutdown process.
75 sudo dd of=/etc/systemd/system/keyscripton.service <<'EOF'
76 [Unit]
77 Description=Turn on automatic decryption of drives on boot
78 # tried using graphical.target, but it made my display manager restart before rebooting.
79 # generally, I don't think targets order shutdown like they do startup.
80 # So, I did systemd-analyze plot > something.svg, and picked a reliably started
81 # service that happens late in the game.
82 After=postfix.service
83 DefaultDependencies=no
84 Conflicts=reboot.target
85
86 [Service]
87 Type=oneshot
88 RemainAfterExit=yes
89 ExecStart=/bin/true
90 ExecStop=/a/bin/keyscript-on
91
92 [Install]
93 WantedBy=keyscriptoff.service
94 EOF
95 sudo systemctl daemon-reload # needed if the file was already there
96 sudo systemctl stop keyscripton.service
97 # sudo systemctl start keyscripton.service
98 sudo systemctl enable keyscripton.service
99
100 sudo dd of=/etc/systemd/system/keyscriptoff.service <<'EOF'
101 [Unit]
102 Description=Turn off automatic decryption of drives on boot
103
104 [Service]
105 Type=oneshot
106 ExecStart=/a/bin/keyscript-off
107
108 [Install]
109 WantedBy=multi-user.target
110 EOF
111 sudo systemctl daemon-reload # needed if the file was already there
112 sudo systemctl enable keyscriptoff.service
113 sudo systemctl start keyscriptoff.service
114 fi
115
116 if iank-dev; then
117 desktop=$(ssh root@iankelling.org grep desktop /etc/hosts | grep -o "^.* ")
118 if $bootstrapfs; then
119 # for bootstrapping at a new job:
120 cp="scp $desktop:"
121 # for moving to a new hd, change $cp to move between filesystems
122 mkdir -p /a/bin
123 chown -R ian:ian /a
124 $cp/a/c /a
125 $cp/a/c/bin/{bash-programs-by-ian,distro-begin,distro-functions,input-setup.sh} /a/bin
126 echo -e \\n\\n\\n | ssh-keygen -t rsa
127 fi
128 fi
129
130 # this script has been designed to be idempotent
131 # todo, it would be nice to cut down on some of the output
132
133
134 # output is below so shellcheck can verify sources
135 for x in /a/bin/bash-programs-by-ian/repos/{errhandle,tee-unique,lnf}/*-function; do
136 echo "# shellcheck source=$x";
137 # shellcheck source=/a/bin/bash-programs-by-ian/repos/errhandle/bash-trace-function
138 # shellcheck source=/a/bin/bash-programs-by-ian/repos/errhandle/errallow-function
139 # shellcheck source=/a/bin/bash-programs-by-ian/repos/errhandle/errcatch-function
140 # shellcheck source=/a/bin/bash-programs-by-ian/repos/errhandle/errexit-function
141 # shellcheck source=/a/bin/bash-programs-by-ian/repos/tee-unique/teeu-function
142 # shellcheck source=/a/bin/bash-programs-by-ian/repos/lnf/lnf-function
143 source $x
144 done
145
146
147 set +e
148 $interactive || errcatch
149 source /a/bin/distro-functions/src/identify-distros
150 echo path:$PATH
151
152
153
154 if isfedora; then
155 # comment out line disallowing calling sudo in scripts
156 sudo sed -i 's/^Defaults *requiretty/#\0 # ian commented/' /etc/sudoers
157 # turn on magic sysrq commands for this boot cycle
158 echo 1 > sudo dd of=/proc/sys/kernel/sysrq
159 # selinux is not user friendly. Like, you enable samba, but you haven't run the magic selinux commands so it doesn't work
160 # and you have no idea why.
161 sudo sed -i 's/^\(SELINUX=\).*/\1disabled/' /etc/selinux/config
162 selinuxenabled && sudo setenforce 0
163 fi
164
165
166
167
168 # link files
169
170
171 for x in /a/c/repos/bash/!(.git); do
172 for homedir in /home/*; do
173 sudo chown -R ian:ian $homedir
174 lnf "$x" $homedir
175 done
176 sudo -i <<EOF
177 source /a/bin/bash-programs-by-ian/repos/lnf/lnf-function
178 lnf $x /root
179 EOF
180 done
181
182 echo path:$PATH
183
184 set +x
185 errallow
186 source ~/.bashrc
187 echo path:$PATH
188 $interactive || errcatch
189 $interactive || set -x
190
191
192 # passwordless sudo
193 tu /etc/sudoers <<'EOF'
194 ian ALL=(ALL) NOPASSWD: ALL
195 Defaults env_keep += SUDOD
196 EOF
197
198
199 # enable magic sysrq keys. debian docs say it is already enabled by default
200 isfedora && tu /etc/sysctl.conf 'kernel.sysrq = 1'
201
202
203
204 if isdebian; then
205 # add contrib non-free to sources for main
206 s sed -i 's/^\(deb.* main\).*/\1 contrib non-free/' /etc/apt/sources.list.d/*
207
208 # non-existent var, as Im not planning to use stable right now
209 if isdebian-stable; then
210 code=$(debian-codename)
211 s dd of=/etc/apt/sources.list.d/mozilla-iceweasel.list <<EOF
212 deb http://mozilla.debian.net/ $code-backports firefox-release
213 deb-src http://mozilla.debian.net/ $code-backports firefox-release
214 EOF
215
216 # we change the mirror from the default, so we cant use tu
217 s dd of=/etc/apt/sources.list.d/main-backports.list <<EOF
218 deb http://http.debian.net/debian $code-backports main contrib non-free
219 deb-src http://http.debian.net/debian $code-backports main contrib non-free
220 EOF
221
222 p update
223 # take care of mozilla signing errors in previous command
224 pi pkg-mozilla-archive-keyring
225 p update
226 else
227 :
228 # this would change stable to testing, but I set that up already.
229 # It\'s just a no-op if its already testing.
230 # sudo sed -ri 's!^( *[^ #]+ +[^ ]+ +)[[:alpha:]]+(.*)!\1testing\2!' /etc/apt/sources.list
231 p update
232 fi
233 fi
234
235 if isarch; then
236 #https://wiki.archlinux.org/index.php/Arch_User_Repository#Installing_packages
237 sudo pacman -S --noconfirm --needed base-devel jq
238 # pacaur seems to be the best, although it + cower has a few minor bugs,
239 # its design goals seem good, so, going for it.
240
241 aurpi() {
242 for p in "$@"; do
243 tempdir=$(mktemp -d)
244 pushd $tempdir
245 aurex "$p"
246 makepkg -sri --skippgpcheck --noconfirm
247 popd
248 rm -rf $tempdir
249 done
250 }
251 aurpi cower pacaur
252
253 # this creates ~/.gnupg. addgnupghome is broken on arch.
254 gpg -k
255 # for aur, automatically dl & add gpg keys.
256 # Just the keyserver-options line goes in dirmngr.conf once
257 # this bug is fixed: https://bugs.gnupg.org/gnupg/issue2147
258 for homedir in /home/*; do
259 teeu $homedir/.gnupg/gpg.conf <<EOF
260 $(grep -o '^ *keyserver .*' $homedir/.gnupg/dirmngr.conf)
261 keyserver-options auto-key-retrieve
262 EOF
263 done
264 pi pacserve
265 x=$(mktemp); pacman.conf-insert_pacserve >$x
266 sudo dd of=/etc/pacman.conf if=$x; rm $x
267 sudo systemctl enable pacserve.service
268 sudo systemctl start pacserve.service
269
270 # strange error if just installing trash-cli: "pyalpm requires python",
271 # so I see that it requires python2, and installing that manually fixes it.
272 # I didn't see this on earlier installation, main thing which changed was
273 # pacserve, so not sure if it's related.
274 pi python2
275 fi
276
277 pup
278 pi trash-cli
279
280
281 ###### link files ###########
282 # convenient to just do all file linking in one place
283
284 s lnf /a/sdx{,d} /
285
286 # if it wasn't set already, we could set hostname here
287 #echo treetowl | s dd of=/etc/hostname
288 #s hostname -F /etc/hostname
289 #HOSTNAME=$(hostname)
290
291 #########################################
292 # NOTE: only /a needs to be mounted for creating links!
293 ###########################################
294
295 # todo: reconcile ~/.ssh/config work/home
296 s lnf -T /q/p /p
297 if has_p; then
298 lnf -T /p/offlineimap ~/Maildir
299 lnf -T /p/News ~/News
300 # don't use /* because I don't want to require it to be mounted
301 s lnf /q/root/.editor-backups /q/root/.undo-tree-history \
302 /q/root/.ssh /a/opt \
303 /a/c/.emacs.d ~/.unison /root
304
305 fi
306
307 s lnf /a/c/.inputrc /a/c/.vim /a/c/.vimrc /a/c/.gvimrc /root
308
309 # machine is going away
310 # if [[ $HOSTNAME == htpc ]]; then
311 # lnf -T /i/Videos ~/Downloads
312 # fi
313
314 if has_p; then
315 # for dovecot
316 lnf -T /i/k/mboxes ~/mail
317 fi
318
319
320 # basic needed packages
321 case $(distro-name) in
322 debian)
323 pi firefox$( isdebian-stable && e /$code-backports )
324 # for hosts which require nonfree drivers
325 case $HOSTNAME in
326 tp|x2) : ;;
327 *) pi linux-image-amd64 firmware-linux-nonfree \
328 firmware-linux-free linux-headers-amd64
329 ;;
330 esac
331 ;;&
332 ubuntu|debian)
333 pi xmacro gtk-redshift xinput
334 ;;&
335 fedora)
336 p -y groupinstall development-tools c-development books admin-tools
337 pi redshift-gtk
338 # debian has this package patched to work, upstream is dead
339 # tried using alien, pi alien, alien -r *.deb, rpm -Uhv *.rpm, got this error, so fuck it
340 # file /usr/bin from install of xmacro-0.3pre_20000911-7.x86_64 conflicts with file from package filesystem-3.2-19.fc20.x86_64
341 # http://packages.debian.org/source/sid/xmacro
342 pi patch libXtst-devel wget man-pages # what is the ubuntu equivalent to man-pages?
343 cd $(mktemp -d)
344 wget http://ftp.de.debian.org/debian/pool/main/x/xmacro/xmacro_0.3pre-20000911.orig.tar.gz
345 wget http://ftp.de.debian.org/debian/pool/main/x/xmacro/xmacro_0.3pre-20000911-6.diff.gz
346 ex *.gz
347 patch -p0 < xmacro_0.3pre-20000911-6.diff
348 cd xmacro-0.3pre-20000911.orig
349 make
350 sleep 1 # not sure why the following command couldn\'t find, so trying this
351 # no make install target
352 s cp -f xmacroplay xmacrorec xmacrorec2 /usr/local/bin
353 ;;&
354 arch)
355 # libxtst is missing dep https://aur.archlinux.org/packages/xmacro/#news
356 pi xorg-server redshift xorg-xinput pkgfile libxtst xmacro
357 # like apt-cache
358 s pkgfile --update
359 ;;&
360 esac
361
362 pi xbindkeys xkbset cryptsetup
363
364 pi lvm2
365 # enables trim for volume delete, other rare commands.
366 sudo sed -ri 's/( *issue_discards\b).*/\1 = 1/' /etc/lvm/lvm.conf
367
368 if encrypted; then
369 if isdeb; then
370 sudo cp /usr/share/doc/util-linux/examples/fstrim.{service,timer} /etc/systemd/system
371 fi
372 # does weekly trim
373 sudo systemctl enable fstrim.timer
374 fi
375
376 dirs=(/mnt/{1,2,3,4,5,6,7,8,9})
377 s mkdir -p "${dirs[@]}"
378 s chown ian:ian "${dirs[@]}"
379
380 if [[ $HOSTNAME == treetowl ]]; then
381 tu /etc/fstab <<'EOF'
382 UUID=3f7b31cd-f299-40b4-a86b-7604282e2715 /i btrfs noatime 0 2
383 EOF
384 else
385 tu /etc/fstab <<'EOF'
386 /q/i /i none bind 0 0
387 EOF
388 fi
389
390 tu /etc/fstab <<'EOF'
391 /i/w /w none bind 0 0
392 /i/k /k none bind 0 0
393 EOF
394
395 if ! mountpoint /kfrodo; then
396 s mkdir -p /kfrodo
397 s chown ian:traci /kfrodo
398 fi
399 if [[ $HOSTNAME == frodo ]]; then
400 tu /etc/fstab <<'EOF'
401 /k /kfrodo none bind 0 0
402 EOF
403 else
404 tu /etc/fstab <<'EOF'
405 frodo:/k /kfrodo nfs defaults 0 0
406 EOF
407 fi
408
409 for x in i w k; do
410 s mount /$i || [[ $? == 32 ]] # 32 = already mounted
411 done
412
413
414 # ssh and probably some other things care about parent directory
415 # ownership, and ssh doesn\'t allow any group writable parent
416 # directories, so we are forced to use a directory structure similar
417 # to home directories
418 s chown root:ian /q
419 s chmod 755 /q
420
421
422 /a/bin/conflink
423
424 # propogate /etc/udev/hwdb.d
425 s systemd-hwdb update
426 ser restart systemd-udev-trigger
427
428 # work desktop doesnt need gpg stuff, but it doesnt hurt
429 s dd of=/etc/profile.d/environment.sh <<'EOF'
430 # IAN: EDIT THIS FROM /a/bin/distro-begin
431
432 if [ -f $HOME/path_add-function ]; then
433 . $HOME/path_add-function
434 path_add /usr/sbin /usr/local/sbin /sbin
435 path_add /a/bin /a/opt/bin $HOME/.cabal/bin
436
437 if [ -r /etc/alternatives/java_sdk ]; then
438 export JAVA_HOME=/etc/alternatives/java_sdk
439 path_add /etc/alternatives/java_sdk
440 fi
441 fi
442
443 export EDITOR="emacsclient"
444 # this makes emacsclient file/-c start a server instance if none is running,
445 # instead of some alternate editor logic
446 export ALTERNATE_EDITOR=""
447
448 # ubuntu starts gpg agent automatically with /etc/X11/Xsession.d/90gpg-agent.
449 # fedora doesn't, which left me to figure this out, and google was no help.
450 # fedora documentation is often quite bad :(
451 # This is mostly copied from that file.
452 # Main difference is that we eval the result of starting gpg-agent,
453 # while that file executes it through xsession specific var.
454 # Also make sourcing the pidfile make more sense.
455 # End result should be the same afaik.
456 # for gpg-agent to work when calling gpg from the command line,
457 # we need an environment variable that is setup via the eval.
458 # which is why we do this upon login, so it can propogate
459 # It is also written to the file $HOME/.gnupg/gpg-agent-info-$(hostname)
460 # I'm not aware if that is ever used, but just fyi.
461 # I also added the bit about xmessaging the stderr,
462 # because I'd like to know if the command fails
463 if [ -f /etc/fedora-release ]; then
464 : ${GNUPGHOME=$HOME/.gnupg}
465
466 GPGAGENT=/usr/bin/gpg-agent
467 PID_FILE="$GNUPGHOME/gpg-agent-info-$(hostname)"
468
469 if ! $GPGAGENT 2>/dev/null; then
470 temp="$(mktemp)"
471 eval "$($GPGAGENT --homedir /p/do-not-delete --daemon --sh --write-env-file=$PID_FILE 2>$temp)"
472 temperr="$(<"$temp")"
473 [ -n "$temperr" ] && xmessage "gpg-agent stderr: $temperr"
474 elif [ -r "$PID_FILE" ]; then
475 . "$PID_FILE"
476 export GPG_AGENT_INFO
477 fi
478 fi
479
480 # ubuntu has 002, debian has 022.
481 # from what I've read, benefit of 002 makes shared groups read/write.
482 # Security concern is where some unixes put everyone in a same group,
483 # so if you copy files there with exact perms, that is probably not
484 # what you want. I don't use a system like that, and I don't really care
485 # either way, but I'd prefer
486 # being able to sync file perms with ubuntu systems at work,
487 # and it's easier to change the debian one.
488
489 umask 002
490 EOF
491
492
493
494 # emacs dependency.
495 # dunno why debian installed postfix with yum-builddep emacs
496 # but I will just explicitly install it here since
497 # I use it for sending mail in emacs.
498 if private-host; then
499 relayhost="[mail.messagingengine.com]:587"
500 else
501 # ses initially suggests port 25, but I had problems connecting to that.
502 relayhost="[email-smtp.us-west-2.amazonaws.com]:587"
503 fi
504 if isdeb; then
505 s debconf-set-selections<<EOF
506 postfix postfix/main_mailer_type select Satellite system
507 postfix postfix/mailname string $host
508 postfix postfix/relayhost string $relayhost
509 EOF
510
511 pi postfix
512 else
513 pi postfix
514 # Settings from reading the output when installing on debian,
515 # then seeing which were different in a default install on arch.
516 # I assume the same works for fedora.
517 postconfin <<EOF
518 mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
519 mailbox_size_limit = 0
520 relayhost = $relayhost
521 inet_interfaces = loopback-only
522 EOF
523
524 s systemctl enable postfix
525 s systemctl start postfix
526 fi
527
528
529 if isarch; then
530 # install so it's build dependencies don't get removed.
531 x=$(mktemp -d)
532 pushd $x
533 aurex emacs-git
534 makepkg -si --noconfirm
535 popd
536 rm -rf $x
537 pi hunspell hunspell-en
538 else
539 # to disable emacs git build,
540 # s apt-get install emacs
541 if $recompile; then
542 /a/bin/buildscripts/emacs
543 else
544 /a/bin/buildscripts/emacs -r
545 fi
546 fi
547
548 # todo, figure this out for arch if we ever try out gnome.
549 if ! isarch; then
550 # install for multiple display managers in case we use one
551 if isdeb; then
552 dir=/etc/gdm3
553 elif isfedora; then
554 # fedora didn\'t have the 3.
555 dir=/etc/gdm
556 fi
557 s mkdir -p $dir/PostLogin
558 s command cp /a/bin/desktop-20-autostart.sh $dir/PostLogin/Default
559 s mkdir /etc/lightdm/lightdm.conf.d
560 s dd of=/etc/lightdm/lightdm.conf.d/12-ian.conf <<'EOF'
561 [SeatDefaults]
562 session-setup-script=/a/bin/desktop-20-autostart.sh
563 EOF
564 fi
565
566 if isubuntu; then
567 # disable crash report annoying crap
568 s dd of=/etc/default/apport <<<'enabled=0'
569 fi
570
571
572 pi ghc sakura
573 # todo, also note for work comp, scp opt/org-mode bin/build-scripts
574
575 # use the package manger version to install the cabal version
576 pi cabal-install
577 cabal update
578 PATH="$PATH:$HOME/.cabal/bin"
579
580 # todo, on older ubuntu I used cabal xmonad + xfce,
581 # see /a/bin/old-unused/xmonad-cabal.sh
582
583 # trying out the distro's versions newer distros
584 pi xmonad
585 if isarch; then
586 # for displaying error messages.
587 # optional dependency in arch, standard elsewhere.
588 pi xorg-xmessage xmonad-contrib xorg-xsetroot xorg-xinit
589
590 # https://wiki.archlinux.org/index.php/Xinitrc
591 for homedir in /homme/*; do
592 cp /etc/X11/xinit/xinitrc $homedir/.xinitrc
593 sed -ri '/^ *twm\b/,$d' $homedir/.xinitrc
594 echo "source /a/bin/xinitrc" | tee -a $homedir/.xinitrc
595 done
596 else
597 pi suckless-tools
598 fi
599 pi dmenu
600
601 if isdeb && (tp || x2); then
602 pi task-laptop
603 fi
604
605 sudo chown -R traci:traci /home/traci