Check kickstart for repo line (#1005580)
[livecd.git] / tools / livecd-creator
bloba2919e6fb61564cd3f4af1c3ea39fdbe16bb0c3a
1 #!/usr/bin/python -tt
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.
20 import os
21 import os.path
22 import sys
23 import time
24 import optparse
25 import logging
27 import imgcreate
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",
53 default=False)
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 "
61 "from base_on.",
62 default="xz")
63 imgopt.add_option("", "--releasever", type="string", dest="releasever",
64 default=None,
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'
104 else:
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
110 if len(args) == 1:
111 options.kscfg = args.pop()
112 if len(args):
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.")
125 return options
127 def main():
128 try:
129 options = parse_options(sys.argv[1:])
130 except Usage, (msg, no_error):
131 if no_error:
132 out = sys.stdout
133 ret = 0
134 else:
135 out = sys.stderr
136 ret = 2
137 if msg:
138 print >> out, msg
139 return ret
141 if os.geteuid () != 0:
142 print >> sys.stderr, "You must run %s as root" % sys.argv[0]
143 return 1
145 if options.fslabel:
146 fslabel = options.fslabel
147 name = fslabel
148 else:
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))
158 if options.title:
159 title = options.title
160 else:
161 try:
162 title = " ".join(name.split("-")[:2])
163 title = title.title()
164 except:
165 title = "Linux"
166 if options.product:
167 product = options.product
168 else:
169 try:
170 product = " ".join(name.split("-")[:2])
171 product = product.title()
172 except:
173 product = "Linux"
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)
179 return 1
181 try:
182 if options.image_type == 'livecd':
183 creator = imgcreate.LiveImageCreator(ks, name,
184 fslabel=fslabel,
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,
193 fslabel=fslabel,
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)
201 return 1
203 creator.compress_type = options.compress_type
204 creator.skip_compression = options.skip_compression
205 creator.skip_minimize = options.skip_minimize
206 if options.cachedir:
207 options.cachedir = os.path.abspath(options.cachedir)
209 try:
210 creator.mount(options.base_on, options.cachedir)
211 creator.install()
212 creator.configure()
213 if options.give_shell:
214 print "Launching shell. Exit to continue."
215 print "----------------------------------"
216 creator.launch_shell()
217 creator.unmount()
218 creator.package()
219 except imgcreate.CreatorError, e:
220 logging.error(u"Error creating Live CD : %s" % e)
221 return 1
222 finally:
223 creator.cleanup()
225 return 0
227 if __name__ == "__main__":
228 sys.exit(main())