add rescue, fix default pxe, ubuntu locale
[automated-distro-installer] / faiserver-setup
index d507f0b3a56b4361eafcb6c9be19fe1fd3d9bb88..f60ec4791a5c58d0fda16878136cfc8db7e8ac1b 100755 (executable)
 #!/bin/bash
+# Copyright (C) 2016 Ian Kelling
 
-# initial setup of a fai server on debian
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 set -eE -o pipefail
-trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
+trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 
 [[ $EUID == 0 ]] || exec sudo "${BASH_SOURCE}" "$@"
 
+usage() {
+    cat <<EOF
+usage: ${0##*/} [-h|--help]
+install fai-server on the current machine
+
+Initial setup of a fai server on debian. works on localhost.
+Set's the current ip as the tftp server. I vaguely remember
+that using a hostname does not work.
+Separate from running this, faiserver needs to be setup in dns
+to point to whatever host this is run on.
+
+For running on arm, it expects Ian's fai-basefiles repository at
+/a/bin/fai-basefiles
+
+EOF
+    exit $1
+}
+case $1 in
+    -h|--help) usage ;;
+esac
+
+
 e() { echo "$@"; "$@"; }
+
+# When stretch becomes stable, change this to stretch.
+# I\'ve tested this with stretch, it works, but notably,
+# the automatic basefile getting will be for stretch
+# instead of jessie, so if you install jessie, you need
+# to setup the basefile and it\'s corresponding class.
+base=jessie
+sed="sed -ri --follow-symlinks"
+
+if ! type -p wget &>/dev/null; then
+  apt-get install -y wget
+fi
+
+armhf() {
+    [[ $(dpkg --print-architecture) == armhf ]]
+}
+
+if grep -xFq 'VERSION="9 (stretch)"' /etc/os-release; then
+  # if we use stretch, no need for fai-project repo.
+  # this will need to be updated when there is a codename
+  # for stretch+1
+  rm -f /etc/apt/sources.list.d/fai.list
+elif armhf; then
+  if apt-cache policy | grep o=Debian,a=testing,n=stretch &>/dev/null; then
+    cat >/etc/apt/sources.list.d/testing.list <<'EOF'
+deb http://http.us.debian.org/debian testing main contrib non-free
+deb-src http://http.us.debian.org/debian testing main contrib non-free
+
+deb http://security.debian.org/ testing/updates main contrib non-free
+deb-src http://security.debian.org/ testing/updates main contrib non-free
+
+deb http://http.us.debian.org/debian testing-updates main contrib non-free
+deb-src http://http.us.debian.org/debian testing-updates main contrib non-free
+EOF
+
+    cat >/etc/apt/preferences.d/fai <<'EOF'
+Package: fai-server fai-client fai-doc
+Pin: release a=testing
+Pin-Priority: 500
+
+Package: *
+Pin: release a=testing
+Pin-Priority: -10
+EOF
+  fi
+else
+    wget -O - http://fai-project.org/download/074BCDE4.asc | apt-key add -
+    cat >/etc/apt/sources.list.d/fai.list <<'EOF'
+deb http://fai-project.org/download jessie koeln
+EOF
+fi
+
 # for ubuntu:
 #add-apt-repository -y ppa:fai/ppa
 
 # for debian:
-wget -O - http://fai-project.org/download/074BCDE4.asc | apt-key add -
-cat >/etc/apt/sources.list.d/fai.list <<'EOF'
+
+
+apt-get update
+
+# Relevant packages from fai-quickstart depends and fai-server recommends.
+# I especially do not wait isc-dhcp-server or an inetd. Also excludes
+# nfs-kernel-server. On an android chroot, we don\'t have nfs in the
+# kernel, or the ability to install it.
+pkgs=(fai-doc tftpd-hpa tar reprepro squashfs-tools binutils)
+if modprobe nfsd &>/dev/null; then
+  pkgs+=(nfs-kernel-server)
+else
+    pkgs+=(apache2)
+fi
+
+
+apt-get install -y ${pkgs[@]}
+apt-get install --no-install-recommends -y fai-server
+
+r=http://http.us.debian.org/debian
+# like default, but scrap httpredir, and nonfree.
+# All my systems should be able to get along without nonfree
+# for a base working system afaik.
+cat >/etc/fai/apt/sources.list <<EOF
+deb $r $base main contrib
+deb http://security.debian.org/debian-security $base/updates main contrib
+EOF
+
+if [[ $base == jessie ]]; then
+  cat >>/etc/fai/apt/sources.list <<'EOF'
+# uncommenting this from the defaults. it's got bug fixes.
+# repository that may contain newer fai packages for jessie
 deb http://fai-project.org/download jessie koeln
