# Local filesystem mounting			-*- shell-script -*-

if [ -e "/scripts/halium-hooks" ]; then
	# Hook file found
	source /scripts/halium-hooks
	WITH_HOOKS="yes"
else
	WITH_HOOKS="no"
fi

_log_msg() {
	if [ "$quiet" = "y" ]; then return; fi
	printf "$@" > /dev/kmsg || true
}

pre_mountroot() {
	[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/local-top"
	run_scripts /scripts/local-top
	[ "$quiet" != "y" ] && log_end_msg
}

tell_kmsg() {
	# Echos a string into /dev/kmsg, ignoring errors.
	echo "initrd: $1" >/dev/kmsg || true
}

halium_hook() {
	# Allows calling an user-defined hook. Hooks must go in /scripts/halium-hooks.
	# Call this function like this:
	#     halium_hook test
	#
	# In this example, it will check for and eventually call the halium_hook_test()
	# function.

	func="halium_hook_${1}"
	shift

	if [ "${WITH_HOOKS}" == "yes" ] && [ "$(type -t ${func})" == "${func}" ]; then
		${func} ${@}
	fi
}

halium_panic() {
	# Puts panic reason into kmsg and then starts the panic handlers
	REASON="$1"
	tell_kmsg "PANIC for reason: $REASON"
	halium_hook panic
	panic $REASON
}

droidian_minienv() {
	# Executes the command preferring the minienv libraries
	executable="$(which ${1})"
	shift
	/minienv/lib/droidian-minienv-linker.so \
		--inhibit-cache \
		--library-path '/minienv/usr/${LIB}:/minienv/${LIB}:/usr/${LIB}:/${LIB}' \
		${executable} ${@}
}

identify_boot_mode() {
	# Our current list of supported boot modes:
	## BOOT_MODE = halium and android
	BOOT_MODE='halium'

	# The boot reason is exported via /proc/cmdline
	# The standard method is using androidboot.mode parameter.

	for x in $(cat /proc/cmdline); do
		case ${x} in
		androidboot.mode=*)
			android_bootmode=${x#*=}
			;;
		# Android 9 system-as-root
		skip_initramfs)
			normal_boot="y"
			;;
		# Android 10+ recovery-as-boot
		androidboot.force_normal_boot=1)
			normal_boot="y"
			;;
		# Android 12+ (GKI 2.0+) recovery-as-boot
		bootconfig)
			if grep -q 'androidboot.force_normal_boot = "1"' /proc/bootconfig; then
				normal_boot="y"
			fi
			android_bootmode=$(awk -F'"' '/androidboot.bootreason/ {print $2}' /proc/bootconfig)
			;;
		esac
	done

	case "${android_bootmode}" in
	charger|usb) BOOT_MODE="android" ;;
	esac

	## Some devices may be using 'bootreason', others 'boot_reason'
	## XXX: Find a better way to handle device specifics here

	# Krillin
	if [ -f /sys/class/BOOT/BOOT/boot/boot_mode ]; then
		boot_reason=$(cat /sys/class/BOOT/BOOT/boot/boot_mode)
		case "${boot_reason}" in
		1) BOOT_MODE="android" ;; # Meta
		4) BOOT_MODE="android" ;; # Factory
		8) BOOT_MODE="android" ;; # Power off charging
		9) BOOT_MODE="android" ;; # Low power charging
		esac
	fi

	# System-as-root or a device without dedicated recovery partition
	if [ -f /ramdisk-recovery.img ] && [ -z "$normal_boot" ]; then
		BOOT_MODE="recovery"
	fi

	# On Android 8+ devices (only which Droidian supports realistically) the
	# 'android' boot mode is broken and should be avoided.
	if [ "$BOOT_MODE" = "android" ] && [ "$HALIUM_RECOVERY" != "yes" ]; then
		tell_kmsg "Rebooting to reset non-standard boot mode..."
		reboot -f
	fi

	tell_kmsg "boot mode: $BOOT_MODE"
}

identify_android_image() {
	# Checks for the provided Android image. If it's called system.img, it
	# should be mounted at Android's /system. If it's called android-rootfs.img,
	# it should be mounted at Android's /.
	# Sets $ANDROID_IMAGE_MODE to:
	#   * "rootfs" if the image should be mounted at '/android/'
	#   * "system" if the image should be mounted at '/android/system/'
	#   * "unknown" if neither is found

	SYSTEM_SEARCH_PATHS="/halium-system/var/lib/lxc/android/system.img"
	[ "${file_layout}" == "halium" ] && SYSTEM_SEARCH_PATHS="/tmpmnt/system.img ${SYSTEM_SEARCH_PATHS}"

	ROOTFS_SEARCH_PATHS="/halium-system/var/lib/lxc/android/android-rootfs.img"
	[ "${file_layout}" == "halium" ] && ROOTFS_SEARCH_PATHS="/tmpmnt/android-rootfs.img ${ROOTFS_SEARCH_PATHS}"

	for image in ${SYSTEM_SEARCH_PATHS}; do
		if [ -f "${image}" ]; then
			ANDROID_IMAGE_MODE="system"
			ANDROID_IMAGE="${image}"

			return
		fi
	done

	for image in ${ROOTFS_SEARCH_PATHS}; do
		if [ -f "${image}" ]; then
			ANDROID_IMAGE_MODE="rootfs"
			ANDROID_IMAGE="${image}"

			return
		fi
	done

	ANDROID_IMAGE_MODE="unknown"
}

