3 #Usage: pnd_run.sh -p your.pnd -e executeable [-a "(arguments)"] [ -s "cd to folder inside pnd"] [-b UID (name of mountpoint/pandora/appdata)] [-x close x before launching(script needs to be started with nohup for this to work]
5 # arguments can be inside -e, -a is optional
7 #/etc/sudoers needs to be adjusted if you touch any of the sudo lines
9 # look at the comments in the nox part, adjust
10 #use "lsof /usr/lib/libX11.so.6 | awk '{print $1}'| sort | uniq > whitelist" with nothing running to generate the whitelist
12 #launch the script with nohup for -x to work!
14 #todo - no proper order
15 #validate params better
16 #make uid/basename mandatory (and rename var, its confusing!)
17 #find a clean way of shutting down x without a fixed dm, mabye avoid nohup usage somehow
18 #add options to just mount iso without union and to mount the union later
20 #Rewrite! - this sucks
23 TEMP=`getopt -o p:e:a:b:s:m::u::n::x::j:c: -- "$@"`
25 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
27 # Note the quotes around `$TEMP': they are essential!
32 -p) echo "pnd set to \`$2'" ;PND=$2;shift 2;;
33 -e) echo "exec set to \`$2'" ;EXENAME=$2;shift 2 ;;
34 # -n) echo "n set, no union pls!";NOUNION=1;shift 2;; # we will reuse -n soon,stop using it if you still did!
35 -b) echo "BASENAME set to $2";BASENAME=$2;shift 2;;
36 -s) echo "startdir set to $2";STARTDIR=$2;shift 2;;
37 -m) echo "mount";mount=1;shift 2;;
38 -u) echo "umount";umount=1;shift 2;;
39 -x) echo "no x";nox=1;shift 2;;
40 -j) echo "join/ also mount those folders";append=$2;shift 2;;
41 -c) echo "set cpu speed to";cpuspeed=$2;shift 2;;
44 "") echo "no arguments"; shift 2 ;;
45 *) echo "args set to \`$2'" ;ARGUMENTS=$2;shift 2 ;;
48 *) echo "Error while parsing arguments!" ; exit 1 ;;
52 if [ ! $PND ]; then #check if theres a pnd suplied, need to clean that up a bit more
53 echo "Usage: pnd_run.sh -p your.pnd -e executeable [-a \"(arguments)\"] [ -s \"cd to folder inside pnd\"] [-b UID (name of mountpoint/pandora/appdata)] [-x close x before launching(script needs to be started with nohup for this to work]"
56 if [ ! $EXENAME ]; then
57 if [ ! $mount ] && [ ! $umount ]; then
58 echo "Usage: pnd_run.sh -p your.pnd -e executeable [-a \"(arguments)\"] [ -s \"cd to folder inside pnd\"] [-b UID (name of mountpoint/pandora/appdata)] [-x close x before launching(script needs to be started with nohup for this to work]"
59 echo "Usage for mounting/umounting pnd_run.sh -p your.pnd -b uid -m or -u"
66 if [ $nox ]; then #the app doesnt want x to run, so we kill it and restart it once the app quits
67 applist=$(lsof /usr/lib/libX11.so.6 | awk '{print $1}'| sort | uniq)
68 whitelist=$(cat ~/pndtest/whitelist) #adjust this to a fixed whitelist, maybe in the config dir
69 filteredlist=$(echo -e "$applist\n\n$whitelist\n\n$whitelist" | sort | uniq -u) #whitelist appended two times so those items are always removed
70 if [ ${#filteredlist} -ge 1 ]; then
71 message=$(echo -e "The following applications are still running, are you sure you want to close x? \n$filteredlist")
73 xmessage -center "$message", -buttons yes,no
77 sudo /etc/init.d/slim-init stop
81 xmessage -center "killing x, nothing of value will be lost", -buttons ok,cancel
85 # close x now, do we want to use slim stop or just kill x?
86 sudo /etc/init.d/slim-init stop
92 DFS=$(file -b $PND | awk '{ print $1 }') #is -p a zip/iso or folder?
93 MOUNTPOINT=$(df $PND | sed -ne 's/.*\% \(\S*\)/\1/p' | tail -n1) #find out on which mountpoint the pnd is
94 if [ ! -d "$MOUNTPOINT" ]; then MOUNTPOINT="/"; fi #make sure folder exists, if it doesnt assume rootfs
95 FILESYSTEM=$(mount | grep "on $MOUNTPOINT " | grep -v rootfs | awk '{print $5}' | tail -n1) #get filesystem appdata is on to determine aufs options
96 echo "Filesystem is $FILESYSTEM"
97 #if the pnd is on / set mountpoint to "" so we dont and up with // at the start,
98 #this is to make sure sudo doesnt get confused
99 if [ $MOUNTPOINT = "/" ]; then MOUNTPOINT=""; fi
100 echo "mountpoint: $MOUNTPOINT"
102 #BASENAME really should be something sensible and somewhat unique
103 #if -b is set use that as basename, else generate it from PND
104 #get basename (strip extension if file) for union mountpoints etc, maybe this should be changed to something specified inside the xml
105 #this should probably be changed to .... something more sensible
106 if [ ! $BASENAME ]; then BASENAME=$(basename "$PND" | cut -d'.' -f1) ; fi
114 #create mountpoints, check if they exist already first to avoid annoying error messages
115 if [ ! -d /mnt/pnd/$BASENAME ]; then sudo mkdir -p /mnt/pnd/$BASENAME ; fi #mountpoint for iso, ro
116 #writeable dir for union
117 if [ ! -d $MOUNTPOINT/pandora/appdata/$BASENAME ]; then sudo mkdir -p $MOUNTPOINT/pandora/appdata/$BASENAME; sudo chmod -R a+xrw $MOUNTPOINT/pandora/appdata/$BASENAME; fi
118 if [ ! -d /mnt/utmp/$BASENAME ]; then sudo mkdir -p /mnt/utmp/$BASENAME; fi #union over the two
122 if [ ! $umount ]; then
123 if [ ! $cpuspeed -eq $(cat /proc/pandora/cpu_mhz_max) ]; then
124 gksu --message "$BASENAME wants to set the cpu speed to $cpuspeed, enter root password to allow" echo $cpuspeed > /proc/pandora/cpu_mhz_max
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
130 echo not mounted on loop yet, doing so
131 #check if pnd is already attached to loop
132 LOOP=$(sudo losetup -a | grep $PND | tail -n1 | awk -F: '{print $1}')
133 #check if the loop device is already mounted
134 loopmountedon=$( mount | grep $(mount | grep $LOOP | awk '{print $3}') | grep utmp | awk '{print $3}' )
135 if [ ! $loopmountedon ]; then #check if the pnd is already attached to some loop device but not used
139 FREELOOP=$(sudo /sbin/losetup -f) #get first free loop device
140 if [ ! $FREELOOP ]; then # no free loop device, create a new one
141 #find a free loop device and use it
142 usedminor=$(sudo /sbin/losetup -a | tail -n1)
143 usedminor=${usedminor:9:1}
144 echo usedminor $usedminor
145 freeminor=$(($usedminor+1))
146 echo freeminor $freeminor
147 sudo mknod -m777 /dev/loop$freeminor b 7 $freeminor
148 FREELOOP=/dev/loop$freeminor
153 if [ $DFS = ISO ]; then
154 sudo /sbin/losetup $FREELOOP $PND #attach the pnd to the loop device
155 mntline="sudo mount $FREELOOP /mnt/pnd/$BASENAME/" #setup the mountline for later
156 #mntline="sudo mount -o loop,mode=777 $PND /mnt/pnd/$BASENAME"
157 echo "Filetype is $DFS"
158 elif [ $DFS = directory ]; then
159 mntline="sudo mount --bind -o ro $PND /mnt/pnd/$BASENAME"
160 #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
161 echo "Filetype is $DFS"
162 elif [ $DFS = Squashfs ]; then
163 sudo /sbin/losetup $FREELOOP $PND #attach the pnd to the loop device
164 mntline="sudo mount -t squashfs $FREELOOP /mnt/pnd/$BASENAME"
165 echo "Filetype is $DFS"
167 echo "error determining fs, output was $DFS"
172 $mntline #mount the pnd/folder
173 echo "mounting union!"
174 if [ $FILESYSTEM = vfat ]; then # use noplink on fat, dont on other fs's
175 #append is fucking dirty, need to clean that up
176 sudo mount -t aufs -o exec,noplink,dirs=$MOUNTPOINT/pandora/appdata/$BASENAME=rw+nolwh:/mnt/pnd/$BASENAME=rr$append none /mnt/utmp/$BASENAME # put union on top
178 sudo mount -t aufs -o exec,dirs=$MOUNTPOINT/pandora/appdata/$BASENAME=rw+nolwh:/mnt/pnd/$BASENAME=rr$append none /mnt/utmp/$BASENAME # put union on top
180 else #the pnd is already mounted but a mount was requested with a different basename/uid, just link it there
181 echo $LOOP already mounted on $loopmountedon skipping losetup - putting link to old mount
183 sudo rmdir /mnt/utmp/$BASENAME
184 sudo ln -s $loopmountedon /mnt/utmp/$BASENAME
188 echo "Union already mounted"
191 if [ $mount ]; then echo "mounted /mnt/utmp/$BASENAME"; exit 1; fi; #mount only, die here
193 cd /mnt/utmp/$BASENAME # cd to union mount
194 if [ $STARTDIR ]; then cd $STARTDIR; fi #cd to folder specified by the optional arg -s
195 LD_LIBRARY_PATH=/mnt/utmp/$BASENAME ./$EXENAME $ARGUMENTS # execute app with ld_lib_path set to the union mount, a bit evil but i think its a good solution
196 #the app could have exited now, OR it went into bg, we still need to wait in that case till it really quits!
197 PID=`pidof -o %PPID -x $EXENAME` #get pid of app
198 while [ "$PID" ] #wait till we get no pid back for tha app, again a bit ugly, but it works
201 PID=`pidof -o %PPID -x $EXENAME`
206 cd $oCWD #cd out of the mountpoint so we can umount, doesnt really matter to where...
208 echo "-u set, nothing to do here"
213 sudo rmdir /mnt/utmp/$BASENAME
214 sudo umount /mnt/utmp/$BASENAME #umount union
215 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
216 #umount the actual pnd
217 sudo umount /mnt/pnd/$BASENAME
218 #delete folders created by aufs if empty
219 sudo rmdir $MOUNTPOINT/pandora/appdata/$BASENAME/.wh..wh.plnk
220 sudo rmdir $MOUNTPOINT/pandora/appdata/$BASENAME/.wh..wh..tmp
221 #delete appdata folder and ancestors if empty
222 sudo rmdir -p $MOUNTPOINT/pandora/appdata/$BASENAME/
223 #delete tmp mountpoint
224 sudo rmdir /mnt/utmp/$BASENAME;
225 if [ $DFS = ISO ] || [ $DFS = Squashfs ]; then # check if we where running an iso, clean up loop device if we did
226 LOOP=$(sudo losetup -a | grep $PND | tail -n1 | awk -F: '{print $1}')
227 sudo /sbin/losetup -d $LOOP
230 sudo rmdir /mnt/pnd/$BASENAME #delete pnd mountpoint
233 echo umount failed, didnt clean up
236 if [ $nox ]; then #restart x if it was killed
237 echo "starting x in 5s"
239 sudo /etc/init.d/slim-init start
246 fork &> /tmp/pndrun.out &
249 echo Running with x, not disowning!