From 89dbeeb59d6677405ee08f8f693adf9e950f2603 Mon Sep 17 00:00:00 2001 From: Gardouille Date: Mon, 19 Oct 2020 16:24:30 +0200 Subject: [PATCH] Exit on failed parted commands --- debian/chroot.install | 6 +-- debian/chroot.mount | 113 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 3 deletions(-) create mode 100755 debian/chroot.mount diff --git a/debian/chroot.install b/debian/chroot.install index 3eccc6e..8a33383 100755 --- a/debian/chroot.install +++ b/debian/chroot.install @@ -54,12 +54,12 @@ if [ "${manage_part}" -eq 0 ]; then ## Recreate partition (/boot and LV) {{{ ### Partition type - parted "${hdd}" mklabel msdos + parted "${hdd}" mklabel msdos || exit 1 ### /boot - parted "${hdd}" mkpart primary 0% 512MB + parted "${hdd}" mkpart primary 0% 512MB || exit 1 parted "${hdd}" set 1 boot on ### LV - parted "${hdd}" mkpart primary 512MB 100% + parted "${hdd}" mkpart primary 512MB 100% || exit 1 parted "${hdd}" set 2 lvm on sudo pvcreate "${hdd}"2 sudo vgcreate "${vgname}" "${hdd}"2 diff --git a/debian/chroot.mount b/debian/chroot.mount new file mode 100755 index 0000000..96cf48a --- /dev/null +++ b/debian/chroot.mount @@ -0,0 +1,113 @@ +#!/bin/sh + +# Vars +## Define the hard drive to use +if [ -b '/dev/sda' ]; then + hdd="/dev/sda" +else + printf '%b\n' "Please check the hard drive to use" + exit 0 +fi + +## If empty, the script will try to get one with nslookup +new_hostname="" +## Volume Group name to use for LVM +vgname="${new_hostname}vg" +## If the script should create extra volume (eg. backup, virt, Proxmox,…) +manage_extra_lv=0 +## Colors definition {{{ +BLACK='\033[49;30m' +BLACKB='\033[49;90m' +RED='\033[0;31m' +REDB='\033[1;31m' +GREEN='\033[0;32m' +YELLOW='\033[0;33m' +BLUE='\033[94;49m' +MAGENTA='\033[0;35m' +CYAN='\033[36;49m' +WHITE='\033[0;37m' +BOLD='\033[1m' +RESET='\033[0m' +## }}} + +## Mount the system {{{ +### Root +mkdir -p -- /target +mountpoint -q /target || mount -- /dev/mapper/"${vgname}"-root /target + +### boot - grub +mountpoint -q /target/boot || mount -- ${hdd}1 /target/boot + +#### home LV +mountpoint -q /target/home || mount -- /dev/mapper/"${vgname}"-home /target/home +#### opt LV +mountpoint -q /target/opt || mount -- /dev/mapper/"${vgname}"-opt /target/opt +#### srv LV +mountpoint -q /target/srv || mount -- /dev/mapper/"${vgname}"-srv /target/srv +#### tmp LV +mountpoint -q /target/tmp || mount -- /dev/mapper/"${vgname}"-tmp /target/tmp +#### usr LV +mountpoint -q /target/usr || mount -- /dev/mapper/"${vgname}"-usr /target/usr +#### var LV +mountpoint -q /target/var || mount -- /dev/mapper/"${vgname}"-var /target/var + +if [ "${manage_extra_lv}" -eq 0 ]; then + ### Extra bkp LV + mountpoint -q /target/srv/backup || mount -- /dev/mapper/"${vgname}"-bkp /target/srv/backup + ### Extra vz LV + mountpoint -q /target/var/lib/vz || mount -- /dev/mapper/"${vgname}"-vz /target/var/lib/vz +fi + +### Swap +swapon -- /dev/mapper/"${vgname}"-swap +## }}} +## Ensure to (re)mount devices for chroot {{{ +mkdir -p -- /target/dev +mountpoint -q /target/dev || mount -t devtmpfs -- none /target/dev +mkdir -p -- /target/dev/pts +mountpoint -q /target/dev/pts || mount -t devpts -- /dev/pts /target/dev/pts +mkdir -p -- /target/proc +mountpoint -q /target/proc || mount -t proc -- none /target/proc +mkdir -p -- /target/sys +mountpoint -q /target/sys || mount -t sysfs -- none /target/sys +### FIXME : /run/lvm needs to be manually set in debootstrap|chroot for Buster {{{ +### See : +### https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=918590 +### https://bbs.archlinux.org/viewtopic.php?pid=1820949#p1820949 +mkdir -p -- /target/run/lvm +mountpoint -q /target/run/lvm || mount --bind -- /run/lvm /target/run/lvm +mkdir -p -- /target/run/udev +mountpoint -q /target/run/udev || mount --bind -- /run/udev /target/run/udev +### }}} +## }}} + +## Network {{{ +### Get all informations from current network configuration in rescue mode +net_device=$(ip r | grep "^default" | head -1 | cut -d" " -f5) +#### TODO: Switch to ip a to get ip address +net_address=$(ip r | grep -vE "(^default|metric)" | grep "${net_device}.*src" | head -1 | awk -F" " '{print $NF}') +read -r net_mac_address /tmp/50-default.network + +## }}} + +printf '%b\n' "${GREEN}The system is available on /target you can now try to chroot.${RESET}" + +exit 0