set_halium_version_properties() {
	halium_system=$1
	android_data=$2

	channel_ini=$1/etc/system-image/channel.ini
	def_language=$1/custom/default_language

	halium="unknown"
	device="unknown"
	custom="unknown"
	version="unknown"
	channel="unknown"
	def_lang="unknown"

	if [ -f "$channel_ini" ]; then
		IFS=','
		for i in $(grep version_detail $channel_ini | awk -F ' ' '{print $2}'); do
			id=${i%=*}
			case $id in
			halium) halium=${i#halium=} ;;
			device) device=${i#device=} ;;
			custom) custom=${i#custom=} ;;
			version) version=${i#version=} ;;
			esac
		done
		unset IFS
		channel=$(grep channel $channel_ini | awk -F ' ' '{print $2}')
	fi

	if [ -f "$def_language" ]; then
		lang=$(cat $def_language)
		if [ -n "$lang" ]; then
			def_lang=$lang
		fi
	fi

	# Write down so the android property system can load them automatically
	mkdir -p $android_data/property
	chmod 700 $android_data/property
	echo -n "$halium" >$android_data/property/persist.halium.version.rootfs
	echo -n "$device" >$android_data/property/persist.halium.version.device
	echo -n "$custom" >$android_data/property/persist.halium.version.custom
	echo -n "$channel" >$android_data/property/persist.halium.version.channel
	echo -n "$version" >$android_data/property/persist.halium.version
	echo -n "$def_lang" >$android_data/property/persist.halium.default_language
	chmod 600 $android_data/property/persist.halium*
}

mount_android_partitions() {
	fstab=$1
	mount_root=$2
	real_userdata=$3

	tell_kmsg "checking fstab $fstab for additional mount points"

	# On systems with A/B partition layout, current slot is provided via cmdline parameter.
	ab_slot_suffix=$(grep -o 'androidboot\.slot_suffix=..' /proc/cmdline |  cut -d "=" -f2)
	[ ! -z "$ab_slot_suffix" ] && tell_kmsg "A/B slot system detected! Slot suffix is $ab_slot_suffix"

	cat ${fstab} | while read line; do
		set -- $line

		# stop processing if we hit the "#endhalium" comment in the file
		echo $1 | egrep -q "^#endhalium" && break

		# Skip any unwanted entry
		echo $1 | egrep -q "^#" && continue
		([ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]) && continue
		([ "$2" = "/system" ] || [ "$2" = "/data" ] || [ "$2" = "/" ]) && continue

		label=$(echo $1 | awk -F/ '{print $NF}')
		[ -z "$label" ] && continue

		tell_kmsg "checking mount label $label"

		# In case fstab provides /dev/mmcblk0p* lines
		path="/dev/$label"
		for dir in by-partlabel by-name by-label by-path by-uuid by-partuuid by-id; do
			# On A/B systems not all of the partitions are duplicated, so we have to check with and without suffix
			if [ -e "/dev/disk/$dir/$label$ab_slot_suffix" ]; then
				path="/dev/disk/$dir/$label$ab_slot_suffix"
				break
			elif [ -e "/dev/disk/$dir/$label" ]; then
				path="/dev/disk/$dir/$label"
				break
			fi
		done

		[ ! -e "$path" ] && continue

		mkdir -p ${mount_root}/$2
		tell_kmsg "mounting $path as ${mount_root}/$2"
		mount $path ${mount_root}/$2 -t $3 -o $4
	done

	# Provide a bind mount from /cache to /userdata/cache on systems without a dedicated cache partition
	if [ ! -e ${mount_root}/cache ]; then
		if [ ! -d ${real_userdata}/cache ]; then
			mkdir ${real_userdata}/cache
		fi
		mkdir ${mount_root}/cache
		mount -o bind ${real_userdata}/cache ${mount_root}/cache
	fi

	# Create an appropriate symlink for vendor files
	if [ ! -e ${mount_root}/vendor ]; then
		ln -sf system/vendor ${mount_root}/vendor
	fi
}

mount_halium_overlay() {
	source=$1
	target=$2

	if [ -d ${source} ]; then
		OLD_PWD=$PWD
		cd ${source}

		for overlay in $(find . -type f); do
			[ -f ${target}/${overlay} ] && mount --bind ${source}/${overlay} ${target}/${overlay}
		done

		cd $OLD_PWD
	fi
}

