#!/bin/sh set -e DIR= DEV_ROOT= while [ $# -gt 0 ]; do case "$1" in -h|--help) echo "Usage: gentoo-mount [OPTION]... DIR" echo "Simple script for mounting chrootable gentoo system to given" echo "directory." echo echo "Options:" echo " -h, --help" echo " Print this help text." echo " -s, --sys-only" echo " Mount only system paths to already given prepared" echo " directory." exit 0 ;; *) if [ -z "$DIR" ]; then DIR="$1" else echo "Unknown argument: $1" >&2 exit 1 fi ;; esac shift done if [ $(id -u) != 0 ]; then echo "This script has to be run only as root." >&2 exit 1 fi if [ ! -d "$DIR" ]; then echo "No DIR specified or no such directory exists: $DIR" >&2 exit 1 fi # Interactive mount # Usage: imount DEVICE PATH OPTIONS NAME # DEVICE: device to mount. If not provided (empty) then ask user. # PATH: path where mount will be mounted. # OPTIONS: mount options (mount -o) # NAME: optional name to be used for mapper imount() { if [ -n "$1" ]; then return mount -o "$3" "$1" "$2" fi echo -e "\e[1;32mPlease choose device\e[0m" lsblk -o NAME,LABEL,SIZE,FSTYPE,MOUNTPOINT while true; do echo -n "Device: /dev/" read DEVICE if [ ! -b "/dev/$DEVICE" ]; then echo -e "No such device: /dev/$DEVICE" >&2 else break fi done # Check if it's Luks partition and open it if it's if cryptsetup isLuks "/dev/$DEVICE"; then local CSNAME="$4" while [ -e "/dev/mapper/$CSNAME" ]; do CSNAME="${CSNAME}2" done echo -e "\e[1;32mOpening encrypted filesystem\e[0m" cryptsetup open "/dev/$DEVICE" "$CSNAME" DEVICE="mapper/$CSNAME" fi local OPTIONS="$3" # Specially handle btrfs if [ -n "$(blkid -t TYPE=btrfs "/dev/$DEVICE")" ]; then if [ -n "$OPTIONS" ]; then OPTIONS="$OPTIONS,compress=lzo" else OPTIONS="compress=lzo" fi local TMP="$(mktemp -d)" mount "/dev/$DEVICE" "$TMP" while true; do echo -e "\e[1;32mPlease choose subvolume\e[0m" btrfs subvolume list -t "$TMP" # TODO default value? Probably same as name? echo -n "Subvolume: " read SUBVOL if btrfs subvolume list "$TMP" | grep -q "$SUBVOL"; then OPTIONS="$OPTIONS,subvol=$SUBVOL" break fi done umount "$TMP" rmdir "$TMP" fi # And at last mount mount -o "$OPTIONS" "/dev/$DEVICE" "$2" } # Mount root filesystem ########################################################## echo -e "\e[1;34mMount root filesystem\e[0m" imount "$DEV_ROOT" "$DIR" "" root # Mount system paths ############################################################# echo -e "\e[1;34mMount system paths\e[0m" mount -t proc /proc "$DIR/proc" mount --rbind /dev "$DIR/dev" mount --rbind /sys "$DIR/sys" # Copy current resolv.conf ####################################################### echo -e "\e[1;34mCopy system resolv.conf\e[0m" cp /etc/resolv.conf "$DIR/etc/resolv.conf" # Mount boot partition ########################################################### echo -e "\e[1;34mMount boot partition\e[0m" imount "$DEV_BOOT" "$DIR/boot" # Mount home partition ########################################################### echo -e "\e[1;34mMount home partition\e[0m" imount "$DEV_HOME" "$DIR/home"