2 # -*- coding: utf-8 -*-
4 # This file is part of Karesansui.
6 # Copyright (C) 2009-2010 HDE, Inc.
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
18 from optparse import OptionParser
20 from ksscommand import KssCommand, KssCommandException, KssCommandOptException
26 from karesansui import __version__
27 from karesansui.lib.virt.virt import KaresansuiVirtConnection
28 from karesansui.lib.utils import load_locale
29 from karesansui.lib.const import DEFAULT_KEYMAP
32 print >>sys.stderr, "[Error] karesansui package was not found."
37 usage = '%prog [options]'
40 optp = OptionParser(usage=usage, version=__version__)
42 optp.add_option('-n', '--name', dest='name', help=_('Domain name'))
43 optp.add_option('-t', '--type', dest='type', help=_('VM Type'), default="xen")
44 optp.add_option('-m', '--mem-size', dest='mem_size', help=_('Memory size (MB)'), default=256)
45 optp.add_option('-d', '--disk', dest='disk', help=_('Disk image file'), default=None)
46 optp.add_option('-v', '--vnc-port', dest='vnc_port', help=_('VNC port number'), default=None)
47 optp.add_option('-u', '--uuid', dest='uuid', help=_('UUID'), default=None)
48 optp.add_option('-a', '--mac', dest='mac', help=_('MAC address'), default=None)
49 optp.add_option('-c', '--vcpus', dest='vcpus', help=_('Number of virtual CPUs to allocate'), default=1)
50 optp.add_option('-f', '--interface-format', dest='interface_format', help=_('Interface format'), default='b:xenbr0')
51 optp.add_option('-b', '--keymap', dest='keymap', help=_('VNC Keyboard Map'), default=DEFAULT_KEYMAP)
52 optp.add_option('-e', '--extra', dest='extra', help=_('Extra kernel options'), default=None)
54 optp.add_option('-P', '--storage-pool', dest='storage_pool', help=_('Storage pool name'), default=None)
55 optp.add_option('-V', '--storage-volume', dest='storage_volume', help=_('Storage volume name'), default=None)
57 optp.add_option('-D', '--disk-format', dest='disk_format', help=_('Disk format'), default=None)
58 optp.add_option('-s', '--disk-size', dest='disk_size', help=_('Disk size (MB)'), default=1024*8)
59 # IDE or SCSI or Virtio
60 optp.add_option('-B', '--bus', dest='bus', help=_('Device bus type'), default=None)
62 optp.add_option('-o', '--iso', dest='iso', help=_('ISO image'), default=None)
63 # Individual designation
64 optp.add_option('-k', '--kernel', dest='kernel', help=_('Kernel image'), default=None)
65 optp.add_option('-i', '--initrd', dest='initrd', help=_('initrd image'), default=None)
67 return optp.parse_args()
71 raise KssCommandOptException('ERROR: -n or --name option is required.')
73 if opts.disk and os.path.isfile(opts.disk) is False:
74 raise KssCommandOptException('ERROR: %s not found.' % opts.disk)
77 if opts.iso is not None:
78 if opts.kernel is not None or opts.initrd is not None:
79 raise KssCommandOptException('ERROR: %s option cannot be specified with %s options.' % ('--iso', '--kernel and --initrd',))
81 if os.path.isfile(opts.iso) is False:
82 raise KssCommandOptException('ERROR: The specified ISO image path does not exist. - %s' % opts.iso)
84 from karesansui.lib.utils import is_iso9660_filesystem_format
85 if is_iso9660_filesystem_format(opts.iso) is False:
86 raise KssCommandOptException('ERROR: The specified ISO image is not valid ISO 9660 CD-ROM filesystem data. - %s' % opts.iso)
89 _r_get_net = re.compile("^(ftp|http)://")
91 if _r_get_net.match(opts.kernel) is None and os.path.isfile(opts.kernel) is False:
92 raise KssCommandOptException('ERROR: The specified kernel image path does not exist. - %s' % opts.kernel)
94 if _r_get_net.match(opts.initrd) is None and os.path.isfile(opts.initrd) is False:
95 raise KssCommandOptException('ERROR: The specified initrd image path does not exist. - %s' % opts.initrd)
101 メモ storage-pool and storage-volume is, virt.py#create_guest() is checked
104 class CreateGuest(KssCommand):
107 (opts, args) = getopts()
111 conn = KaresansuiVirtConnection(readonly=False)
113 conn.set_interface_format(opts.interface_format)
115 active_guests = conn.list_active_guest()
116 inactive_guests = conn.list_inactive_guest()
120 if opts.name in active_guests or opts.name in inactive_guests:
121 raise KssCommandException('guest already exists. - dom=%s' % (opts.name))
125 conn.create_guest(opts.name,
147 self.logger.error('Failed to create guest. - dom=%s - detail %s' % (opts.name, str(e.args)))
148 print >>sys.stderr, _('Failed to create guest. - dom=%s - detail %s') % (opts.name, str(e.args))
149 #raise KssCommandException(
150 # 'Failed to create guest. - dom=%s' % (opts.name))
154 active_guests = conn.list_active_guest()
155 inactive_guests = conn.list_inactive_guest()
156 if not (opts.name in active_guests or opts.name in inactive_guests):
157 raise KssCommandException('Guest OS is not recognized. - dom=%s' % (opts.name))
159 self.logger.info('Created guest. - dom=%s' % (opts.name))
160 print >>sys.stdout, 'Created guest. - dom=%s' % opts.name
166 if __name__ == "__main__":
167 target = CreateGuest()
168 sys.exit(target.run())