OSDN Git Service

Add validate
[karesansui/karesansui.git] / bin / add_disk.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # This file is part of Karesansui.
5 #
6 # Copyright (C) 2009-2010 HDE, Inc.
7 #
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.
12 #
13
14 import os
15 import sys
16 import logging
17 from optparse import OptionParser
18
19 from ksscommand import KssCommand, KssCommandOptException
20
21 import __cmd__
22
23 try:
24     import karesansui
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
29
30 except ImportError:
31     print >>sys.stderr, "[Error] karesansui package was not found."
32     sys.exit(1)
33
34 _ = load_locale()
35
36 usage = '%prog [options]'
37
38 def getopts():
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()
54
55 def chkopts(opts):
56     if not opts.name:
57         raise KssCommandOptException('ERROR: %s option is required.' % '-n or --name')
58     if not opts.pool:
59         raise KssCommandOptException('ERROR: %s option is required.' % '-p or --pool')
60     if not opts.volume:
61         raise KssCommandOptException('ERROR: %s option is required.' % '-v or --volume')
62     #if not opts.disk:
63     #    raise KssCommandOptException('ERROR: %s option is required.' % '-d or --disk')
64
65     #if os.path.exists(opts.disk):
66     #    raise KssCommandOptException('ERROR: disk image is found.')
67     """TODO : valid
68     オプションチェックを行う
69     """
70
71 class AddDisk(KssCommand):
72
73     def process(self):
74         (opts, args) = getopts()
75         chkopts(opts)
76         self.up_progress(10)
77
78         conn = KaresansuiVirtConnection(readonly=False)
79         try:
80             conn.set_domain_name(opts.name)
81             self.up_progress(10)
82             if opts.type == 'iscsi':
83                 real_volume_path = conn.get_storage_volume_iscsi_rpath_bystorage(opts.pool, opts.volume)
84                 format = None
85                 disk_type = 'block'
86
87             elif opts.type == 'file':
88                 real_volume_path = "%s/%s/%s/%s.img" % \
89                                    (conn.get_storage_pool_targetpath(opts.pool),
90                                     opts.name,
91                                     DISK_USES["DISK"],
92                                     opts.volume)
93                 format = opts.format
94                 disk_type = 'file'
95
96             if opts.target:
97                 target = opts.target
98             else:
99                 target = conn.guest.next_disk_target(opts.bus)
100
101             # 同じディスクパスのイメージが存在している場合はエラー
102             if opts.format is None:
103                 format = get_disk_img_info(real_volume_path)['file_format']
104
105             conn.guest.append_disk(real_volume_path,
106                                    target,
107                                    bus=opts.bus,
108                                    disk_type=disk_type,
109                                    driver_name=None,
110                                    driver_type=format,
111                                    )
112
113             self.up_progress(40)
114         finally:
115             conn.close()
116
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)
121
122         return True
123
124 if __name__ == "__main__":
125     target = AddDisk()
126     sys.exit(target.run())