278265
diff --git a/common.sh b/common.sh
278265
index c2c5f46..8fb3c5f 100644
278265
--- a/common.sh
278265
+++ b/common.sh
278265
@@ -155,6 +155,7 @@ parse_proc_mounts () {
278265
 	done
278265
 }
278265
 
278265
+# add forth parameter to pickup btrfs subvol info
278265
 parsefstab () {
278265
 	while read -r line; do
278265
 		case "$line" in
278265
@@ -165,12 +166,22 @@ parsefstab () {
278265
 				set -f
278265
 				set -- $line
278265
 				set +f
278265
-				printf '%s %s %s\n' "$1" "$2" "$3"
278265
+				printf '%s %s %s %s\n' "$1" "$2" "$3" "$4"
278265
 			;;
278265
 		esac
278265
 	done
278265
 }
278265
 
278265
+#check_btrfs_mounted $bootsv $bootuuid)
278265
+check_btrfs_mounted () {
278265
+	bootsv="$1"
278265
+	bootuuid="$2"
278265
+	bootdev=$(blkid | grep "$bootuuid" | cut -d ':' -f  1)
278265
+	bindfrom=$(grep " btrfs " /proc/self/mountinfo |
278265
+		   grep " $bootdev " | grep " /$bootsv " | cut -d ' ' -f 5)
278265
+	printf "%s" "$bindfrom"
278265
+}
278265
+
278265
 unescape_mount () {
278265
 	printf %s "$1" | \
278265
 		sed 's/\\011/	/g; s/\\012/\n/g; s/\\040/ /g; s/\\134/\\/g'
278265
diff --git a/linux-boot-prober b/linux-boot-prober
278265
index e32dc84..2a60fa2 100755
278265
--- a/linux-boot-prober
278265
+++ b/linux-boot-prober
278265
@@ -5,16 +5,143 @@ set -e
278265
 
278265
 newns "$@"
278265
 require_tmpdir
278265
+ERR="n"
278265
+
278265
+tmpmnt=/var/lib/os-prober/mount
278265
+if [ ! -d "$tmpmnt" ]; then
278265
+	mkdir "$tmpmnt"
278265
+fi
278265
+
278265
+mounted=
278265
+bootmnt=
278265
+bootsv=
278265
+bootuuid=
278265
 
278265
 grep "^/dev/" /proc/mounts | parse_proc_mounts >"$OS_PROBER_TMP/mounted-map" || true
278265
 
278265
-partition="$1"
278265
+if [ -z "$1" ]; then
278265
+	ERR=y
278265
+elif [ "$1" = btrfs -a -z "$2" ]; then
278265
+	ERR=y
278265
+elif [ "$1" = btrfs -a -z "$3" ]; then
278265
+	ERR=y
278265
+elif [ "$1" = btrfs ]; then
278265
+	type=btrfs
278265
+	echo "$2" | grep -q "^UUID=" || ERR=y
278265
+	echo "$3" | grep -q "^subvol=" || ERR=y
278265
+	export "$2"
278265
+	export "$3"
278265
+	partition=$(blkid | grep "$UUID" | cut -d ':' -f 1 | tr '\n' ' ' | cut -d ' ' -f 1)
278265
+	debug "btrfs: partition=$partition, UUID=$UUID, subvol=$subvol"
278265
+else
278265
+	partition="$1"
278265
+	type=other
278265
+fi
278265
 
278265
-if [ -z "$partition" ]; then
278265
+if [ "x$ERR" != xn ]; then
278265
 	echo "usage: linux-boot-prober partition" >&2
278265
+	echo "       linux-boot-prober btrfs UUID=<> subvol=<>" >&2
278265
 	exit 1
278265
 fi
278265
 
278265
+if [ "$type" = btrfs ]; then
278265
+	# handle all of the btrfs stuff here
278265
+	if [ ! -e "/proc/self/mountinfo" ]; then
278265
+		warn "/proc/self/mountinfo does not exist, exiting"
278265
+		umount "$tmpmnt" 2>/dev/null
278265
+		rmdir "$tmpmnt" 2>/dev/null
278265
+		exit 1
278265
+	fi
278265
+	mpoint=$(grep "btrfs" /proc/self/mountinfo | grep " /$subvol " | grep " $partition " | cut -d ' ' -f 5)
278265
+	if [ "$mpoint" = "/" ]; then
278265
+		warn "specifying active root not valid, exiting"
278265
+		umount "$tmpmnt" 2>/dev/null
278265
+		rmdir "$tmpmnt" 2>/dev/null
278265
+		exit 1
278265
+	fi
278265
+	if [ "$mpoint" = "$tmpmnt" ]; then
278265
+		warn "btrfs subvol=$subvool, UUID=$UUID, already mounted on $tmpmnt **ERROR**"
278265
+		umount "$tmpmnt" 2>/dev/null
278265
+		rmdir "$tmpmnt" 2>/dev/null
278265
+		exit 1
278265
+	fi
278265
+	if [ -z "$mpoint" ]; then
278265
+		# mount the btrfs root
278265
+		if ! mount -o subvol=$subvol -t btrfs -U $UUID "$tmpmnt" 2>/dev/null; then
278265
+			warn "error mounting btrfs subvol=$subvol UUID=$UUID"
278265
+			umount "$tmpmnt/boot" 2>/dev/null
278265
+			umount "$tmpmnt" 2>/dev/null
278265
+			rmdir "$tmpmnt" 2>/dev/null
278265
+			exit 1
278265
+		fi
278265
+	else
278265
+		# bind-mount
278265
+		if ! mount -o bind "$mpoint" "$tmpmnt" 2>/dev/null; then
278265
+			warn "error mounting btrfs bindfrom=$mpoint subvol=$subvol UUID=$UUID"
278265
+			umount "$tmpmnt/boot" 2>/dev/null
278265
+			umount "$tmpmnt" 2>/dev/null
278265
+			rmdir "$tmpmnt" 2>/dev/null
278265
+			exit 1
278265
+		fi
278265
+	fi
278265
+	debug "mounted btrfs $partition, subvol=$subvol on $tmpmnt"
278265
+	if [ ! -e "$tmpmnt/etc/fstab" ]; then
278265
+		warn "btrfs subvol=$subvol not root"
278265
+		umount "$tmpmnt" 2>/dev/null
278265
+		rmdir "$tmpmnt" 2>/dev/null
278265
+		exit 1
278265
+	fi
278265
+	bootmnt=$(parsefstab < "$tmpmnt/etc/fstab" | grep " /boot ") || true
278265
+	if [ -z "$bootmnt" ]; then
278265
+		# /boot is part of the root
278265
+		bootpart="$partition"
278265
+		bootsv="$subvol"
278265
+	elif echo "$bootmnt" | cut -d ' ' -f 3 | grep -q "btrfs"; then
278265
+		# separate btrfs /boot subvolume
278265
+		bootsv=$(echo "$bootmnt" | cut -d ' ' -f 4 | grep "^subvol=" | sed "s/subvol=//" )
278265
+		bootuuid=$(echo "$bootmnt" | cut -d ' ' -f 1 | grep "^UUID=" | sed "s/UUID=//" )
278265
+		debug "mounting btrfs $tmpmnt/boot UUID=$bootuuid subvol=$bootsv"
278265
+		bindfrom=$(check_btrfs_mounted $bootsv $bootuuid)
278265
+		if [ -n "$bindfrom" ]; then
278265
+			# already mounted some place
278265
+			if ! mount -o bind $bindfrom "$tmpmnt/boot" 2>/dev/null; then
278265
+				warn "error bind mounting btrfs boot subvol=$bootsv, from=$bindfrom"
278265
+				umount "$tmpmnt/boot" 2>/dev/null
278265
+				umount "$tmpmnt" 2>/dev/null
278265
+				rmdir "$tmpmnt" 2>/dev/null
278265
+				exit 1
278265
+			fi
278265
+		elif ! mount -o subvol=$bootsv -t btrfs -U $bootuuid "$tmpmnt/boot" 2>/dev/null; then
278265
+			warn "error mounting btrfs boot partition subvol=$bootsv, UUID=$bootuuid"
278265
+			umount "$tmpmnt/boot" 2>/dev/null
278265
+			umount "$tmpmnt" 2>/dev/null
278265
+			rmdir "$tmpmnt" 2>/dev/null
278265
+			exit 1
278265
+		fi
278265
+		bootpart=$(grep " btrfs " /proc/self/mountinfo | grep " /$bootsv " | cut -d ' ' -f 10)
278265
+	else
278265
+		# non-btrfs partition or logical volume
278265
+		linux_mount_boot $partition $tmpmnt
278265
+		bootpart="${mountboot%% *}"
278265
+		bootsv=
278265
+	fi
278265
+
278265
+	test="/usr/lib/linux-boot-probes/mounted/40grub2"
278265
+	if [ -f $test ] && [ -x $test ]; then
278265
+		debug "running $test $partition $bootpart $tmpmnt $type $subvol $bootsv"
278265
+		if $test "$partition" "$bootpart" "$tmpmnt" "$type" "$subvol" "$bootsv"; then
278265
+			debug "$test succeeded"
278265
+		fi
278265
+	fi
278265
+	umount "$tmpmnt/boot" 2>/dev/null || true
278265
+	if ! umount "$tmpmnt" 2>/dev/null; then
278265
+		warn "problem umount $tmpmnt"
278265
+	fi
278265
+	rmdir "$tmpmnt" 2>/dev/null || true
278265
+
278265
+	exit 0
278265
+fi
278265
+
278265
 if ! mapped="$(mapdevfs "$partition")"; then
278265
 	log "Device '$partition' does not exist; skipping"
278265
 	continue
278265
@@ -22,8 +149,8 @@ fi
278265
 
278265
 if ! grep -q "^$mapped " "$OS_PROBER_TMP/mounted-map"; then
278265
 	for test in /usr/lib/linux-boot-probes/*; do
278265
-		debug "running $test"
278265
 		if [ -x $test ] && [ -f $test ]; then
278265
+			debug "running $test"
278265
 			if $test "$partition"; then
278265
 				debug "linux detected by $test"
278265
 				break
278265
diff --git a/linux-boot-probes/mounted/common/40grub2 b/linux-boot-probes/mounted/common/40grub2
278265
index 885614e..db5cbfd 100755
278265
--- a/linux-boot-probes/mounted/common/40grub2
278265
+++ b/linux-boot-probes/mounted/common/40grub2
278265
@@ -2,17 +2,30 @@
278265
 . /usr/share/os-prober/common.sh
278265
 set -e
278265
 
278265
+# add support for btrfs with no separate /boot
278265
+# that is, rootsv = bootsv
278265
 partition="$1"
278265
 bootpart="$2"
278265
 mpoint="$3"
278265
 type="$4"
278265
+rootsv="$5"
278265
+bootsv="$6"
278265
 
278265
 found_item=0
278265
 
278265
 entry_result () {
278265
+	if [ "x$type" = "xbtrfs" -a "$partition" = "$bootpart" ]; then
278265
+		# trim off the leading subvol
278265
+		kernelfile=$(echo "$kernel" | cut -d '/' -f 2- | cut -d '/' -f 2-)
278265
+		if [ "x$rootsv" != "x$bootsv" ]; then
278265
+		   kernelfile="/boot/$kernelfile"
278265
+		fi
278265
+	else
278265
+		kernelfile=$kernel
278265
+	fi
278265
 	if [ "$ignore_item" = 0 ] && \
278265
 	   [ -n "$kernel" ] && \
278265
-	   [ -e "$mpoint/$kernel" ]; then
278265
+	   [ -e "$mpoint/$kernelfile" ]; then
278265
 		result "$rootpart:$bootpart:$title:$kernel:$initrd:$parameters"
278265
 		found_item=1
278265
 	fi
278265
diff --git a/os-prober b/os-prober
278265
index 8852887..482c3c2 100755
278265
--- a/os-prober
278265
+++ b/os-prober
278265
@@ -76,9 +76,12 @@ partitions () {
278265
 
278265
 	# Also detect OSes on LVM volumes (assumes LVM is active)
278265
 	if type lvs >/dev/null 2>&1; then
278265
-		echo "$(LVM_SUPPRESS_FD_WARNINGS=1 log_output lvs --noheadings --separator : -o vg_name,lv_name |
278265
+		echo "$(LVM_SUPPRESS_FD_WARNINGS=1 log_output lvs --noheadings --separator : -o vg_name,lv_name 2>/dev/null |
278265
 			sed "s|-|--|g;s|^[[:space:]]*\(.*\):\(.*\)$|/dev/mapper/\1-\2|")"
278265
 	fi
278265
+
278265
+	# now lets make sure we got all of the btrfs partitions and disks
278265
+	blkid | grep 'TYPE="btrfs"' | cut -d ':' -f 1
278265
 }
278265
 
278265
 parse_proc_swaps () {
278265
@@ -136,6 +139,8 @@ if [ -f /proc/mdstat ] ; then
278265
 	grep "^md" /proc/mdstat | cut -d: -f2- | parse_proc_mdstat >"$OS_PROBER_TMP/raided-map" || true
278265
 fi
278265
 
278265
+: >"$OS_PROBER_TMP/btrfs-vols"
278265
+
278265
 for partition in $(partitions); do
278265
 	if ! mapped="$(mapdevfs "$partition")"; then
278265
 		log "Device '$partition' does not exist; skipping"
278265
@@ -154,7 +159,26 @@ for partition in $(partitions); do
278265
 		continue
278265
 	fi
278265
 
278265
-	if ! grep -q "^$mapped " "$OS_PROBER_TMP/mounted-map" ; then
278265
+	# do btrfs processing here; both mounted and unmounted will
278265
+	# be handled by 50mounted-tests so we can do a subvol only once.
278265
+	type=$(blkid -o value -s TYPE $mapped || true)
278265
+	if [ "$type" = btrfs ]; then
278265
+		uuid=$(blkid -o value -s UUID $mapped)
278265
+		if grep -q "^$uuid" "$OS_PROBER_TMP/btrfs-vols" ; then
278265
+			continue
278265
+		fi
278265
+		debug "btrfs volume uuid=$uuid partition=$partition"
278265
+		echo "$uuid" >>"$OS_PROBER_TMP/btrfs-vols"
278265
+		test="/usr/lib/os-probes/50mounted-tests"
278265
+		if [ -f "$test" ] && [ -x "$test" ]; then
278265
+			debug "running $test on btrfs $partition"
278265
+			if "$test" btrfs "$uuid" "$partition"; then
278265
+				debug "os detected by $test"
278265
+				continue
278265
+			fi
278265
+		fi
278265
+
278265
+	elif ! grep -q "^$mapped " "$OS_PROBER_TMP/mounted-map" ; then
278265
 		for test in /usr/lib/os-probes/*; do
278265
 			if [ -f "$test" ] && [ -x "$test" ]; then
278265
 				debug "running $test on $partition"
278265
diff --git a/os-probes/common/50mounted-tests b/os-probes/common/50mounted-tests
278265
index 2951ef9..e33eb82 100755
278265
--- a/os-probes/common/50mounted-tests
278265
+++ b/os-probes/common/50mounted-tests
278265
@@ -19,19 +19,31 @@ do_unmount() {
278265
 	rmdir "$tmpmnt" || true
278265
 }
278265
 
278265
-types="$(fs_type "$partition")"
278265
+if [ "x$1" = xbtrfs ]; then
278265
+	types=btrfs
278265
+	if [ -z "$2" -o -z "$3" ]; then
278265
+		debug "missing btrfs parameters, exiting"
278265
+		exit 1
278265
+	fi
278265
+	UUID="$2"
278265
+	BTRFSDEV="$3"
278265
+else
278265
+	partition="$1"
278265
+	types="$(fs_type "$partition")" || types=NOT-DETECTED
278265
+fi
278265
+
278265
 if [ "$types" = NOT-DETECTED ]; then
278265
 	debug "$1 type not recognised; skipping"
278265
-	exit 0
278265
+	exit 1
278265
 elif [ "$types" = swap ]; then
278265
 	debug "$1 is a swap partition; skipping"
278265
-	exit 0
278265
+	exit 1
278265
 elif [ "$types" = crypto_LUKS ]; then
278265
 	debug "$1 is a LUKS partition; skipping"
278265
-	exit 0
278265
+	exit 1
278265
 elif [ "$types" = LVM2_member ]; then
278265
 	debug "$1 is an LVM member; skipping"
278265
-	exit 0
278265
+	exit 1
278265
 elif [ "$types" = ntfs ]; then
278265
 	if type ntfs-3g >/dev/null 2>&1; then
278265
 		types='ntfs-3g ntfs'
278265
@@ -40,7 +52,7 @@ elif [ -z "$types" ]; then
278265
 	if type cryptsetup >/dev/null 2>&1 && \
278265
 	   cryptsetup luksDump "$partition" >/dev/null 2>&1; then
278265
 		debug "$1 is a LUKS partition; skipping"
278265
-		exit 0
278265
+		exit 1
278265
 	fi
278265
 	for type in $(grep -v nodev /proc/filesystems); do
278265
 		# hfsplus filesystems are mountable as hfs. Try hfs last so
278265
@@ -63,6 +75,108 @@ if [ ! -d "$tmpmnt" ]; then
278265
 fi
278265
 
278265
 mounted=
278265
+
278265
+# all btrfs processing here.  Handle both unmounted and
278265
+# mounted subvolumes.
278265
+if [ "$types" = btrfs ]; then
278265
+	partition="$BTRFSDEV"
278265
+	debug "begin btrfs processing for $UUID"
278265
+	# note that the btrfs volume must not be mounted ro
278265
+	if mount -t btrfs -U "$UUID" "$tmpmnt"  2>/dev/null; then
278265
+		debug "btrfs volume $UUID mounted"
278265
+	else
278265
+		warn "cannot mount btrfs volume $UUID, exiting"
278265
+		rmdir "$tmpmnt" || true
278265
+		exit 1
278265
+	fi
278265
+	# besides regular subvols, get ro and snapshot so thet can be excluded
278265
+        subvols=$(btrfs subvolume list "$tmpmnt" | cut -d ' ' -f 9)
278265
+        rosubvols=$(btrfs subvolume list -r "$tmpmnt" | cut -d ' ' -f 9)
278265
+        sssubvols=$(btrfs subvolume list -s "$tmpmnt" | cut -d ' ' -f 14)
278265
+        if ! umount "$tmpmnt"; then
278265
+            warn "failed to umount btrfs volume on $tmpmnt"
278265
+            rmdir "$tmpmnt" || true
278265
+            exit 1
278265
+        fi
278265
+
278265
+	found=
278265
+	mounted=
278265
+
278265
+	mpoint="$(grep btrfs /proc/self/mountinfo | grep "$partition " | cut -d ' ' -f 5)"
278265
+	if [ -n "$mpoint" -a "x$mpoint" = "x/" ]; then
278265
+		debug "This is the root for the running system" #running system must be done elsewhere
278265
+	else
278265
+	    #partition was not root of running system, so lets look for bootable subvols
278265
+	    if [ -n "$mpoint" ] ; then
278265
+		mounted=1  #partition was already mounted,so lets not unmount it when done
278265
+	    else
278265
+		# again, do not mount btrfs ro
278265
+		mount -t btrfs -U "$UUID" "$tmpmnt"
278265
+		mpoint="$tmpmnt"
278265
+	    fi
278265
+
278265
+	    test="/usr/lib/os-probes/mounted/90linux-distro"
278265
+	    if [ -f "$test" ] && [ -x "$test" ]; then
278265
+		debug "running subtest $test"
278265
+		if "$test" "$partition" "$mpoint" btrfs "UUID=$UUID"; then
278265
+		    debug "os found by subtest $test on $partition"
278265
+		    found=1
278265
+		fi
278265
+	    fi
278265
+	    if [ -z "$mounted" ]; then
278265
+		if ! umount "$tmpmnt"; then
278265
+		    warn "failed to umount $tmpmnt"
278265
+		fi
278265
+	    fi
278265
+	fi
278265
+
278265
+	if [ -z "$subvols" ]; then
278265
+	        debug "no subvols found on btrfs volume $UUID"
278265
+	else
278265
+		found=
278265
+                for subvol in $subvols; do
278265
+			debug "begin btrfs processing for $UUID subvol=$subvol"
278265
+			if echo "$rosubvols" | grep -q -x "$subvol"; then
278265
+				continue
278265
+			fi
278265
+			if echo "$sssubvols" | grep -q -x "$subvol"; then
278265
+				continue
278265
+			fi
278265
+			mounted=
278265
+			mpoint="$(grep btrfs /proc/self/mountinfo | grep "$partition " | grep "/$subvol " | cut -d ' ' -f 5)"
278265
+			if [ -n "$mpoint" ]; then
278265
+				if [ "x$mpoint" = "x/" ]; then
278265
+					continue # this is the root for the running system
278265
+				fi
278265
+				mounted=1
278265
+			else
278265
+				# again, do not mount btrfs ro
278265
+				mount -t btrfs -o subvol="$subvol" -U "$UUID" "$tmpmnt"
278265
+				mpoint="$tmpmnt"
278265
+			fi
278265
+			test="/usr/lib/os-probes/mounted/90linux-distro"
278265
+			if [ -f "$test" ] && [ -x "$test" ]; then
278265
+				debug "running subtest $test"
278265
+				if "$test" "$partition" "$mpoint" btrfs "UUID=$UUID" "subvol=$subvol"; then
278265
+					debug "os found by subtest $test on subvol $subvol"
278265
+					found=1
278265
+				fi
278265
+			fi
278265
+			if [ -z "$mounted" ]; then
278265
+				if ! umount "$tmpmnt"; then
278265
+				    warn "failed to umount $tmpmnt"
278265
+				fi
278265
+			fi
278265
+		done
278265
+	fi
278265
+	rmdir "$tmpmnt" || true
278265
+	if [ "$found" ]; then
278265
+		exit 0
278265
+	else
278265
+		exit 1
278265
+	fi
278265
+fi
278265
+
278265
 if type grub-mount >/dev/null 2>&1 && \
278265
    type grub-probe >/dev/null 2>&1 && \
278265
    grub-mount "$partition" "$tmpmnt" 2>/dev/null; then
278265
diff --git a/os-probes/mounted/common/90linux-distro b/os-probes/mounted/common/90linux-distro
278265
index badfbb1..9bc5154 100755
278265
--- a/os-probes/mounted/common/90linux-distro
278265
+++ b/os-probes/mounted/common/90linux-distro
278265
@@ -7,6 +7,8 @@ set -e
278265
 partition="$1"
278265
 dir="$2"
278265
 type="$3"
278265
+uuid="$4"
278265
+subvol="$5"
278265
 
278265
 # This test is inaccurate, but given separate / and /boot partitions and the
278265
 # fact that only some architectures have ld-linux.so, I can't see anything
278265
@@ -143,7 +145,11 @@ if (ls "$dir"/lib*/ld*.so* && [ -d "$dir/boot" ] || ls "$dir"/usr/lib*/ld*.so*)
278265
 	fi
278265
 	
278265
         label="$(count_next_label "$short")"
278265
-	result "$partition:$long:$label:linux"
278265
+	if [ "x$type" = "xbtrfs" -a "x$uuid" != "x" -a "x$subvol" != "x" ]; then
278265
+		result "$partition:$long:$label:linux:$type:$uuid:$subvol"
278265
+	else
278265
+		result "$partition:$long:$label:linux"
278265
+	fi
278265
 	exit 0
278265
 else
278265
 	exit 1