Note that lzo can be used as a compressor.
[livecd.git] / tools / livecd-creator
blobd352d747656f8df70e95d8c8f87b5fd6cc8ff1e8
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="fs_label",
43 help="File system label (default based on config name)")
44 imgopt.add_option("", "--compression-type", type="string", dest="compress_type",
45 help="Compression type recognized by mksquashfs (default gzip, lzma needs custom kernel, lzo needs a 2.6.36+ kernel)",
46 default="gzip")
47 parser.add_option_group(imgopt)
49 # options related to the config of your system
50 sysopt = optparse.OptionGroup(parser, "System directory options",
51 "These options define directories used on your system for creating the live image")
52 sysopt.add_option("-t", "--tmpdir", type="string",
53 dest="tmpdir", default="/var/tmp",
54 help="Temporary directory to use (default: /var/tmp)")
55 sysopt.add_option("", "--cache", type="string",
56 dest="cachedir", default=None,
57 help="Cache directory to use (default: private cache")
58 parser.add_option_group(sysopt)
60 imgcreate.setup_logging(parser)
62 # debug options not recommended for "production" images
63 # Start a shell in the chroot for post-configuration.
64 parser.add_option("-l", "--shell", action="store_true", dest="give_shell",
65 help=optparse.SUPPRESS_HELP)
66 # Don't compress the image.
67 parser.add_option("-s", "--skip-compression", action="store_true", dest="skip_compression",
68 help=optparse.SUPPRESS_HELP)
69 parser.add_option("", "--skip-minimize", action="store_true", dest="skip_minimize",
70 help=optparse.SUPPRESS_HELP)
72 (options, args) = parser.parse_args()
73 if not options.kscfg:
74 raise Usage("Kickstart file must be provided")
75 if options.base_on and not os.path.isfile(options.base_on):
76 raise Usage("Live CD ISO '%s' does not exist" %(options.base_on,))
77 if options.fs_label and len(options.fs_label) > imgcreate.FSLABEL_MAXLEN:
78 raise Usage("CD labels are limited to 32 characters")
79 if options.fs_label and options.fs_label.find(" ") != -1:
80 raise Usage("CD labels cannot contain spaces.")
82 return options
84 def main():
85 try:
86 options = parse_options(sys.argv[1:])
87 except Usage, (msg, no_error):
88 if no_error:
89 out = sys.stdout
90 ret = 0
91 else:
92 out = sys.stderr
93 ret = 2
94 if msg:
95 print >> out, msg
96 return ret
98 if os.geteuid () != 0:
99 print >> sys.stderr, "You must run livecd-creator as root"
100 return 1
102 if options.fs_label:
103 fs_label = options.fs_label
104 name = fs_label
105 else:
106 name = imgcreate.build_name(options.kscfg, "livecd-")
108 fs_label = imgcreate.build_name(options.kscfg,
109 "livecd-",
110 maxlen = imgcreate.FSLABEL_MAXLEN,
111 suffix = "%s-%s" %(os.uname()[4], time.strftime("%Y%m%d%H%M")))
113 logging.info("Using label '%s' and name '%s'" % (fs_label, name))
115 ks = imgcreate.read_kickstart(options.kscfg)
117 creator = imgcreate.LiveImageCreator(ks, name, fs_label)
118 creator.tmpdir = os.path.abspath(options.tmpdir)
119 creator.compress_type = options.compress_type
120 creator.skip_compression = options.skip_compression
121 creator.skip_minimize = options.skip_minimize
122 if options.cachedir:
123 options.cachedir = os.path.abspath(options.cachedir)
125 try:
126 creator.mount(options.base_on, options.cachedir)
127 creator.install()
128 creator.configure()
129 if options.give_shell:
130 print "Launching shell. Exit to continue."
131 print "----------------------------------"
132 creator.launch_shell()
133 creator.unmount()
134 creator.package()
135 except imgcreate.CreatorError, e:
136 logging.error(u"Error creating Live CD : %s" % e)
137 return 1
138 finally:
139 creator.cleanup()
141 return 0
143 if __name__ == "__main__":
144 sys.exit(main())