+ mount | grep "on /mnt/utmp/${PND_NAME} type"
+ if [ $? -ne 0 ];then
+ mount | grep "on /mnt/pnd/${PND_NAME} type"
+ if [ $? -ne 0 ];then
+ echo not mounted on loop yet, doing so
+ #check if pnd is already attached to loop
+ LOOP=$(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
+ echo "LoopMountedon: $loopmountedon"
+ 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
+ echo $FREELOOP
+ 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)
+ usedminor=${usedminor:9:1}
+ echo usedminor $usedminor
+ freeminor=$(($usedminor+1))
+ echo freeminor $freeminor
+ mknod -m777 /dev/loop$freeminor b 7 $freeminor
+ FREELOOP=/dev/loop$freeminor
+ fi
+ fi
+ #detect fs
+
+ case $PND_FSTYPE in
+ ISO)
+ /sbin/losetup $FREELOOP "$PND" #attach the pnd to the loop device
+ mntline="mount" #setup the mountline for later
+ mntdev="${FREELOOP}"
+ #mntline="mount -o loop,mode=777 $PND /mnt/pnd/$PND_NAME"
+ echo "Filetype is $PND_FSTYPE";;
+ 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}"
+ echo "Filetype is $PND_FSTYPE";;
+ Squashfs)
+ /sbin/losetup $FREELOOP "$PND" #attach the pnd to the loop device
+ mntline="mount -t squashfs"
+ mntdev="${FREELOOP}"
+ echo "Filetype is $PND_FSTYPE";;
+ *)
+ echo "error determining fs, output was $PND_FSTYPE"
+ exit 1;;
+ esac
+ echo "Mounting PND ($mntline) :"
+ $mntline "$mntdev" "/mnt/pnd/${PND_NAME}" #mount the pnd/folder
+
+ else #the pnd is already mounted but a mount was requested with a different basename/uid, just link it there
+ echo $LOOP already mounted on $loopmountedon skipping losetup - putting link to old mount
+ #this is bullshit
+ rmdir "/mnt/utmp/$PND_NAME"
+ ln -s $loopmountedon "/mnt/utmp/$PND_NAME"
+ fi
+
+ mount | grep "on /mnt/pnd/${PND_NAME} type"
+ if [ $? -ne 0 ];then
+ echo "The PND File-system is not mounted ! - Union wont work anyway"
+ return 2
+ fi
+
+ else
+ echo "the PND is already mounted"
+ fi
+ FILESYSTEM=$(mount | grep "on $MOUNTPOINT " | grep -v rootfs | awk '{print $5}' | tail -n1) #get filesystem appdata is on to determine aufs options
+ RO=0;for o in $(mount|grep "on $MOUNTPOINT "|sed 's/.*(//;s/)$//;s/,/ /g');do [[ $o = "ro" ]]&& RO=1;done
+ if [ $RO -eq 1 ];then
+ echo "SD-Card is mounted Read-only !! Trying to remount RW"
+ mount -oremount,rw $MOUNTPOINT
+ fi
+ echo "Filesystem is $FILESYSTEM"
+ echo "Mounting the Union FS using ${APPDATADIR} as Write directory:"
+ if [[ "$FILESYSTEM" = "vfat" ]]; then # use noplink on fat, dont on other fs's
+ #append is fucking dirty, need to clean that up
+ echo mount -t aufs -o exec,noplink,dirs="${APPDATADIR}=rw+nolwh":"/mnt/pnd/$PND_NAME=rr$append" none "/mnt/utmp/$PND_NAME"
+ mount -t aufs -o exec,noplink,dirs="${APPDATADIR}=rw+nolwh":"/mnt/pnd/$PND_NAME=rr$append" none "/mnt/utmp/$PND_NAME"
+ # put union on top
+ else
+ mount -t aufs -o exec,dirs="${APPDATADIR}=rw+nolwh":"/mnt/pnd/$PND_NAME=rr$append" none "/mnt/utmp/$PND_NAME"
+ # put union on top
+ fi
+
+ mount | grep "on /mnt/utmp/${PND_NAME} type"
+ if [ $? -ne 0 ];then
+ echo "The Union File-system is not mounted !"
+ return 1
+ fi
+