+# fix tar https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=819978
+deb http://ftp.debian.org/debian jessie-backports main
 EOF
 
-apt-get update
-# all the dependencies except the dhcp server
-deps="$(apt-cache show fai-quickstart | grep ^Depends: |head -n 1|\
-              sed -r 's/^Depends:|,|\|[^,]+|isc-dhcp-server//g')"
-to_install=()
-for pkg in $deps; do
-    dpkg -s $pkg &>/dev/null && continue ||:
-    to_install+=($pkg)
-    echo $pkg >>/etc/fai/fai-manually-installed-packages
-done
-if [[ $to_install ]]; then
-    apt-get -y install ${to_install[@]}
+  # note, fai doesn\'t look at /etc/fai/apt/preferences.d
+  cat >/etc/fai/apt/preferences <<'EOF'
+Package: tar
+Pin: release a=jessie-backports
+Pin-Priority: 500
+EOF
 fi
 
-sed -i 's/^#deb/deb/' /etc/fai/apt/sources.list
-sed -i 's/#LOGUSER/LOGUSER/' /etc/fai/fai.conf
+
+# tried out a stretch base, doesn't work yet.
+#
+$sed -f - /etc/fai/nfsroot.conf <<EOF
+s,^( *FAI_DEBOOTSTRAP=).*,\1"$base $r",
+# add --arch amd64. this is needed on arm system which is
+# used to install amd64 clients. On amd64 servers, it's redundant.
+# disabled for now, since creating fai nfsroot on my arm machine
+# is not working
+#/--arch amd64/!s/^(\s*FAI_DEBOOTSTRAP_OPTS=")/\1--arch amd64 /
+/^\s*FAI_ROOTPW/d
+$ a FAI_ROOTPW="$(</q/root/shadow/standard)"
+EOF
+
+$sed 's/#LOGUSER/LOGUSER/' /etc/fai/fai.conf
 # from man fai-make-nfsroot,
 # figured out after partitioning ignored my crypt partition
+
+
 if ! grep cryptsetup /etc/fai/NFSROOT &>/dev/null; then
-    sed -ri '/^PACKAGES install$/a cryptsetup' /etc/fai/NFSROOT
+  $sed '/^PACKAGES install$/a cryptsetup' /etc/fai/NFSROOT
 fi
-fai-setup -vf
-{ head -n 1 /srv/fai/nfsroot/root/.ssh/known_hosts | awk '{print $1}' \
-        | tr '\n' ' '; ssh-keyscan localhost | grep -o "ecdsa-sha2-nistp256.*"; \
-    } >>/srv/fai/nfsroot/root/.ssh/known_hosts
 
