#!/usr/bin/env python # # live_part.py # # (c) Copyright 2009 - 2010 Michael Towers (larch42 at googlemail dot com) # # This file is part of the larch project. # # larch is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # larch is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with larch; if not, write to the Free Software Foundation, Inc., # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # #---------------------------------------------------------------------------- # 2010.07.18 # This is a command line script to prepare a larch live medium from an # already larchified Arch Linux installation, or another larch medium. # Parameters are passed as options and arguments. import os from config import * from backend import * from media_common import * def build_partition(options, devicename): """Install the larchified system to the partition specified by the options. 'devicename' is the name (e.g. 'sdb1', without '/dev/') of the partition to receive the larch image """ medium = Medium(options) ipath = medium.chrootpath build = medium.build # Check and format device device = '/dev/' + devicename if not os.path.exists(device): errout(_("Invalid device: %s") % device) if options.format: # label if options.syslinux: labellength = 11 opt = 'n' else: labellength = 16 opt = 'L' l = ('-%s "%s"' % (opt, check_label(options.label, labellength)) if options.label else '') # set partition type, reload partition table, and format if not (chroot(ipath, 'bash -c "echo -e \',,%s,*\\n\' |' ' sfdisk --no-reread %s -N%s"' % ('0c' if options.syslinux else 'L', device[:8], device[8:]), ['dev']) and chroot(ipath, 'partprobe %s' % device[:8], ['dev']) and chroot(ipath, 'mkfs.%s %s %s' % ('vfat' if options.syslinux else 'ext2', l, device), ['dev'])): errout(_("Couldn't format %s") % device) # Need to get the label - if not formatting (an option for experts) # it is probably not a good idea to change the volume label, so # use the old one. label = medium.get_device_label(device) # Check device format ok, lines = runcmd('blkid -c /dev/null -o value -s TYPE %s' % device) if not ok: errout(_("Couldn't get format information for %s") % device) fstype = lines[0] if options.syslinux: if fstype != 'vfat': errout(_("syslinux is only supported on vfat")) elif not fstype.startswith('ext'): errout(_("GRUB is at present only supported on extN")) # Rename the syslinux boot directory if necessary if os.path.isdir(build + '/boot/isolinux'): runcmd('mv %s/boot/isolinux %s/boot/syslinux' % (build, build)) # Write bootloader configuration file bootconfig(build, options.syslinux, label, device, options.detection) # Mount partition and remove larch and boot dirs runcmd('rm -rf %s' % MP) runcmd('mkdir -p %s' % MP) if not mount(device, MP): errout(_("Couldn't mount larch partition, %s") % device) runcmd('rm -rf %s/larch' % MP) runcmd('rm -rf %s/boot' % MP) # Copy files to device runcmd('cp -r %s/larch %s' % (medium.medium_dir, MP)) runcmd('cp -r %s/boot %s' % (build, MP)) medium.unmount() # To boot in 'search' mode the file larch/larchboot must be present # (though at present this is only relevant for partitions, CDs will # be booted even without this file). # To enable session saving the file larch/save must be present # (only relevant if not building an iso). runcmd('bash -c "rm -f %s/larch/{larchboot,save}"' % MP) if options.larchboot: add_larchboot(MP) if options.nosave: if options.save: errout(_("Option '-a' conflicts with option '-A'")) ssave = False else: ssave = not os.path.isfile(MP + '/larch/nosave') if options.save or ssave: writefile("The presence of the file 'larch/save'" " enables session saving.\n", MP + '/larch/save') # Unmount partition unmount(MP) # Now set up bootloader in MBR if options.mbr: if options.syslinux: runcmd('dd if=%s/boot/syslinux/mbr.bin of=%s' % (build, device[:8])) chroot(ipath, 'syslinux %s' % device, ('dev', 'proc')) else: script('larch-mbr-grub %s %s' % (ipath if ipath else '/', device)) runcmd('rm -rf %s' % build) comment(" *** %s ***" % (_("%s was successfully written") % device)) if __name__ == '__main__': start_translator() from optparse import OptionParser, OptionGroup parser = OptionParser(usage=_("usage: %prog [options] partition" " (e.g. sdb1)")) parser.add_option("-p", "--profile", action="store", type="string", default="", dest="profile", help=_("Profile: 'user:profile-name' or path to profile directory" " (conflicts with -S)")) parser.add_option("-S", "--source", action="store", type="string", default="", dest="source", help=_("Source: path to larch medium image (conflicts with -p)." " It can also be a device ('/dev/...') or an 'iso' file.")) parser.add_option("-i", "--installation-dir", action="store", type="string", default="", dest="idir", help=_("Path to larchified directory (default %s)") % INSTALLATION) parser.add_option("-s", "--slave", action="store_true", dest="slave", default=False, help=_("Run as a slave from a controlling program" " (e.g. from a gui)")) parser.add_option("-q", "--quiet", action="store_true", dest="quiet", default=False, help=_("Suppress output messages, except errors" " (no effect if -s specified)")) parser.add_option("-f", "--force", action="store_true", dest="force", default=False, help=_("Don't ask for confirmation")) parser.add_option("-d", "--detect", action="store", type="string", default="label", dest="detection", help=(_("Method for boot partition detection: %s (default: label)") % detection_methods)) parser.add_option("-b", "--syslinux", action="store_true", dest="syslinux", default=False, help=_("Use the syslinux bootloader" " (the default is GRUB)")) parser.add_option("-l", "--label", action="store", type="string", default=LABEL, dest="label", help=_("Volume label for boot partition (default %s)") % LABEL) parser.add_option("-n", "--nosearchboot", action="store_false", dest="larchboot", default=True, help=_("Don't generate 'larch/larchboot' file")) parser.add_option("-a", "--save", action="store_true", dest="save", default=False, help=_("Override profile larch/nosave" " (force enable session saving) - conflicts with '-A'")) parser.add_option("-A", "--nosave", action="store_true", dest="nosave", default=False, help=_("Force disabling of session saving - conflicts with '-a'")) parser.add_option("-x", "--noformat", action="store_false", dest="format", default=True, help=_("Don't format partition (only for experts!)")) parser.add_option("-m", "--noboot", action="store_false", dest="mbr", default=True, help=_("Don't install the bootloader (to the MBR)")) parser.add_option("-C", "--chroot", action="store_true", dest="chroot", default=False, help=_("Use chroot for build (default when -S not specified)")) parser.add_option("-c", "--nochroot", action="store_true", dest="nochroot", default=False, help=_("Don't use chroot for build (default when -S specified)")) (options, args) = parser.parse_args() # To avoid error messages options.testmedium = False options.setdir = '' if not args: print _("You must specify the partition to receive larch\n") parser.print_help() sys.exit(1) if os.getuid() != 0: print _("This application must be run as root") sys.exit(1) init('live_part', options) build_partition(options, devicename=args[0])