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.
17 from optparse import OptionParser
19 from ksscommand import KssCommand, KssCommandOptException
25 from karesansui import __version__
26 from karesansui.lib.virt.virt import KaresansuiVirtConnection
27 from karesansui.lib.utils import load_locale, get_disk_img_info
28 from karesansui.lib.const import ISCSI_DEVICE_DIR, DISK_USES
31 print >>sys.stderr, "[Error] karesansui package was not found."
36 usage = '%prog [options]'
39 optp = OptionParser(usage=usage, version=__version__)
40 optp.add_option('-n', '--name', dest='name', help=_('Domain name'))
41 #optp.add_option('-d', '--disk', dest='disk', help=_('Disk image file'))
42 #optp.add_option('-s', '--size', dest='size', help=_('Disk size (MB)'))
43 #optp.add_option('-p', '--sparse', dest='sparse', action="store_true", help=_('Sparse file'))
44 #optp.add_option('-t', '--target', dest='target', help=_('Device target'), default=None)
45 optp.add_option('-b', '--bus', dest='bus', help=_('Device type'), default=None)
46 #optp.add_option('-f', '--format', dest='format', help=_('Disk format'), default=None)
47 optp.add_option('-t', '--type', dest='type', help=_('Storage Type'))
48 optp.add_option('-p', '--pool', dest='pool', help=_('Storage Pool'))
49 optp.add_option('-v', '--volume', dest='volume', help=_('Storage Volume'))
50 optp.add_option('-f', '--format', dest='format', help=_('Disk Format'))
51 optp.add_option('-T', '--target', dest='target',
52 help=_('Device name of your drive. example=hda or sda or vda...'))
53 return optp.parse_args()
57 raise KssCommandOptException('ERROR: %s option is required.' % '-n or --name')
59 raise KssCommandOptException('ERROR: %s option is required.' % '-p or --pool')
61 raise KssCommandOptException('ERROR: %s option is required.' % '-v or --volume')
63 # raise KssCommandOptException('ERROR: %s option is required.' % '-d or --disk')
65 #if os.path.exists(opts.disk):
66 # raise KssCommandOptException('ERROR: disk image is found.')
71 class AddDisk(KssCommand):
74 (opts, args) = getopts()
78 conn = KaresansuiVirtConnection(readonly=False)
80 conn.set_domain_name(opts.name)
82 if opts.type == 'iscsi':
83 real_volume_path = conn.get_storage_volume_iscsi_rpath_bystorage(opts.pool, opts.volume)
87 elif opts.type == 'file':
88 real_volume_path = "%s/%s/%s/%s.img" % \
89 (conn.get_storage_pool_targetpath(opts.pool),
99 target = conn.guest.next_disk_target(opts.bus)
101 # 同じディスクパスのイメージが存在している場合はエラー
102 if opts.format is None:
103 format = get_disk_img_info(real_volume_path)['file_format']
105 conn.guest.append_disk(real_volume_path,
117 self.logger.info('Added disk device. - dom=%s target=%s path=%s' \
118 % (opts.name, target, real_volume_path))
119 print >>sys.stderr, 'Added disk device. - dom=%s target=%s path=%s' \
120 % (opts.name, target, real_volume_path)
124 if __name__ == "__main__":
126 sys.exit(target.run())