Skip to content

Instantly share code, notes, and snippets.

@espoelstra
Last active November 14, 2020 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save espoelstra/79a00f372cf6adb815b2815c2fbee215 to your computer and use it in GitHub Desktop.
Save espoelstra/79a00f372cf6adb815b2815c2fbee215 to your computer and use it in GitHub Desktop.
Script for mounting chrx partition for editing grub options or manipulating other packages
#!/bin/bash
# Manual Grub fixing for Chrx
# This exports any variables set between -a/+a
# while the := allows passing in an alternate path to mount/
# Temporarily stealing /dev/sd* /dev/mmcblk* /dev/nvme* case from Chrx functions to print partitions properly
set -a
: ${CHRX_INSTALL_ROOT:=/tmp/chrxroot} \
${CHRX_ROOT_DISK:=$(rootdev -s -d)} \
${CHRX_TARGET_DISK:=$(rootdev -s -d)}
set +a
get_disk_plus_partition() {
pnum=$1
case "$CHRX_TARGET_DISK" in
/dev/sd*) echo "${CHRX_TARGET_DISK}${pnum}" ;;
/dev/mmcblk*) echo "${CHRX_TARGET_DISK}p${pnum}" ;;
/dev/nvme*) echo "${CHRX_TARGET_DISK}p${pnum}" ;;
*) echo_fail "\nunrecognized disk device: \"$CHRX_TARGET_DISK\"!"; exit 1 ;;
esac
}
# Export this so we can call it from subshells to capture values instead of setting globals
export -f get_disk_plus_partition
: ${CHRX_ROOT_PARTITION:=$(get_disk_plus_partition 7)} \
${CHRX_EFI_PARTITION:=$(get_disk_plus_partition 12)}
# First is default to match installer
# Second and third assume we are fixing a chroot on the internal disk
sudo mkdir -p ${CHRX_INSTALL_ROOT}
sudo mount ${CHRX_ROOT_PARTITION} ${CHRX_INSTALL_ROOT}
sudo mount -o bind /proc ${CHRX_INSTALL_ROOT}/proc
sudo mount -o bind /dev ${CHRX_INSTALL_ROOT}/dev
sudo mount -o bind /dev/pts ${CHRX_INSTALL_ROOT}/dev/pts
sudo mount -o bind /sys ${CHRX_INSTALL_ROOT}/sys
sudo mount -o bind /run ${CHRX_INSTALL_ROOT}/run
sudo mkdir -p ${CHRX_INSTALL_ROOT}/run/resolvconf
sudo mkdir -p ${CHRX_INSTALL_ROOT}/run/systemd/resolve
cat /etc/resolv.conf | sudo tee ${CHRX_INSTALL_ROOT}/etc/resolv.conf
sudo mount ${CHRX_EFI_PARTITION} ${CHRX_INSTALL_ROOT}/efi
sudo chroot ${CHRX_INSTALL_ROOT} /bin/bash
: <<-'EOC'
# This block comment is the snippet of chrx-install-chroot that I've been tweaking to get a proper EFI booting grub
ESP_DIR=/efi
mkdir -p ${ESP_DIR}/grub/
mount ${CHRX_TARGET_DISK}p12 ${ESP_DIR}
grub-install --target=x86_64-efi --efi-directory=${ESP_DIR} --bootloader-id=GRUB --removable --boot-directory=${ESP_DIR}
grub-mkconfig -o ${ESP_DIR}/grub/grub.cfg
EOC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment