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, KssCommandException, KssCommandOptException
25 from karesansui import __version__
26 from karesansui.lib.virt.virt import KaresansuiVirtConnection
27 from karesansui.lib.utils import load_locale
29 print >>sys.stderr, "[Error] karesansui package was not found."
34 usage = '%prog [options]'
37 optp = OptionParser(usage=usage, version=__version__)
38 optp.add_option('-n', '--name', dest='name', help=_('Domain name'))
39 optp.add_option('-s', '--memory', dest='memory', help=_('Memory size (MB)'), default=None)
40 optp.add_option('-m', '--maxmem', dest='maxmem', help=_('Max memory size (MB)'), default=None)
41 return optp.parse_args()
45 KssCommandOptException('ERROR: %s option is required.' % '-n or --name')
47 class SetMemory(KssCommand):
50 (opts, args) = getopts()
55 opts.maxmem = str(opts.maxmem) + 'm'
57 opts.memory = str(opts.memory) + 'm'
60 conn = KaresansuiVirtConnection(readonly=False)
62 conn.set_domain_name(opts.name)
64 active_guests = conn.list_active_guest()
65 inactive_guests = conn.list_inactive_guest()
66 if opts.name in active_guests or opts.name in inactive_guests:
69 conn.guest.set_memory(opts.maxmem,opts.memory)
71 info = conn.guest.get_info()
74 self.logger.info('Set memory size. - dom=%s max=%d mem=%d' \
75 % (opts.name, info['maxMem'], info['memory']))
76 print >>sys.stdout, _('Set memory size. - dom=%s max=%d mem=%d') \
77 % (opts.name, info['maxMem'], info['memory'])
80 self.logger.error('Failed to set memory size. - dom=%s' % (opts.name))
81 print >>sys.stderr, _('Failed to set memory size. - dom=%s') % (opts.name)
85 raise KssCommandException('guest not found. - dom=%s' % (opts.name))
92 if __name__ == "__main__":
94 sys.exit(target.run())