-# this does not alter the config on a new install
-sed -ri 's#^([[:space:]]*TFTP_DIRECTORY[[:space:]]*=).*#\1"/srv/tftp"#' \
-    /etc/default/tftpd-hpa
+if armhf; then
+  cd /srv/fai
+  rm -rf nfsroot
+  tar Jxf /a/bin/fai-basefiles/base.tar.xz
+  # background: Can't build the nfsroot on my arm system now.  First,
+  # fai-make-nfsroot won't work out of the box. One idea to make it work
+  # is by installing qemu-user-static, then copying qemu-x86_64-static
+  # into the nfsroot, and prepending it to chroot commands in
+  # fai-make-nfsroot, but that fails in odd ways. ls has permissions
+  # problems on reading directories, various programs segfault
+  # immediately, cat can't open a file, etc.
+
+  NFSROOT=/srv/fai/nfsroot
+  TFTPROOT=/srv/tftp/fai
+
+  # test if our copy of setup_tftp has changed in fai-make-nfsroot,
+  # and if not, run it.
+  setup_tftp(){
 
-service tftpd-hpa restart
+      # tftp environment
+      local pxebin
+
+      # wheezy path
+      if [ -f $NFSROOT/usr/lib/PXELINUX/pxelinux.0 ]; then
+        pxebin=$NFSROOT/usr/lib/PXELINUX/pxelinux.0
+      else
+         # jessie/stretch path
+          pxebin=$NFSROOT/usr/lib/syslinux/pxelinux.0
+      fi
+
+      rm -f $NFSROOT/boot/*.bak
+      mkdir -p $TFTPROOT/pxelinux.cfg
+      chmod a+r $NFSROOT/boot/initrd.img-* || die 9 "No initrd was created. Check the package name of the linux-image package in /etc/fai/NFSROOT."
+      cp -p $v $NFSROOT/boot/vmlinu?-* $NFSROOT/boot/initrd.img-* $TFTPROOT
+      cp -u $pxebin $TFTPROOT
+      if [ -f $NFSROOT/usr/lib/syslinux/modules/bios/ldlinux.c32 ]; then
+       cp -u $NFSROOT/usr/lib/syslinux/modules/bios/ldlinux.c32 $TFTPROOT
+      fi
+      if [ X$verbose = X1 ]; then
+       echo "TFTP environment prepared. Enable DHCP and start the TFTP daemon on root $TFTPROOT."
+      fi
+  }
+  diff -u <(type setup_tftp) <(cat <(sed -n '/^setup_tftp(){/,/^}/p' $(which fai-make-nfsroot) ) - <<'EOF' |bash
+type setup_tftp
+EOF
+)
+  setup_tftp
+
+  # -g causes skipping set_root_pw() in fai-make-nfsroot, -ag
+  # is the only way to make it run without chrooting. the options
+  # seem contradictory, but it works.
+  fai-setup -evag
+
+else
+    e fai-setup -e -vf
+    # make the faiserver also the apt proxy server
+    apt-get -y install apt-cacher-ng
+fi
+
+{ head -n 1 /srv/fai/nfsroot/root/.ssh/known_hosts | awk '{print $1}' \
+    | tr '\n' ' '; ssh-keyscan localhost |& grep -o "ecdsa-sha2-nistp256.*"; \
+  } >>/srv/fai/nfsroot/root/.ssh/known_hosts
 
 # initially did the basic fai-chboot -Iv $std_arg default
 # but found in console that it wanted to mount nfsroot
@@ -59,36 +240,24 @@ service tftpd-hpa restart
 
 # Add debug to -f flag for more verbose output.
 
-std_arg="-u nfs://faiserver/srv/fai/config"
-fai-chboot -Iv $std_arg default # reset so we are idempotent
-kernel=$(fai-chboot -L '^default$' | awk '{print $3}')
-type -t host &>/dev/null || apt-get -y install dnsutils
-my_ip=$(host faiserver $(route -n | sed -rn 's/^(0\.){3}0\s+(\S+).*/\2/p') | \
-               sed -rn 's/^\S+ has address //p')
-k_args=$(fai-chboot -L '^default$' | \
-                sed -r "s/^(\S+\s+){3}(.*root=)(.*)/\2$my_ip:\3/")
-e fai-chboot -k "$k_args" -v -f verbose,sshd,createvt,reboot $std_arg $kernel default
-
-# make the faiserver also the apt proxy server
-apt-get -y install apt-cacher-ng
 
 # background on choosing apt-cacher-ng:
 # googling around a bit finds 2 main solutions:
 # http://askubuntu.com/questions/3503/best-way-to-cache-apt-downloads-on-a-lan
-# apt-cacher-ng doesn't have zeroconf.
-# It touts having minimal dependencies, but I don't care.
-# The downside to squid-deb-proxy is that it's config is for specific repos,
+# apt-cacher-ng doesn\'t have zeroconf.
+# It touts having minimal dependencies, but I don\'t care.
+# The downside to squid-deb-proxy is that it\'s config is for specific repos,
 # you have to add all the repos you use.
 # That is the main reason I use apt-cacher-ng.
 # It has a web portal, at http://faiserver:3142/acng-report.html
 
 
 # random fai note: as far as I can tell, profiles are just for putting
-# in a selectable boot menu, which I don't want.
+# in a selectable boot menu, which I don\'t want.
 
 # the logsave prompted because the hostname faiserver was uknown.
 # Here it was faiserver.lan when running from a faiserver vm.
 # When running from a normal host with faiserver alias, it was the normal hosts name.
-sed -ri 's/(^[^,]+,)\S+/\1faiserver/' /srv/fai/nfsroot/root/.ssh/known_hosts
+$sed 's/(^[^,]+,)\S+/\1faiserver/' /srv/fai/nfsroot/root/.ssh/known_hosts
 # ditch the logo banner up top which screws with less.
 touch /srv/fai/nfsroot/.nocolorlogo