Skip to content

Instantly share code, notes, and snippets.

@Holzhaus
Last active August 29, 2015 14:19
Show Gist options
  • Save Holzhaus/94d3a4c73892e67f3ff0 to your computer and use it in GitHub Desktop.
Save Holzhaus/94d3a4c73892e67f3ff0 to your computer and use it in GitHub Desktop.
Simple pacman script to fix broken USB-Stick installations
#!/bin/bash
#
# This is a simple script I wrote to fix broken ArchLinuxARM installation
# on USB drives or SD cards. If you encountered a crash, power failure or
# kernel panic while updating the kernel, systemd or something like that
# and are unable to boot your device, you can simply plug the USB drive/
# SD card into your desktop computer and run this script.
# You can't simply chroot and run the script from there, because your
# desktop computer's architecture is probably x86/x86_64 and your single
# board computer is ARM-based.
#
# The script passes all command line arguments to pacman. Make sure to
# modify the $MOUNTED_ROOT to match your mount point.
#
# Note: You'll possibly encounter execv errors if you use this script on
# a i686/AMD64 computer to fix an ArchLinuxARM installation, but it worked
# anyway.
#
# Example Usage:
# sudo ./pacman-mount.sh -Syu linux-kirkwood
#
MOUNTED_ROOT="/run/media/myuser/usb-drive" # No trailing slash!
if [ -z "${MOUNTED_ROOT}" ]; then
echo "MOUNTED_ROOT variable is empty!"
exit 1
fi
if [ ! -a "${MOUNTED_ROOT}/etc/pacman.conf" ]; then
echo "File '${MOUNTED_ROOT}/etc/pacman.conf' does not exist!"
exit 1
fi
if [ ! -w "${MOUNTED_ROOT}/etc/pacman.conf" ]; then
echo "File '${MOUNTED_ROOT}/etc/pacman.conf' is not writable!"
exit 1
fi
# Update paths in /etc/pacman.conf
sed -i 's#Include = ${MOUNTED_ROOT}/etc/pacman.d#Include = /etc/pacman.d#g' "${MOUNTED_ROOT}/etc/pacman.conf"
# Run the actual pacman command
pacman --root "${MOUNTED_ROOT}" --dbpath "${MOUNTED_ROOT}/var/lib/pacman" --cachedir "${MOUNTED_ROOT}/var/cache/pacman/pkg" --config "${MOUNTED_ROOT}/etc/pacman.conf" "$@"
# Restore original paths in /etc/pacman.conf
sed -i 's#Include = /etc/pacman.d#Include = ${MOUNTED_ROOT}/etc/pacman.d#g' "${MOUNTED_ROOT}/etc/pacman.conf"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment