summaryrefslogtreecommitdiffstats
path: root/build_tools/larch7/larch0/cli/boot_iso.py
blob: 19c35100e8840a9a4200d747e70212d5c9c4ab7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/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.12

"""This is a command line script to prepare a boot 'iso' for a USB larch
device, to make this bootable via CD on systems which cannot boot from USB
devices.
Parameters are passed as options and arguments.
"""

import os
from config import *
from backend import *
from media_common import *

def build_bootiso(options, devicename):
    """Create a boot iso for the specified medium.
    'devicename' is the name (e.g. 'sdb1', without '/dev/') of the
    source partition.
    """
    # Check device
    device = '/dev/' + devicename
    if not os.path.exists(device):
        errout(_("Invalid device: %s") % device)
    options.source = device
    medium = Medium(options)
    ipath = medium.chrootpath
    build = medium.build

    # Need to get the label
    label = medium.get_device_label(device)

    # Write bootloader configuration file
    bootconfig(build, options.syslinux, label, device, options.detection)

    # Select bootloader
    if options.syslinux:
        # Get fresh isolinux.bin
        runcmd('cp %s/boot/isolinux/isolinux.bin0 %s/boot/isolinux/isolinux.bin'
                % (build, build))
        parms = '-b boot/isolinux/isolinux.bin -c boot/isolinux/isolinux.boot'
    # Select bootloader
    else:
        parms = '-b boot/grub/stage2_eltorito'

    # Need to adjust paths to cater for chroot!
    source0 = medium.chroot_medium_dir()
    isopath0 = medium.iso_path()

    # Actually the volume label can be 32 bytes, but 16 is compatible
    # with ext2 (though a little longer than vfat)
    label = check_label(options.label, 16)
    # Build iso
    if not chroot(ipath, ('mkisofs -R -l %s -no-emul-boot -boot-load-size 4'
            ' -boot-info-table -input-charset=UTF-8'
            ' -V "%s"'
            ' -o "%s"'
            ' "%s"') % (parms, label, isopath0, BUILD0),
            filter=mkisofs_filter_gen()):

        errout(_("iso build failed"))

    medium.unmount()
    runcmd('rm -rf %s' % build)

    comment(" *** %s ***" % (_("%s was successfully created")
            % (options.isofile)))



if __name__ == '__main__':
    start_translator()

    from optparse import OptionParser, OptionGroup
    parser = OptionParser(usage=_("usage: %prog [options] partition"
            " (e.g. sdb1)"))

    parser.add_option("-o", "--isofile", action="store", type="string",
            default=BOOTISO, dest="isofile",
            help=_("Specify the output file (default '%s'). It will be"
                " generated to the current directory.") % BOOTISO)
    parser.add_option("-D", "--setdir", action="store", type="string",
            dest='setdir', default='',
            help=_("Set current directory,"
                    " so that the 'iso' can be placed there"))
    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("-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("-l", "--label", action="store", type="string",
            default=ISOLABEL, dest="label",
            help=_("Volume label for boot iso (default %s)")
                    % ISOLABEL)
    parser.add_option("-C", "--chroot", action="store_true",
            dest="chroot", default=False,
            help=_("Use chroot for build"))


    (options, args) = parser.parse_args()
    if not args:
        print _("You must specify the source partition\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)

    # To avoid error messages
    options.profile = None
    options.nochroot = None
    options.testmedium = False

    build_bootiso(options, devicename=args[0])