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