#!/bin/execlineb -S0
#
# anopa - Copyright (C) 2015-2017 Olivier Brunel
#
# aa-stage4
# Copyright (C) 2015 Olivier Brunel <jjk@jjacky.com>
#
# This file is part of anopa.
#
# anopa is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# anopa is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# anopa. If not, see http://www.gnu.org/licenses/
#
/bin/emptyenv /bin/s6-envdir /etc/anopa/env /bin/exec
# Make sure we're sane
cd /
redirfd -r 0 /dev/console
redirfd -w 1 /dev/console
fdmove -c 2 1
foreground { aa-echo -B "Stage 4: Unmounting root file system..." }
foreground {
foreground { emptyenv -c s6-setsid aa-ctty -O3 -s aa-stop -r /services -a }
if { aa-test -e /services/.anopa/post-stop }
importas -u RC ? emptyenv -c /services/.anopa/post-stop $RC
}
# At this point everything should be unmounted, so we need to remount what's
# needed for aa-terminate to work. We can't not umount /proc & /dev in aa-stop
# above and then assume they're here, since e.g. moving them back (from root-fs)
# might have failed. (And trying to mount /dev on top of it won't prevent
# aa-terminate from doing its thing properly.)
foreground { aa-echo -B "Mounting /dev & /proc and terminating..." }
foreground { aa-mount -t devtmpfs dev /dev }
foreground { aa-mount -t proc proc /proc }
foreground { if -n { aa-terminate -la }
foreground { aa-echo -Be "Failed to terminate (umount/close all)" }
foreground { aa-echo -t "Mounting /dev & /proc and trying to open a shell; " +g exit +w " to ${1}" }
foreground { aa-mount -t devtmpfs dev /dev }
foreground { aa-mount -t proc proc /proc }
redirfd -r 0 /dev/console redirfd -w 1 /dev/console fdmove -c 2 1
emptyenv -c sh -i
}
aa-reboot --${1}