#!/bin/bash

[ "$1" == "--clean" ] || exit 0

function err {
       echo "$1"
       exit 1
}

# Check shared ids don't match in criu mntns and test mntns
OUT="zdtm/static/mnt_ext_sharing.out"
[ -f "$OUT" ] || err "No $OUT file"

SHID=$(cat $OUT | grep internal_shared_id | awk '{print $5}')
[ -z "$SHID" ] && err "Failed to get shared id from file"
MATCH=$(cat /proc/self/mountinfo | grep "\<shared:$SHID\>.* - tmpfs zdtm_ext_sharing.internal")
[ -z "$MATCH" ] || err "Can lookup internal shared id $SHID in criu mntns"

TMP="/tmp/zdtm_ext_sharing.tmp"
echo "Cleanup mnt_ext_sharing"
umount "$TMP/internal"
umount "$TMP"
rm -rf $TMP

rm -rf "mnt_ext_sharing.test"

exit 0
