aboutsummaryrefslogtreecommitdiffstats
path: root/main/mkinitfs/mount-move.patch
blob: 4931663246bf3635a79dba0ba6bfa8e8e9e74db7 (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
diff --git a/initramfs-init.in b/initramfs-init.in
index 2ff2585..f64ef26 100755
--- a/initramfs-init.in
+++ b/initramfs-init.in
@@ -104,10 +104,32 @@ unpack_apkovl() {
 }
 
 # find mount dir for given device in an fstab
+# returns global MNTOPTS
 find_mnt() {
-	local dev="$1"
-	local fsfile="$2"
-	awk "\$1 == \"$dev\" {print \$2}\"" "$fsfile" 2>/dev/null
+	local search_dev="$1" fstab="$2"
+	MNTOPTS=
+	[ -r "$fstab" ] || return 1
+	local dev mnt fs chk
+	case "$search_dev" in
+		UUID=*|LABEL=*|/dev/*);;
+		*) search_dev=/dev/$search_dev;;
+	esac
+	local search_real_dev=$(resolve_dev $search_dev)
+	while read dev mnt fs MNTOPTS chk; do
+		local real_dev=$(resolve_dev $dev)
+		local i j
+		for i in "$search_dev" "$search_real_dev"; do
+			[ -z "$i" ] && continue
+			for j in "$dev" "$real_dev"; do
+				[ -z "$j" ] && continue
+				if [ "$i" = "$j" ]; then
+					echo "$mnt"
+					return
+				fi
+			done
+		done
+	done < $fstab
+	MNTOPTS=
 }
 
 # Wait for usb to settle
@@ -214,6 +236,29 @@ start_lvm() {
 		lvm vgchange --ignorelockingfailure -a y >/dev/null 2>&1
 }
 
+# resolve an uuid or symlink to the real device
+resolve_dev() {
+	case "$1" in
+	UUID=*|LABEL=*) findfs "$1";;
+	*) readlink -f "$1";;
+	esac
+}
+
+# remount ALPINE_MNT according given fstab
+remount_alpine_mnt() {
+	local fstab="$1"
+	local mnt=$(find_mnt $ALPINE_DEV $fstab)
+	if [ "$ALPINE_MNT" != "$mnt" ]; then
+		mkdir -p "$mnt"
+		mount -o move $ALPINE_MNT $mnt
+		ALPINE_MNT=$mnt
+	fi
+	# respect users mount options in fstab
+	if [ -n "$MNTOPTS" ]; then
+		mount -o remount,$MNTOPTS "$ALPINE_MNT"
+	fi
+}
+
 # gotta start from somewhere :)
 echo "Alpine Init $VERSION"
 
@@ -270,7 +315,7 @@ if [ -n "$KOPT_ovl_dev" ] ; then
 fi
 
 case "$ALPINE_DEV" in
-	UUID=*) ;;
+	UUID=*|LABEL=*) ;;
 	*) ALPINE_DEV=/dev/$ALPINE_DEV ;;
 esac
 	
@@ -453,20 +498,9 @@ pkgs="$pkgs alpine-base"
 
 # move the ALPINE_MNT if ALPINE_DEV is specified in users fstab
 # this is so a generated /etc/apk/repositories will use correct mount dir
-new_mnt=$(find_mnt $ALPINE_DEV $sysroot/etc/fstab)
-if [ -n "$new_mnt" ] && [ "$new_mnt" != "$ALPINE_MNT" ]; then
-	mkdir -p $new_mnt
-	mount -o move $ALPINE_MNT $new_mnt
-	ALPINE_MNT="$new_mnt"
-fi
-
-# let user set ALPINE_MNT as readonly in fstab
-if [ -f $sysroot/etc/fstab ]; then
-	mountopts=$(awk "\$2 == \"$ALPINE_MNT\" { print \$4 }" \
-		$sysroot/etc/fstab)
-	if [ -n "$mountopts" ]; then
-		mount -o remount,$mountopts $ALPINE_MNT
-	fi
+if [ -f "$sysroot"/etc/fstab ]; then
+	has_fstab=1
+	remount_alpine_mnt "$sysroot"/etc/fstab
 fi
 
 # copy keys so apk finds them. apk looks for stuff relative --root
@@ -503,6 +537,11 @@ else
 fi
 eend $?
 
+# remount ALPINE_MNT according default fstab from package
+if [ -z "$has_fstab" ] && [ -f "$sysroot"/etc/fstab ]; then
+	remount_alpine_mnt "$sysroot"/etc/fstab
+fi
+
 # fix inittab if alternative console
 setup_inittab_console $CONSOLE