3 # livecd-creator : Creates Live CD based for Fedora.
5 # Copyright 2007, Red Hat Inc.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; version 2 of the License.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Library General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 from imgcreate
.fs
import makedirs
30 class Usage(Exception):
31 def __init__(self
, msg
= None, no_error
= False):
32 Exception.__init
__(self
, msg
, no_error
)
34 def parse_options(args
):
35 parser
= optparse
.OptionParser()
37 imgopt
= optparse
.OptionGroup(parser
, "Image options",
38 "These options define the created image.")
39 imgopt
.add_option("-c", "--config", type="string", dest
="kscfg",
40 help="Path or url to kickstart config file")
41 imgopt
.add_option("-b", "--base-on", type="string", dest
="base_on",
42 help="Add packages to an existing live CD iso9660 image.")
43 imgopt
.add_option("-f", "--fslabel", type="string", dest
="fslabel",
44 help="File system label (default based on config name)")
45 imgopt
.add_option("", "--title", type="string", dest
="title",
46 help="Title used by syslinux.cfg file"),
47 imgopt
.add_option("", "--product", type="string", dest
="product",
48 help="Product name used in syslinux.cfg boot stanzas and countdown"),
49 # Provided for img-create compatibility
50 imgopt
.add_option("-n", "--name", type="string", dest
="fslabel",
51 help=optparse
.SUPPRESS_HELP
)
52 imgopt
.add_option("", "--image-type", type="string", dest
="image_type",
53 help=optparse
.SUPPRESS_HELP
)
54 imgopt
.add_option("", "--compression-type", type="string", dest
="compress_type",
55 help="Compression type recognized by mksquashfs "
56 "(default xz needs a 2.6.38+ kernel, gzip works "
57 "with all kernels, lzo needs a 2.6.36+ kernel, lzma "
58 "needs custom kernel.) Set to 'None' to force read "
61 imgopt
.add_option("", "--releasever", type="string", dest
="releasever",
63 help="Value to substitute for $releasever in kickstart repo urls")
64 parser
.add_option_group(imgopt
)
66 # options related to the config of your system
67 sysopt
= optparse
.OptionGroup(parser
, "System directory options",
68 "These options define directories used on your system for creating the live image")
69 sysopt
.add_option("-t", "--tmpdir", type="string",
70 dest
="tmpdir", default
="/var/tmp",
71 help="Temporary directory to use (default: /var/tmp)")
72 sysopt
.add_option("", "--cache", type="string",
73 dest
="cachedir", default
=None,
74 help="Cache directory to use (default: private cache")
75 parser
.add_option_group(sysopt
)
77 imgcreate
.setup_logging(parser
)
79 # debug options not recommended for "production" images
80 # Start a shell in the chroot for post-configuration.
81 parser
.add_option("-l", "--shell", action
="store_true", dest
="give_shell",
82 help=optparse
.SUPPRESS_HELP
)
83 # Don't compress the image.
84 parser
.add_option("-s", "--skip-compression", action
="store_true", dest
="skip_compression",
85 help=optparse
.SUPPRESS_HELP
)
86 parser
.add_option("", "--skip-minimize", action
="store_true", dest
="skip_minimize",
87 help=optparse
.SUPPRESS_HELP
)
89 (options
, args
) = parser
.parse_args()
91 # Pretend to be a image-creator if called with that name
92 if not options
.image_type
:
93 if sys
.argv
[0].endswith('image-creator'):
94 options
.image_type
= 'image'
96 options
.image_type
= 'livecd'
97 if options
.image_type
not in ('livecd', 'image'):
98 raise Usage("'%s' is a recognized image type" % options
.image_type
)
100 # image-create compatibility: Last argument is kickstart file
102 options
.kscfg
= args
.pop()
104 raise Usage("Extra arguments given")
106 if not options
.kscfg
:
107 raise Usage("Kickstart file must be provided")
108 if options
.base_on
and not os
.path
.isfile(options
.base_on
):
109 raise Usage("Image file '%s' does not exist" %(options
.base_on
,))
110 if options
.image_type
== 'livecd':
111 if options
.fslabel
and len(options
.fslabel
) > imgcreate
.FSLABEL_MAXLEN
:
112 raise Usage("CD labels are limited to 32 characters")
113 if options
.fslabel
and options
.fslabel
.find(" ") != -1:
114 raise Usage("CD labels cannot contain spaces.")
120 options
= parse_options(sys
.argv
[1:])
121 except Usage
, (msg
, no_error
):
132 if os
.geteuid () != 0:
133 print >> sys
.stderr
, "You must run %s as root" % sys
.argv
[0]
137 fslabel
= options
.fslabel
140 name
= imgcreate
.build_name(options
.kscfg
, options
.image_type
+ "-")
142 fslabel
= imgcreate
.build_name(options
.kscfg
,
143 options
.image_type
+ "-",
144 maxlen
= imgcreate
.FSLABEL_MAXLEN
,
145 suffix
= "%s-%s" %(os
.uname()[4], time
.strftime("%Y%m%d%H%M")))
147 logging
.info("Using label '%s' and name '%s'" % (fslabel
, name
))
150 title
= options
.title
153 title
= " ".join(name
.split("-")[:2])
154 title
= title
.title()
158 product
= options
.product
161 product
= " ".join(name
.split("-")[:2])
162 product
= product
.title()
165 logging
.info("Using title '%s' and product '%s'" % (title
, product
))
167 ks
= imgcreate
.read_kickstart(options
.kscfg
)
169 if options
.image_type
== 'livecd':
170 creator
= imgcreate
.LiveImageCreator(ks
, name
,
172 releasever
=options
.releasever
,
173 tmpdir
=os
.path
.abspath(options
.tmpdir
),
174 title
=title
, product
=product
)
175 elif options
.image_type
== 'image':
176 creator
= imgcreate
.LoopImageCreator(ks
, name
,
178 releasever
=options
.releasever
,
179 tmpdir
=os
.path
.abspath(options
.tmpdir
))
181 # Cannot happen, we validate this when parsing options.
182 logging
.error(u
"'%s' is not a valid image type" % options
.image_type
)
185 creator
.compress_type
= options
.compress_type
186 creator
.skip_compression
= options
.skip_compression
187 creator
.skip_minimize
= options
.skip_minimize
189 options
.cachedir
= os
.path
.abspath(options
.cachedir
)
192 creator
.mount(options
.base_on
, options
.cachedir
)
195 if options
.give_shell
:
196 print "Launching shell. Exit to continue."
197 print "----------------------------------"
198 creator
.launch_shell()
201 except imgcreate
.CreatorError
, e
:
202 logging
.error(u
"Error creating Live CD : %s" % e
)
209 if __name__
== "__main__":