stop services in reverse order
[rrq/overlay-boot.git] / functions
index 0b01df810cd0504583bb79261146a83edb9b472d..67ea3ad51b143476c0ba5f3a82bfaa52d438c4ee 100644 (file)
--- a/functions
+++ b/functions
@@ -8,25 +8,29 @@ die() {
 # grab and set a configuration variable
 # $1 = variable, [ $2 = default .. error otherwise ]
 config() {
-    eval $1="$(sed "/^$1=.*/{s|^$1=||;b};d" $CONFIG)"
+    eval $1="'$(sed "/^$1=.*/{s|^$1=||;b};d" $CONFIG)'"
     [ -z "$(eval echo "\$$1")" ] || return 0
     [ $# -lt 2 ] && die "Missing $1=... in $CONFIG"
     eval $1="'$2'"
     eval echo "$1=\$$1"
 }
 
-# Unless the subhost already has a private /etc/network/interfaces,
-# install an "empty" one
+# Install a default $1/etc/network/interfaces on the subhost root $1
 setup_networking() {
-    [ -r $UPPER/etc/network/interfaces ] && return 0
-    [ "$UPPER/etc/network" = "/etc/network" ] && exit 1
-    mkdir -p $UPPER/etc/network
-    cat <<EOF > $UPPER/etc/network/interfaces
+    [ -r $1/etc/network/interfaces ] && return 0
+    mkdir -p $1/etc/network
+    cat <<EOF >> $1/etc/network/interfaces
 # Generated for $NAME subhost
 auto lo
 iface lo inet loopback
 EOF
-    return 1
+    for IF in $(ip netns exec $NAME ip link show | grep "^eth") ; do
+       cat <<EOF >> $1/etc/network/interfaces
+
+auto eth$i
+iface eth$i inet manual
+EOF
+    done
 }
 
 # Setup the network namespace for the given $CABLES
@@ -36,19 +40,12 @@ setup_veth_cables() {
     NETNS="$1"
     shift 1
     i=0
-    ADD=false
-    setup_networking || ADD=true
     for C in "$@" ; do
        IF=$NETNS$i
        MAC="${C#*=}"
        [ -z "$MAC" ] || MAC="address $MAC"
        ip link add $IF type veth peer name eth$i $MAC netns $NETNS
        ip link set $IF up
-       $ADD && cat <<EOF >> $UPPER/etc/network/interfaces
-
-auto eth$i
-iface eth$i inet manual
-EOF
        BR="${C%=*}"
        if [ -z "$BR" ] ; then
            ip link set $IF
@@ -92,13 +89,14 @@ setup_overlay() {
     # LIVE is the same as LOWER then skip the overlay; just assume
     # a proper chroot system exists at LIVE.
     if [ "$LIVE" != "$LOWER" ] ; then
-       # setup $UPPER/run
-       mkdir -p "$UPPER/run"
-       mount -t tmpfs -osize=100M tmpfs "$UPPER/run"
-       mkdir -p "$UPPER/run/lock"
+       # sanity check
+       [ -d "$WORK" ] || die "WORK=$WORK is not a directory"
+       [ -d "$UPPER" ] || die "UPPER=$UPPER is not a directory"
+       [ -d "$LOWER" ] || die "LOWER=LOWPER is not a directory"
+       [ -d "$LIVE" ] || die "LOWER=LOWPER is not a directory"
        # setup $UPPER/dev
        mkdir -p "$UPPER/dev"
-       mount -t tmpfs -osize=100M tmpfs "$UPPER/dev"
+       mount -t tmpfs -osize=50M tmpfs "$UPPER/dev"
        mknod -m 622 "$UPPER/dev/console" c 5 1
        mknod -m 666 "$UPPER/dev/null" c 1 3
        mknod -m 666 "$UPPER/dev/zero" c 1 5
@@ -118,10 +116,11 @@ setup_overlay() {
        mkdir "$UPPER/dev/pts"
        chmod 1777 "$UPPER/dev/shm"
 
+       # all good so far ; now avoid using the host's networking setup
+       setup_networking "$UPPER"
+
        OLY="-olowerdir=$3,upperdir=$UPPER,workdir=$5"
-       if mount -t overlay "$OLY" $1 $2 ; then
-           : # all good
-       else
+       if ! mount -t overlay "$OLY" $1 $2 ; then
            umount -R "$UPPER/dev"
            umount "$UPPER/run"
            die "Cannot set up the overlay mount $2"