+def is_writeable(device):
+ """Check if the device is writeable for the current user"""
+
+ if not device:
+ return False
+ #raise Exception, "no device for checking write permissions"
+
+ if not os.path.exists(device):
+ return False
+
+ return os.access(device, os.W_OK) and os.access(device, os.R_OK)
+
+def install_mbr(device):
+ """Install a default master boot record on given device
+
+ @device: device where MBR should be installed to"""
+
+ if not is_writeable(device):
+ raise IOError, "device not writeable for user"
+
+ lilo = './lilo/lilo.static' # FIXME
+
+ if not is_exe(lilo):
+ raise Exception, "lilo executable not available."
+
+ # to support -A for extended partitions:
+ print("debug: ./lilo/lilo.static -S /dev/null -M %s ext") % device
+ proc = subprocess.Popen(["./lilo/lilo.static", "-S", "/dev/null", "-M", device, "ext"])
+ proc.wait()
+ if proc.returncode != 0:
+ raise Exception, "error executing lilo"
+
+ # activate partition:
+ print("debug: ./lilo/lilo.static -S /dev/null -A %s 1") % device
+ proc = subprocess.Popen(["./lilo/lilo.static", "-S", "/dev/null", "-A", device, "1"])
+ proc.wait()
+ if proc.returncode != 0:
+ raise Exception, "error executing lilo"
+
+ # lilo's mbr is broken, use the one from syslinux instead:
+ print("debug: cat /usr/lib/syslinux/mbr.bin > %s") % device
+ try:
+ # TODO use Popen instead?
+ retcode = subprocess.call("cat /usr/lib/syslinux/mbr.bin > "+ device, shell=True)
+ if retcode < 0:
+ print >> sys.stderr, "Error copying MBR to device", -retcode
+ except OSError, error:
+ print >> sys.stderr, "Execution failed:", error