diff options
author | Scott Garman <scott.a.garman@intel.com> | 2010-10-08 14:40:47 -0700 |
---|---|---|
committer | Scott Garman <scott.a.garman@intel.com> | 2010-10-08 14:48:20 -0700 |
commit | c805a6ed281279ef0ccc4e6e084d52a725260e09 (patch) | |
tree | 89868cd32164b13e7e76fb42dac7b735358d2248 | |
parent | 7cd824a5388f7284a1252e9f61af2c88d26ca163 (diff) | |
download | poky-c805a6ed281279ef0ccc4e6e084d52a725260e09.tar.gz |
poky-qemu-internal: implement file locking in bash
There does not appear to be a universal lockfile utility that
meets our needs. For example:
* 'lockfile' is part of the procmail pacakge in Ubuntu, a
requirement we don't want to impose on our users
* lockfile-[create|remove] from the Ubuntu lockfile-progs
package does not appear to be available in Fedora/openSUSE
So, the most portable way to do this is just to implement it
in bash. The likelihood of race conditions is minimal for
what we need this for.
Signed-off-by: Scott Garman <scott.a.garman@intel.com>
-rwxr-xr-x | scripts/poky-qemu-internal | 83 |
1 files changed, 53 insertions, 30 deletions
diff --git a/scripts/poky-qemu-internal b/scripts/poky-qemu-internal index 01c92ee980..834624dede 100755 --- a/scripts/poky-qemu-internal +++ b/scripts/poky-qemu-internal | |||
@@ -60,12 +60,39 @@ QEMUIFDOWN=`which poky-qemu-ifdown` | |||
60 | 60 | ||
61 | NFSRUNNING="false" | 61 | NFSRUNNING="false" |
62 | 62 | ||
63 | LOCKUTIL=`which lockfile-create` | 63 | acquire_lock() { |
64 | if [ -z "$LOCKUTIL" ]; then | 64 | lockfile=$1 |
65 | echo "Error: Unable to find the lockfile-create utility" | 65 | if [ -z "$lockfile" ]; then |
66 | echo "On Ubuntu systems this is included in the lockfile-progs package" | 66 | echo "Error: missing lockfile arg passed to acquire_lock()" |
67 | return | 67 | return 1 |
68 | fi | 68 | fi |
69 | |||
70 | if [ -e "$lockfile.lock" ]; then | ||
71 | # Check that the lockfile is not stale | ||
72 | ps=`ps -ewwo pid | grep $(cat $lockfile.lock)` | ||
73 | if [ -z "$ps" ]; then | ||
74 | echo "Warning: Stale lock file detected, deleting $lockfile.lock" | ||
75 | rm -f $lockfile.lock | ||
76 | echo $$ > $lockfile.lock | ||
77 | else | ||
78 | return 1 | ||
79 | fi | ||
80 | else | ||
81 | echo $$ > $lockfile.lock | ||
82 | fi | ||
83 | |||
84 | return 0 | ||
85 | } | ||
86 | |||
87 | release_lock() { | ||
88 | lockfile=$1 | ||
89 | if [ -z "$lockfile" ]; then | ||
90 | echo "Error: missing lockfile arg passed to release_lock()" | ||
91 | return 1 | ||
92 | fi | ||
93 | |||
94 | rm -f $lockfile.lock | ||
95 | } | ||
69 | 96 | ||
70 | LOCKDIR="/tmp/qemu-tap-locks" | 97 | LOCKDIR="/tmp/qemu-tap-locks" |
71 | [ ! -d "$LOCKDIR" ] && mkdir $LOCKDIR | 98 | [ ! -d "$LOCKDIR" ] && mkdir $LOCKDIR |
@@ -76,10 +103,8 @@ LOCKFILE="" | |||
76 | for tap in $POSSIBLE; do | 103 | for tap in $POSSIBLE; do |
77 | LOCKFILE="$LOCKDIR/$tap" | 104 | LOCKFILE="$LOCKDIR/$tap" |
78 | echo "Acquiring lockfile for $tap..." | 105 | echo "Acquiring lockfile for $tap..." |
79 | if lockfile-create --use-pid -r 1 $LOCKFILE; then | 106 | acquire_lock $LOCKFILE |
80 | # the --use-pid option to lockfile-create will give use | 107 | if [ $? -eq 0 ]; then |
81 | # the subshell's pid, so override it with the shell's pid: | ||
82 | echo $$ > $LOCKFILE.lock | ||
83 | TAP=$tap | 108 | TAP=$tap |
84 | break | 109 | break |
85 | fi | 110 | fi |
@@ -94,7 +119,7 @@ if [ "$TAP" = "" ]; then | |||
94 | fi | 119 | fi |
95 | 120 | ||
96 | GROUPID=`id -g` | 121 | GROUPID=`id -g` |
97 | echo 'Setting up tap interface under sudo' | 122 | echo "Setting up tap interface under sudo" |
98 | tap=`sudo $QEMUIFUP $GROUPID $POKY_NATIVE_SYSROOT` | 123 | tap=`sudo $QEMUIFUP $GROUPID $POKY_NATIVE_SYSROOT` |
99 | if [ $? -ne 0 ]; then | 124 | if [ $? -ne 0 ]; then |
100 | # Re-run standalone to see verbose errors | 125 | # Re-run standalone to see verbose errors |
@@ -103,22 +128,20 @@ if [ "$TAP" = "" ]; then | |||
103 | fi | 128 | fi |
104 | LOCKFILE="$LOCKDIR/$tap" | 129 | LOCKFILE="$LOCKDIR/$tap" |
105 | echo "Acquiring lockfile for $tap..." | 130 | echo "Acquiring lockfile for $tap..." |
106 | if lockfile-create --use-pid -r 1 $LOCKFILE; then | 131 | acquire_lock $LOCKFILE |
107 | # the --use-pid option to lockfile-create will give us | 132 | if [ $? -eq 0 ]; then |
108 | # the subshell's pid, so override it with the shell's pid: | ||
109 | echo $$ > $LOCKFILE.lock | ||
110 | TAP=$tap | 133 | TAP=$tap |
111 | fi | 134 | fi |
112 | else | 135 | else |
113 | echo "Using preconfigured tap device '$TAP'" | 136 | echo "Using preconfigured tap device '$TAP'" |
114 | fi | 137 | fi |
115 | 138 | ||
116 | release_lock() { | 139 | cleanup() { |
117 | if [ ! -e "$NOSUDO_FLAG" ]; then | 140 | if [ ! -e "$NOSUDO_FLAG" ]; then |
118 | sudo $QEMUIFDOWN $TAP $POKY_NATIVE_SYSROOT | 141 | sudo $QEMUIFDOWN $TAP $POKY_NATIVE_SYSROOT |
119 | fi | 142 | fi |
120 | echo "Releasing lockfile of preconfigured tap device '$TAP'" | 143 | echo "Releasing lockfile of preconfigured tap device '$TAP'" |
121 | lockfile-remove $LOCKFILE | 144 | release_lock $LOCKFILE |
122 | 145 | ||
123 | if [ "$NFSRUNNING" = "true" ]; then | 146 | if [ "$NFSRUNNING" = "true" ]; then |
124 | echo "Shutting down the userspace NFS server..." | 147 | echo "Shutting down the userspace NFS server..." |
@@ -162,13 +185,13 @@ esac | |||
162 | 185 | ||
163 | if [ ! -f "$KERNEL" ]; then | 186 | if [ ! -f "$KERNEL" ]; then |
164 | echo "Error: Kernel image file $KERNEL doesn't exist" | 187 | echo "Error: Kernel image file $KERNEL doesn't exist" |
165 | release_lock | 188 | cleanup |
166 | return | 189 | return |
167 | fi | 190 | fi |
168 | 191 | ||
169 | if [ "$FSTYPE" != "nfs" -a ! -f "$ROOTFS" ]; then | 192 | if [ "$FSTYPE" != "nfs" -a ! -f "$ROOTFS" ]; then |
170 | echo "Error: Image file $ROOTFS doesn't exist" | 193 | echo "Error: Image file $ROOTFS doesn't exist" |
171 | release_lock | 194 | cleanup |
172 | return | 195 | return |
173 | fi | 196 | fi |
174 | 197 | ||
@@ -186,7 +209,7 @@ if [ "$FSTYPE" = "nfs" ]; then | |||
186 | portmap_running=`ps ax | grep portmap | grep -v grep | wc -l` | 209 | portmap_running=`ps ax | grep portmap | grep -v grep | wc -l` |
187 | if [[ $rpcbind_running == 0 && $portmap_running == 0 ]]; then | 210 | if [[ $rpcbind_running == 0 && $portmap_running == 0 ]]; then |
188 | echo "You need to be running either rpcbind or portmap to continue" | 211 | echo "You need to be running either rpcbind or portmap to continue" |
189 | release_lock | 212 | cleanup |
190 | return | 213 | return |
191 | fi | 214 | fi |
192 | 215 | ||
@@ -194,7 +217,7 @@ if [ "$FSTYPE" = "nfs" ]; then | |||
194 | echo "poky-export-rootfs restart $ROOTFS" | 217 | echo "poky-export-rootfs restart $ROOTFS" |
195 | poky-export-rootfs restart $ROOTFS | 218 | poky-export-rootfs restart $ROOTFS |
196 | if [ $? != 0 ]; then | 219 | if [ $? != 0 ]; then |
197 | release_lock | 220 | cleanup |
198 | return | 221 | return |
199 | fi | 222 | fi |
200 | NFSRUNNING="true" | 223 | NFSRUNNING="true" |
@@ -224,7 +247,7 @@ if [ "$MACHINE" = "qemuarm" -o "$MACHINE" = "qemuarmv6" -o "$MACHINE" = "qemuarm | |||
224 | if [ "$FSTYPE" = "nfs" ]; then | 247 | if [ "$FSTYPE" = "nfs" ]; then |
225 | if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then | 248 | if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then |
226 | echo "Error: NFS mount point $ROOTFS doesn't exist" | 249 | echo "Error: NFS mount point $ROOTFS doesn't exist" |
227 | release_lock | 250 | cleanup |
228 | return | 251 | return |
229 | fi | 252 | fi |
230 | KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" | 253 | KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" |
@@ -248,7 +271,7 @@ if [ "$MACHINE" = "qemux86" ]; then | |||
248 | if [ "$FSTYPE" = "nfs" ]; then | 271 | if [ "$FSTYPE" = "nfs" ]; then |
249 | if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then | 272 | if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then |
250 | echo "Error: NFS mount point $ROOTFS doesn't exist." | 273 | echo "Error: NFS mount point $ROOTFS doesn't exist." |
251 | release_lock | 274 | cleanup |
252 | return | 275 | return |
253 | fi | 276 | fi |
254 | KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" | 277 | KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" |
@@ -269,7 +292,7 @@ if [ "$MACHINE" = "qemux86-64" ]; then | |||
269 | fi | 292 | fi |
270 | if [ ! -d "$ROOTFS" ]; then | 293 | if [ ! -d "$ROOTFS" ]; then |
271 | echo "Error: NFS mount point $ROOTFS doesn't exist." | 294 | echo "Error: NFS mount point $ROOTFS doesn't exist." |
272 | release_lock | 295 | cleanup |
273 | return | 296 | return |
274 | fi | 297 | fi |
275 | KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" | 298 | KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" |
@@ -303,7 +326,7 @@ if [ "$MACHINE" = "qemumips" ]; then | |||
303 | if [ "$FSTYPE" = "nfs" ]; then | 326 | if [ "$FSTYPE" = "nfs" ]; then |
304 | if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then | 327 | if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then |
305 | echo "Error: NFS mount point $ROOTFS doesn't exist" | 328 | echo "Error: NFS mount point $ROOTFS doesn't exist" |
306 | release_lock | 329 | cleanup |
307 | return | 330 | return |
308 | fi | 331 | fi |
309 | KERNCMDLINE="root=/dev/nfs console=ttyS0 console=tty nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" | 332 | KERNCMDLINE="root=/dev/nfs console=ttyS0 console=tty nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" |
@@ -324,7 +347,7 @@ if [ "$MACHINE" = "qemuppc" ]; then | |||
324 | if [ "$FSTYPE" = "nfs" ]; then | 347 | if [ "$FSTYPE" = "nfs" ]; then |
325 | if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then | 348 | if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then |
326 | echo "Error: NFS mount point $ROOTFS doesn't exist" | 349 | echo "Error: NFS mount point $ROOTFS doesn't exist" |
327 | release_lock | 350 | cleanup |
328 | return | 351 | return |
329 | fi | 352 | fi |
330 | KERNCMDLINE="root=/dev/nfs console=ttyS0 console=tty0 nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" | 353 | KERNCMDLINE="root=/dev/nfs console=ttyS0 console=tty0 nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" |
@@ -346,7 +369,7 @@ fi | |||
346 | 369 | ||
347 | if [ "x$QEMUOPTIONS" = "x" ]; then | 370 | if [ "x$QEMUOPTIONS" = "x" ]; then |
348 | echo "Error: Unable to support this combination of options" | 371 | echo "Error: Unable to support this combination of options" |
349 | release_lock | 372 | cleanup |
350 | return | 373 | return |
351 | fi | 374 | fi |
352 | 375 | ||
@@ -369,7 +392,7 @@ QEMUBIN=`which $QEMU` | |||
369 | 392 | ||
370 | if [ ! -x "$QEMUBIN" ]; then | 393 | if [ ! -x "$QEMUBIN" ]; then |
371 | echo "Error: No QEMU binary '$QEMU' could be found." | 394 | echo "Error: No QEMU binary '$QEMU' could be found." |
372 | release_lock | 395 | cleanup |
373 | return | 396 | return |
374 | fi | 397 | fi |
375 | 398 | ||
@@ -378,7 +401,7 @@ function _quit() { | |||
378 | #echo kill `cat $PIDFILE` | 401 | #echo kill `cat $PIDFILE` |
379 | kill `cat $PIDFILE` | 402 | kill `cat $PIDFILE` |
380 | fi | 403 | fi |
381 | release_lock | 404 | cleanup |
382 | return | 405 | return |
383 | } | 406 | } |
384 | 407 | ||
@@ -399,7 +422,7 @@ echo "Running $QEMU..." | |||
399 | echo $QEMUBIN -kernel $KERNEL $QEMUOPTIONS $SERIALOPTS $SCRIPT_QEMU_CMDLINE_OPT --append '"'$KERNCMDLINE $SCRIPT_KERNEL_OPT'"' | 422 | echo $QEMUBIN -kernel $KERNEL $QEMUOPTIONS $SERIALOPTS $SCRIPT_QEMU_CMDLINE_OPT --append '"'$KERNCMDLINE $SCRIPT_KERNEL_OPT'"' |
400 | $QEMUBIN -kernel $KERNEL $QEMUOPTIONS $SERIALOPTS $SCRIPT_QEMU_OPT --append "$KERNCMDLINE $SCRIPT_KERNEL_OPT" || /bin/true | 423 | $QEMUBIN -kernel $KERNEL $QEMUOPTIONS $SERIALOPTS $SCRIPT_QEMU_OPT --append "$KERNCMDLINE $SCRIPT_KERNEL_OPT" || /bin/true |
401 | 424 | ||
402 | release_lock | 425 | cleanup |
403 | 426 | ||
404 | trap - INT TERM QUIT | 427 | trap - INT TERM QUIT |
405 | return | 428 | return |