slim > slim-init
[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"] [-b UID (name of mountpoint/pandora/appdata)] [-x close x before launching(script needs to be started with nohup for this to work]
4 # -s startdir
5 # arguments can be inside -e, -a is optional
6  
7 #/etc/sudoers needs to be adjusted if you touch any of the sudo lines
8  
9 # look at the comments in the nox part, adjust 
10  
11 #use "lsof /usr/lib/libX11.so.6 | awk '{print $1}'| sort | uniq > whitelist" with nothing running to generate the whitelist
12  
13 #launch the script with nohup for -x to work!
14  
15 #todo
16 #validate params better
17 #make uid/basename mandatory (and rename var, its confusing!)
18 #find a clean way of shutting down x without a fixed dm, mabye avoid nohup usage somehow
19 #cleanup
20  
21 # parse arguments
22 TEMP=`getopt -o p:e:a:b:s:m::u::n::x:: -- "$@"`
23  
24 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
25  
26 # Note the quotes around `$TEMP': they are essential!
27 eval set -- "$TEMP"
28  
29 while true ; do
30         case "$1" in
31                 -p) echo "pnd set to \`$2'" ;PND=$2;shift 2;;
32                 -e) echo "exec set to \`$2'" ;EXENAME=$2;shift 2 ;;
33 #               -n) echo "n set, no union pls!";NOUNION=1;shift 2;; # we will reuse -n soon,stop using it if you still did!
34                 -b) echo "BASENAME set to $2";BASENAME=$2;shift 2;;
35                 -s) echo "startdir set to $2";STARTDIR=$2;shift 2;;
36                 -m) echo "mount";mount=1;shift 2;;
37                 -u) echo "umount";umount=1;shift 2;;
38                 -x) echo "no x";nox=1;shift 2;;
39                 -a) 
40                         case "$2" in
41                                 "") echo "no arguments"; shift 2 ;;
42                                 *)  echo "args set to \`$2'" ;ARGUMENTS=$2;shift 2 ;;
43                         esac ;;
44                 --) shift ; break ;;
45                 *) echo "Error while parsing arguments!" ; exit 1 ;;
46         esac
47 done
48  
49 if [ ! $PND ]; then #check if theres a pnd suplied, need to clean that up a bit more
50         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]"
51         exit 1
52 fi
53 if [ ! $EXENAME ]; then
54         if [ ! $mount ] && [ ! $umount ]; then
55                 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                 echo "Usage for mounting/umounting pnd_run.sh -p your.pnd -b uid -m or -u"
57                 exit 1
58         fi
59 fi
60
61
62 if [ $nox ]; then #the app doesnt want x to run, so we kill it and restart it once the app quits
63         applist=$(lsof /usr/lib/libX11.so.6 | awk '{print $1}'| sort | uniq)
64         whitelist=$(cat ~/pndtest/whitelist) #adjust this to a fixed whitelist, maybe in the config dir
65         filteredlist=$(echo -e "$applist\n\n$whitelist\n\n$whitelist" | sort | uniq -u) #whitelist appended two times so those items are always removed
66         if [ ${#filteredlist} -ge 1 ]; then
67                 message=$(echo -e "The following applications are still running, are you sure you want to close x? \n$filteredlist")
68                 echo -e “ae[34me[30m”
69                 xmessage -center "$message", -buttons yes,no
70                 if [ $? = 102 ]; then
71                 exit 1
72                 fi
73                 sudo /etc/init.d/slim-init stop
74                 sleep 5s
75         else
76                 echo -e “ae[34me[30m”
77                 xmessage -center "killing x, nothing of value will be lost", -buttons ok,cancel
78                 if [ $? = 102 ]; then
79                 exit 1
80                 fi
81                 # close x now, do we want to use slim stop or just kill x?
82                 sudo /etc/init.d/slim-init stop
83                 sleep 5s
84         fi
85 fi
86  
87 #vars
88 DFS=$(file -b $PND | awk '{ print $1 }') #is -p a zip/iso or folder?
89 MOUNTPOINT=$(df $PND | sed -ne 's/.*\% \(\S*\)/\1/p' | tail -n1) #find out on which mountpoint the pnd is
90 if [ ! -d "$MOUNTPOINT" ]; then MOUNTPOINT="/"; fi #make sure folder exists, if it doesnt assume rootfs
91 FILESYSTEM=$(mount | grep "on $MOUNTPOINT " | grep -v rootfs | awk '{print $5}' | tail -n1) #get filesystem appdata is on to determine aufs options
92 echo "Filesystem is $FILESYSTEM"
93 #if the pnd is on / set mountpoint to "" so we dont and up with // at the start,
94 #this is to make sure sudo doesnt get confused
95 if [ $MOUNTPOINT = "/" ]; then MOUNTPOINT=""; fi
96 echo "mountpoint: $MOUNTPOINT"
97  
98 #BASENAME really should be something sensible and somewhat unique
99 #if -b is set use that as basename, else generate it from PND
100 #get basename (strip extension if file) for union mountpoints etc, maybe  this should be changed to something specified inside the xml
101 #this should probably be changed to .... something more sensible
102 if [ ! $BASENAME ]; then BASENAME=$(basename "$PND" | cut -d'.' -f1) ; fi
103  
104  
105  
106 oCWD=$(pwd)
107
108
109  
110 #create mountpoints, check if they exist already first to avoid annoying error messages
111 if [ ! -d /mnt/pnd/$BASENAME ]; then sudo mkdir -p /mnt/pnd/$BASENAME ; fi #mountpoint for iso, ro
112 #writeable dir for union
113 if [ ! -d $MOUNTPOINT/pandora/appdata/$BASENAME ]; then sudo mkdir -p $MOUNTPOINT/pandora/appdata/$BASENAME; sudo chmod -R a+xrw $MOUNTPOINT/pandora/appdata/$BASENAME; fi
114 if [ ! -d /mnt/utmp/$BASENAME ]; then sudo mkdir -p /mnt/utmp/$BASENAME; fi #union over the two
115
116 #mount
117  
118 if [ ! $umount ]; then
119         #is the union already mounted? if not mount evrything, else launch the stuff
120         mount | grep "on /mnt/utmp/$BASENAME type" # > /dev/null
121         if [ ! $? -eq 0 ]; then 
122
123           FREELOOP=$(sudo /sbin/losetup -f) #get first free loop device
124           if [ ! $FREELOOP  ]; then  # no free loop device, create a new one
125                   #find a free loop device and use it 
126                   usedminor=$(sudo /sbin/losetup -a | tail -n1)
127                   usedminor=${usedminor:9:1}
128                   echo usedminor $usedminor
129                   freeminor=$(($usedminor+1))
130                   echo freeminor $freeminor
131                   sudo mknod -m777 /dev/loop$freeminor b 7 $freeminor
132                   FREELOOP=/dev/loop$freeminor
133           fi
134           
135           #detect fs
136           if [ $DFS = ISO ]; then
137                   sudo /sbin/losetup $FREELOOP $PND #attach the pnd to the loop device
138                   mntline="sudo mount $FREELOOP /mnt/pnd/$BASENAME/" #setup the mountline for later
139           #     mntline="sudo mount -o loop,mode=777 $PND /mnt/pnd/$BASENAME"
140                   echo "Filetype is $DFS"
141           elif [ $DFS = directory ]; then
142                   mntline="sudo mount --bind -o ro $PND /mnt/pnd/$BASENAME"
143           #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
144                   echo "Filetype is $DFS"
145           elif [ $DFS = Squashfs ]; then
146                   sudo /sbin/losetup $FREELOOP $PND #attach the pnd to the loop device
147                   mntline="sudo mount -t squashfs  $FREELOOP /mnt/pnd/$BASENAME"
148                   echo "Filetype is $DFS"
149           else
150                   echo "error determining fs, output was $DFS"
151                   exit 1;
152           fi
153
154
155                 echo "$mntline"
156                 $mntline #mount the pnd/folder
157                 echo "mounting union!"
158                 if [ $FILESYSTEM = vfat ]; then # use noplink on fat, dont on other fs's
159                 sudo mount -t aufs -o exec,noplink,dirs=$MOUNTPOINT/pandora/appdata/$BASENAME=rw+nolwh:/mnt/pnd/$BASENAME=rr none /mnt/utmp/$BASENAME # put union on top
160                 else
161                 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
162                 fi
163  
164         else
165                 echo "Union already mounted"
166         fi
167  
168         if [ $mount ]; then echo "mounted /mnt/utmp/$BASENAME"; exit 1; fi; #mount only, die here
169         
170         cd /mnt/utmp/$BASENAME # cd to union mount
171         if [ $STARTDIR ]; then cd $STARTDIR; fi #cd to folder specified by the optional arg -s
172         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
173         #the app could have exited now, OR it went into bg, we still need to wait in that case till it really quits!
174         PID=`pidof -o %PPID -x $EXENAME` #get pid of app
175         while [ "$PID" ] #wait till we get no pid back for tha app, again a bit ugly, but it works
176         do
177         sleep 10s
178         PID=`pidof -o %PPID -x $EXENAME`
179         done
180         echo end
181  
182         #app exited
183         cd $oCWD #cd out of the mountpoint so we can umount, doesnt really matter to where...
184 else
185 echo "-u set, nothing to do here"
186 fi
187  
188  
189 #clean up
190 sudo umount /mnt/utmp/$BASENAME #umount union
191 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
192         #umount the actual pnd
193         sudo umount /mnt/pnd/$BASENAME
194         #delete folders created by aufs if empty
195         sudo rmdir $MOUNTPOINT/pandora/appdata/$BASENAME/.wh..wh.plnk
196         sudo rmdir $MOUNTPOINT/pandora/appdata/$BASENAME/.wh..wh..tmp
197         #delete appdata folder and ancestors if empty
198         sudo rmdir -p $MOUNTPOINT/pandora/appdata/$BASENAME/
199         #delete tmp mountpoint
200         sudo rmdir /mnt/utmp/$BASENAME;
201         if [ $DFS = ISO ] || [ $DFS = Squashfs ]; then # check if we where running an iso, clean up loop device if we did
202                 sudo /sbin/losetup -d $FREELOOP
203                 sudo rm $FREELOOP
204         fi
205         sudo rmdir /mnt/pnd/$BASENAME #delete pnd mountpoint
206 fi
207
208 if [ $nox ]; then #restart x if it was killed
209 echo "starting x in 5s"
210 sleep 5
211 sudo /etc/init.d/slim-init-init start
212 fi