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