summaryrefslogtreecommitdiff
path: root/sys-apps/personal-utils/files/gentoo-mount
blob: 812d7d5a6e573eb0cc1e45d4154d59d5112af7d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/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"