disable dracut hostonly and rescue image (#921422)
[livecd.git] / tools / livecd-creator
bloba39e43fd1c7be798b4365d3b171e9749773a25d0
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
26 import selinux
28 import imgcreate
29 from imgcreate.fs import makedirs
31 class Usage(Exception):
32 def __init__(self, msg = None, no_error = False):
33 Exception.__init__(self, msg, no_error)
35 def parse_options(args):
36 parser = optparse.OptionParser()
38 imgopt = optparse.OptionGroup(parser, "Image options",
39 "These options define the created image.")
40 imgopt.add_option("-c", "--config", type="string", dest="kscfg",
41 help="Path or url to kickstart config file")
42 imgopt.add_option("-b", "--base-on", type="string", dest="base_on",
43 help="Add packages to an existing live CD iso9660 image.")
44 imgopt.add_option("-f", "--fslabel", type="string", dest="fslabel",
45 help="File system label (default based on config name)")
46 imgopt.add_option("", "--title", type="string", dest="title",
47 help="Title used by syslinux.cfg file"),
48 imgopt.add_option("", "--product", type="string", dest="product",
49 help="Product name used in syslinux.cfg boot stanzas and countdown"),
50 # Provided for img-create compatibility
51 imgopt.add_option("-n", "--name", type="string", dest="fslabel",
52 help=optparse.SUPPRESS_HELP)
53 imgopt.add_option("-p", "--plugins", action="store_true", dest="plugins",
54 help="Use yum plugins during image creation",
55 default=False)
56 imgopt.add_option("", "--image-type", type="string", dest="image_type",
57 help=optparse.SUPPRESS_HELP)
58 imgopt.add_option("", "--compression-type", type="string", dest="compress_type",
59 help="Compression type recognized by mksquashfs "
60 "(default xz needs a 2.6.38+ kernel, gzip works "
61 "with all kernels, lzo needs a 2.6.36+ kernel, lzma "
62 "needs custom kernel.) Set to 'None' to force read "
63 "from base_on.",
64 default="xz")
65 imgopt.add_option("", "--releasever", type="string", dest="releasever",
66 default=None,
67 help="Value to substitute for $releasever in kickstart repo urls")
68 parser.add_option_group(imgopt)
70 # options related to the config of your system
71 sysopt = optparse.OptionGroup(parser, "System directory options",
72 "These options define directories used on your system for creating the live image")
73 sysopt.add_option("-t", "--tmpdir", type="string",
74 dest="tmpdir", default="/var/tmp",
75 help="Temporary directory to use (default: /var/tmp)")
76 sysopt.add_option("", "--cache", type="string",
77 dest="cachedir", default=None,
78 help="Cache directory to use (default: private cache")
79 sysopt.add_option("", "--cacheonly", action="store_true",
80 dest="cacheonly", default=False,
81 help="Work offline from cache, use together with --cache (default: False)")
82 sysopt.add_option("", "--nocleanup", action="store_true",
83 dest="nocleanup", default=False,
84 help="Skip cleanup of temporary files")
86 parser.add_option_group(sysopt)
88 imgcreate.setup_logging(parser)
90 # debug options not recommended for "production" images
91 # Start a shell in the chroot for post-configuration.
92 parser.add_option("-l", "--shell", action="store_true", dest="give_shell",
93 help=optparse.SUPPRESS_HELP)
94 # Don't compress the image.
95 parser.add_option("-s", "--skip-compression", action="store_true", dest="skip_compression",
96 help=optparse.SUPPRESS_HELP)
97 parser.add_option("", "--skip-minimize", action="store_true", dest="skip_minimize",
98 help=optparse.SUPPRESS_HELP)
100 (options, args) = parser.parse_args()
102 # Pretend to be a image-creator if called with that name
103 if not options.image_type:
104 if sys.argv[0].endswith('image-creator'):
105 options.image_type = 'image'
106 else:
107 options.image_type = 'livecd'
108 if options.image_type not in ('livecd', 'image'):
109 raise Usage("'%s' is a recognized image type" % options.image_type)
111 # image-create compatibility: Last argument is kickstart file
112 if len(args) == 1:
113 options.kscfg = args.pop()
114 if len(args):
115 raise Usage("Extra arguments given")
117 if not options.kscfg:
118 raise Usage("Kickstart file must be provided")
119 if options.base_on and not os.path.isfile(options.base_on):
120 raise Usage("Image file '%s' does not exist" %(options.base_on,))
121 if options.image_type == 'livecd':
122 if options.fslabel and len(options.fslabel) > imgcreate.FSLABEL_MAXLEN:
123 raise Usage("CD labels are limited to 32 characters")
124 if options.fslabel and options.fslabel.find(" ") != -1:
125 raise Usage("CD labels cannot contain spaces.")
127 return options
129 def main():
130 try:
131 options = parse_options(sys.argv[1:])
132 except Usage, (msg, no_error):
133 if no_error:
134 out = sys.stdout
135 ret = 0
136 else:
137 out = sys.stderr
138 ret = 2
139 if msg:
140 print >> out, msg
141 return ret
143 if os.geteuid () != 0:
144 print >> sys.stderr, "You must run %s as root" % sys.argv[0]
145 return 1
147 # Set selinux to Permissive if it is enforcing
148 selinux_enforcing = False
149 if selinux.is_selinux_enabled() and selinux.security_getenforce():
150 selinux_enforcing = True
151 selinux.security_setenforce(0)
153 if options.fslabel:
154 fslabel = options.fslabel
155 name = fslabel
156 else:
157 name = imgcreate.build_name(options.kscfg, options.image_type + "-")
159 fslabel = imgcreate.build_name(options.kscfg,
160 options.image_type + "-",
161 maxlen = imgcreate.FSLABEL_MAXLEN,
162 suffix = "%s-%s" %(os.uname()[4], time.strftime("%Y%m%d%H%M")))
164 logging.info("Using label '%s' and name '%s'" % (fslabel, name))
166 if options.title:
167 title = options.title
168 else:
169 try:
170 title = " ".join(name.split("-")[:2])
171 title = title.title()
172 except:
173 title = "Linux"
174 if options.product:
175 product = options.product
176 else:
177 try:
178 product = " ".join(name.split("-")[:2])
179 product = product.title()
180 except:
181 product = "Linux"
182 logging.info("Using title '%s' and product '%s'" % (title, product))
184 ks = imgcreate.read_kickstart(options.kscfg)
186 if options.image_type == 'livecd':
187 creator = imgcreate.LiveImageCreator(ks, name,
188 fslabel=fslabel,
189 releasever=options.releasever,
190 tmpdir=os.path.abspath(options.tmpdir),
191 useplugins=options.plugins,
192 title=title, product=product,
193 cacheonly=options.cacheonly,
194 docleanup=not options.nocleanup)
195 elif options.image_type == 'image':
196 creator = imgcreate.LoopImageCreator(ks, name,
197 fslabel=fslabel,
198 releasever=options.releasever,
199 useplugins=options.plugins,
200 tmpdir=os.path.abspath(options.tmpdir),
201 cacheonly=options.cacheonly,
202 docleanup=not options.nocleanup)
203 else:
204 # Cannot happen, we validate this when parsing options.
205 logging.error(u"'%s' is not a valid image type" % options.image_type)
206 if selinux_enforcing:
207 selinux.security_setenforce(1)
208 return 1
210 creator.compress_type = options.compress_type
211 creator.skip_compression = options.skip_compression
212 creator.skip_minimize = options.skip_minimize
213 if options.cachedir:
214 options.cachedir = os.path.abspath(options.cachedir)
216 try:
217 creator.mount(options.base_on, options.cachedir)
218 creator.install()
219 creator.configure()
220 if options.give_shell:
221 print "Launching shell. Exit to continue."
222 print "----------------------------------"
223 creator.launch_shell()
224 creator.unmount()
225 creator.package()
226 except imgcreate.CreatorError, e:
227 logging.error(u"Error creating Live CD : %s" % e)
228 return 1
229 finally:
230 creator.cleanup()
231 if selinux_enforcing:
232 selinux.security_setenforce(1)
234 return 0
236 if __name__ == "__main__":
237 sys.exit(main())