+PND_CloseX(){
+ if [ $CLOSE_X ]; then #the app doesnt want x to run, so we kill it and restart it once the app quits
+ if [ ! $(pidof X) ]; then
+ unset $CLOSE_X
+ else
+ applist=$(lsof /usr/lib/libX11.so.6 | awk '{print $1}'| sort | uniq)
+ whitelist=$(cat ~/pndtest/whitelist) #adjust this to a fixed whitelist, maybe in the config dir
+ filteredlist=$(echo -e "$applist\n\n$whitelist\n\n$whitelist" | sort | uniq -u) #whitelist appended two times so those items are always removed
+ if [ ${#filteredlist} -ge 1 ]; then
+ message=$(echo -e "The following applications are still running, are you sure you want to close x? \n$filteredlist")
+ echo -e "?ae[34me[30m?"
+ xmessage -center "$message", -buttons yes,no
+ if [ $? = 102 ]; then
+ exit 1
+ fi
+ sudo /etc/init.d/slim-init stop
+ sleep 5s
+ else
+ echo -e "?ae[34me[30m?"
+ xmessage -center "killing x, nothing of value will be lost", -buttons ok,cancel
+ if [ $? = 102 ]; then
+ exit 1
+ fi
+ # close x now, do we want to use slim stop or just kill x?
+ sudo /etc/init.d/slim-init stop
+ sleep 5s
+ fi
+ fi
+ fi
+}
+
+PND_RestartX(){
+ if [ $CLOSE_X ]; then #restart x if it was killed
+ # We need to wait a bit, doing it nicely ;)
+ sleep 5
+ sudo /etc/init.d/slim-init start
+ fi
+}
+
+
+#=============================================================================
+# (u)Mounting functions
+
+show_mounted_info(){
+ echo "+++++++"
+ echo "Loopback devices :"
+ sudo /sbin/losetup -a
+ echo "Are mounted on :"
+ mount|grep loop
+ echo "For these Union :"
+ mount|grep aufs
+}
+
+is_union_mounted() {
+ mount | grep -q "on $UNION_MOUNT_DIR/${PND_NAME} type aufs"
+}
+
+is_pnd_mounted() {
+ mount |grep -v aufs | grep -q "on $PND_MOUNT_DIR/${PND_NAME} type" || \
+ mount |grep -v aufs | grep -q "on $UNION_MOUNT_DIR/${PND_NAME} type"
+}
+
+noMoreProcessPnd() {
+ [ -z "$(list_using_fs "$PND_MOUNT_DIR/$PND_NAME")" ]
+}
+
+noMoreProcessUnion() {
+ [ -z "$(list_using_fs "$UNION_MOUNT_DIR/$PND_NAME")" ]
+}
+
+mountPnd() {
+ MOUNT_TARGET="${1:-$PND_MOUNT_DIR}"
+ if ! is_pnd_mounted;then
+ #check if pnd is already attached to loop
+ LOOP=$(/sbin/losetup -a | grep "$PND" | tail -n1 | awk -F: '{print $1}')
+ #check if the loop device is already mounted
+ if ! [ -z "$LOOP" ];then
+ echo "Found a loop ($LOOP), using it"
+ loopmountedon=$( mount | grep "$(mount | grep "$LOOP" | awk '{print $3}')" | grep utmp | awk '{print $3}' )
+ else
+ loopmountedon=""
+ fi
+ if [ ! "$loopmountedon" ]; then #check if the pnd is already attached to some loop device but not used
+ FREELOOP=$LOOP
+ #reuse existing loop
+ if [ ! "$LOOP" ]; then
+ FREELOOP=$(/sbin/losetup -f) #get first free loop device
+ if [ ! "$FREELOOP" ]; then # no free loop device, create a new one
+ #find a free loop device and use it
+ usedminor=$(/sbin/losetup -a | tail -n1|sed 's/.*loop\(.*\)\: .*/\1/')
+ #usedminor=${usedminor:9:1}
+ freeminor=$(($usedminor+1))
+ echo "Creating a new device : mknod -m777 /dev/loop$freeminor b 7 $freeminor"
+ mknod -m777 /dev/loop$freeminor b 7 $freeminor
+ FREELOOP=/dev/loop$freeminor
+ fi
+ fi
+
+ #detect fs
+ case $PND_FSTYPE in
+ ISO)
+ /sbin/losetup -r $FREELOOP "$PND" #attach the pnd to the loop device
+ mntline="mount -o ro" #setup the mountline for later
+ mntdev="${FREELOOP}"
+ ;;
+ directory)
+ #we bind the folder, now it can be treated in a unified way
+ #ATENTION: -o ro doesnt work for --bind at least on 25, on 26 its possible using remount, may have changed on 27
+ mntline="mount --bind -o ro"
+ mntdev="${PND}"
+ ;;
+ Squashfs)
+ /sbin/losetup -r $FREELOOP "$PND" #attach the pnd to the loop device
+ mntline="mount -t squashfs -o ro"
+ mntdev="${FREELOOP}"
+ ;;
+ *)
+ echo "ERROR Unknown filesystem type : $PND_FSTYPE"
+ exit 1;;
+ esac
+ echo "Mounting : $mntline \"$mntdev\" \"$MOUNT_TARGET/${PND_NAME}\""
+ $mntline "$mntdev" "$MOUNT_TARGET/${PND_NAME}" #mount the pnd/folder
+
+ if ! is_pnd_mounted ;then
+ sleep 1
+ echo "WARNING : mount faild, re-tring"
+ sleep 1
+ $mntline "$mntdev" "$MOUNT_TARGET/${PND_NAME}" #mount the pnd/folder
+ if ! is_pnd_mounted ;then
+ echo "ERROR The PND File-system is not mounted !"
+ show_mounted_info
+ return 2
+ fi
+ fi
+ else #the pnd is already mounted but a mount was requested with a different basename/uid, just link it there
+ echo WARNING $LOOP already mounted on $loopmountedon skipping losetup - putting link to old mount
+ #this is bullshit
+ rmdir "$UNION_MOUNT_DIR/$PND_NAME"
+ ln -s $loopmountedon "$UNION_MOUNT_DIR/$PND_NAME"
+ fi
+ fi
+
+ # For backward compatibility
+ if [[ "$MOUNT_TARGET" != "$PND_MOUNT_DIR" ]];then
+ if [ -d "$PND_MOUNT_DIR/$PND_NAME" ];then
+ rmdir "$PND_MOUNT_DIR/$PND_NAME"
+ else
+ rm "$PND_MOUNT_DIR/$PND_NAME"
+ fi
+ if [ ! -e "$PND_MOUNT_DIR/$PND_NAME" ];then
+ ln -s "$MOUNT_TARGET/$PND_NAME" "$PND_MOUNT_DIR/$PND_NAME"
+ fi
+ fi
+}
+
+mountUnion() {
+ if [ $(id -u) -ne 0 ];then
+ sudo /usr/pandora/scripts/pnd_run.sh -m -p "$PND" -b "$PND_NAME"
+ if ! is_union_mounted;then
+ echo "ERROR: The Union File-system is not mounted !"
+ show_mounted_info
+ return 1
+ fi
+ return $RC
+ fi
+ #create mountpoints, check if they exist already first to avoid annoying error messages
+ if ! [ -d "$PND_MOUNT_DIR/${PND_NAME}" ]; then
+ mkdir -p "$PND_MOUNT_DIR/${PND_NAME}" #mountpoint for iso, ro
+ fi
+ #writeable dir for union
+ if ! [ -d "${APPDATADIR}" ]; then
+ mkdir -p "${APPDATADIR}"
+ chmod -R a+xrw "${APPDATADIR}" 2>/dev/null
+ fi
+ # create the union mountpoint
+ if ! [ -d "$UNION_MOUNT_DIR/${PND_NAME}" ]; then
+ mkdir -p "$UNION_MOUNT_DIR/${PND_NAME}" # union over the two