3 # Purpose: install grml-system to usb-device
4 # Authors: grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports: see http://grml.org/bugs/
6 # License: This file is licensed under the GPL v2 or any later version.
7 ################################################################################
10 # * write error messages to stderr
11 # * log wrapper (log important messages to syslog, depending on loglevel)
12 # * trap handling (umount devices when interrupting?)
14 prog_version = "0.0.1"
16 import os, re, subprocess, sys
17 from optparse import OptionParser
20 usage = "Usage: %prog [options] <ISO[s]> <partition>\n\
22 %prog installs a grml ISO to an USB device to be able to boot from it.\n\
23 Make sure you have at least a grml ISO or a running grml system,\n\
24 syslinux (just run 'aptitude install syslinux' on Debian-based systems)\n\
26 parser = OptionParser(usage=usage)
28 parser.add_option("--bootoptions", dest="bootoptions", action="store", type="string",
29 help="use specified bootoptions as defaut")
30 parser.add_option("--dry-run", dest="dryrun", action="store_true",
31 help="do not actually execute any commands")
32 parser.add_option("--fat16", dest="fat16", action="store_true",
33 help="format specified partition with FAT16")
34 parser.add_option("--force", dest="force", action="store_true",
35 help="force any actions requiring manual interaction")
36 parser.add_option("--grub", dest="grub", action="store_true",
37 help="install grub bootloader instead of syslinux")
38 parser.add_option("--initrd", dest="initrd", action="store", type="string",
39 help="install specified initrd instead of the default")
40 parser.add_option("--kernel", dest="kernel", action="store", type="string",
41 help="install specified kernel instead of the default")
42 parser.add_option("--mbr", dest="mbr", action="store_true",
43 help="install master boot record (MBR) on the device")
44 parser.add_option("--squashfs", dest="squashfs", action="store", type="string",
45 help="install specified squashfs file instead of the default")
46 parser.add_option("--uninstall", dest="uninstall", action="store_true",
47 help="remove grml ISO files")
48 parser.add_option("--verbose", dest="verbose", action="store_true",
49 help="enable verbose mode")
50 parser.add_option("-v", "--version", dest="version", action="store_true",
51 help="display version and exit")
53 (options, args) = parser.parse_args()
56 # wrapper functions {{{
57 # TODO: implement argument handling
59 """Wrapper for executing a command. Either really executes
60 the command (default) or when using --dry-run commandline option
61 just displays what would be executed."""
63 print "would execute %s now" % command
65 print "executing %s" % command
69 return os.path.exists(fpath) and os.access(fpath, os.X_OK)
71 fpath, fname = os.path.split(program)
76 for path in os.environ["PATH"].split(os.pathsep):
77 exe_file = os.path.join(path, program)
86 """Check for root permissions"""
87 if not os.geteuid()==0:
88 sys.exit("Error: please run this script with uid 0 (root).")
91 def install_syslinux(device):
92 """Install syslinux on specified device."""
93 print("syslinux %s") % device
96 def install_grub(device):
97 """Install grub on specified device."""
98 print("grub-install %s") % device
101 def install_bootloader(partition):
102 """Install bootloader on device."""
103 # Install bootloader on the device (/dev/sda), not on the partition itself (/dev/sda1)
104 if partition[-1:].isdigit():
105 device = re.match(r'(.*?)\d*$', partition).group(1)
112 install_syslinux(device)
115 def install_mbr(target):
116 """Install a default master boot record on given target"""
120 def loopback_mount(iso, target):
121 """Loopback mount specified ISO on given target"""
122 print("mount -o loop %s %s") % (iso, target)
125 def check_for_vat(partition):
126 """Check whether specified partition is VFAT/FAT16 filesystem"""
128 udev_info = subprocess.Popen(["/lib/udev/vol_id", "-t",
129 partition],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
130 filesystem = udev_info.communicate()[0].rstrip()
132 if udev_info.returncode == 2:
133 print("failed to read device %s - wrong UID / permissions?") % partition
136 if filesystem != "vfat":
139 # TODO: check for ID_FS_VERSION=FAT16?
142 print("Sorry, /lib/udev/vol_id not available.")
145 def copy_grml_files(target):
146 """Copy files from ISO on given target"""
150 def uninstall_files(device):
156 print("%s %s")% (os.path.basename(sys.argv[0]), prog_version)
160 parser.error("invalid usage")
164 device = args[len(args) - 1]
165 isos = args[0:len(args) - 1]
167 if not which("syslinux2"):
168 print("Sorry, syslinux not available. Exiting.")
169 print("Please install syslinux or consider using the --grub option.")
172 # TODO check for valid blockdevice, vfat and mount functions
173 # if device is not None:
174 # check_for_vat(device)
175 # mount_target(partition)
177 # TODO it doesn't need to be a ISO, could be /live/image as well!
179 print("iso = %s") % iso
180 # loopback_mount(iso)
181 # copy_grml_files(iso, target)
182 # loopback_unmount(iso)
185 print("would install MBR now")
187 install_bootloader(device)
189 if __name__ == "__main__":
192 ## END OF FILE #################################################################
193 # vim:foldmethod=marker expandtab ai ft=python