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>
This commit is contained in:
Scott Garman 2010-10-08 14:40:47 -07:00
parent 7cd824a538
commit c805a6ed28
1 changed files with 53 additions and 30 deletions

View File

@ -60,12 +60,39 @@ QEMUIFDOWN=`which poky-qemu-ifdown`
NFSRUNNING="false" NFSRUNNING="false"
LOCKUTIL=`which lockfile-create` acquire_lock() {
if [ -z "$LOCKUTIL" ]; then lockfile=$1
echo "Error: Unable to find the lockfile-create utility" if [ -z "$lockfile" ]; then
echo "On Ubuntu systems this is included in the lockfile-progs package" echo "Error: missing lockfile arg passed to acquire_lock()"
return return 1
fi fi
if [ -e "$lockfile.lock" ]; then
# Check that the lockfile is not stale
ps=`ps -ewwo pid | grep $(cat $lockfile.lock)`
if [ -z "$ps" ]; then
echo "Warning: Stale lock file detected, deleting $lockfile.lock"
rm -f $lockfile.lock
echo $$ > $lockfile.lock
else
return 1
fi
else
echo $$ > $lockfile.lock
fi
return 0
}
release_lock() {
lockfile=$1
if [ -z "$lockfile" ]; then
echo "Error: missing lockfile arg passed to release_lock()"
return 1
fi
rm -f $lockfile.lock
}
LOCKDIR="/tmp/qemu-tap-locks" LOCKDIR="/tmp/qemu-tap-locks"
[ ! -d "$LOCKDIR" ] && mkdir $LOCKDIR [ ! -d "$LOCKDIR" ] && mkdir $LOCKDIR
@ -76,10 +103,8 @@ LOCKFILE=""
for tap in $POSSIBLE; do for tap in $POSSIBLE; do
LOCKFILE="$LOCKDIR/$tap" LOCKFILE="$LOCKDIR/$tap"
echo "Acquiring lockfile for $tap..." echo "Acquiring lockfile for $tap..."
if lockfile-create --use-pid -r 1 $LOCKFILE; then acquire_lock $LOCKFILE
# the --use-pid option to lockfile-create will give use if [ $? -eq 0 ]; then
# the subshell's pid, so override it with the shell's pid:
echo $$ > $LOCKFILE.lock
TAP=$tap TAP=$tap
break break
fi fi
@ -94,7 +119,7 @@ if [ "$TAP" = "" ]; then
fi fi
GROUPID=`id -g` GROUPID=`id -g`
echo 'Setting up tap interface under sudo' echo "Setting up tap interface under sudo"
tap=`sudo $QEMUIFUP $GROUPID $POKY_NATIVE_SYSROOT` tap=`sudo $QEMUIFUP $GROUPID $POKY_NATIVE_SYSROOT`
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
# Re-run standalone to see verbose errors # Re-run standalone to see verbose errors
@ -103,22 +128,20 @@ if [ "$TAP" = "" ]; then
fi fi
LOCKFILE="$LOCKDIR/$tap" LOCKFILE="$LOCKDIR/$tap"
echo "Acquiring lockfile for $tap..." echo "Acquiring lockfile for $tap..."
if lockfile-create --use-pid -r 1 $LOCKFILE; then acquire_lock $LOCKFILE
# the --use-pid option to lockfile-create will give us if [ $? -eq 0 ]; then
# the subshell's pid, so override it with the shell's pid:
echo $$ > $LOCKFILE.lock
TAP=$tap TAP=$tap
fi fi
else else
echo "Using preconfigured tap device '$TAP'" echo "Using preconfigured tap device '$TAP'"
fi fi
release_lock() { cleanup() {
if [ ! -e "$NOSUDO_FLAG" ]; then if [ ! -e "$NOSUDO_FLAG" ]; then
sudo $QEMUIFDOWN $TAP $POKY_NATIVE_SYSROOT sudo $QEMUIFDOWN $TAP $POKY_NATIVE_SYSROOT
fi fi
echo "Releasing lockfile of preconfigured tap device '$TAP'" echo "Releasing lockfile of preconfigured tap device '$TAP'"
lockfile-remove $LOCKFILE release_lock $LOCKFILE
if [ "$NFSRUNNING" = "true" ]; then if [ "$NFSRUNNING" = "true" ]; then
echo "Shutting down the userspace NFS server..." echo "Shutting down the userspace NFS server..."
@ -162,13 +185,13 @@ esac
if [ ! -f "$KERNEL" ]; then if [ ! -f "$KERNEL" ]; then
echo "Error: Kernel image file $KERNEL doesn't exist" echo "Error: Kernel image file $KERNEL doesn't exist"
release_lock cleanup
return return
fi fi
if [ "$FSTYPE" != "nfs" -a ! -f "$ROOTFS" ]; then if [ "$FSTYPE" != "nfs" -a ! -f "$ROOTFS" ]; then
echo "Error: Image file $ROOTFS doesn't exist" echo "Error: Image file $ROOTFS doesn't exist"
release_lock cleanup
return return
fi fi
@ -186,7 +209,7 @@ if [ "$FSTYPE" = "nfs" ]; then
portmap_running=`ps ax | grep portmap | grep -v grep | wc -l` portmap_running=`ps ax | grep portmap | grep -v grep | wc -l`
if [[ $rpcbind_running == 0 && $portmap_running == 0 ]]; then if [[ $rpcbind_running == 0 && $portmap_running == 0 ]]; then
echo "You need to be running either rpcbind or portmap to continue" echo "You need to be running either rpcbind or portmap to continue"
release_lock cleanup
return return
fi fi
@ -194,7 +217,7 @@ if [ "$FSTYPE" = "nfs" ]; then
echo "poky-export-rootfs restart $ROOTFS" echo "poky-export-rootfs restart $ROOTFS"
poky-export-rootfs restart $ROOTFS poky-export-rootfs restart $ROOTFS
if [ $? != 0 ]; then if [ $? != 0 ]; then
release_lock cleanup
return return
fi fi
NFSRUNNING="true" NFSRUNNING="true"
@ -224,7 +247,7 @@ if [ "$MACHINE" = "qemuarm" -o "$MACHINE" = "qemuarmv6" -o "$MACHINE" = "qemuarm
if [ "$FSTYPE" = "nfs" ]; then if [ "$FSTYPE" = "nfs" ]; then
if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then
echo "Error: NFS mount point $ROOTFS doesn't exist" echo "Error: NFS mount point $ROOTFS doesn't exist"
release_lock cleanup
return return
fi fi
KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" 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
if [ "$FSTYPE" = "nfs" ]; then if [ "$FSTYPE" = "nfs" ]; then
if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then
echo "Error: NFS mount point $ROOTFS doesn't exist." echo "Error: NFS mount point $ROOTFS doesn't exist."
release_lock cleanup
return return
fi fi
KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" 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
fi fi
if [ ! -d "$ROOTFS" ]; then if [ ! -d "$ROOTFS" ]; then
echo "Error: NFS mount point $ROOTFS doesn't exist." echo "Error: NFS mount point $ROOTFS doesn't exist."
release_lock cleanup
return return
fi fi
KERNCMDLINE="root=/dev/nfs nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" 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
if [ "$FSTYPE" = "nfs" ]; then if [ "$FSTYPE" = "nfs" ]; then
if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then
echo "Error: NFS mount point $ROOTFS doesn't exist" echo "Error: NFS mount point $ROOTFS doesn't exist"
release_lock cleanup
return return
fi fi
KERNCMDLINE="root=/dev/nfs console=ttyS0 console=tty nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" 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
if [ "$FSTYPE" = "nfs" ]; then if [ "$FSTYPE" = "nfs" ]; then
if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then if [ "$NFS_SERVER" = "192.168.7.1" -a ! -d "$NFS_DIR" ]; then
echo "Error: NFS mount point $ROOTFS doesn't exist" echo "Error: NFS mount point $ROOTFS doesn't exist"
release_lock cleanup
return return
fi fi
KERNCMDLINE="root=/dev/nfs console=ttyS0 console=tty0 nfsroot=$NFS_SERVER:$NFS_DIR,$UNFS_OPTS rw $KERNEL_NETWORK_CMD mem=$QEMU_MEMORY" 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
if [ "x$QEMUOPTIONS" = "x" ]; then if [ "x$QEMUOPTIONS" = "x" ]; then
echo "Error: Unable to support this combination of options" echo "Error: Unable to support this combination of options"
release_lock cleanup
return return
fi fi
@ -369,7 +392,7 @@ QEMUBIN=`which $QEMU`
if [ ! -x "$QEMUBIN" ]; then if [ ! -x "$QEMUBIN" ]; then
echo "Error: No QEMU binary '$QEMU' could be found." echo "Error: No QEMU binary '$QEMU' could be found."
release_lock cleanup
return return
fi fi
@ -378,7 +401,7 @@ function _quit() {
#echo kill `cat $PIDFILE` #echo kill `cat $PIDFILE`
kill `cat $PIDFILE` kill `cat $PIDFILE`
fi fi
release_lock cleanup
return return
} }
@ -399,7 +422,7 @@ echo "Running $QEMU..."
echo $QEMUBIN -kernel $KERNEL $QEMUOPTIONS $SERIALOPTS $SCRIPT_QEMU_CMDLINE_OPT --append '"'$KERNCMDLINE $SCRIPT_KERNEL_OPT'"' echo $QEMUBIN -kernel $KERNEL $QEMUOPTIONS $SERIALOPTS $SCRIPT_QEMU_CMDLINE_OPT --append '"'$KERNCMDLINE $SCRIPT_KERNEL_OPT'"'
$QEMUBIN -kernel $KERNEL $QEMUOPTIONS $SERIALOPTS $SCRIPT_QEMU_OPT --append "$KERNCMDLINE $SCRIPT_KERNEL_OPT" || /bin/true $QEMUBIN -kernel $KERNEL $QEMUOPTIONS $SERIALOPTS $SCRIPT_QEMU_OPT --append "$KERNCMDLINE $SCRIPT_KERNEL_OPT" || /bin/true
release_lock cleanup
trap - INT TERM QUIT trap - INT TERM QUIT
return return