sync_dirs() {
	base=$1
	source=$2
	target=$3

	OLD_PWD=$PWD
	cd $base

	for file in $source/*; do
		# Skip empty directories
		[ ! -e "$base/$file" ] && continue

		# If the target already exists as a file or link, there's nothing we can do
		[ -e "$target/$file" -o -L "$target/$file" ] && [ ! -d "$target/$file" ] && continue

		# If the target doesn't exist, just copy it over
		if [ ! -e "$target/$file" -a ! -L "$target/$file" ]; then
			cp -Ra "$base/$file" "$target/$file"
			continue
		fi

		# That leaves us with directories and a recursive call
		[ -d $file ] && sync_dirs $base $file $target
	done

	cd $OLD_PWD
}

resize_userdata_if_needed() {

	# See if the filesystem on the userdata partition needs resizing (usually on first boot).
	# If the difference between the partition size and the filesystem size is above a small
	# threshold, assume it needs resizing to fill the partition.

	path=$1

	# Partition size in 1k blocks
	case $path in
	/dev/mmcblk*)
		pblocks=$(grep ${path#/dev/*} /proc/partitions | awk {'print $3'})
		;;
	/dev/disk*)
		pblocks=$(grep $(basename $(readlink $path)) /proc/partitions | awk {'print $3'})
		;;
	esac
	# Filesystem size in 4k blocks
	fsblocks=$(dumpe2fs -h $path | grep "Block count" | awk {'print $3'})
	# Difference between the reported sizes in 1k blocks
	dblocks=$((pblocks - 4 * fsblocks))
	if [ $dblocks -gt 10000 ]; then
		resize2fs -f $path
		tell_kmsg "resized userdata filesystem to fill $path"
	fi
}

resize_lvm_if_needed() {
	# Like resize_userdata_if_needed, but for devices with the droidian
	# LVM vg in userdata.
	#
	# Note: this is meant to be done as an online resize. Thus, the stamp
	# file is checked on the mounted rootfs partition.

	vg=${1}
	lv=${2}

	if [ ! -e "/halium-system/var/lib/halium/requires-lvm-resize" ]; then
		# Bye bye
		return
	fi

	part="/dev/${vg}/${lv}"
	pv="$(lvm lvs --noheadings -o name,devices "$vg" | awk "/$lv/ {print \$2}" | head -n1 | cut -d'(' -f1)"

	# Resize the underlying Physical Volume
	if ! lvm pvresize ${pv}; then
		tell_kmsg "Unable to resize PV ${pv}"
		return
	fi

	# Now resize the rootfs LV with all the free extents
	if ! lvm lvresize -l +100%FREE ${part}; then
		tell_kmsg "Unable to resize LV ${lv}"
		return
	fi

	# Finally resize the filesystem
	if [ "$(blkid ${part} -o value -s TYPE)" == "ext4" ]; then
		e2fsck -fy ${part}

		if ! resize2fs -f ${part}; then
			tell_kmsg "Unable to resize ext4 partition ${part}"
			return
		fi
	else
		tell_kmsg "Unsupported filesystem on ${part}"
	fi

	rm -f /halium-system/var/lib/halium/requires-lvm-resize
}

identify_file_layout() {
	# Determine if we have a Halium rootfs.img & system.img

	# $file_layout = "halium" means there is a separate rootfs.img and system.img on userdata
	#
	# = "partition" means the rootfs is located on the device's system partition
	# and will contain /var/lib/lxc/android/system.img
	#
	# = "subdir" means the rootfs is located in a folder on the device's userdata partition
	# and will contain /var/lib/lxc/android/system.img

	if [ -e /tmpmnt/rootfs.img ]; then
		imagefile=/tmpmnt/rootfs.img
		file_layout="halium"
	elif [ -e /tmpmnt/ubuntu.img ]; then
		imagefile=/tmpmnt/ubuntu.img
		file_layout="legacy"
	elif [ -d /tmpmnt/halium-rootfs ]; then
		imagefile=/tmpmnt/halium-rootfs
		file_layout="subdir"
	else
		file_layout="partition"
	fi

}

process_bind_mounts() {
	# Goes over /etc/system-image/writable-paths to create the correct fstab for
	# the bind-mounts. Writes them into ${rootmnt}/run/image.fstab which is
	# bind-mounted to /etc/fstab

	if [ ! -e ${rootmnt}/etc/system-image/writable-paths ]; then
		tell_kmsg "This rootfs does not have any writable-paths defined"
		return 0
	fi

	# Mount a tmpfs in /run of rootfs to put the future image.fstab
	mount -o rw,nosuid,noexec,relatime,mode=755 -t tmpfs tmpfs ${rootmnt}/run
	# Prepare the fstab
	FSTAB=${rootmnt}/etc/fstab
	touch ${rootmnt}/run/image.fstab
	mount -o bind ${rootmnt}/run/image.fstab $FSTAB ||halium_panic "Could not bind-mount fstab"
	echo "/dev/root / rootfs defaults,ro 0 0" >>$FSTAB

	tell_kmsg "Adding bind-mounts to $FSTAB"
	# Process the list of bind-mounts
	# (but don't mount them, mountall will do it)
	cat ${rootmnt}/etc/system-image/writable-paths | while read line; do
		set -- $line
		# Skip invalid/commented entries
		([ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ] || [ -z "$5" ]) && continue
		[ "$1" = "#" ] && continue

		# Skip invalid mount points
		dstpath="${rootmnt}/$1"
		[ ! -e "$dstpath" ] && continue

		if [ "$3" = "temporary" ]; then
			# Temporary entries are simple, just mount a tmpfs
			echo "tmpfs $1 tmpfs $5 0 0" >>$FSTAB
		elif [ "$3" = "persistent" ] || [ "$3" = "synced" ]; then
			# Figure out the source path
			if [ "$2" = "auto" ]; then
				srcpath="${rootmnt}/userdata/system-data/$1"
				path="/userdata/system-data/$1"
			else
				srcpath="${rootmnt}/userdata/$2"
				path="/userdata/$2"
			fi

			if [ ! -e "$srcpath" ]; then
				# Process new persistent or synced paths
				dstown=$(stat -c "%u:%g" $dstpath)
				dstmode=$(stat -c "%a" $dstpath)
				mkdir -p ${srcpath%/*}
				if [ ! -d "$dstpath" ]; then
					# Deal with redirected files
					if [ "$4" = "transition" ]; then
						cp -a $dstpath $srcpath
					else
						touch $srcpath
						chown $dstown $srcpath
						chmod $dstmode $srcpath
					fi
				else
					# Deal with redirected directories
					if [ "$4" = "transition" ] || [ "$3" = "synced" ]; then
						cp -aR $dstpath $srcpath
					else
						mkdir $srcpath
						chown $dstown $srcpath
						chmod $dstmode $srcpath
					fi
				fi
			elif [ "$3" = "synced" ]; then
				# Process existing synced paths
				sync_dirs $dstpath . $srcpath
			fi

			# Write the fstab entry
			if [ "$5" = "none" ]; then
				echo "$path $1 none bind 0 0" >>$FSTAB
			else
				echo "$path $1 none bind,$5 0 0" >>$FSTAB
			fi
		else
			continue
		fi
	done
}

extract_android_ramdisk() {
	# Extracts the ramdisk from /android-system/boot/android-ramdisk.img to
	# /android-rootfs

	# NOTE: we should find a faster way of doing that or cache it
	tell_kmsg "extracting android ramdisk"
	OLD_CWD=$(pwd)
	mount -n -t tmpfs tmpfs /android-rootfs
	cd /android-rootfs
	cat /android-system/boot/android-ramdisk.img | gzip -d | cpio -i
	cd $OLD_CWD
}

mount_kernel_modules() {
	# Bind-mount /lib/modules from Android
	[ -e ${rootmnt}/android/system/lib/modules ] && mount --bind ${rootmnt}/android/system/lib/modules ${rootmnt}/lib/modules
}

load_kernel_modules() {
	mkdir -p /lib/modules
	cd /lib/modules
	ln -sf /lib/modules "/lib/modules/$(uname -r)"

	files="/override/modules.load /lib/modules/modules.load /override/modules.load.recovery /lib/modules/modules.load.recovery"
	for file in $files; do
		if [ -f "$file" ]; then
			module_list="$file"
			break
		fi
	done

	if [ -n "$module_list" ]; then
	        tell_kmsg "Loading kernel modules from $module_list"

		cat $module_list | while read line; do
			set -- $line
			# Skip commented entries
			[ "$1" = "#" ] && continue
			tell_kmsg "Loading module $1"
			modprobe -a "$1"
		done
	fi

	cd -
}


unlock_encrypted_partition() {
	part="${1}"
	header="${2}"
	name="${3}"
	is_recovery="${4}"

	# FIXME: quit plymouth, it would be better to hide/reshow but we
	# can't when using the drm backend
	[ -e "/usr/bin/plymouth" ] && droidian_minienv plymouth quit || true

	halium_hook setup_touchscreen

	tries="0"
	while [ "${1}" ]; do
		CRYPTTAB_TRIED="${tries}" droidian_minienv unl0kr | \
			droidian_minienv droidian-encryption-helper \
				--device "${part}" \
				--header "${header}" \
				--name "${name}" \
				--rootmnt "${rootmnt}" \
				--strip-newlines
		err="${?}"
		case "${err}" in
			2)
				# Wrong passphrase
				let tries=tries+1

				if [ "${is_recovery}" == "yes" ] && [ "${tries}" > 5 ]; then
					# Recovery requested, trigger panic after 5 failures
					halium_panic "Unable to unlock root partition"
				fi
				;;
			0)
				if [ ! -e /dev/mapper/${name} ]; then
					halium_panic "Root partition unlocked, but device has not been mapped"
				fi

				break
				;;
			*)
				# Unknown error
				halium_panic "Unknown error ${err} while unlocking root partition"
				break
				;;
		esac
	done

	halium_hook teardown_touchscreen
}

signal_root_move_done() {
	# Signal to droidian-encryption-helper that the root move has
	# been done so that it can chroot() into it

	# No need to do this if the system is already encrypted
	[ -e /run/droidian-encryption-helper.pid ] || return

	touch /run/halium-mounted
	count=0
	while [ -e /run/halium-mounted ] && [ ${count} -lt 20 ]; do
		sleep 1

		let count=count+1
	done
}

mount_vendor_partitions() {
	mkdir /vendor
	mkdir /vendor_dlkm
	if [ -e "/dev/disk/by-partlabel/super" ]; then
		tell_kmsg "mapping super partition"
		losetup -r /dev/loop0 /dev/disk/by-partlabel/super
		/minienv/lib/droidian-minienv-linker.so \
			--inhibit-cache \
			--library-path '/minienv/usr/${LIB}:/minienv/${LIB}:/usr/${LIB}:/${LIB}' \
			/usr/sbin/dmsetup create --concise "$(droidian_minienv /usr/sbin/parse-android-dynparts /dev/loop0)"
	fi

	vendor_images="/tmpmnt/vendor.img /dev/disk/by-partlabel/vendor${1} /dev/disk/by-partlabel/vendor_a /dev/disk/by-partlabel/vendor_b /dev/mapper/dynpart-vendor /dev/mapper/dynpart-vendor${1} /dev/mapper/dynpart-vendor_a /dev/mapper/dynpart-vendor_b"
	for image in $vendor_images; do
	    if [ -e $image ]; then
	        tell_kmsg "mounting vendor from $image"
	        mount $image /vendor -o ro

	        if [ -e "/vendor/build.prop" ]; then
	            tell_kmsg "found valid vendor partition: $image"
	            break
	        else
	            tell_kmsg "$image is not a valid vendor partition"
	            umount /vendor
	        fi
	    fi
	done

	vendor_dlkm_images="/dev/mapper/dynpart-vendor_dlkm /dev/mapper/dynpart-vendor_dlkm${1} /dev/mapper/dynpart-vendor_dlkm_a /dev/mapper/dynpart-vendor_dlkm_b"
	for image in $vendor_dlkm_images; do
	    if [ -e $image ]; then
	        tell_kmsg "mounting vendor_dlkm from $image"
	        mount $image /vendor_dlkm -o ro

	        if [ -e "/vendor_dlkm/etc/build.prop" ]; then
	            tell_kmsg "found valid vendor_dlkm partition: $image"
	            break
	        else
	            tell_kmsg "$image is not a valid vendor_dlkm partition"
	            umount /vendor_dlkm
	        fi
	    fi
	done
}

mountroot() {
	# list of possible userdata partition names
	partlist="userdata UDA DATAFS USERDATA"

	pre_mountroot

	[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/local-premount"
	run_scripts /scripts/local-premount
	[ "$quiet" != "y" ] && log_end_msg

	# Put all of this script's output into /dev/kmsg
	exec &>/dev/kmsg

	load_kernel_modules
	tell_kmsg "Finished loading kernel modules"
	sleep 1

	# busybox mdev -s
	# udevadm trigger

	# Mount root
	#
	# Create a temporary mountpoint for the bindmount
	mkdir -p /tmpmnt

	# Make sure the device has been created by udev before we try to mount
	udevadm settle

	# find the right partition
	for partname in $partlist; do
		part=$(find /dev -name $partname | tail -1)
		[ -z "$part" ] && continue
		path=$(readlink -f $part)
		[ -n "$path" ] && break
	done

	# On systems with A/B partition layout, current slot is provided via cmdline parameter or bootconfig.
	if [ -e /proc/bootconfig ]; then
	    ab_slot_suffix=$(grep -o 'androidboot\.slot_suffix = ".."' /proc/bootconfig | cut -d '"' -f2)
	fi

	if [ -z "$ab_slot_suffix" ]; then
	    ab_slot_suffix=$(grep -o 'androidboot\.slot_suffix=..' /proc/cmdline |  cut -d "=" -f2)
	fi

	if [ -z "$path" ] && [ ! -z "$ab_slot_suffix" ] ; then
		tell_kmsg "Searching for A/B data partition on slot $ab_slot_suffix."

		for partname in $partlist; do
			part=$(find /dev -name "$partname$ab_slot_suffix" | tail -1)
			[ -z "$part" ] && continue
			path=$(readlink -f $part)
			[ -n "$path" ] && break
		done
	fi

	# override with a possible cmdline parameter
	if grep -q datapart= /proc/cmdline; then
		for x in $(cat /proc/cmdline); do
			case ${x} in
			datapart=*)
				path=${x#*=}
				;;
			esac
		done
	fi

	if [ -z "$path" ]; then
		halium_panic "Couldn't find data partition."
	fi

	# Check if this is a recovery image. If so, check the cmdline for
	# hints on the current requested mode - we support booting the system
	# even on recovery images
	if grep -q halium.recovery /proc/cmdline; then
		HALIUM_RECOVERY="yes"
		for x in $(cat /proc/cmdline); do
			case ${x} in
			skip_initramfs|androidboot.force_normal_boot=1)
				HALIUM_RECOVERY="no"
				;;
			bootconfig) # Android 12+ (GKI) recovery-as-boot
				if grep -q 'androidboot.force_normal_boot = "1"' /proc/bootconfig; then
					HALIUM_RECOVERY="no"
				fi
			esac
		done
	else
		HALIUM_RECOVERY="no"
	fi

	identify_boot_mode

	mount_vendor_partitions "$ab_slot_suffix"

	# Check if we should prefer LVM mounts. If that's the case, try
	# mounting the droidian-rootfs LV first, then fallback to the standard
	# halium way (where we assume that userdata is a mountable partition)
	use_lvm="yes"
	if grep -q droidian.lvm /proc/cmdline; then
		for x in $(cat /proc/cmdline); do
			case ${x} in
			droidian.lvm.disable)
				use_lvm="no"
				;;
			droidian.lvm.vg=*)
				search_vg=${x#*=}
				;;
			droidian.lvm.root_lv=*)
				root_lv=${x#*=}
				;;
			droidian.lvm.reserved_lv=*)
				reserved_lv=${x#*=}
				;;
			droidian.lvm.persistent_lv=*)
				persistent_lv=${x#*=}
				;;
			esac
		done
	fi

	if [ "${use_lvm}" == "yes" ]; then
		# Set default search vgs and root_lv
		[ -z "${search_vg}" ] && search_vg="droidian sailfish"
		[ -z "${root_lv}" ] && root_lv="droidian-rootfs"
		[ -z "${reserved_lv}" ] && reserved_lv="droidian-reserved"
		[ -z "${persistent_lv}" ] && persistent_lv="droidian-persistent"

		for try in 1 2 3 4 5; do
			lvm vgscan --mknodes
			for vg in ${search_vg}; do
				if lvm vgchange -ay ${vg} && [ -e "/dev/${vg}/${root_lv}" ]; then
					tell_kmsg "Found LVM VG ${vg}"
					_syspart="/dev/${vg}/${root_lv}"
					file_layout="partition"
					break
				fi
			done
			[ -n "${_syspart}" ] && break
			sleep 2
		done
		
		# If _syspart is empty, LVM discovery failed. Let's continue
		# normally
		if [ -z "${_syspart}" ]; then
			unset use_lvm
		elif [ -e "/dev/${vg}/${reserved_lv}" ] && [ "$(blkid /dev/${vg}/${reserved_lv} -o value -s TYPE)" == "crypto_LUKS" ]; then
			# LUKS header, we should unlock
			unlocked_syspart_name="droidian_encrypted"
			use_luks="yes"
			unlock_encrypted_partition /dev/${vg}/${root_lv} /dev/${vg}/${reserved_lv} ${unlocked_syspart_name} ${HALIUM_RECOVERY}
			_syspart="/dev/mapper/${unlocked_syspart_name}"
		fi
	fi

	if [ "${use_lvm}" != "yes" ]; then
		tell_kmsg "checking filesystem integrity for the userdata partition"
		# Mounting and umounting first, let the kernel handle the journal and
		# orphaned inodes (faster than e2fsck). Then, just run e2fsck forcing -y.
		# Also check the amount of time used by to check the filesystem.
		fsck_start=$(date +%s)
		mount -o errors=remount-ro $path /tmpmnt
		umount /tmpmnt
		e2fsck -y $path >/run/e2fsck.out 2>&1
		fsck_end=$(date +%s)
		tell_kmsg "checking filesystem for userdata took (including e2fsck) $((fsck_end - fsck_start)) seconds"

		resize_userdata_if_needed ${path}

		tell_kmsg "mounting $path"

		# Mount the data partition to a temporary mount point
		# FIXME: data=journal used on ext4 as a workaround for bug 1387214
		[ `blkid $path -o value -s TYPE` = "ext4" ] && OPTIONS="data=journal,"
		mount -o discard,$OPTIONS $path /tmpmnt

		# Set $_syspart if it is specified as systempart= on the command line
		# If the .ignore_systempart flag is in the data partition, ignore
		if grep -q systempart= /proc/cmdline && [ ! -e /tmpmnt/.ignore_systempart ]; then
			for x in $(cat /proc/cmdline); do
				case ${x} in
				systempart=*)
					_syspart=${x#*=}
					;;
				esac
			done
		fi

		identify_file_layout
	fi

	# If both $imagefile and $_syspart are set, something is wrong. The strange
	# output from this could be a clue in that situation.
	tell_kmsg "Halium rootfs is $imagefile $_syspart"

	# Prepare the root filesystem
	# NOTE: We mount it read-write in all cases, then remount read-only.
	#       This is to workaround a behaviour change in busybox which now
	#       uses read-only loops if the fs is initially mounted read-only.
	#       An alternative implementation would be to add losetup support
	#       to busybox and do the mount in two steps (rw loop, ro fs).

	mkdir -p /halium-system

	tell_kmsg "mounting system rootfs at /halium-system"
	if [ -n "$_syspart" ]; then
		mount -o rw $_syspart /halium-system

		[ "${use_lvm}" == "yes" ] && resize_lvm_if_needed "${vg}" "${root_lv}"
	elif [ -f "$imagefile" ]; then
		# Rootfs is an image file
		mount -o loop,rw $imagefile /halium-system
	elif [ -d "$imagefile" ]; then
		# Rootfs is a directory
		mount -o bind /tmpmnt/halium-rootfs /halium-system
	fi

	# Identify image mode: either "rootfs" or "system"
	mkdir -p /android-rootfs
	mkdir -p /android-system

	identify_android_image
	[ $ANDROID_IMAGE_MODE = "unknown" ] && tell_kmsg "WARNING: Android system image not found."

	# If either (android) /data/.writable_image or (on rootfs)
	# /.writable_image exist, mount the rootfs as rw
	if [ -e /tmpmnt/.writable_image ] || [ -e /halium-system/.writable_image ]; then
		tell_kmsg "mounting $_syspart $imagefile (image developer mode)"
		mountroot_status="$?"
	else
		# Neither of those exist, remount read-only
		tell_kmsg "mounting $_syspart $imagefile (user mode)"
		mount -o remount,ro /halium-system
		mountroot_status="$?"
	fi

	# Mount the android system partition to a temporary location
	MOUNT="ro"
	MOUNT_LOCATION="/android-$ANDROID_IMAGE_MODE"
	[ -e /tmpmnt/.writable_device_image -o -e /halium-system/.writable_device_image ] && MOUNT="rw"
	tell_kmsg "mounting android system image ($ANDROID_IMAGE) $MOUNT, in $MOUNT_LOCATION ($ANDROID_IMAGE_MODE mode)"
	if [ -n "${ANDROID_IMAGE}" ]; then
		mount -o loop,$MOUNT "$ANDROID_IMAGE" $MOUNT_LOCATION \
			|| tell_kmsg "WARNING: Failed to mount Android system.img."
	else
		tell_kmsg "WARNING: Unable to mount Android system image as it hasn't been found."
	fi

	[ $ANDROID_IMAGE_MODE = "rootfs" ] && mount -o bind $MOUNT_LOCATION/system /android-system
	[ $ANDROID_IMAGE_MODE = "system" ] && extract_android_ramdisk

	# Determine whether we should boot to rootfs or Android
	if [ "${HALIUM_RECOVERY}" == "yes" ]; then
		halium_hook recovery_ready
		halium_panic "Recovery mode ready"
	fi

	if [ "$BOOT_MODE" = "recovery" ]; then
		tell_kmsg "Recovery boot mode for system-as-root devices"

		# Clean up mounted partitions so recovery can manage them
		umount -d /android-system /android-rootfs /halium-system /tmpmnt
		dmsetup remove_all

		mount -n -t tmpfs tmpfs ${rootmnt}
		cd ${rootmnt}
		if [ -d /lib/modules ]; then
			mkdir -p lib/modules
			mv /lib/modules/* lib/modules/
		fi
		cat /ramdisk-recovery.img | gzip -d | cpio -i
		cd -
		mkdir -p ${rootmnt}/sbin
		ln -s ../init ${rootmnt}/sbin/init
	elif ([ -e $imagefile ] || [ -n "$_syspart" ]) && [ "$BOOT_MODE" = "android" ]; then
		# Bootloader says this is factory or charger mode, boot into Android.
		tell_kmsg "Android boot mode for factory or charger mode"

		mount --move /android-rootfs ${rootmnt}
		[ $ANDROID_IMAGE_MODE = "system" ] && mount --move /android-system ${rootmnt}/system

		[ "${use_luks}" == "yes" ] && signal_root_move_done

		# Mount all the Android partitions
		mount_android_partitions "${rootmnt}/fstab*" ${rootmnt} /tmpmnt

		mkdir -p ${rootmnt}/halium-system
		mount --move /halium-system ${rootmnt}/halium-system

		# Mounting userdata
		mkdir -p ${rootmnt}/data
		mkdir -p /tmpmnt/android-data
		mount -o bind /tmpmnt/android-data ${rootmnt}/data

		# Set halium version properties
		set_halium_version_properties ${rootmnt}/halium-system ${rootmnt}/data

		# Make sure we're booting into android's init
		ln -s ../init ${rootmnt}/sbin/init
		ln -s ../init ${rootmnt}/sbin/recovery
		tell_kmsg "booting android..."
	elif [ -e "$imagefile" ] || [ -n "$_syspart" ]; then
		# Regular image boot
		tell_kmsg "Normal boot"

		mount --move /halium-system ${rootmnt}
		mkdir -p ${rootmnt}/android

		[ "${use_luks}" == "yes" ] && signal_root_move_done

		# Mounting userdata outside of /android, to avoid having LXC container access it
		mkdir -p ${rootmnt}/userdata
		[ "${use_lvm}" != "yes" ] && mount --move /tmpmnt ${rootmnt}/userdata

		mount --move /android-rootfs ${rootmnt}/var/lib/lxc/android/rootfs
		[ $ANDROID_IMAGE_MODE = "system" ] && mount -o rw,size=4096 -t tmpfs none ${rootmnt}/android
		[ $ANDROID_IMAGE_MODE = "rootfs" ] && mount -o bind ${rootmnt}/var/lib/lxc/android/rootfs ${rootmnt}/android

		mkdir -p ${rootmnt}/android/data ${rootmnt}/android/system

		# Create a fake android data, shared by rootfs and LXC container
		mkdir -p ${rootmnt}/userdata/android-data
		mount -o bind ${rootmnt}/userdata/android-data ${rootmnt}/android/data
		[ ! -h ${rootmnt}/data ] && ln -sf /android/data ${rootmnt}/data

		set_halium_version_properties ${rootmnt} ${rootmnt}/userdata/android-data

		# Get device information
		device=$(grep ^ro.product.device= /android-system/build.prop | sed -e 's/.*=//')
		[ -z "$device" ] && device="unknown" && tell_kmsg "WARNING: Didn't find a device name. Is the Android system image mounted correctly?"
		tell_kmsg "device is $device"

		process_bind_mounts

		# Mount all the Android partitions
		mount_android_partitions "${rootmnt}/var/lib/lxc/android/rootfs/fstab*" ${rootmnt}/android ${rootmnt}/userdata

		# system is a special case
		tell_kmsg "moving Android system to /android/system"
		mount --move /android-system ${rootmnt}/android/system

		if [ -e "/vendor/build.prop" ]; then
			tell_kmsg "moving Android vendor to /android/vendor"
			mkdir -p ${rootmnt}/android/vendor
			mount --move /vendor ${rootmnt}/android/vendor
		fi

		if [ -e "/vendor_dlkm/etc/build.prop" ]; then
			tell_kmsg "moving Android vendor_dlkm to /android/vendor_dlkm"
			mkdir -p ${rootmnt}/android/vendor_dlkm
			mount --move /vendor_dlkm ${rootmnt}/android/vendor_dlkm
		fi

		# halium overlay available in the Android system image (hardware specific configs)
		if [ -e ${rootmnt}/android/system/halium ]; then
			mount_halium_overlay ${rootmnt}/android/system/halium ${rootmnt}
		fi

		# Apply device-specific udev rules
		if [ -e ${rootmnt}/usr/lib/lxc-android-config/70-$device.rules ] &&
			[ ! -f ${rootmnt}/android/system/halium/lib/udev/rules.d/70-android.rules ] &&
			[ "$device" != "unknown" ]; then
			mount --bind ${rootmnt}/usr/lib/lxc-android-config/70-$device.rules ${rootmnt}/lib/udev/rules.d/70-android.rules
		fi

		# Bind-mount /lib/modules from Android
		mount_kernel_modules

		# Bind-mount /var/lib/ureadahead if available on persistent storage
		# this is required because ureadahead runs before mountall
		if [ -e ${rootmnt}/userdata/system-data/var/lib/ureadahead ] &&
			[ -e ${rootmnt}/var/lib/ureadahead ]; then
			mount --bind ${rootmnt}/userdata/system-data/var/lib/ureadahead ${rootmnt}/var/lib/ureadahead
		fi

		# Setup the swap device
		[ -e ${rootmnt}/userdata/SWAP.img ] && swapon ${rootmnt}/userdata/SWAP.img

		# Apply customized content
		for user in ${rootmnt}/userdata/user-data/*; do
			if [ -d ${rootmnt}/custom/home ] && [ ! -e "$user/.customized" ]; then
				tell_kmsg "copying custom content tp "
				cp -Rap ${rootmnt}/custom/home/* "$user/"
				cp -Rap ${rootmnt}/custom/home/.[a-zA-Z0-9]* "$user/"
				touch "$user/.customized"
				dstown=$(stat -c "%u:%g" "$user")
				chown -R $dstown "$user/"
			fi
		done

	else
		# Possibly a re-partitioned device
		halium_panic "Couldn't find a system partition."
	fi

	[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/local-bottom"
	run_scripts /scripts/local-bottom
	[ "$quiet" != "y" ] && log_end_msg
}
