- logging.debug("umount %s" % directory)
- proc = subprocess.Popen(["umount"] + [directory])
- proc.wait()
- if proc.returncode != 0:
- raise Exception, "Error executing umount"
+ # make sure we unmount only already mounted targets
+ target_unmount = False
+ mounts = open('/proc/mounts').readlines()
+ mountstring = re.compile(".*%s.*" % re.escape(target))
+ for line in mounts:
+ if re.match(mountstring, line):
+ target_unmount = True
+
+ if not target_unmount:
+ logging.debug("%s not mounted anymore" % target)
+ else:
+ logging.debug("umount %s %s" % (list(options), target))
+ proc = subprocess.Popen(["umount"] + list(options) + [target])
+ proc.wait()
+ if proc.returncode != 0:
+ raise Exception, "Error executing umount"
+ else:
+ logging.debug("unregister_mountpoint(%s)" % target)
+ unregister_mountpoint(target)
+
+
+def check_for_usbdevice(device):
+ """Check whether the specified device is a removable USB device
+
+ @device: device name, like /dev/sda1 or /dev/sda
+ """
+
+ usbdevice = re.match(r'/dev/(.*?)\d*$', device).group(1)
+ usbdevice = os.path.realpath('/sys/class/block/' + usbdevice + '/removable')
+ if os.path.isfile(usbdevice):
+ is_usb = open(usbdevice).readline()
+ if is_usb == "1":
+ return 0
+ else:
+ return 1