Some more cleanup after closing a pnd, /pandora/appdata now gets removed as well...
[pandora-libraries.git] / testdata / scripts / pnd_run.sh
1 #!/bin/bash
2  
3 #Usage: pnd_run.sh -p your.pnd -e executeable [-a "(arguments)"] [ -s "cd to folder inside pnd"] [-u (skip union)] [-b override BASENAME (name of mountpoint/pandora/appdata)] [-x close x before launching(script needs to be started with nohup for this to work]
4 # -n to skip union mount, should probably be removed before release
5 # -s startdir
6 # arguments can be inside -e, -a is optional
7  
8 #/etc/sudoers needs to be adjusted if you touch any of the sudo lines in the wrong place.
9  
10 # look at the comments in the nox part, adjust 
11  
12 #use "lsof /usr/lib/libX11.so.6 | awk '{print $1}'| sort | uniq > whitelist" with nothing running to generate the whitelist
13  
14 #launch the script with nohup for -x to work!
15  
16 #todo
17 #make sure to only use free loop devices!
18 #cleanup
19  
20 # parse arguments
21 TEMP=`getopt -o p:e:a:b:s:m::u::n::x:: -- "$@"`
22  
23 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
24  
25 # Note the quotes around `$TEMP': they are essential!
26 eval set -- "$TEMP"
27  
28 while true ; do
29         case "$1" in
30                 -p) echo "pnd set to \`$2'" ;PND=$2;shift 2;;
31                 -e) echo "exec set to \`$2'" ;EXENAME=$2;shift 2 ;;
32                 -n) echo "n set, no union pls!";NOUNION=1;shift 2;;
33                 -b) echo "BASENAME set to $2";BASENAME=$2;shift 2;;
34                 -s) echo "startdir set to $2";STARTDIR=$2;shift 2;;
35                 -m) echo "mount";mount=1;shift 2;;
36                 -u) echo "umount";umount=1;shift 2;;
37                 -x) echo "no x";nox=1;shift 2;;
38                 -a) 
39                         case "$2" in
40                                 "") echo "no arguments"; shift 2 ;;
41                                 *)  echo "args set to \`$2'" ;ARGUMENTS=$2;shift 2 ;;
42                         esac ;;
43                 --) shift ; break ;;
44                 *) echo "Error while parsing arguments!" ; exit 1 ;;
45         esac
46 done
47  
48 if [ ! $PND ]; then
49         echo "Usage: pnd_run.sh -p your.pnd -e executeable [-a \"(arguments)\"] [ -s \"cd to folder inside pnd\"] [-u (skip union)] [-b override BASENAME (name of mountpoint/pandora/appdata)] [-x close x before launching(script needs to be started with nohup for this to work]"
50         exit 1
51 fi
52 if [ $nox ]; then
53         applist=$(lsof /usr/lib/libX11.so.6 | awk '{print $1}'| sort | uniq)
54         whitelist=$(cat ~/pndtest/whitelist) #adjust this to a fixed whitelist, maybe in the config dir
55         filteredlist=$(echo -e "$applist\n\n$whitelist\n\n$whitelist" | sort | uniq -u) #whitelist appended two times so those items are always removed
56         if [ ${#filteredlist} -ge 1 ]; then
57                 message=$(echo -e "The following applications are still running, are you sure you want to close x? \n$filteredlist")
58                 echo -e “ae[34me[30m”
59                 xmessage -center "$message", -buttons yes,no
60                 if [ $? = 102 ]; then
61                 exit 1
62                 fi
63                 sudo /etc/init.d/gdm stop
64                 sleep 5s
65         else
66                 echo -e “ae[34me[30m”
67                 xmessage -center "killing x, nothing of value will be lost", -buttons ok,cancel
68                 if [ $? = 102 ]; then
69                 exit 1
70                 fi
71                 # close x now, do we want to use gdm stop or just kill x?
72                 sudo /etc/init.d/gdm stop
73                 sleep 5s
74         fi
75 fi
76  
77 #vars
78 DFS=$(file -b $PND | awk '{ print $1 }') #is -p a zip/iso or folder?
79 MOUNTPOINT=$(df $PND | sed -ne 's/.*\% \(\S*\)/\1/p' | tail -n1)
80 if [ $MOUNTPOINT = "/" ]; then MOUNTPOINT=""; fi
81 echo "mountpoint: $MOUNTPOINT"
82 #MOUNTPOINT=$(df -h $PND | grep -E '[1-9]%' | awk '{ print $6  }') #find out which mountpoint the pnd/folder is on, there probably is a better way to do this
83  
84  
85 #BASENAME really should be something sensible and somewhat unique
86 #if -b is set use that as basename, else generate it from PND
87 #get basename (strip extension if file) for union mountpoints etc, maybe  this should be changed to something specified inside the xml
88 #this should probably be changed to .... something more sensible
89 if [ ! $BASENAME ]; then BASENAME=$(basename "$PND" | cut -d'.' -f1) ; fi
90  
91  
92  
93 oCWD=$(pwd)
94  
95 #detect fs
96 if [ $DFS = ISO ]; then
97  
98         usedminor=$( ls -l /dev/loop* | awk '{print $6}')
99         freeminor=$( echo -e "$(seq 0 64)\n$usedminor" | sort -rn | uniq -u | tail -n1)
100         sudo mknod -m777 /dev/loop$freeminor b 7 $freeminor
101         sudo losetup /dev/loop$freeminor $PND
102  
103         mntline="sudo mount /dev/loop$freeminor /mnt/pnd/$BASENAME/"
104 #       mntline="sudo mount -o loop,mode=777 $PND /mnt/pnd/$BASENAME"
105         echo "Filetype is $DFS"
106 elif [ $DFS = Zip ]; then
107         mntline="fuse-zip $PND /mnt/pnd/$BASENAME -o ro,fmask=000" #TOTALLY untested right now
108         echo "Filetype is $DFS"
109 elif [ $DFS = directory ]; then
110         mntline="sudo mount --bind -o ro $PND /mnt/pnd/$BASENAME"
111 #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
112         echo "Filetype is $DFS"
113 else
114         echo "error"
115         exit 1;
116 fi
117  
118 #create mountpoints, check if they exist already first to avoid annoying error messages
119 if [ ! -d /mnt/pnd/$BASENAME ]; then sudo mkdir -p /mnt/pnd/$BASENAME ; fi
120 if [ ! -d $MOUNTPOINT/pandora/appdata/$BASENAME ]; then sudo mkdir -p $MOUNTPOINT/pandora/appdata/$BASENAME; fi
121 if [ ! -d /mnt/utmp/$BASENAME ]; then sudo mkdir -p /mnt/utmp/$BASENAME; fi 
122  
123 #mount
124  
125 if [ ! $NOUNION ] && [ ! $umount ]; then
126         #is the union already mounted? if not mount evrything, else launch the stuff
127         mount | grep "on /mnt/utmp/$BASENAME type" # > /dev/null
128         if [ ! $? -eq 0 ]; then 
129                 echo "$mntline"
130                 $mntline #mount the pnd/folder
131                 echo "mounting union!"
132                 sudo mount -t aufs -o exec,dirs\=$MOUNTPOINT/pandora/appdata/$BASENAME=rw+nolwh:/mnt/pnd/$BASENAME=rr none /mnt/utmp/$BASENAME # put union on top
133  
134         else
135                 echo "Union already mounted"
136         fi
137  
138         if [ $mount ]; then echo "mounted /mnt/utmp/$BASENAME"; exit 1; fi;
139  
140         #start app
141         cd /mnt/utmp/$BASENAME
142         if [ $STARTDIR ]; then cd $STARTDIR; fi #cd to folder specified by the optional arg -s
143         #echo "/lib/ld-linux.so.2 --library-path /mnt/utmp/$BASENAME/ $EXENAME $ARGUMENTS"
144         #/lib/ld-linux.so.2 --library-path /mnt/utmp/$BASENAME/ $EXENAME $ARGUMENTS
145         LD_LIBRARY_PATH=/mnt/utmp/$BASENAME ./$EXENAME $ARGUMENTS
146         #the app could have exited now, OR it went into bg, we still need to wait in that case till it really quits!
147         PID=`pidof -o %PPID -x $EXENAME`
148         while [ "$PID" ]
149         do
150         sleep 10s
151         PID=`pidof -o %PPID -x $EXENAME`
152         done
153         echo end
154  
155         #app exited
156         cd $oCWD #cd out of the mountpoint so we can umount, doesnt really matter to where...
157  
158 elif [ ! $umount ]; then
159         $mntline
160         if [ $mount ]; then echo "mounted /mnt/pnd/$BASENAME"; exit 1; fi;
161         cd /mnt/pnd/$BASENAME
162         if [ $STARTDIR ]; then cd $STARTDIR; fi
163         echo $(pwd)
164         #/lib/ld-linux.so.2 --library-path /mnt/pnd/$BASENAME/ $EXENAME $ARGUMENTS      
165         ./$EXENAME $ARGUMENTS 
166         LD_LIBRARY_PATH=/mnt/pnd/$BASENAME ./$EXENAME $ARGUMENTS
167         #the app could have exited now, OR it went into bg, we still need to wait in that case till it really quits!
168         PID=`pidof -o %PPID -x $EXENAME`
169         while [ $PID ]
170         do
171         sleep 10s
172         PID=`pidof -o %PPID -x $EXENAME`
173         done
174         echo end
175  
176         cd $oCWD
177 else
178 echo "-u set, nothing to do here"
179 fi
180  
181  
182 #clean up
183 if [ ! $NOUNION ] ; then sudo umount /mnt/utmp/$BASENAME; fi #umount union if -u wasnt set
184 if [ $NOUNION ] ; then sudo umount /mnt/pnd/$BASENAME; fi #umount iso if -u WAS set
185 if [ $? -eq 0 ]; then # check if the umount was successfull, if it wasnt it would mean that theres still something running so we skip this stuff, this WILL lead to clutter if it happens, so we should make damn sure it never happens
186         if [ ! $NOUNION ] ; then
187                 sudo umount /mnt/pnd/$BASENAME
188                 sudo rmdir $MOUNTPOINT/pandora/appdata/$BASENAME/.wh..wh.plnk
189                 sudo rmdir $MOUNTPOINT/pandora/appdata/$BASENAME/.wh..wh..tmp 
190                 sudo rmdir -p $MOUNTPOINT/pandora/appdata/$BASENAME/
191                 sudo rmdir /mnt/utmp/$BASENAME;
192         fi
193         if [ $DFS = ISO ]; then
194                 sudo losetup -d /dev/loop$freeminor
195                 sudo rm /dev/loop$freeminor
196         fi
197         sudo rmdir /mnt/pnd/$BASENAME 
198 fi
199 if [ $nox ]; then
200 echo "starting x in 5s"
201 sleep 5
202 sudo /etc/init.d/gdm start
203 fi