From e729557712e9355ffa9103e6083d1161d5fd6063 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Thu, 13 Oct 2016 16:10:52 -0700 Subject: [PATCH] various fixes --- fai/config/class/50-host-classes | 11 ++ .../distro-install-common/libreboot_grub.cfg | 6 +- fresize | 114 +++++++++++------- live-kexec | 26 ++++ myfai-chboot | 8 +- myfai-chboot-local | 2 +- pxe-server | 2 - ubuntu-xenial-live-fai-kexec | 13 -- 8 files changed, 123 insertions(+), 59 deletions(-) create mode 100644 live-kexec delete mode 100644 ubuntu-xenial-live-fai-kexec diff --git a/fai/config/class/50-host-classes b/fai/config/class/50-host-classes index 49fa2fa..4f776f5 100755 --- a/fai/config/class/50-host-classes +++ b/fai/config/class/50-host-classes @@ -27,6 +27,17 @@ # to have a class of either DEBIAN + (STABLE or STRETCH64), # or UBUNTU + XENIAL64. # +# Other notable classes: +# +# REPARTITION: we try to reuse partitions/filesystems to install a new +# os into a multi-os system, if we see some basic hueristics, like the +# right amount of them. This overrides that. +# +# PARTITION_PROMPT: If we don't see partitions to reuuse, prompt +# to make sure we really want to repartition and use a completely +# fresh install. I use this in case our repartition check has +# a bug in it, or I accidentally set REPARTITION. +# # It's shell looks like this: # if [[ ! -e /a/bin/fai/fai-wrapper ]]; then # case $HOSTNAME in diff --git a/fai/config/distro-install-common/libreboot_grub.cfg b/fai/config/distro-install-common/libreboot_grub.cfg index b48ea52..32ab392 100644 --- a/fai/config/distro-install-common/libreboot_grub.cfg +++ b/fai/config/distro-install-common/libreboot_grub.cfg @@ -10,7 +10,6 @@ function save_vars { } function save_chosen { - last_boot=$CHOSEN save_vars did_fai_check last_boot } @@ -37,6 +36,10 @@ did_fai_check=false bs_dir=/debianstable_bootstrap menuentry $bs_dir --id=$bs_dir { + # note, we might be able to use $chosen and avoid setting this here, + # and set it inside save_chosen. but I haven't tested it, + # it's just one less line of repitition. + last_boot=$1 save_chosen configfile $bs_dir/boot/grub/grub.cfg } @@ -46,6 +49,7 @@ for dir in /boot_*; do break fi menuentry $dir --id=$dir { + last_boot=$1 save_chosen configfile $1/grub/grub.cfg } diff --git a/fresize b/fresize index f41e6fb..4509823 100755 --- a/fresize +++ b/fresize @@ -52,13 +52,25 @@ usage() { cat <. - --n Dry run --r Reboot now if it's needed. +Assuming Ian Kelling's partition scheme and we are currently into one of +it's encrypted oses (we it's btrfs filesystem to be mounted), resize +swap or boot, expanding or shrinking the root fs and partition to +compensate. If it changes the partition tables incorrectly, the +originals are stored in /root/backup_partition_table_. + +TODO: only tested on stretch. deactivation of swap on reboot +probably needs to be fixed on other oses. Even on stretch, +we get 1.5 minutes of waiting for the crypt_dev and normal +boot .device units. + +Warning!!! Backup your data. This script could have bugs in it. + +-n Dry run. Note, this likely won't be the exact commands, + for example, if you are running outside a vm, there will + probably be a reboot required in the middle so the kernel + can know about partition changes. +-r Reboot right away if it's needed. +-f Force running on a distro that has not been tested. -h|--help Print help and exit. SIZE is MiB, or if g is specified, GiB. @@ -73,13 +85,15 @@ EOF reboot_not=false dry_run=false +force=false -temp=$(getopt -l help rnh "$@") || usage 1 +temp=$(getopt -l help rnfh "$@") || usage 1 eval set -- "$temp" while true; do case $1 in -r) reboot_now=true; shift ;; -n) dry_run=true; shift ;; + -f) force=true; shift ;; -h|--help) usage ;; --) shift; break ;; *) echo "$0: Internal error!" ; exit 1 ;; @@ -105,6 +119,11 @@ fi case $2 in swap|boot) : ;; *) echo "$0: error: bad 2nd arg"; usage 1 ;; esac +if ! $force && ! grep -q 'VERSION=.*stretch' /etc/os-release; then + echo "$0: error: This distro is untested. Only tested version atm is Stretch." + exit 1 +fi + #### end arg error checking #### @@ -115,8 +134,8 @@ op_size=$1 # operator plus size if [[ $op_size == *g ]]; then op_size=${op_size%g} size=${op_size#?} - op=${op_size%size} - size=$(( ${size} * 1024 )) + op=${op_size%$size} + size=$(( $size * 1024 )) op_size=$op$size else size=${op_size#?} @@ -152,8 +171,10 @@ pmk() { # partition make # will remain in use. You should reboot now before making further # changes. - p mkpart primary "$fs_type" \ - $((${ptable[start$part]} $start_op)) $((${ptable[end$part]} $end_op)) ||: + if ! p mkpart primary "$fs_type" \ + $((${ptable[start$part]} $start_op)) $((${ptable[end$part]} $end_op)); then + echo "$0: warning: ignoring failure return of mkpart" + fi } def-e() { @@ -165,19 +186,30 @@ def-e() { } def-e -e swapoff -a + while read devid dev; do - if [[ $dev != /dev/mapper/* ]]; then - # older oses, it points to /dev/dm-x - dev=$(dmsetup info $dev | sed -rn 's/^\s*Name:\s*(\S*)/\1/p') - else - dev=${dev#/dev/mapper/} + case $dev in + /dev/dm-[0-9]) + # older oses, it points to /dev/dm-x + dev=$(dmsetup info $dev | sed -rn 's/^\s*Name:\s*(\S*)/\1/p') + ;; + /dev/mapper/*) + dev=${dev#/dev/mapper/} + ;; + *) + echo "$0: error: could not find devicemapper root dev, +make sure you are running from a encrypted root this script is resizing" + exit 1 + ;; + esac + if [[ $dev != crypt_dev_*-part$rootn ]]; then + echo "$0: error: unexpected root device name, +make sure you are running from a encrypted root this script is resizing" + exit 1 fi dev=${dev#crypt_dev_} - r='-part[0-9]*$' - [[ $dev =~ $r ]] ||: - dev=${dev%$BASH_REMATCH} + dev=${dev%-part$rootn} devpath=/dev/disk/by-id/$dev echo skip=$size def-e @@ -192,7 +224,9 @@ while read devid dev; do p() { e parted -a optimal -s -- $devpath unit MiB "$@"; } unit=systemd-cryptsetup@crypt_dev_$dev-part$swapn # note systemctl show can test if a unit exists. - e systemctl stop $unit || [[ $? == 5 ]] # fail due to not loaded + if ! e systemctl stop $unit; then + e swapoff -a + fi # there is a bug in jessie. this and the .swap unit are # generated from /etc/fstab, and it escapes - to x2d, then doesn't escape it # when looking for the file to use as swap. so, no swap is working on jessie @@ -223,24 +257,26 @@ while read devid dev; do grep "but we have been unable to inform the kernel" &>/dev/null; then needs_reboot=true fi - if ! $grow; then - if $needs_reboot; then - e systemctl mask dev-mapper-crypt_swap_$dev$swapn.swap - e systemctl mask systemd-cryptsetup@crypt_swap_$dev$swapn.service - e() { echo "$@" >> /root/finish-resize; } - if ! $reboot_script_initialized; then - reboot_script_initialized=true - rm -rf /root/finish-resize - cat >/root/finish-resize <<'EOF' + if $needs_reboot; then + # note: even if these units don't exist, this will succeed. + e systemctl mask dev-mapper-crypt_swap_$dev$swapn.swap + e systemctl mask systemd-cryptsetup@crypt_swap_$dev$swapn.service + e() { echo "$@" >> /root/finish-resize; } + if ! $reboot_script_initialized; then + reboot_script_initialized=true + rm -rf /root/finish-resize + cat >/root/finish-resize <<'EOF' #!/bin/bash -x set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR EOF - chmod +x /root/finish-resize - fi - e systemctl unmask systemd-cryptsetup@crypt_swap_$dev$swapn.service - e systemctl unmask dev-mapper-crypt_swap_$dev$swapn.swap + chmod +x /root/finish-resize fi + e swapoff -a + e systemctl unmask systemd-cryptsetup@crypt_swap_$dev$swapn.service + e systemctl unmask dev-mapper-crypt_swap_$dev$swapn.swap + fi + if ! $grow; then $root_resize_cmd fi if $boot; then @@ -294,12 +330,8 @@ if $boot; then fi if $needs_reboot; then - if ! $grow; then - echo "$0: Reboot, run /root/finish-resize. It's contents:" - cat /root/finish-resize - else - echo "$0: If you want to resize again later, a reboot is required first." - fi + echo "$0: Reboot, run /root/finish-resize. It's contents:" + cat /root/finish-resize if $reboot_now; then echo "$0: rebooting now" reboot now diff --git a/live-kexec b/live-kexec new file mode 100644 index 0000000..10975a8 --- /dev/null +++ b/live-kexec @@ -0,0 +1,26 @@ +#!/bin/bash +# +# You can copy this to a http server, then wget -O- url|sudo bash +# or curl|bash, if curl is installed. +# +# This has been tested on trisquel belanos and ubuntu xenial. +# +# If the screen just sits in a weird color inverted, corrupted looking state, +# it's probably nothing wrong with the computer, but a problem +# with the fai server. If you can do this from a virtual terminal, +# it will print out more info (I know from running it on a vm). + +set -ex +if grep -q ID=ubuntu /etc/os-release; then + sed -ri '/^\s*deb/{/universe/!s/$/ universe/}' /etc/apt/sources.list +fi +apt-get update +apt-get install -y debconf +debconf-set-selections </dev/null || apt-get -y install dnsutils gateway_ip=$(route -n | sed -rn 's/^(0\.){3}0\s+(\S+).*/\2/p') my_ip=$(host faiserver $gateway_ip | sed -rn 's/^\S+ has address //p') k_args=$(fai-chboot -L '^default$' | \ sed -r "s/^(\S+\s+){3}(.*root=)(.*)/\2$my_ip:\3/") +rm -f /srv/tftp/fai/pxelinux.cfg/* e fai-chboot -k "$k_args" -v -f verbose,sshd,createvt,reboot $std_arg $kernel "$@" diff --git a/pxe-server b/pxe-server index 6adbf19..4e4aaa8 100755 --- a/pxe-server +++ b/pxe-server @@ -143,10 +143,8 @@ if $set; then if $redep; then fai-redep fi - faiserver-enable else myfai-chboot - faiserver-disable fi fi diff --git a/ubuntu-xenial-live-fai-kexec b/ubuntu-xenial-live-fai-kexec deleted file mode 100644 index c44e9b2..0000000 --- a/ubuntu-xenial-live-fai-kexec +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash -# You can copy this to a http server, then curl url|sudo bash - -set -ex -sed -ri '/^\s*deb\s+http/s/$/ universe/' /etc/apt/sources.list -apt-get update -apt-get install -y debconf -debconf-set-selections <