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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
#!/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])
|