root / image_creator / main.py @ c54fc0e8
History | View | Annotate | Download (5.9 kB)
1 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from image_creator import get_os_class |
35 |
from image_creator import __version__ as version |
36 |
from image_creator import FatalError |
37 |
from image_creator.disk import Disk |
38 |
from image_creator.util import get_command |
39 |
|
40 |
import sys |
41 |
import os |
42 |
import optparse |
43 |
|
44 |
dd = get_command('dd')
|
45 |
|
46 |
|
47 |
def check_writable_dir(option, opt_str, value, parser): |
48 |
dirname = os.path.dirname(value) |
49 |
name = os.path.basename(value) |
50 |
if dirname and not os.path.isdir(dirname): |
51 |
parser.error("`%s' is not an existing directory" % dirname)
|
52 |
|
53 |
if not name: |
54 |
parser.error("`%s' is not a valid file name" % dirname)
|
55 |
|
56 |
setattr(parser.values, option.dest, value)
|
57 |
|
58 |
|
59 |
def parse_options(input_args): |
60 |
usage = "Usage: %prog [options] <input_media>"
|
61 |
parser = optparse.OptionParser(version=version, usage=usage) |
62 |
|
63 |
parser.add_option("-f", "--force", dest="force", default=False, |
64 |
action="store_true", help="Overwrite output files if they exist") |
65 |
|
66 |
parser.add_option("--no-cleanup", dest="cleanup", default=True, |
67 |
help="Don't cleanup sensitive data before extracting the image",
|
68 |
action="store_false")
|
69 |
|
70 |
parser.add_option("--no-sysprep", dest="sysprep", default=True, |
71 |
help="Don't perform system preperation before extracting the image",
|
72 |
action="store_false")
|
73 |
|
74 |
parser.add_option("--no-shrink", dest="shrink", default=True, |
75 |
help="Don't shrink any partition before extracting the image",
|
76 |
action="store_false")
|
77 |
|
78 |
parser.add_option("-o", "--outfile", type="string", dest="outfile", |
79 |
default=None, action="callback", callback=check_writable_dir, |
80 |
help="Output image file",
|
81 |
metavar="FILE")
|
82 |
|
83 |
parser.add_option("-u", "--upload", dest="upload", default=False, |
84 |
help="Upload image to a pithos repository using kamaki",
|
85 |
action="store_true")
|
86 |
|
87 |
parser.add_option("-r", "--register", dest="register", default=False, |
88 |
help="Register image to okeanos using kamaki", action="store_true") |
89 |
|
90 |
options, args = parser.parse_args(input_args) |
91 |
|
92 |
if len(args) != 1: |
93 |
parser.error('Wrong number of arguments')
|
94 |
options.source = args[0]
|
95 |
if not os.path.exists(options.source): |
96 |
parser.error('input media is not accessible')
|
97 |
|
98 |
if options.register:
|
99 |
options.upload = True
|
100 |
|
101 |
if options.outfile is None and not options.upload: |
102 |
parser.error('either outfile (-o) or upload (-u) must be set.')
|
103 |
|
104 |
return options
|
105 |
|
106 |
|
107 |
def image_creator(): |
108 |
|
109 |
options = parse_options(sys.argv[1:])
|
110 |
|
111 |
if os.geteuid() != 0: |
112 |
raise FatalError("You must run %s as root" \ |
113 |
% os.path.basename(sys.argv[0]))
|
114 |
|
115 |
if not options.force: |
116 |
for extension in ('', '.meta'): |
117 |
filename = "%s%s" % (options.outfile, extension)
|
118 |
if os.path.exists(filename):
|
119 |
raise FatalError("Output file %s exists " |
120 |
"(use --force to overwrite it)." % filename)
|
121 |
|
122 |
disk = Disk(options.source) |
123 |
try:
|
124 |
dev = disk.get_device() |
125 |
dev.mount() |
126 |
osclass = get_os_class(dev.distro, dev.ostype) |
127 |
image_os = osclass(dev.root, dev.g) |
128 |
metadata = image_os.get_metadata() |
129 |
|
130 |
if options.sysprep:
|
131 |
image_os.sysprep() |
132 |
|
133 |
if options.cleanup:
|
134 |
image_os.data_cleanup() |
135 |
|
136 |
dev.umount() |
137 |
|
138 |
size = options.shrink and dev.shrink() or dev.size() |
139 |
metadata['size'] = str(size // 2 ** 20) |
140 |
|
141 |
outfile = ""
|
142 |
if options.outfile is not None: |
143 |
outfile = options.outfile |
144 |
f = open('%s.%s' % (options.outfile, 'meta'), 'w') |
145 |
try:
|
146 |
for key in metadata.keys(): |
147 |
f.write("%s=%s\n" % (key, metadata[key]))
|
148 |
finally:
|
149 |
f.close() |
150 |
else:
|
151 |
outfd, outfile = tmpfile.mkstemp() |
152 |
os.close(outfd) |
153 |
|
154 |
dd('if=%s' % dev.device,
|
155 |
'of=%s' % outfile,
|
156 |
'bs=4M', 'count=%d' % ((size + 1) // 2 ** 22)) |
157 |
|
158 |
finally:
|
159 |
disk.cleanup() |
160 |
|
161 |
#The image is ready, lets call kamaki if necessary
|
162 |
if options.upload:
|
163 |
pass
|
164 |
|
165 |
if options.outfile is None: |
166 |
os.unlink(outfile) |
167 |
|
168 |
return 0 |
169 |
|
170 |
COLOR_BLACK = "\033[00m"
|
171 |
COLOR_RED = "\033[1;31m"
|
172 |
|
173 |
def main(): |
174 |
try:
|
175 |
ret = image_creator() |
176 |
sys.exit(ret) |
177 |
except FatalError as e: |
178 |
print >> sys.stderr, "\n%sError: %s%s\n" % (COLOR_RED, e, COLOR_BLACK) |
179 |
sys.exit(1)
|
180 |
|
181 |
|
182 |
if __name__ == '__main__': |
183 |
main() |
184 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|