dmsquash-live: add dmsquash-generator
[dracut.git] / test / TEST-16-DMSQUASH / create.py
blob53a62665192336d613663c6caa9456cd393168c7
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 shutil
27 from distutils.dir_util import copy_tree
29 import imgcreate
30 from imgcreate.fs import makedirs
32 class myLiveImageCreator(imgcreate.x86LiveImageCreator):
33 def __init__(self, ks, name, fslabel=None, releasever=None, tmpdir="/tmp",
34 title="Linux", product="Linux"):
36 imgcreate.x86LiveImageCreator.__init__(self, ks, name,
37 fslabel=fslabel,
38 releasever=releasever,
39 tmpdir=tmpdir)
41 #self._outdir=os.getenv("TESTDIR", ".")
43 def install(self, repo_urls = {}):
44 copy_tree(os.environ.get("TESTDIR", ".") + "/root-source", self._instroot)
46 def configure(self):
47 self._create_bootconfig()
49 def _get_kernel_versions(self):
50 ret = {}
51 version=os.uname()
52 version=version[2]
53 ret["kernel-" + version] = [version]
54 return ret
56 def __sanity_check(self):
57 pass
59 class Usage(Exception):
60 def __init__(self, msg = None, no_error = False):
61 Exception.__init__(self, msg, no_error)
63 def parse_options(args):
64 parser = optparse.OptionParser()
66 imgopt = optparse.OptionGroup(parser, "Image options",
67 "These options define the created image.")
68 imgopt.add_option("-c", "--config", type="string", dest="kscfg",
69 help="Path or url to kickstart config file")
70 imgopt.add_option("-b", "--base-on", type="string", dest="base_on",
71 help="Add packages to an existing live CD iso9660 image.")
72 imgopt.add_option("-f", "--fslabel", type="string", dest="fslabel",
73 help="File system label (default based on config name)")
74 # Provided for img-create compatibility
75 imgopt.add_option("-n", "--name", type="string", dest="fslabel",
76 help=optparse.SUPPRESS_HELP)
77 imgopt.add_option("", "--image-type", type="string", dest="image_type",
78 help=optparse.SUPPRESS_HELP)
79 imgopt.add_option("", "--compression-type", type="string", dest="compress_type",
80 help="Compression type recognized by mksquashfs "
81 "(default xz needs a 2.6.38+ kernel, gzip works "
82 "with all kernels, lzo needs a 2.6.36+ kernel, lzma "
83 "needs custom kernel.) Set to 'None' to force read "
84 "from base_on.",
85 default="xz")
86 imgopt.add_option("", "--releasever", type="string", dest="releasever",
87 default=None,
88 help="Value to substitute for $releasever in kickstart repo urls")
89 parser.add_option_group(imgopt)
91 # options related to the config of your system
92 sysopt = optparse.OptionGroup(parser, "System directory options",
93 "These options define directories used on your system for creating the live image")
94 sysopt.add_option("-t", "--tmpdir", type="string",
95 dest="tmpdir", default="/var/tmp",
96 help="Temporary directory to use (default: /var/tmp)")
97 sysopt.add_option("", "--cache", type="string",
98 dest="cachedir", default=None,
99 help="Cache directory to use (default: private cache")
100 parser.add_option_group(sysopt)
102 imgcreate.setup_logging(parser)
104 # debug options not recommended for "production" images
105 # Start a shell in the chroot for post-configuration.
106 parser.add_option("-l", "--shell", action="store_true", dest="give_shell",
107 help=optparse.SUPPRESS_HELP)
108 # Don't compress the image.
109 parser.add_option("-s", "--skip-compression", action="store_true", dest="skip_compression",
110 help=optparse.SUPPRESS_HELP)
111 parser.add_option("", "--skip-minimize", action="store_true", dest="skip_minimize",
112 help=optparse.SUPPRESS_HELP)
114 (options, args) = parser.parse_args()
116 # Pretend to be a image-creator if called with that name
117 options.image_type = 'livecd'
118 if options.image_type not in ('livecd', 'image'):
119 raise Usage("'%s' is a recognized image type" % options.image_type)
121 # image-create compatibility: Last argument is kickstart file
122 if len(args) == 1:
123 options.kscfg = args.pop()
124 if len(args):
125 raise Usage("Extra arguments given")
127 if options.base_on and not os.path.isfile(options.base_on):
128 raise Usage("Image file '%s' does not exist" %(options.base_on,))
129 if options.image_type == 'livecd':
130 if options.fslabel and len(options.fslabel) > imgcreate.FSLABEL_MAXLEN:
131 raise Usage("CD labels are limited to 32 characters")
132 if options.fslabel and options.fslabel.find(" ") != -1:
133 raise Usage("CD labels cannot contain spaces.")
135 return options
137 def main():
138 try:
139 options = parse_options(sys.argv[1:])
140 except Usage, (msg, no_error):
141 if no_error:
142 out = sys.stdout
143 ret = 0
144 else:
145 out = sys.stderr
146 ret = 2
147 if msg:
148 print >> out, msg
149 return ret
151 if os.geteuid () != 0:
152 print >> sys.stderr, "You must run %s as root" % sys.argv[0]
153 return 1
155 if options.fslabel:
156 fslabel = options.fslabel
157 name = fslabel
158 else:
159 name = "livecd"
161 fslabel = "LiveCD"
162 logging.info("Using label '%s' and name '%s'" % (fslabel, name))
164 ks = imgcreate.read_kickstart(options.kscfg)
166 creator = myLiveImageCreator(ks, name,
167 fslabel=fslabel,
168 releasever=options.releasever,
169 tmpdir=os.path.abspath(options.tmpdir))
171 creator.compress_type = options.compress_type
172 creator.skip_compression = options.skip_compression
173 creator.skip_minimize = options.skip_minimize
174 if options.cachedir:
175 options.cachedir = os.path.abspath(options.cachedir)
177 try:
178 creator.mount(options.base_on, options.cachedir)
179 creator.install()
180 creator.configure()
181 if options.give_shell:
182 print "Launching shell. Exit to continue."
183 print "----------------------------------"
184 creator.launch_shell()
185 creator.unmount()
186 creator.package(os.environ.get("TESTDIR", "."))
187 except imgcreate.CreatorError, e:
188 logging.error(u"Error creating Live CD : %s" % e)
189 return 1
190 finally:
191 creator.cleanup()
193 return 0
195 if __name__ == "__main__":
196 sys.exit(main())