use cp -r instead of -a
[livecd.git] / tools / livecd-creator
blob44d07a114141579557f8249a4527a40096901679
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
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",
54 default=False)
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 "
62 "from base_on.",
63 default="xz")
64 imgopt.add_option("", "--releasever", type="string", dest="releasever",
65 default=None,
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'
105 else:
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
111 if len(args) == 1:
112 options.kscfg = args.pop()
113 if len(args):
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.")
126 return options
128 def main():
129 try:
130 options = parse_options(sys.argv[1:])
131 except Usage, (msg, no_error):
132 if no_error:
133 out = sys.stdout
134 ret = 0
135 else:
136 out = sys.stderr
137 ret = 2
138 if msg:
139 print >> out, msg
140 return ret
142 if os.geteuid () != 0:
143 print >> sys.stderr, "You must run %s as root" % sys.argv[0]
144 return 1
146 if options.fslabel:
147 fslabel = options.fslabel
148 name = fslabel
149 else:
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))
159 if options.title:
160 title = options.title
161 else:
162 try:
163 title = " ".join(name.split("-")[:2])
164 title = title.title()
165 except:
166 title = "Linux"
167 if options.product:
168 product = options.product
169 else:
170 try:
171 product = " ".join(name.split("-")[:2])
172 product = product.title()
173 except:
174 product = "Linux"
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,
181 fslabel=fslabel,
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,
190 fslabel=fslabel,
191 releasever=options.releasever,
192 useplugins=options.plugins,
193 tmpdir=os.path.abspath(options.tmpdir),
194 cacheonly=options.cacheonly,
195 docleanup=not options.nocleanup)
196 else:
197 # Cannot happen, we validate this when parsing options.
198 logging.error(u"'%s' is not a valid image type" % options.image_type)
199 return 1
201 creator.compress_type = options.compress_type
202 creator.skip_compression = options.skip_compression
203 creator.skip_minimize = options.skip_minimize
204 if options.cachedir:
205 options.cachedir = os.path.abspath(options.cachedir)
207 try:
208 creator.mount(options.base_on, options.cachedir)
209 creator.install()
210 creator.configure()
211 if options.give_shell:
212 print "Launching shell. Exit to continue."
213 print "----------------------------------"
214 creator.launch_shell()
215 creator.unmount()
216 creator.package()
217 except imgcreate.CreatorError, e:
218 logging.error(u"Error creating Live CD : %s" % e)
219 return 1
220 finally:
221 creator.cleanup()
223 return 0
225 if __name__ == "__main__":
226 sys.exit(main())