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.
16 すべてのディスクがfile形式のみ実行可能です。
19 The only executable file format all disks.
28 from optparse import OptionParser
30 from ksscommand import KssCommand, KssCommandException, KssCommandOptException
36 from karesansui import __version__
37 from karesansui.lib.virt.virt import KaresansuiVirtConnection
38 from karesansui.lib.utils import load_locale
40 print >>sys.stderr, "[Error] karesansui package was not found."
45 usage = '%prog [options]'
48 optp = OptionParser(usage=usage, version=__version__)
49 optp.add_option('-s', '--src-name', dest='src_name', help=_('Source domain name'))
50 optp.add_option('-d', '--dest-name', dest='name', help=_('Destination domain name'))
51 optp.add_option('-v', '--vnc-port', dest='vnc_port', help=_('VNC port number'), default=None)
52 optp.add_option('-u', '--uuid', dest='uuid', help=_('UUID'), default=None)
53 optp.add_option('-a', '--mac', dest='mac', help=_('MAC address'), default=None)
54 optp.add_option('-p', '--pool', dest='pool', help=_('Destination storage pool'))
55 return optp.parse_args()
59 raise KssCommandOptException('ERROR: -d or --dest-name option is required.')
61 raise KssCommandOptException('ERROR: -s or --src-name option is required.')
63 raise KssCommandOptException('ERROR: -p or --pool option is required.')
65 class ReplicateGuest(KssCommand):
68 (opts, args) = getopts()
72 conn = KaresansuiVirtConnection(readonly=False)
75 dest_target_path = conn.get_storage_pool_targetpath(opts.pool)
76 if not dest_target_path:
77 raise KssCommandException(
78 "Could not get the target path of the storage pool. - path=%s" % dest_target_path)
80 self.dest_disk = "%s/%s/images/%s.img" \
81 % (dest_target_path, opts.name,opts.name,)
84 src_pool = conn.get_storage_pool_name_bydomain(opts.src_name, "os")
86 raise KssCommandException("Source storage pool is not found.")
87 src_pool_type = conn.get_storage_pool_type(src_pool)
88 if src_pool_type == 'dir':
89 raise KssCommandException(
90 "Storage pool type 'dir' is not. - type=%s" % src_pool_type)
92 src_target_path = conn.get_storage_pool_targetpath(src_pool[0])
93 self.src_disk = "%s/%s/images/%s.img" \
94 % (src_target_path, opts.src_name,opts.src_name,)
96 if os.path.isfile(self.src_disk) is False:
97 raise KssCommandException(
98 'source disk image is not found. - src=%s' % (self.src_disk))
100 if os.path.isfile(self.dest_disk) is True:
101 raise KssCommandException(
102 'destination disk image already exists. - dest=%s' % (self.dest_disk))
106 active_storage_pools = conn.list_active_storage_pool()
108 if not (opts.pool in active_storage_pools):
109 raise KssCommandException('Storage pool does not exist. - pool=%s'
113 active_guests = conn.list_active_guest()
114 inactive_guests = conn.list_inactive_guest()
116 if not (opts.src_name in active_guests or opts.src_name in inactive_guests):
117 raise KssCommandException(
118 "Unable to get the source guest OS. - src_name=%s" % opts.src_name)
120 if (opts.name in active_guests or opts.name in inactive_guests):
121 raise KssCommandException(
122 "Destination Guest OS is already there. - dest_name=%s" % opts.name)
126 conn.replicate_guest(opts.name,
134 self.logger.error('Failed to replicate guest. - src=%s dom=%s' % (opts.src_name,opts.name))
139 conn1 = KaresansuiVirtConnection(readonly=False)
142 active_guests = conn1.list_active_guest()
143 inactive_guests = conn1.list_inactive_guest()
144 if opts.name in active_guests or opts.name in inactive_guests:
145 self.logger.info('Replicated guest. - src=%s dom=%s' % (opts.src_name,opts.name))
146 print >>sys.stderr, _('Replicated guest. - src=%s dom=%s') % (opts.src_name,opts.name)
149 raise KssCommandException(
150 'Replicate guest not found. - src=%s dom=%s' % (opts.src_name,opts.name))
157 def sigusr1_handler(self, signum, frame):
158 if os.path.exists(self.src_disk) and os.path.exists(self.dest_disk):
159 s_size = os.path.getsize(self.src_disk)
160 d_size = os.path.getsize(self.dest_disk)
161 print int(d_size*100/s_size)
164 def sigint_handler(self, signum, frame):
165 if os.path.exists(self.dest_disk):
166 os.unlink(self.dest_disk)
167 self.logger.error('Aborted by user request.')
168 print >> sys.stderr, _("Aborted by user request.")
172 if __name__ == "__main__":
173 target = ReplicateGuest()
174 #signal.signal(signal.SIGUSR1, target.sigusr1_handler)
175 #signal.signal(signal.SIGINT, target.sigint_handler)
176 sys.exit(target.run())