3 # Copyright 2011 GRNET S.A. All rights reserved.
5 # Redistribution and use in source and binary forms, with or
6 # without modification, are permitted provided that the following
9 # 1. Redistributions of source code must retain the above
10 # copyright notice, this list of conditions and the following
13 # 2. Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials
16 # provided with the distribution.
18 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
31 # The views and conclusions contained in the software and
32 # documentation are those of the authors and should not be
33 # interpreted as representing official policies, either expressed
34 # or implied, of GRNET S.A.
36 from image_creator import get_os_class
37 from image_creator import __version__ as version
38 from image_creator import FatalError
39 from image_creator.disk import Disk
40 from image_creator.util import get_command, error, success, output
41 from image_creator import util
46 dd = get_command('dd')
49 def check_writable_dir(option, opt_str, value, parser):
50 dirname = os.path.dirname(value)
51 name = os.path.basename(value)
52 if dirname and not os.path.isdir(dirname):
53 parser.error("`%s' is not an existing directory" % dirname)
56 parser.error("`%s' is not a valid file name" % dirname)
58 setattr(parser.values, option.dest, value)
61 def parse_options(input_args):
62 usage = "Usage: %prog [options] <input_media>"
63 parser = optparse.OptionParser(version=version, usage=usage)
65 parser.add_option("-f", "--force", dest="force", default=False,
66 action="store_true", help="overwrite output files if they exist")
68 parser.add_option("--no-cleanup", dest="cleanup", default=True,
69 help="don't cleanup sensitive data", action="store_false")
71 parser.add_option("--no-sysprep", dest="sysprep", default=True,
72 help="don't perform system preperation", action="store_false")
74 parser.add_option("--no-shrink", dest="shrink", default=True,
75 help="don't shrink any partition", action="store_false")
77 parser.add_option("-o", "--outfile", type="string", dest="outfile",
78 default=None, action="callback", callback=check_writable_dir,
79 help="dump image to FILE", metavar="FILE")
81 parser.add_option("--print-sysprep", dest="print_sysprep", default=False,
82 help="Print the enabled and disable sysprep actions for this image",
85 parser.add_option("--print-data-cleanup", dest="print_data_cleanup",
86 default=False, help="Print the enabled and disable data cleanup "
87 "operations actions for this source", action="store_true")
89 parser.add_option("-s", "--silent", dest="silent", default=False,
90 help="silent mode, only output error", action="store_true")
92 parser.add_option("-u", "--upload", dest="upload", default=False,
93 help="upload the image to pithos", action="store_true")
95 parser.add_option("-r", "--register", dest="register", default=False,
96 help="register the image to ~okeanos", action="store_true")
98 options, args = parser.parse_args(input_args)
101 parser.error('Wrong number of arguments')
102 options.source = args[0]
103 if not os.path.exists(options.source):
104 parser.error('input media is not accessible')
107 options.upload = True
113 options = parse_options(sys.argv[1:])
118 if options.outfile is None and not options.upload \
119 and not options.print_sysprep and not options.print_data_cleanup:
120 FatalError("At least one of the following: `-o', `-u', "
121 "`--print-sysprep' `--print-data-cleanup' must be set")
123 output('snf-image-creator %s\n' % version)
125 if os.geteuid() != 0:
126 raise FatalError("You must run %s as root" \
127 % os.path.basename(sys.argv[0]))
129 if not options.force and options.outfile is not None:
130 for extension in ('', '.meta'):
131 filename = "%s%s" % (options.outfile, extension)
132 if os.path.exists(filename):
133 raise FatalError("Output file %s exists "
134 "(use --force to overwrite it)." % filename)
136 disk = Disk(options.source)
138 dev = disk.get_device()
141 osclass = get_os_class(dev.distro, dev.ostype)
142 image_os = osclass(dev.root, dev.g)
143 metadata = image_os.get_metadata()
147 if options.print_sysprep:
148 image_os.print_sysprep()
151 if options.print_data_cleanup:
152 image_os.print_data_cleanup()
155 if options.outfile is None and not options.upload:
162 image_os.data_cleanup()
166 size = options.shrink and dev.shrink() or dev.size()
167 metadata['SIZE'] = str(size // 2 ** 20)
169 if options.outfile is not None:
170 f = open('%s.%s' % (options.outfile, 'meta'), 'w')
172 for key in metadata.keys():
173 f.write("%s=%s\n" % (key, metadata[key]))
177 dev.dump(options.outfile)
179 output('cleaning up...')
187 ret = image_creator()
189 except FatalError as e:
194 if __name__ == '__main__':
197 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :