#!/bin/bash
#%stage: block
#

# many guestfish commands need a mount point
# in guestfsd the mount point defaults to /sysroot
mkdir -vp $tmp_mnt/sysroot

# guestfsd tries to bind mount this directory
mkdir -vp $tmp_mnt/selinux

for f in /etc/magic /usr/share/misc/magic*
do
	if test -e $f
	then
		cp -av --parents $f $tmp_mnt/
	fi
done

for t in \
	screen \
	vt100 \
	vt102 \
	linux
do
	ti="`echo /usr/share/terminfo/*/${t}`"
	for f in $ti
	do
		if test -f "${f}"
		then
			cp -av --parents $f $tmp_mnt
		fi
	done
done

# Bug 674684 - mount-rootfs-and-do-chroot.sh
cat > $tmp_mnt/bin/mount-rootfs-and-do-chroot.sh <<'__EOF__'
#!/bin/bash
# Usage: $0 /dev/sda5
rootfs=$1
mnt=/sysroot
mounts=

if test -b "${rootfs}"
then

	mkdir -v -p "${mnt}"

	if mount -v "${rootfs}" "${mnt}"
	then

		for i in dev proc sys selinux
		do
			if test -d /${i} && test -d "${mnt}/${i}"
			then
				mount -v --bind /${i} "${mnt}/${i}"
			fi
		done

		chroot "${mnt}" su -

		while read b m rest
		do
			case "${m}" in
				${mnt}*)
					mounts="${m} ${mounts}"
				;;
			esac
		done <<-EOF
`
cat < /proc/mounts
`
EOF

		for i in ${mounts}
		do
			umount -v "${i}"
		done

	fi

fi
__EOF__