summaryrefslogtreecommitdiff
path: root/sys-boot/os-prober/files/os-prober-btrfsfix.patch
blob: bb5e6c03d0f379f9a4715b3b1a458fa52984b471 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
Index: os-prober/common.sh
===================================================================
--- os-prober.orig/common.sh
+++ os-prober/common.sh
@@ -155,6 +155,7 @@ parse_proc_mounts () {
 	done
 }
 
+# add forth parameter to pickup btrfs subvol info
 parsefstab () {
 	while read -r line; do
 		case "$line" in
@@ -165,12 +166,22 @@ parsefstab () {
 				set -f
 				set -- $line
 				set +f
-				printf '%s %s %s\n' "$1" "$2" "$3"
+				printf '%s %s %s %s\n' "$1" "$2" "$3" "$4"
 			;;
 		esac
 	done
 }
 
+#check_btrfs_mounted $bootsv $bootuuid)
+check_btrfs_mounted () {
+	bootsv="$1"
+	bootuuid="$2"
+	bootdev=$(blkid | grep "$bootuuid" | cut -d ':' -f  1)
+	bindfrom=$(grep " btrfs " /proc/self/mountinfo | 
+		   grep " $bootdev " | grep " /$bootsv " | cut -d ' ' -f 5)
+	printf "%s" "$bindfrom"
+}
+
 unescape_mount () {
 	printf %s "$1" | \
 		sed 's/\\011/	/g; s/\\012/\n/g; s/\\040/ /g; s/\\134/\\/g'
Index: os-prober/linux-boot-prober
===================================================================
--- os-prober.orig/linux-boot-prober
+++ os-prober/linux-boot-prober
@@ -5,16 +5,143 @@ set -e
 
 newns "$@"
 require_tmpdir
+ERR="n"
+
+tmpmnt=/var/lib/os-prober/mount
+if [ ! -d "$tmpmnt" ]; then
+	mkdir "$tmpmnt"
+fi
+
+mounted=
+bootmnt=
+bootsv=
+bootuuid=
 
 grep "^/dev/" /proc/mounts | parse_proc_mounts >"$OS_PROBER_TMP/mounted-map" || true
 
-partition="$1"
+if [ -z "$1" ]; then
+	ERR=y
+elif [ "$1" = btrfs -a -z "$2" ]; then
+	ERR=y
+elif [ "$1" = btrfs -a -z "$3" ]; then
+	ERR=y
+elif [ "$1" = btrfs ]; then
+	type=btrfs
+	echo "$2" | grep -q "^UUID=" || ERR=y
+	echo "$3" | grep -q "^subvol=" || ERR=y	
+	export "$2"
+	export "$3"
+	partition=$(blkid | grep "$UUID" | cut -d ':' -f 1 | tr '\n' ' ' | cut -d ' ' -f 1)
+	debug "btrfs: partition=$partition, UUID=$UUID, subvol=$subvol"
+else
+	partition="$1"
+	type=other
+fi
 
-if [ -z "$partition" ]; then
+if [ "x$ERR" != xn ]; then
 	echo "usage: linux-boot-prober partition" >&2
+	echo "       linux-boot-prober partition | btrfs UUID=<> subvol=<>" >&2
 	exit 1
 fi
 
+if [ "$type" = btrfs ]; then
+	# handle all of the btrfs stuff here
+	if [ ! -e "/proc/self/mountinfo" ]; then
+		warn "/proc/self/mountinfo does not exist, exiting"
+		umount "$tmpmnt" 2>/dev/null
+		rmdir "$tmpmnt" 2>/dev/null
+		exit 1
+	fi
+	mpoint=$(grep "btrfs" /proc/self/mountinfo | grep " /$subvol " | grep " $partition " | cut -d ' ' -f 5)
+	if [ "$mpoint" = "/" ]; then
+		warn "specifying active root not valid, exiting"
+		umount "$tmpmnt" 2>/dev/null
+		rmdir "$tmpmnt" 2>/dev/null
+		exit 1
+	fi
+	if [ "$mpoint" = "$tmpmnt" ]; then
+		warn "btrfs subvol=$subvool, UUID=$UUID, already mounted on $tmpmnt **ERROR**"
+		umount "$tmpmnt" 2>/dev/null
+		rmdir "$tmpmnt" 2>/dev/null
+		exit 1
+	fi
+	if [ -z "$mpoint" ]; then
+		# mount the btrfs root
+		if ! mount -o subvol=$subvol -t btrfs -U $UUID "$tmpmnt" 2>/dev/null; then
+			warn "error mounting btrfs subvol=$subvol UUID=$UUID"
+			umount "$tmpmnt/boot" 2>/dev/null
+			umount "$tmpmnt" 2>/dev/null
+			rmdir "$tmpmnt" 2>/dev/null
+			exit 1
+		fi
+	else
+		# bind-mount
+		if ! mount -o bind "$mpoint" "$tmpmnt" 2>/dev/null; then
+			warn "error mounting btrfs bindfrom=$mpoint subvol=$subvol UUID=$UUID"
+			umount "$tmpmnt/boot" 2>/dev/null
+			umount "$tmpmnt" 2>/dev/null
+			rmdir "$tmpmnt" 2>/dev/null
+			exit 1
+		fi
+	fi
+	debug "mounted btrfs $partition, subvol=$subvol on $tmpmnt"
+	if [ ! -e "$tmpmnt/etc/fstab" ]; then
+		warn "btrfs subvol=$subvol not root"
+		umount "$tmpmnt" 2>/dev/null
+		rmdir "$tmpmnt" 2>/dev/null
+		exit 1
+	fi
+	bootmnt=$(parsefstab < "$tmpmnt/etc/fstab" | grep " /boot ") || true
+	if [ -z "$bootmnt" ]; then
+		# /boot is part of the root
+		bootpart="$partition"
+		bootsv="$subvol"
+	elif echo "$bootmnt" | cut -d ' ' -f 3 | grep -q "btrfs"; then
+		# separate btrfs /boot subvolume
+		bootsv=$(echo "$bootmnt" | cut -d ' ' -f 4 | grep "^subvol=" | sed "s/subvol=//" )
+		bootuuid=$(echo "$bootmnt" | cut -d ' ' -f 1 | grep "^UUID=" | sed "s/UUID=//" )
+		debug "mounting btrfs $tmpmnt/boot UUID=$bootuuid subvol=$bootsv"
+		bindfrom=$(check_btrfs_mounted $bootsv $bootuuid)
+		if [ -n "$bindfrom" ]; then
+			# already mounted some place
+			if ! mount -o bind $bindfrom "$tmpmnt/boot" 2>/dev/null; then
+				warn "error bind mounting btrfs boot subvol=$bootsv, from=$bindfrom"
+				umount "$tmpmnt/boot" 2>/dev/null
+				umount "$tmpmnt" 2>/dev/null
+				rmdir "$tmpmnt" 2>/dev/null
+				exit 1
+			fi
+		elif ! mount -o subvol=$bootsv -t btrfs -U $bootuuid "$tmpmnt/boot" 2>/dev/null; then
+			warn "error mounting btrfs boot partition subvol=$bootsv, UUID=$bootuuid"
+			umount "$tmpmnt/boot" 2>/dev/null
+			umount "$tmpmnt" 2>/dev/null
+			rmdir "$tmpmnt" 2>/dev/null
+			exit 1
+		fi
+		bootpart=$(grep " btrfs " /proc/self/mountinfo | grep " /$bootsv " | cut -d ' ' -f 10)
+	else
+		# non-btrfs partition or logical volume
+		linux_mount_boot $partition $tmpmnt
+		bootpart="${mountboot%% *}"
+		bootsv=
+	fi
+	
+	test="/usr/lib/linux-boot-probes/mounted/40grub2"
+	if [ -f $test ] && [ -x $test ]; then
+		debug "running $test $partition $bootpart $tmpmnt $type $subvol $bootsv"
+		if $test "$partition" "$bootpart" "$tmpmnt" "$type" "$subvol" "$bootsv"; then
+			debug "$test succeeded"
+		fi
+	fi
+	umount "$tmpmnt/boot" 2>/dev/null || true
+	if ! umount "$tmpmnt" 2>/dev/null; then
+		warn "problem umount $tmpmnt"
+	fi
+	rmdir "$tmpmnt" 2>/dev/null || true
+
+	exit 0
+fi
+
 if ! mapped="$(mapdevfs "$partition")"; then
 	log "Device '$partition' does not exist; skipping"
 	continue
Index: os-prober/linux-boot-probes/mounted/common/40grub2
===================================================================
--- os-prober.orig/linux-boot-probes/mounted/common/40grub2
+++ os-prober/linux-boot-probes/mounted/common/40grub2
@@ -2,17 +2,27 @@
 . /usr/share/os-prober/common.sh
 set -e
 
+# add support for btrfs with no separate /boot
+# that is, rootsv = bootsv
 partition="$1"
 bootpart="$2"
 mpoint="$3"
 type="$4"
+rootsv="$5"
+bootsv="$6"
 
 found_item=0
 
 entry_result () {
+	if [ "x$type" = "xbtrfs" -a "$rootsv" = "$bootsv" ]; then
+		# trim off the leading subvol
+		kernelfile=$(echo "$kernel" | cut -d '/' -f 2- | cut -d '/' -f 2-)
+	else
+		kernelfile=$kernel
+	fi
 	if [ "$ignore_item" = 0 ] && \
 	   [ -n "$kernel" ] && \
-	   [ -e "$mpoint/$kernel" ]; then
+	   [ -e "$mpoint/$kernelfile" ]; then
 		result "$rootpart:$bootpart:$title:$kernel:$initrd:$parameters"
 		found_item=1
 	fi
Index: os-prober/os-prober
===================================================================
--- os-prober.orig/os-prober
+++ os-prober/os-prober
@@ -79,6 +79,9 @@ partitions () {
 		echo "$(LVM_SUPPRESS_FD_WARNINGS=1 log_output lvs --noheadings --separator : -o vg_name,lv_name |
 			sed "s|-|--|g;s|^[[:space:]]*\(.*\):\(.*\)$|/dev/mapper/\1-\2|")"
 	fi
+
+	# now lets make sure we got all of the btrfs partitions and disks
+	blkid | grep btrfs | cut -d ':' -f 1
 }
 
 parse_proc_swaps () {
@@ -137,6 +140,8 @@ if [ -f /proc/mdstat ] ; then
 	grep "^md" /proc/mdstat | parse_proc_mdstat >"$OS_PROBER_TMP/raided-map" || true
 fi
 
+: >"$OS_PROBER_TMP/btrfs-vols"
+
 for partition in $(partitions); do
 	if ! mapped="$(mapdevfs "$partition")"; then
 		log "Device '$partition' does not exist; skipping"
@@ -155,7 +160,26 @@ for partition in $(partitions); do
 		continue
 	fi
 
-	if ! grep -q "^$mapped " "$OS_PROBER_TMP/mounted-map" ; then
+	# do btrfs processing here; both mounted and unmounted will
+	# be handled by 50mounted-tests so we can do a subvol only once.
+	type=$(blkid -o value -s TYPE $mapped || true)
+	if [ "$type" = btrfs ]; then
+		uuid=$(blkid -o value -s UUID $mapped)
+		if grep -q "^$uuid" "$OS_PROBER_TMP/btrfs-vols" ; then
+			continue
+		fi
+		debug "btrfs volume uuid=$uuid partition=$partition"
+		echo "$uuid" >>"$OS_PROBER_TMP/btrfs-vols"
+		test="/usr/lib/os-probes/50mounted-tests"
+		if [ -f "$test" ] && [ -x "$test" ]; then
+			debug "running $test on btrfs $partition"
+			if "$test" btrfs "$uuid" "$partition"; then
+				debug "os detected by $test"
+		   		continue
+			fi
+		fi
+
+	elif ! grep -q "^$mapped " "$OS_PROBER_TMP/mounted-map" ; then
 		for test in /usr/lib/os-probes/*; do
 			if [ -f "$test" ] && [ -x "$test" ]; then
 				debug "running $test on $partition"
Index: os-prober/os-probes/common/50mounted-tests
===================================================================
--- os-prober.orig/os-probes/common/50mounted-tests
+++ os-prober/os-probes/common/50mounted-tests
@@ -19,7 +19,19 @@ do_unmount() {
 	rmdir "$tmpmnt" || true
 }
 
-types="$(fs_type "$partition")"
+if [ "x$1" = xbtrfs ]; then
+       types=btrfs
+       if [ -z "$2" -o -z "$3" ]; then
+               debug "missing btrfs parameters, exiting"
+               exit 1
+       fi
+       UUID="$2"
+       BTRFSDEV="$3"
+else
+       partition="$1"
+       types="$(fs_type "$partition")"
+fi
+
 if [ "$types" = NOT-DETECTED ]; then
 	debug "$1 type not recognised; skipping"
 	exit 0
@@ -66,6 +78,76 @@ if [ ! -d "$tmpmnt" ]; then
 fi
 
 mounted=
+
+# all btrfs processing here.  Handle both unmounted and
+# mounted subvolumes.
+if [ "$types" = btrfs ]; then
+	partition="$BTRFSDEV"
+	debug "begin btrfs processing for $UUID"
+	# note that the btrfs volume must not be mounted ro
+	if mount -t btrfs -U "$UUID" "$tmpmnt"  2>/dev/null; then
+		debug "btrfs volume $UUID mounted"
+	else
+		warn "cannot mount btrfs volume $UUID, exiting"
+		rmdir "$tmpmnt" || true
+		exit 1
+	fi
+	# besides regular subvols, get ro and snapshot so thet can be excluded
+        subvols=$(btrfs subvolume list "$tmpmnt" | cut -d ' ' -f 9)
+        rosubvols=$(btrfs subvolume list -r "$tmpmnt" | cut -d ' ' -f 9)
+        sssubvols=$(btrfs subvolume list -s "$tmpmnt" | cut -d ' ' -f 14)
+        if ! umount "$tmpmnt"; then
+            warn "failed to umount btrfs volume on $tmpmnt"
+            rmdir "$tmpmnt" || true
+            exit 1
+        fi
+	if [ -z "$subvols" ]; then
+		debug "no subvols found on btrfs volume $UUID"
+		exit 1
+	fi
+	found=
+        for subvol in $subvols; do
+		debug "begin btrfs processing for $UUID subvol=$subvol"
+		if echo "$rosubvols" | grep -q -x "$subvol"; then
+			continue
+		fi
+		if echo "$sssubvols" | grep -q -x "$subvol"; then
+			continue
+		fi
+		mounted=
+		mpoint="$(grep btrfs /proc/self/mountinfo | grep "$partition " | grep "/$subvol " | cut -d ' ' -f 5)"
+		if [ -n "$mpoint" ]; then
+			if [ "x$mpoint" = "x/" ]; then
+				continue # this is the root for the running system
+			fi
+			mounted=1
+		else
+			# again, do not mount btrfs ro
+			mount -t btrfs -o subvol="$subvol" -U "$UUID" "$tmpmnt"
+			mpoint="$tmpmnt"
+		fi
+		test="/usr/lib/os-probes/mounted/90linux-distro"
+		if [ -f "$test" ] && [ -x "$test" ]; then
+			debug "running subtest $test"
+			if "$test" "$partition" "$mpoint" btrfs "UUID=$UUID" "subvol=$subvol"; then
+				debug "os found by subtest $test on subvol $subvol"
+				found=1
+			fi
+		fi
+		if [ -z "$mounted" ]; then
+			if ! umount "$tmpmnt"; then
+			    warn "failed to umount $tmpmnt"
+			fi
+		fi
+	done
+	rmdir "$tmpmnt" || true
+	if [ "$found" ]; then
+		exit 0
+	else
+		exit 1
+	fi
+fi
+
 if type grub2-mount >/dev/null 2>&1 && \
    type grub2-probe >/dev/null 2>&1 && \
    grub2-mount "$partition" "$tmpmnt" 2>/dev/null; then
Index: os-prober/os-probes/mounted/common/90linux-distro
===================================================================
--- os-prober.orig/os-probes/mounted/common/90linux-distro
+++ os-prober/os-probes/mounted/common/90linux-distro
@@ -7,6 +7,8 @@ set -e
 partition="$1"
 dir="$2"
 type="$3"
+uuid="$4"
+subvol="$5"
 
 # This test is inaccurate, but given separate / and /boot partitions and the
 # fact that only some architectures have ld-linux.so, I can't see anything
@@ -143,7 +145,11 @@ if (ls "$dir"/lib*/ld*.so* && [ -d "$dir
 	fi
 	
         label="$(count_next_label "$short")"
-	result "$partition:$long:$label:linux"
+	if [ "x$type" = "xbtrfs" -a "x$uuid" != "x" -a "x$subvol" != "x" ]; then
+		result "$partition:$long:$label:linux:$type:$uuid:$subvol"
+	else
+		result "$partition:$long:$label:linux"
+	fi
 	exit 0
 else
 	exit 1
Index: os-prober/README.btrfs
===================================================================
--- /dev/null
+++ os-prober/README.btrfs
@@ -0,0 +1,15 @@
+BTRFS is a new filesystem which combines the filesystem with logical volume
+management (subvolumes).  For further information, see:
+      https://btrfs.wiki.kernel.org/index.php/Main_Page
+      https://btrfs.wiki.kernel.org/index.php/FAQ
+
+In order to support BTRFS, a number of changes were necessary to os-prober,
+os-probes/common/50mounted-tests, os-probes/mounted/common/90linux-distro,
+linux-boot-prober, and linux-boot-probes/common/50mounted-tests.
+
+The biggest impact will be to grub2 where there is additional information
+output by os-prober and where, if a BTRFS subvolume is being used for root,
+the parameters for linux-boot-prober have changed.
+
+Sun 30 Dec 2012 11:49:52 AM EST Gene Czarcinski <gene@czarc.net>
+