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.
27 from distutils
.dir_util
import copy_tree
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
,
38 releasever
=releasever
,
41 #self._outdir=os.getenv("TESTDIR", ".")
43 def install(self
, repo_urls
= {}):
44 copy_tree(os
.environ
.get("TESTDIR", ".") + "/root-source", self
._instroot
)
47 self
._create
_bootconfig
()
49 def _get_kernel_versions(self
):
53 ret
["kernel-" + version
] = [version
]
56 def __sanity_check(self
):
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 "
86 imgopt
.add_option("", "--releasever", type="string", dest
="releasever",
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
123 options
.kscfg
= args
.pop()
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.")
139 options
= parse_options(sys
.argv
[1:])
140 except Usage
, (msg
, no_error
):
151 if os
.geteuid () != 0:
152 print >> sys
.stderr
, "You must run %s as root" % sys
.argv
[0]
156 fslabel
= options
.fslabel
162 logging
.info("Using label '%s' and name '%s'" % (fslabel
, name
))
164 ks
= imgcreate
.read_kickstart(options
.kscfg
)
166 creator
= myLiveImageCreator(ks
, name
,
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
175 options
.cachedir
= os
.path
.abspath(options
.cachedir
)
178 creator
.mount(options
.base_on
, options
.cachedir
)
181 if options
.give_shell
:
182 print "Launching shell. Exit to continue."
183 print "----------------------------------"
184 creator
.launch_shell()
186 creator
.package(os
.environ
.get("TESTDIR", "."))
187 except imgcreate
.CreatorError
, e
:
188 logging
.error(u
"Error creating Live CD : %s" % e
)
195 if __name__
== "__main__":