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("-p", "--plugins", action
="store_true", dest
="plugins",
53 help="Use yum plugins during image creation",
55 imgopt
.add_option("", "--image-type", type="string", dest
="image_type",
56 help=optparse
.SUPPRESS_HELP
)
57 imgopt
.add_option("", "--compression-type", type="string", dest
="compress_type",
58 help="Compression type recognized by mksquashfs "
59 "(default xz needs a 2.6.38+ kernel, gzip works "
60 "with all kernels, lzo needs a 2.6.36+ kernel, lzma "
61 "needs custom kernel.) Set to 'None' to force read "
64 imgopt
.add_option("", "--releasever", type="string", dest
="releasever",
66 help="Value to substitute for $releasever in kickstart repo urls")
67 parser
.add_option_group(imgopt
)
69 # options related to the config of your system
70 sysopt
= optparse
.OptionGroup(parser
, "System directory options",
71 "These options define directories used on your system for creating the live image")
72 sysopt
.add_option("-t", "--tmpdir", type="string",
73 dest
="tmpdir", default
="/var/tmp",
74 help="Temporary directory to use (default: /var/tmp)")
75 sysopt
.add_option("", "--cache", type="string",
76 dest
="cachedir", default
=None,
77 help="Cache directory to use (default: private cache")
78 sysopt
.add_option("", "--cacheonly", action
="store_true",
79 dest
="cacheonly", default
=False,
80 help="Work offline from cache, use together with --cache (default: False)")
81 sysopt
.add_option("", "--nocleanup", action
="store_true",
82 dest
="nocleanup", default
=False,
83 help="Skip cleanup of temporary files")
85 parser
.add_option_group(sysopt
)
87 imgcreate
.setup_logging(parser
)
89 # debug options not recommended for "production" images
90 # Start a shell in the chroot for post-configuration.
91 parser
.add_option("-l", "--shell", action
="store_true", dest
="give_shell",
92 help=optparse
.SUPPRESS_HELP
)
93 # Don't compress the image.
94 parser
.add_option("-s", "--skip-compression", action
="store_true", dest
="skip_compression",
95 help=optparse
.SUPPRESS_HELP
)
96 parser
.add_option("", "--skip-minimize", action
="store_true", dest
="skip_minimize",
97 help=optparse
.SUPPRESS_HELP
)
99 (options
, args
) = parser
.parse_args()
101 # Pretend to be a image-creator if called with that name
102 if not options
.image_type
:
103 if sys
.argv
[0].endswith('image-creator'):
104 options
.image_type
= 'image'
106 options
.image_type
= 'livecd'
107 if options
.image_type
not in ('livecd', 'image'):
108 raise Usage("'%s' is a recognized image type" % options
.image_type
)
110 # image-create compatibility: Last argument is kickstart file
112 options
.kscfg
= args
.pop()
114 raise Usage("Extra arguments given")
116 if not options
.kscfg
:
117 raise Usage("Kickstart file must be provided")
118 if options
.base_on
and not os
.path
.isfile(options
.base_on
):
119 raise Usage("Image file '%s' does not exist" %(options
.base_on
,))
120 if options
.image_type
== 'livecd':
121 if options
.fslabel
and len(options
.fslabel
) > imgcreate
.FSLABEL_MAXLEN
:
122 raise Usage("CD labels are limited to 32 characters")
123 if options
.fslabel
and options
.fslabel
.find(" ") != -1:
124 raise Usage("CD labels cannot contain spaces.")
130 options
= parse_options(sys
.argv
[1:])
131 except Usage
, (msg
, no_error
):
142 if os
.geteuid () != 0:
143 print >> sys
.stderr
, "You must run %s as root" % sys
.argv
[0]
147 fslabel
= options
.fslabel
150 name
= imgcreate
.build_name(options
.kscfg
, options
.image_type
+ "-")
152 fslabel
= imgcreate
.build_name(options
.kscfg
,
153 options
.image_type
+ "-",
154 maxlen
= imgcreate
.FSLABEL_MAXLEN
,
155 suffix
= "%s-%s" %(os
.uname()[4], time
.strftime("%Y%m%d%H%M")))
157 logging
.info("Using label '%s' and name '%s'" % (fslabel
, name
))
160 title
= options
.title
163 title
= " ".join(name
.split("-")[:2])
164 title
= title
.title()
168 product
= options
.product
171 product
= " ".join(name
.split("-")[:2])
172 product
= product
.title()
175 logging
.info("Using title '%s' and product '%s'" % (title
, product
))
177 ks
= imgcreate
.read_kickstart(options
.kscfg
)
179 if options
.image_type
== 'livecd':
180 creator
= imgcreate
.LiveImageCreator(ks
, name
,
182 releasever
=options
.releasever
,
183 tmpdir
=os
.path
.abspath(options
.tmpdir
),
184 useplugins
=options
.plugins
,
185 title
=title
, product
=product
,
186 cacheonly
=options
.cacheonly
,
187 docleanup
=not options
.nocleanup
)
188 elif options
.image_type
== 'image':
189 creator
= imgcreate
.LoopImageCreator(ks
, name
,
191 releasever
=options
.releasever
,
192 useplugins
=options
.plugins
,
193 tmpdir
=os
.path
.abspath(options
.tmpdir
),
194 cacheonly
=options
.cacheonly
,
195 docleanup
=not options
.nocleanup
)
197 # Cannot happen, we validate this when parsing options.
198 logging
.error(u
"'%s' is not a valid image type" % options
.image_type
)
201 creator
.compress_type
= options
.compress_type
202 creator
.skip_compression
= options
.skip_compression
203 creator
.skip_minimize
= options
.skip_minimize
205 options
.cachedir
= os
.path
.abspath(options
.cachedir
)
208 creator
.mount(options
.base_on
, options
.cachedir
)
211 if options
.give_shell
:
212 print "Launching shell. Exit to continue."
213 print "----------------------------------"
214 creator
.launch_shell()
217 except imgcreate
.CreatorError
, e
:
218 logging
.error(u
"Error creating Live CD : %s" % e
)
225 if __name__
== "__main__":