#!/bin/sh
# Shutdown script for initramfs. It's automatically started by
# systemd (if you use it) on shutdown, no need for any tweaks.
# Purpose of this script is to unmount everything cleanly.
#
# Author: Tomas M <http://www.linux-live.org/>
#

. /lib/config
. /lib/livekitlib

debug_start

debug_log "Entering shutdown procedures of Linux Live Kit"
debug_log "Called with arguments: " "$*"

# if debug is enabled, run shell now
debug_shell

detach_free_loops()
{
   losetup | cut -d : -f 1 | xargs -r -n 1 losetup -d
}

# $1=dir
umount_all()
{
   tac /proc/mounts | cut -d " " -f 2 | grep ^$1 | while read LINE; do
      umount $LINE 2>/dev/null
   done
}

# Update devs so we are aware of all active /dev/loop* files.
# Detach loop devices which are no longer used
debug_log "- Detaching loops"
mdev -s
detach_free_loops

# do it the dirty way, simply try to umount everything to get rid of most mounts
debug_log "- Unmounting submounts of union"
umount_all /oldroot

# free aufs of /run mount, and umount aufs
debug_log "- Unmounting union itself"
mkdir /run2
mount --move /oldroot/run /run2
umount /oldroot

# remember from which device we are started, so we can eject it later
DEVICE="$(cat /proc/mounts | grep /memory/data | grep /dev/ | cut -d " " -f 1)"

debug_log "- going through several cycles of umounts to clear everything left"
umount_all /run2
detach_free_loops
umount_all /run2
detach_free_loops
umount_all /run2

# eject cdrom device if we were running from it
for i in $(cat /proc/sys/dev/cdrom/info | grep name); do
   if [ "$DEVICE" = "/dev/$i" ]; then
      echo "[  OK  ] Attemptiong to eject /dev/$i..."
      eject /dev/$i
      echo "[  OK  ] CD/DVD tray will close in 6 seconds..."
      sleep 6
      eject -t /dev/$i
   fi
done

debug_shell

debug_log $1 -f
$1 -f

debug_log reboot -f
reboot -f

echo We should never reach so far. Something is totally fucked up.
echo Here you have a shell, to experiment with the universe.
/bin/sh
