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.
29 class Usage(Exception):
30 def __init__(self
, msg
= None, no_error
= False):
31 Exception.__init
__(self
, msg
, no_error
)
33 def parse_options(args
):
34 parser
= optparse
.OptionParser()
36 imgopt
= optparse
.OptionGroup(parser
, "Image options",
37 "These options define the created image.")
38 imgopt
.add_option("-c", "--config", type="string", dest
="kscfg",
39 help="Path or url to kickstart config file")
40 imgopt
.add_option("-b", "--base-on", type="string", dest
="base_on",
41 help="Add packages to an existing live CD iso9660 image.")
42 imgopt
.add_option("-f", "--fslabel", type="string", dest
="fslabel",
43 help="File system label (default based on config name)")
44 imgopt
.add_option("", "--title", type="string", dest
="title",
45 help="Title used by syslinux.cfg file"),
46 imgopt
.add_option("", "--product", type="string", dest
="product",
47 help="Product name used in syslinux.cfg boot stanzas and countdown"),
48 # Provided for img-create compatibility
49 imgopt
.add_option("-n", "--name", type="string", dest
="fslabel",
50 help=optparse
.SUPPRESS_HELP
)
51 imgopt
.add_option("-p", "--plugins", action
="store_true", dest
="plugins",
52 help="Use yum plugins during image creation",
54 imgopt
.add_option("", "--image-type", type="string", dest
="image_type",
55 help=optparse
.SUPPRESS_HELP
)
56 imgopt
.add_option("", "--compression-type", type="string", dest
="compress_type",
57 help="Compression type recognized by mksquashfs "
58 "(default xz needs a 2.6.38+ kernel, gzip works "
59 "with all kernels, lzo needs a 2.6.36+ kernel, lzma "
60 "needs custom kernel.) Set to 'None' to force read "
63 imgopt
.add_option("", "--releasever", type="string", dest
="releasever",
65 help="Value to substitute for $releasever in kickstart repo urls")
66 parser
.add_option_group(imgopt
)
68 # options related to the config of your system
69 sysopt
= optparse
.OptionGroup(parser
, "System directory options",
70 "These options define directories used on your system for creating the live image")
71 sysopt
.add_option("-t", "--tmpdir", type="string",
72 dest
="tmpdir", default
="/var/tmp",
73 help="Temporary directory to use (default: /var/tmp)")
74 sysopt
.add_option("", "--cache", type="string",
75 dest
="cachedir", default
=None,
76 help="Cache directory to use (default: private cache")
77 sysopt
.add_option("", "--cacheonly", action
="store_true",
78 dest
="cacheonly", default
=False,
79 help="Work offline from cache, use together with --cache (default: False)")
80 sysopt
.add_option("", "--nocleanup", action
="store_true",
81 dest
="nocleanup", default
=False,
82 help="Skip cleanup of temporary files")
84 parser
.add_option_group(sysopt
)
86 imgcreate
.setup_logging(parser
)
88 # debug options not recommended for "production" images
89 # Start a shell in the chroot for post-configuration.
90 parser
.add_option("-l", "--shell", action
="store_true", dest
="give_shell",
91 help=optparse
.SUPPRESS_HELP
)
92 # Don't compress the image.
93 parser
.add_option("-s", "--skip-compression", action
="store_true", dest
="skip_compression",
94 help=optparse
.SUPPRESS_HELP
)
95 parser
.add_option("", "--skip-minimize", action
="store_true", dest
="skip_minimize",
96 help=optparse
.SUPPRESS_HELP
)
98 (options
, args
) = parser
.parse_args()
100 # Pretend to be a image-creator if called with that name
101 if not options
.image_type
:
102 if sys
.argv
[0].endswith('image-creator'):
103 options
.image_type
= 'image'
105 options
.image_type
= 'livecd'
106 if options
.image_type
not in ('livecd', 'image'):
107 raise Usage("'%s' is not a recognized image type" % options
.image_type
)
109 # image-create compatibility: Last argument is kickstart file
111 options
.kscfg
= args
.pop()
113 raise Usage("Extra arguments given")
115 if not options
.kscfg
:
116 raise Usage("Kickstart file must be provided")
117 if options
.base_on
and not os
.path
.isfile(options
.base_on
):
118 raise Usage("Image file '%s' does not exist" %(options
.base_on
,))
119 if options
.image_type
== 'livecd':
120 if options
.fslabel
and len(options
.fslabel
) > imgcreate
.FSLABEL_MAXLEN
:
121 raise Usage("CD labels are limited to 32 characters")
122 if options
.fslabel
and options
.fslabel
.find(" ") != -1:
123 raise Usage("CD labels cannot contain spaces.")
129 options
= parse_options(sys
.argv
[1:])
130 except Usage
, (msg
, no_error
):
141 if os
.geteuid () != 0:
142 print >> sys
.stderr
, "You must run %s as root" % sys
.argv
[0]
146 fslabel
= options
.fslabel
149 name
= imgcreate
.build_name(options
.kscfg
, options
.image_type
+ "-")
151 fslabel
= imgcreate
.build_name(options
.kscfg
,
152 options
.image_type
+ "-",
153 maxlen
= imgcreate
.FSLABEL_MAXLEN
,
154 suffix
= "%s-%s" %(os
.uname()[4], time
.strftime("%Y%m%d%H%M")))
156 logging
.info("Using label '%s' and name '%s'" % (fslabel
, name
))
159 title
= options
.title
162 title
= " ".join(name
.split("-")[:2])
163 title
= title
.title()
167 product
= options
.product
170 product
= " ".join(name
.split("-")[:2])
171 product
= product
.title()
174 logging
.info("Using title '%s' and product '%s'" % (title
, product
))
176 ks
= imgcreate
.read_kickstart(options
.kscfg
)
177 if not ks
.handler
.repo
.seen
:
178 print >> sys
.stderr
, "Kickstart (%s) must have at least one repository." % (options
.kscfg
)
182 if options
.image_type
== 'livecd':
183 creator
= imgcreate
.LiveImageCreator(ks
, name
,
185 releasever
=options
.releasever
,
186 tmpdir
=os
.path
.abspath(options
.tmpdir
),
187 useplugins
=options
.plugins
,
188 title
=title
, product
=product
,
189 cacheonly
=options
.cacheonly
,
190 docleanup
=not options
.nocleanup
)
191 elif options
.image_type
== 'image':
192 creator
= imgcreate
.LoopImageCreator(ks
, name
,
194 releasever
=options
.releasever
,
195 useplugins
=options
.plugins
,
196 tmpdir
=os
.path
.abspath(options
.tmpdir
),
197 cacheonly
=options
.cacheonly
,
198 docleanup
=not options
.nocleanup
)
199 except imgcreate
.CreatorError
as e
:
200 logging
.error(u
"%s creation failed: %s", options
.image_type
, e
)
203 creator
.compress_type
= options
.compress_type
204 creator
.skip_compression
= options
.skip_compression
205 creator
.skip_minimize
= options
.skip_minimize
207 options
.cachedir
= os
.path
.abspath(options
.cachedir
)
210 creator
.mount(options
.base_on
, options
.cachedir
)
213 if options
.give_shell
:
214 print "Launching shell. Exit to continue."
215 print "----------------------------------"
216 creator
.launch_shell()
219 except imgcreate
.CreatorError
, e
:
220 logging
.error(u
"Error creating Live CD : %s" % e
)
227 if __name__
== "__main__":