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?)
13 # * integrate https://www.mirbsd.org/cvs.cgi/src/sys/arch/i386/stand/mbr/mbr.S?rev=HEAD;content-type=text%2Fplain
14 # -> gcc -D_ASM_SOURCE -D__BOOT_VER=\"GRML\" -DBOOTMANAGER -c mbr.S; ld
15 # -nostdlib -Ttext 0 -N -Bstatic --oformat binary mbr.o -o mbrmgr
17 prog_version = "0.0.1"
19 import os, re, subprocess, sys
20 from optparse import OptionParser
21 from os.path import exists, join, abspath
22 from os import pathsep
23 from string import split
26 usage = "Usage: %prog [options] <ISO[s]> <partition>\n\
28 %prog installs a grml ISO to an USB device to be able to boot from it.\n\
29 Make sure you have at least a grml ISO or a running grml system,\n\
30 syslinux (just run 'aptitude install syslinux' on Debian-based systems)\n\
32 parser = OptionParser(usage=usage)
34 parser.add_option("--bootoptions", dest="bootoptions", action="store", type="string",
35 help="use specified bootoptions as defaut")
36 parser.add_option("--dry-run", dest="dryrun", action="store_true",
37 help="do not actually execute any commands")
38 parser.add_option("--fat16", dest="fat16", action="store_true",
39 help="format specified partition with FAT16")
40 parser.add_option("--force", dest="force", action="store_true",
41 help="force any actions requiring manual interaction")
42 parser.add_option("--grub", dest="grub", action="store_true",
43 help="install grub bootloader instead of syslinux")
44 parser.add_option("--initrd", dest="initrd", action="store", type="string",
45 help="install specified initrd instead of the default")
46 parser.add_option("--kernel", dest="kernel", action="store", type="string",
47 help="install specified kernel instead of the default")
48 parser.add_option("--mbr", dest="mbr", action="store_true",
49 help="install master boot record (MBR) on the device")
50 parser.add_option("--squashfs", dest="squashfs", action="store", type="string",
51 help="install specified squashfs file instead of the default")
52 parser.add_option("--uninstall", dest="uninstall", action="store_true",
53 help="remove grml ISO files")
54 parser.add_option("--verbose", dest="verbose", action="store_true",
55 help="enable verbose mode")
56 parser.add_option("-v", "--version", dest="version", action="store_true",
57 help="display version and exit")
59 (options, args) = parser.parse_args()
62 # wrapper functions {{{
63 # TODO: implement argument handling
65 """Wrapper for executing a command. Either really executes
66 the command (default) or when using --dry-run commandline option
67 just displays what would be executed."""
69 print "would execute %s now" % command
71 print "executing %s" % command
75 return os.path.exists(fpath) and os.access(fpath, os.X_OK)
77 fpath, fname = os.path.split(program)
82 for path in os.environ["PATH"].split(os.pathsep):
83 exe_file = os.path.join(path, program)
89 def search_file(filename, search_path='/bin' + pathsep + '/usr/bin'):
90 """Given a search path, find file"""
92 paths = split(search_path, pathsep)
94 for current_dir, directories, files in os.walk(path):
95 if exists(join(current_dir, filename)):
99 return abspath(join(current_dir, filename))
105 def check_uid_root():
106 """Check for root permissions"""
107 if not os.geteuid()==0:
108 sys.exit("Error: please run this script with uid 0 (root).")
111 def install_syslinux(device):
112 """Install syslinux on specified device."""
113 print("debug: syslinux %s") % device
116 def install_grub(device):
117 """Install grub on specified device."""
118 print("grub-install %s") % device
121 def install_bootloader(partition):
122 """Install bootloader on device."""
123 # Install bootloader on the device (/dev/sda), not on the partition itself (/dev/sda1)
124 if partition[-1:].isdigit():
125 device = re.match(r'(.*?)\d*$', partition).group(1)
132 install_syslinux(device)
135 def install_mbr(target):
136 """Install a default master boot record on given target"""
140 def loopback_mount(iso, target):
141 """Loopback mount specified ISO on given target"""
142 print("mount -o loop %s %s") % (iso, target)
145 def check_for_vat(partition):
146 """Check whether specified partition is VFAT/FAT16 filesystem"""
148 udev_info = subprocess.Popen(["/lib/udev/vol_id", "-t",
149 partition],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
150 filesystem = udev_info.communicate()[0].rstrip()
152 if udev_info.returncode == 2:
153 print("failed to read device %s - wrong UID / permissions?") % partition
156 if filesystem != "vfat":
159 # TODO: check for ID_FS_VERSION=FAT16?
162 print("Sorry, /lib/udev/vol_id not available.")
165 def copy_grml_files(grml_flavour, iso_mount, target):
166 """Copy files from ISO on given target"""
168 # TODO: provide alternative search_file() if file information is stored in
170 squashfs = search_file(grml_flavour + '.squashfs', iso_mount)
171 print("debug: copy squashfs to %s") % target + '/live/' + grml_flavour + '.squashfs'
173 filesystem_module = search_file('filesystem.module', iso_mount)
174 print("debug: copy filesystem.module to %s") % target + '/live/' + grml_flavour + '.module'
176 kernel = search_file('linux26', iso_mount)
177 print("debug: copy kernel to %s") % target + '/boot/release/' + grml_flavour + '/linux26'
179 initrd = search_file('initrd.gz', iso_mount)
180 print("debug: copy initrd to %s") % target + '/boot/release/' + grml_flavour + '/initrd.gz'
182 logo = search_file('logo.16', iso_mount)
183 print("debug: copy logo.16 to %s") % target + '/boot/isolinux/' + 'logo.16'
185 for file in 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'f10':
186 bootsplash = search_file(file, iso_mount)
187 print("debug: copy %s to %s") % (bootsplash, target + '/boot/isolinux/' + file)
190 def uninstall_files(device):
194 def identify_grml_flavour(mountpath):
195 version_file = search_file('grml-version', mountpath)
196 file = open(version_file, 'r')
197 grml_info = file.readline()
199 grml_flavour = re.match(r'[\w-]*', grml_info).group()
205 print("%s %s")% (os.path.basename(sys.argv[0]), prog_version)
209 parser.error("invalid usage")
213 device = args[len(args) - 1]
214 isos = args[0:len(args) - 1]
216 if not which("syslinux"):
217 print("Sorry, syslinux not available. Exiting.")
218 print("Please install syslinux or consider using the --grub option.")
221 # TODO check for valid blockdevice, vfat and mount functions
222 # if device is not None:
223 # check_for_vat(device)
224 # mount_target(partition)
226 target = '/mnt/target'
228 # TODO it doesn't need to be a ISO, could be /live/image as well
230 print("debug: iso = %s") % iso
231 # loopback_mount(iso)
232 # copy_grml_files(iso, target)
233 # loopback_unmount(iso)
234 iso_mount = '/mnt/test' # FIXME
236 grml_flavour = identify_grml_flavour(iso_mount)
237 print("debug: grml_flavour = %s") % grml_flavour
239 grml_flavour_short = grml_flavour.replace('-','')
240 print("debug: grml_flavour_short = %s") % grml_flavour_short
242 copy_grml_files(grml_flavour, iso_mount, target)
245 print("debug: would install MBR now")
247 install_bootloader(device)
249 if __name__ == "__main__":
252 ## END OF FILE #################################################################
253 # vim:foldmethod=marker expandtab ai ft=python tw=120