Skip to content

Instantly share code, notes, and snippets.

@argraur
Created March 27, 2020 16:35
Show Gist options
  • Save argraur/068b9f82e9dc79b90efc0f60875e42ad to your computer and use it in GitHub Desktop.
Save argraur/068b9f82e9dc79b90efc0f60875e42ad to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
# Never allow running as user
if test "$(whoami)" != "root"; then echo "Run as root!"; exit 1; fi
export ARCH_ROOT=/archlinux
export ARCH_CHROOT=${ARCH_ROOT}/usr/bin/arch-chroot
function mount_drives() {
for x in $(ls /mnt); do echo "Mounting drive ${x^^}: to ${ARCH_ROOT}/mnt/${x}"; mkdir ${ARCH_ROOT}/mnt/${x}; mount -o bind /mnt/${x} ${ARCH_ROOT}/mnt/${x}; done
}
function umount_drives() {
for x in $(ls /mnt); do if test -d /archlinux/mnt/${x}; then echo "Unmounting drive ${x^^}:"; umount ${ARCH_ROOT}/mnt/${x}; rm -rv ${ARCH_ROOT}/mnt/${x}; fi; done
}
function attach() {
if test -e "/run/arch"; then detach; fi
export ARCH_DISK=${1}
echo "Attaching ${ARCH_DISK}..."
echo $(losetup -f -P --show ${1}) > /run/arch
}
function detach() {
if test ! -e "/run/arch"; then echo "Disk is not attached!"; exit 1; fi
export ARCH_LOOP=$(cat /run/arch)
echo "Detaching ${ARCH_LOOP}..."
losetup -d ${ARCH_LOOP}
rm -v /run/arch
}
function arch_swapoff() {
if test -n "$(cat /proc/swaps | grep $(cat /run/arch))"; then echo "Turning off ArchLinux swap..."; swapoff $(cat /run/arch)p2; fi
}
function arch_umount() {
if test ! -e /run/arch; then echo "ArchLinux is not mounted!"; exit 1; fi
export ARCH_ROOT=/archlinux
export LOOP_DEVICE=$(cat /run/arch)
export ARCH_ROOT_PART=${LOOP_DEVICE}p1
export ARCH_SWAP=${LOOP_DEVICE}p2
umount_drives
echo "Unmounting ArchLinux fs partition..."
umount ${ARCH_ROOT}
rm -rv ${ARCH_ROOT}
arch_swapoff
detach
}
function arch_mount {
export ARCH_DISK=${1}
if test ! -e /run/arch && test -n "${ARCH_DISK}"; then echo "Found ArchLinux GPT disk: ${ARCH_DISK}"; attach ${ARCH_DISK};
else if test -n "${ARCH_DISK}"; then echo "Found ArchLinux GPT disk: ${ARCH_DISK}"; attach ${ARCH_DISK}; else if test ! -e /run/arch; then exit 1; fi fi; fi
export LOOP_DEVICE=$(cat /run/arch)
export ARCH_ROOT_PART=${LOOP_DEVICE}p1
export ARCH_SWAP=${LOOP_DEVICE}p2
echo "Mounting ArchLinux fs partition ${ARCH_ROOT_PART} on ${ARCH_ROOT}..."
mkdir ${ARCH_ROOT}
mount -t ext4 ${ARCH_ROOT_PART} ${ARCH_ROOT}
}
function arch_shell() {
${ARCH_CHROOT} ${ARCH_ROOT}
}
function arch_swapon() {
echo "Enabling ArchLinux swap..."
export LOOP_DEVICE=$(cat /run/arch)
export SWAP=${LOOP_DEVICE}p2
swapon ${SWAP}
}
function arch_start() {
echo "Starting ArchLinux..."
arch_mount ${1}
arch_swapon
arch_shell
arch_umount
}
function arch_status() {
echo -n "Arch root exists: "; if test -d /archlinux; then echo "true"; else echo "false"; fi
echo -n "Arch root mounted: "; if test -n "$(mount | grep '/archlinux')"; then echo "true"; export ARCH_MOUNTED="true"; else echo "false"; fi
echo -n "Arch disk attached: "; if test -n "$(losetup | grep 'arch')"; then echo "true"; else echo "false"; fi
echo -n "Arch swap on: "; if test -e "/run/arch"; then if test -n "$(cat /proc/swaps | grep $(cat /run/arch))"; then echo "true"; else echo "false"; fi; else echo "false"; fi
echo -n "Packages installed: "; if test "${ARCH_MOUNTED}" == "true"; then chroot /archlinux pacman -Qq | wc -l; fi
}
function arch_help() {
echo " arch start [disk image path] - Attaches disk, mounts Arch root, enables Arch swap and chroots into Arch system"
echo " arch mount [disk image path] - Attaches disk and mounts Arch root"
echo " arch umount [loop device] - Unmounts Arch and detaches disk"
echo " arch chroot - Chroots into Arch root"
echo " arch mount_drives - Mounts Windows drives to Arch"
echo " arch umount_drives - Unmounts Windows drives from Arch"
echo " arch status - Shows Arch status"
echo " arch help - Shows usage"
echo " arch --help - Shows usage"
}
case "$1" in
"attach") attach ${2} ;;
"detach") detach ;;
"start") arch_start ${2} ;;
"mount") arch_mount ${2} ;;
"umount") arch_umount ;;
"mount_drives") mount_drives ;;
"umount_drives") umount_drives ;;
"swapon") arch_swapon ;;
"swapoff") arch_swapoff ;;
"chroot") arch_shell ;;
"status") arch_status ;;
"help") arch_help ;;
"--help") arch_help ;;
*) arch_help ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment