Blame linux-boot-prober

Packit d86cd3
#!/bin/sh
rpm-build 614032
rpm-build 614032
# dash shell does not have "{varname}>&1" feature that bash shell has
rpm-build 614032
# for auto-assignment of new filedescriptors.
rpm-build 614032
# It is cumbersome to write the 'eval' to use our own variables in redirections.
rpm-build 614032
# Therefore use fixed numbers.
rpm-build 614032
export fd_result=3  # file descriptor for external results
rpm-build 614032
export fd_logger=9  # file descriptor for input to logger
rpm-build 614032
Packit d86cd3
. /usr/share/os-prober/common.sh
Packit d86cd3
Packit d86cd3
set -e
Packit d86cd3
Packit d86cd3
newns "$@"
Packit d86cd3
require_tmpdir
rpm-build fe73fc
ERR="n"
rpm-build fe73fc
rpm-build fe73fc
tmpmnt=/var/lib/os-prober/mount
rpm-build fe73fc
if [ ! -d "$tmpmnt" ]; then
rpm-build fe73fc
	mkdir "$tmpmnt"
rpm-build fe73fc
fi
rpm-build fe73fc
rpm-build fe73fc
mounted=
rpm-build fe73fc
bootmnt=
rpm-build fe73fc
bootsv=
rpm-build fe73fc
bootuuid=
Packit d86cd3
Packit d86cd3
grep "^/dev/" /proc/mounts | parse_proc_mounts >"$OS_PROBER_TMP/mounted-map" || true
Packit d86cd3
rpm-build 614032
( (
rpm-build fe73fc
if [ -z "$1" ]; then
rpm-build fe73fc
	ERR=y
rpm-build fe73fc
elif [ "$1" = btrfs -a -z "$2" ]; then
rpm-build fe73fc
	ERR=y
rpm-build fe73fc
elif [ "$1" = btrfs -a -z "$3" ]; then
rpm-build fe73fc
	ERR=y
rpm-build fe73fc
elif [ "$1" = btrfs ]; then
rpm-build fe73fc
	type=btrfs
rpm-build fe73fc
	echo "$2" | grep -q "^UUID=" || ERR=y
rpm-build fe73fc
	echo "$3" | grep -q "^subvol=" || ERR=y
rpm-build fe73fc
	export "$2"
rpm-build fe73fc
	export "$3"
rpm-build fe73fc
	partition=$(blkid | grep "$UUID" | cut -d ':' -f 1 | tr '\n' ' ' | cut -d ' ' -f 1)
rpm-build fe73fc
	debug "btrfs: partition=$partition, UUID=$UUID, subvol=$subvol"
rpm-build fe73fc
else
rpm-build fe73fc
	partition="$1"
rpm-build fe73fc
	type=other
rpm-build fe73fc
fi
Packit d86cd3
rpm-build fe73fc
if [ "x$ERR" != xn ]; then
Packit d86cd3
	echo "usage: linux-boot-prober partition" >&2
rpm-build fe73fc
	echo "       linux-boot-prober btrfs UUID=<> subvol=<>" >&2
Packit d86cd3
	exit 1
Packit d86cd3
fi
Packit d86cd3
rpm-build fe73fc
if [ "$type" = btrfs ]; then
rpm-build fe73fc
	# handle all of the btrfs stuff here
rpm-build fe73fc
	if [ ! -e "/proc/self/mountinfo" ]; then
rpm-build fe73fc
		warn "/proc/self/mountinfo does not exist, exiting"
rpm-build fe73fc
		umount "$tmpmnt" 2>/dev/null
rpm-build fe73fc
		rmdir "$tmpmnt" 2>/dev/null
rpm-build fe73fc
		exit 1
rpm-build fe73fc
	fi
rpm-build fe73fc
	mpoint=$(grep "btrfs" /proc/self/mountinfo | grep " /$subvol " | grep " $partition " | cut -d ' ' -f 5)
rpm-build fe73fc
	if [ "$mpoint" = "/" ]; then
rpm-build fe73fc
		warn "specifying active root not valid, exiting"
rpm-build fe73fc
		umount "$tmpmnt" 2>/dev/null
rpm-build fe73fc
		rmdir "$tmpmnt" 2>/dev/null
rpm-build fe73fc
		exit 1
rpm-build fe73fc
	fi
rpm-build fe73fc
	if [ "$mpoint" = "$tmpmnt" ]; then
rpm-build fe73fc
		warn "btrfs subvol=$subvool, UUID=$UUID, already mounted on $tmpmnt **ERROR**"
rpm-build fe73fc
		umount "$tmpmnt" 2>/dev/null
rpm-build fe73fc
		rmdir "$tmpmnt" 2>/dev/null
rpm-build fe73fc
		exit 1
rpm-build fe73fc
	fi
rpm-build fe73fc
	if [ -z "$mpoint" ]; then
rpm-build fe73fc
		# mount the btrfs root
rpm-build fe73fc
		if ! mount -o subvol=$subvol -t btrfs -U $UUID "$tmpmnt" 2>/dev/null; then
rpm-build fe73fc
			warn "error mounting btrfs subvol=$subvol UUID=$UUID"
rpm-build fe73fc
			umount "$tmpmnt/boot" 2>/dev/null
rpm-build fe73fc
			umount "$tmpmnt" 2>/dev/null
rpm-build fe73fc
			rmdir "$tmpmnt" 2>/dev/null
rpm-build fe73fc
			exit 1
rpm-build fe73fc
		fi
rpm-build fe73fc
	else
rpm-build fe73fc
		# bind-mount
rpm-build fe73fc
		if ! mount -o bind "$mpoint" "$tmpmnt" 2>/dev/null; then
rpm-build fe73fc
			warn "error mounting btrfs bindfrom=$mpoint subvol=$subvol UUID=$UUID"
rpm-build fe73fc
			umount "$tmpmnt/boot" 2>/dev/null
rpm-build fe73fc
			umount "$tmpmnt" 2>/dev/null
rpm-build fe73fc
			rmdir "$tmpmnt" 2>/dev/null
rpm-build fe73fc
			exit 1
rpm-build fe73fc
		fi
rpm-build fe73fc
	fi
rpm-build fe73fc
	debug "mounted btrfs $partition, subvol=$subvol on $tmpmnt"
rpm-build fe73fc
	if [ ! -e "$tmpmnt/etc/fstab" ]; then
rpm-build fe73fc
		warn "btrfs subvol=$subvol not root"
rpm-build fe73fc
		umount "$tmpmnt" 2>/dev/null
rpm-build fe73fc
		rmdir "$tmpmnt" 2>/dev/null
rpm-build fe73fc
		exit 1
rpm-build fe73fc
	fi
rpm-build fe73fc
	bootmnt=$(parsefstab < "$tmpmnt/etc/fstab" | grep " /boot ") || true
rpm-build fe73fc
	if [ -z "$bootmnt" ]; then
rpm-build fe73fc
		# /boot is part of the root
rpm-build fe73fc
		bootpart="$partition"
rpm-build fe73fc
		bootsv="$subvol"
rpm-build fe73fc
	elif echo "$bootmnt" | cut -d ' ' -f 3 | grep -q "btrfs"; then
rpm-build fe73fc
		# separate btrfs /boot subvolume
rpm-build fe73fc
		bootsv=$(echo "$bootmnt" | cut -d ' ' -f 4 | grep "^subvol=" | sed "s/subvol=//" )
rpm-build fe73fc
		bootuuid=$(echo "$bootmnt" | cut -d ' ' -f 1 | grep "^UUID=" | sed "s/UUID=//" )
rpm-build fe73fc
		debug "mounting btrfs $tmpmnt/boot UUID=$bootuuid subvol=$bootsv"
rpm-build fe73fc
		bindfrom=$(check_btrfs_mounted $bootsv $bootuuid)
rpm-build fe73fc
		if [ -n "$bindfrom" ]; then
rpm-build fe73fc
			# already mounted some place
rpm-build fe73fc
			if ! mount -o bind $bindfrom "$tmpmnt/boot" 2>/dev/null; then
rpm-build fe73fc
				warn "error bind mounting btrfs boot subvol=$bootsv, from=$bindfrom"
rpm-build fe73fc
				umount "$tmpmnt/boot" 2>/dev/null
rpm-build fe73fc
				umount "$tmpmnt" 2>/dev/null
rpm-build fe73fc
				rmdir "$tmpmnt" 2>/dev/null
rpm-build fe73fc
				exit 1
rpm-build fe73fc
			fi
rpm-build fe73fc
		elif ! mount -o subvol=$bootsv -t btrfs -U $bootuuid "$tmpmnt/boot" 2>/dev/null; then
rpm-build fe73fc
			warn "error mounting btrfs boot partition subvol=$bootsv, UUID=$bootuuid"
rpm-build fe73fc
			umount "$tmpmnt/boot" 2>/dev/null
rpm-build fe73fc
			umount "$tmpmnt" 2>/dev/null
rpm-build fe73fc
			rmdir "$tmpmnt" 2>/dev/null
rpm-build fe73fc
			exit 1
rpm-build fe73fc
		fi
rpm-build fe73fc
		bootpart=$(grep " btrfs " /proc/self/mountinfo | grep " /$bootsv " | cut -d ' ' -f 10)
rpm-build fe73fc
	else
rpm-build fe73fc
		# non-btrfs partition or logical volume
rpm-build fe73fc
		linux_mount_boot $partition $tmpmnt
rpm-build fe73fc
		bootpart="${mountboot%% *}"
rpm-build fe73fc
		bootsv=
rpm-build fe73fc
	fi
rpm-build fe73fc
Packit Service 8d4c46
	test="/usr/libexec/linux-boot-probes/mounted/40grub2"
rpm-build fe73fc
	if [ -f $test ] && [ -x $test ]; then
rpm-build fe73fc
		debug "running $test $partition $bootpart $tmpmnt $type $subvol $bootsv"
rpm-build fe73fc
		if $test "$partition" "$bootpart" "$tmpmnt" "$type" "$subvol" "$bootsv"; then
rpm-build fe73fc
			debug "$test succeeded"
rpm-build fe73fc
		fi
rpm-build fe73fc
	fi
rpm-build fe73fc
	umount "$tmpmnt/boot" 2>/dev/null || true
rpm-build fe73fc
	if ! umount "$tmpmnt" 2>/dev/null; then
rpm-build fe73fc
		warn "problem umount $tmpmnt"
rpm-build fe73fc
	fi
rpm-build fe73fc
	rmdir "$tmpmnt" 2>/dev/null || true
rpm-build fe73fc
rpm-build fe73fc
	exit 0
rpm-build fe73fc
fi
rpm-build fe73fc
Packit d86cd3
if ! mapped="$(mapdevfs "$partition")"; then
Packit d86cd3
	log "Device '$partition' does not exist; skipping"
Packit d86cd3
	continue
Packit d86cd3
fi
Packit d86cd3
Packit d86cd3
if ! grep -q "^$mapped " "$OS_PROBER_TMP/mounted-map"; then
Packit Service 8d4c46
	for test in /usr/libexec/linux-boot-probes/*; do
Packit d86cd3
		if [ -x $test ] && [ -f $test ]; then
rpm-build fe73fc
			debug "running $test"
Packit d86cd3
			if $test "$partition"; then
Packit d86cd3
				debug "linux detected by $test"
Packit d86cd3
				break
Packit d86cd3
			fi
Packit d86cd3
		fi
Packit d86cd3
	done
Packit d86cd3
else
Packit d86cd3
	mpoint=$(grep "^$mapped " "$OS_PROBER_TMP/mounted-map" | head -n1 | cut -d " " -f 2)
Packit d86cd3
	mpoint="$(unescape_mount "$mpoint")"
Packit d86cd3
	if [ "$mpoint" != "/target/boot" ] && [ "$mpoint" != "/target" ] && [ "$mpoint" != "/" ]; then
Packit d86cd3
		type=$(grep "^$mapped " "$OS_PROBER_TMP/mounted-map" | head -n1 | cut -d " " -f 3)
Packit d86cd3
		if ! grep -q " $mpoint/boot " "$OS_PROBER_TMP/mounted-map"; then
Packit d86cd3
			linux_mount_boot "$partition" "$mpoint"
Packit d86cd3
			bootpart="${mountboot%% *}"
Packit d86cd3
			bootmounted="${mountboot#* }"
Packit d86cd3
		else
rpm-build 0110b5
			bootpart="$(grep " $mpoint/boot " "$OS_PROBER_TMP/mounted-map" | head -n1 | cut -d " " -f 4)"
Packit d86cd3
			bootmounted=0
Packit d86cd3
		fi
Packit Service 8d4c46
		for test in /usr/libexec/linux-boot-probes/mounted/*; do
Packit d86cd3
			if [ -f $test ] && [ -x $test ]; then
Packit d86cd3
				debug "running $test on mounted $partition"
Packit d86cd3
				if $test "$partition" "$bootpart" "$mpoint" "$type"; then
Packit d86cd3
					debug "$test succeeded"
Packit d86cd3
					break
Packit d86cd3
				fi
Packit d86cd3
			fi
Packit d86cd3
		done
Packit d86cd3
		if [ "$bootmounted" = 1 ]; then
Packit d86cd3
			if ! umount "$mpoint/boot"; then
Packit d86cd3
				warn "failed to umount $mpoint/boot"
Packit d86cd3
			fi
Packit d86cd3
		fi
Packit d86cd3
	fi
Packit d86cd3
fi
rpm-build 614032
) 9>&1 | logger 1>&-  # fd_logger
rpm-build 614032
) 3>&1  # fd_result