1eb8dc403SDave Cobbley#!/bin/sh
2eb8dc403SDave Cobbley# Copyright (C) 2011 O.S. Systems Software LTDA.
3eb8dc403SDave Cobbley# Licensed on MIT
4eb8dc403SDave Cobbley
5eb8dc403SDave Cobbleyfinish_enabled() {
6eb8dc403SDave Cobbley	return 0
7eb8dc403SDave Cobbley}
8eb8dc403SDave Cobbley
9eb8dc403SDave Cobbleyfinish_run() {
10eb8dc403SDave Cobbley	if [ -n "$ROOTFS_DIR" ]; then
11eb8dc403SDave Cobbley		if [ ! -d $ROOTFS_DIR/dev ]; then
12eb8dc403SDave Cobbley			fatal "ERROR: There's no '/dev' on rootfs."
13eb8dc403SDave Cobbley		fi
14eb8dc403SDave Cobbley
157e0e3c0cSAndrew Geissler		# Unmount anything that was automounted by busybox via mdev-mount.sh.
167e0e3c0cSAndrew Geissler		# We're about to switch_root, and leaving anything mounted will prevent
177e0e3c0cSAndrew Geissler		# the next rootfs from modifying the block device.  Ignore ROOT_DISK,
187e0e3c0cSAndrew Geissler		# if it was set by setup-live, because it'll be mounted over loopback
197e0e3c0cSAndrew Geissler		# to ROOTFS_DIR.
207e0e3c0cSAndrew Geissler		local dev
217e0e3c0cSAndrew Geissler		for dev in /run/media/*; do
227e0e3c0cSAndrew Geissler			if mountpoint -q "${dev}" && [ "${dev##*/}" != "${ROOT_DISK}" ]; then
237e0e3c0cSAndrew Geissler				umount -f "${dev}" || debug "Failed to unmount ${dev}"
247e0e3c0cSAndrew Geissler			fi
257e0e3c0cSAndrew Geissler		done
267e0e3c0cSAndrew Geissler
27eb8dc403SDave Cobbley		info "Switching root to '$ROOTFS_DIR'..."
28eb8dc403SDave Cobbley
29*615f2f11SAndrew Geissler		debug "Moving basic mounts onto rootfs"
30*615f2f11SAndrew Geissler		for dir in `awk '/\/dev.* \/run\/media/{print $2}' /proc/mounts`; do
31*615f2f11SAndrew Geissler			# Parse any OCT or HEX encoded chars such as spaces
32*615f2f11SAndrew Geissler			# in the mount points to actual ASCII chars
33*615f2f11SAndrew Geissler			dir=`printf $dir`
34*615f2f11SAndrew Geissler			mkdir -p "${ROOTFS_DIR}/media/${dir##*/}"
35*615f2f11SAndrew Geissler			mount -n --move "$dir" "${ROOTFS_DIR}/media/${dir##*/}"
36*615f2f11SAndrew Geissler		done
37*615f2f11SAndrew Geissler
38eb8dc403SDave Cobbley		debug "Moving /dev, /proc and /sys onto rootfs..."
39eb8dc403SDave Cobbley		mount --move /dev $ROOTFS_DIR/dev
40eb8dc403SDave Cobbley		mount --move /proc $ROOTFS_DIR/proc
41eb8dc403SDave Cobbley		mount --move /sys $ROOTFS_DIR/sys
42eb8dc403SDave Cobbley
43eb8dc403SDave Cobbley		cd $ROOTFS_DIR
44eb8dc403SDave Cobbley		exec switch_root -c /dev/console $ROOTFS_DIR ${bootparam_init:-/sbin/init}
45eb8dc403SDave Cobbley	else
46eb8dc403SDave Cobbley		debug "No rootfs has been set"
47eb8dc403SDave Cobbley	fi
48eb8dc403SDave Cobbley}
49