summaryrefslogtreecommitdiffstats
path: root/nova/virt/hyperv/vmops.py
blob: fea1034f4c16227b645897bde50b7f064dfa2ec2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2012 Cloudbase Solutions Srl
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""
Management class for basic VM operations.
"""
import os
import uuid

from nova.api.metadata import base as instance_metadata
from nova import exception
from nova.openstack.common import cfg
from nova.openstack.common import lockutils
from nova.openstack.common import log as logging
from nova import utils
from nova.virt import configdrive
from nova.virt.hyperv import baseops
from nova.virt.hyperv import constants
from nova.virt.hyperv import vmutils

LOG = logging.getLogger(__name__)

hyperv_opts = [
    cfg.StrOpt('vswitch_name',
                default=None,
                help='Default vSwitch Name, '
                    'if none provided first external is used'),
    cfg.BoolOpt('limit_cpu_features',
               default=False,
               help='Required for live migration among '
                    'hosts with different CPU features'),
    cfg.BoolOpt('config_drive_inject_password',
               default=False,
               help='Sets the admin password in the config drive image'),
    cfg.StrOpt('qemu_img_cmd',
               default="qemu-img.exe",
               help='qemu-img is used to convert between '
                    'different image types'),
    cfg.BoolOpt('config_drive_cdrom',
               default=False,
               help='Attaches the Config Drive image as a cdrom drive '
                    'instead of a disk drive')
    ]

CONF = cfg.CONF
CONF.register_opts(hyperv_opts)
CONF.import_opt('use_cow_images', 'nova.config')


class VMOps(baseops.BaseOps):
    def __init__(self, volumeops):
        super(VMOps, self).__init__()

        self._vmutils = vmutils.VMUtils()
        self._volumeops = volumeops

    def list_instances(self):
        """ Return the names of all the instances known to Hyper-V. """
        vms = [v.ElementName
                for v in self._conn.Msvm_ComputerSystem(['ElementName'],
                    Caption="Virtual Machine")]
        return vms

    def get_info(self, instance):
        """Get information about the VM"""
        LOG.debug(_("get_info called for instance"), instance=instance)
        instance_name = instance["name"]
        return self._get_info(instance_name)

    def _get_info(self, instance_name):
        vm = self._vmutils.lookup(self._conn, instance_name)
        if vm is None:
            raise exception.InstanceNotFound(instance=instance_name)
        vm = self._conn.Msvm_ComputerSystem(
            ElementName=instance_name)[0]
        vs_man_svc = self._conn.Msvm_VirtualSystemManagementService()[0]
        vmsettings = vm.associators(
                       wmi_association_class='Msvm_SettingsDefineState',
                       wmi_result_class='Msvm_VirtualSystemSettingData')
        settings_paths = [v.path_() for v in vmsettings]
        #See http://msdn.microsoft.com/en-us/library/cc160706%28VS.85%29.aspx
        summary_info = vs_man_svc.GetSummaryInformation(
                                       [constants.VM_SUMMARY_NUM_PROCS,
                                        constants.VM_SUMMARY_ENABLED_STATE,
                                        constants.VM_SUMMARY_MEMORY_USAGE,
                                        constants.VM_SUMMARY_UPTIME],
                                            settings_paths)[1]
        info = summary_info[0]

        LOG.debug(_("hyperv vm state: %s"), info.EnabledState)
        state = constants.HYPERV_POWER_STATE[info.EnabledState]
        memusage = str(info.MemoryUsage)
        numprocs = str(info.NumberOfProcessors)
        uptime = str(info.UpTime)

        LOG.debug(_("Got Info for vm %(instance_name)s: state=%(state)d,"
                " mem=%(memusage)s, num_cpu=%(numprocs)s,"
                " uptime=%(uptime)s"), locals())

        return {'state': state,
                'max_mem': info.MemoryUsage,
                'mem': info.MemoryUsage,
                'num_cpu': info.NumberOfProcessors,
                'cpu_time': info.UpTime}

    def spawn(self, context, instance, image_meta, injected_files,
        admin_password, network_info, block_device_info=None):
        """ Create a new VM and start it."""
        instance_name = instance["name"]
        vm = self._vmutils.lookup(self._conn, instance_name)
        if vm is not None:
            raise exception.InstanceExists(name=instance_name)

        ebs_root = self._volumeops.volume_in_mapping(
            self._volumeops.get_default_root_device(),
           block_device_info)

        #If is not a boot from volume spawn
        if not (ebs_root):
            #Fetch the file, assume it is a VHD file.
            vhdfile = self._vmutils.get_vhd_path(instance_name)
            try:
                self._cache_image(fn=self._vmutils.fetch_image,
                  context=context,
                  target=vhdfile,
                  fname=instance['image_ref'],
                  image_id=instance['image_ref'],
                  user=instance['user_id'],
                  project=instance['project_id'],
                  cow=CONF.use_cow_images)
            except Exception as exn:
                LOG.exception(_('cache image failed: %s'), exn)
                self.destroy(instance)

        try:
            self._create_vm(instance)

            if not ebs_root:
                self._attach_ide_drive(instance['name'], vhdfile, 0, 0,
                    constants.IDE_DISK)
            else:
                self._volumeops.attach_boot_volume(block_device_info,
                                             instance_name)

            #A SCSI controller for volumes connection is created
            self._create_scsi_controller(instance['name'])

            for vif in network_info:
                mac_address = vif['address'].replace(':', '')
                self._create_nic(instance['name'], mac_address)

            if configdrive.required_by(instance):
                self._create_config_drive(instance, injected_files,
                    admin_password)

            LOG.debug(_('Starting VM %s '), instance_name)
            self._set_vm_state(instance['name'], 'Enabled')
            LOG.info(_('Started VM %s '), instance_name)
        except Exception as exn:
            LOG.exception(_('spawn vm failed: %s'), exn)
            self.destroy(instance)
            raise exn

    def _create_config_drive(self, instance, injected_files, admin_password):
        if CONF.config_drive_format != 'iso9660':
            vmutils.HyperVException(_('Invalid config_drive_format "%s"') %
                CONF.config_drive_format)

        LOG.info(_('Using config drive'), instance=instance)
        extra_md = {}
        if admin_password and CONF.config_drive_inject_password:
            extra_md['admin_pass'] = admin_password

        inst_md = instance_metadata.InstanceMetadata(instance,
            content=injected_files, extra_md=extra_md)

        instance_path = self._vmutils.get_instance_path(
            instance['name'])
        configdrive_path_iso = os.path.join(instance_path, 'configdrive.iso')
        LOG.info(_('Creating config drive at %(path)s'),
                 {'path': configdrive_path_iso}, instance=instance)

        cdb = configdrive.ConfigDriveBuilder(instance_md=inst_md)
        try:
            cdb.make_drive(configdrive_path_iso)
        except exception.ProcessExecutionError, e:
            LOG.error(_('Creating config drive failed with error: %s'),
                      e, instance=instance)
            raise
        finally:
            cdb.cleanup()

        if not CONF.config_drive_cdrom:
            drive_type = constants.IDE_DISK
            configdrive_path = os.path.join(instance_path,
                'configdrive.vhd')
            utils.execute(CONF.qemu_img_cmd,
                          'convert',
                          '-f',
                          'raw',
                          '-O',
                          'vpc',
                          configdrive_path_iso,
                          configdrive_path,
                          attempts=1)
            os.remove(configdrive_path_iso)
        else:
            drive_type = constants.IDE_DVD
            configdrive_path = configdrive_path_iso

        self._attach_ide_drive(instance['name'], configdrive_path, 1, 0,
            drive_type)

    def _create_vm(self, instance):
        """Create a VM but don't start it.  """
        instance_name = instance["name"]
        vs_man_svc = self._conn.Msvm_VirtualSystemManagementService()[0]

        vs_gs_data = self._conn.Msvm_VirtualSystemGlobalSettingData.new()
        vs_gs_data.ElementName = instance_name
        (job, ret_val) = vs_man_svc.DefineVirtualSystem(
                [], None, vs_gs_data.GetText_(1))[1:]
        if ret_val == constants.WMI_JOB_STATUS_STARTED:
            success = self._vmutils.check_job_status(job)
        else:
            success = (ret_val == 0)

        if not success:
            raise vmutils.HyperVException(_('Failed to create VM %s') %
                instance_name)

        LOG.debug(_('Created VM %s...'), instance_name)
        vm = self._conn.Msvm_ComputerSystem(ElementName=instance_name)[0]

        vmsettings = vm.associators(
                          wmi_result_class='Msvm_VirtualSystemSettingData')
        vmsetting = [s for s in vmsettings
                        if s.SettingType == 3][0]  # avoid snapshots
        memsetting = vmsetting.associators(
                           wmi_result_class='Msvm_MemorySettingData')[0]
        #No Dynamic Memory, so reservation, limit and quantity are identical.
        mem = long(str(instance['memory_mb']))
        memsetting.VirtualQuantity = mem
        memsetting.Reservation = mem
        memsetting.Limit = mem

        (job, ret_val) = vs_man_svc.ModifyVirtualSystemResources(
                vm.path_(), [memsetting.GetText_(1)])
        LOG.debug(_('Set memory for vm %s...'), instance_name)
        procsetting = vmsetting.associators(
                wmi_result_class='Msvm_ProcessorSettingData')[0]
        vcpus = long(instance['vcpus'])
        procsetting.VirtualQuantity = vcpus
        procsetting.Reservation = vcpus
        procsetting.Limit = 100000  # static assignment to 100%

        if CONF.limit_cpu_features:
            procsetting.LimitProcessorFeatures = True

        (job, ret_val) = vs_man_svc.ModifyVirtualSystemResources(
                vm.path_(), [procsetting.GetText_(1)])
        LOG.debug(_('Set vcpus for vm %s...'), instance_name)

    def _create_scsi_controller(self, vm_name):
        """ Create an iscsi controller ready to mount volumes """
        LOG.debug(_('Creating a scsi controller for %(vm_name)s for volume '
                'attaching') % locals())
        vms = self._conn.MSVM_ComputerSystem(ElementName=vm_name)
        vm = vms[0]
        scsicontrldefault = self._conn.query(
                "SELECT * FROM Msvm_ResourceAllocationSettingData \
                WHERE ResourceSubType = 'Microsoft Synthetic SCSI Controller'\
                AND InstanceID LIKE '%Default%'")[0]
        if scsicontrldefault is None:
            raise vmutils.HyperVException(_('Controller not found'))
        scsicontrl = self._vmutils.clone_wmi_obj(self._conn,
                'Msvm_ResourceAllocationSettingData', scsicontrldefault)
        scsicontrl.VirtualSystemIdentifiers = ['{' + str(uuid.uuid4()) + '}']
        scsiresource = self._vmutils.add_virt_resource(self._conn,
            scsicontrl, vm)
        if scsiresource is None:
            raise vmutils.HyperVException(
                _('Failed to add scsi controller to VM %s') %
                vm_name)

    def _get_ide_controller(self, vm, ctrller_addr):
        #Find the IDE controller for the vm.
        vmsettings = vm.associators(
            wmi_result_class='Msvm_VirtualSystemSettingData')
        rasds = vmsettings[0].associators(
            wmi_result_class='MSVM_ResourceAllocationSettingData')
        ctrller = [r for r in rasds
            if r.ResourceSubType == 'Microsoft Emulated IDE Controller'
            and r.Address == str(ctrller_addr)]
        return ctrller

    def _attach_ide_drive(self, vm_name, path, ctrller_addr, drive_addr,
        drive_type=constants.IDE_DISK):
        """Create an IDE drive and attach it to the vm"""
        LOG.debug(_('Creating disk for %(vm_name)s by attaching'
                ' disk file %(path)s') % locals())

        vms = self._conn.MSVM_ComputerSystem(ElementName=vm_name)
        vm = vms[0]

        ctrller = self._get_ide_controller(vm, ctrller_addr)

        if drive_type == constants.IDE_DISK:
            resSubType = 'Microsoft Synthetic Disk Drive'
        elif drive_type == constants.IDE_DVD:
            resSubType = 'Microsoft Synthetic DVD Drive'

        #Find the default disk drive object for the vm and clone it.
        drivedflt = self._conn.query(
            "SELECT * FROM Msvm_ResourceAllocationSettingData \
            WHERE ResourceSubType LIKE '%(resSubType)s'\
            AND InstanceID LIKE '%%Default%%'" % locals())[0]
        drive = self._vmutils.clone_wmi_obj(self._conn,
                'Msvm_ResourceAllocationSettingData', drivedflt)
        #Set the IDE ctrller as parent.
        drive.Parent = ctrller[0].path_()
        drive.Address = drive_addr
        #Add the cloned disk drive object to the vm.
        new_resources = self._vmutils.add_virt_resource(self._conn,
            drive, vm)
        if new_resources is None:
            raise vmutils.HyperVException(
                _('Failed to add drive to VM %s') %
                    vm_name)
        drive_path = new_resources[0]
        LOG.debug(_('New %(drive_type)s drive path is %(drive_path)s') %
            locals())

        if drive_type == constants.IDE_DISK:
            resSubType = 'Microsoft Virtual Hard Disk'
        elif drive_type == constants.IDE_DVD:
            resSubType = 'Microsoft Virtual CD/DVD Disk'

        #Find the default VHD disk object.
        drivedefault = self._conn.query(
                "SELECT * FROM Msvm_ResourceAllocationSettingData \
                 WHERE ResourceSubType LIKE '%(resSubType)s' AND \
                 InstanceID LIKE '%%Default%%' " % locals())[0]

        #Clone the default and point it to the image file.
        res = self._vmutils.clone_wmi_obj(self._conn,
                'Msvm_ResourceAllocationSettingData', drivedefault)
        #Set the new drive as the parent.
        res.Parent = drive_path
        res.Connection = [path]

        #Add the new vhd object as a virtual hard disk to the vm.
        new_resources = self._vmutils.add_virt_resource(self._conn, res, vm)
        if new_resources is None:
            raise vmutils.HyperVException(
                _('Failed to add %(drive_type)s image to VM %(vm_name)s') %
                    locals())
        LOG.info(_('Created drive type %(drive_type)s for %(vm_name)s') %
            locals())

    def _create_nic(self, vm_name, mac):
        """Create a (synthetic) nic and attach it to the vm"""
        LOG.debug(_('Creating nic for %s '), vm_name)
        #Find the vswitch that is connected to the physical nic.
        vms = self._conn.Msvm_ComputerSystem(ElementName=vm_name)
        extswitch = self._find_external_network()
        if extswitch is None:
            raise vmutils.HyperVException(_('Cannot find vSwitch'))

        vm = vms[0]
        switch_svc = self._conn.Msvm_VirtualSwitchManagementService()[0]
        #Find the default nic and clone it to create a new nic for the vm.
        #Use Msvm_SyntheticEthernetPortSettingData for Windows or Linux with
        #Linux Integration Components installed.
        syntheticnics_data = self._conn.Msvm_SyntheticEthernetPortSettingData()
        default_nic_data = [n for n in syntheticnics_data
                            if n.InstanceID.rfind('Default') > 0]
        new_nic_data = self._vmutils.clone_wmi_obj(self._conn,
                'Msvm_SyntheticEthernetPortSettingData',
                default_nic_data[0])
        #Create a port on the vswitch.
        (new_port, ret_val) = switch_svc.CreateSwitchPort(
            Name=str(uuid.uuid4()),
            FriendlyName=vm_name,
            ScopeOfResidence="",
            VirtualSwitch=extswitch.path_())
        if ret_val != 0:
            LOG.error(_('Failed creating a port on the external vswitch'))
            raise vmutils.HyperVException(_('Failed creating port for %s') %
                    vm_name)
        ext_path = extswitch.path_()
        LOG.debug(_("Created switch port %(vm_name)s on switch %(ext_path)s")
                % locals())
        #Connect the new nic to the new port.
        new_nic_data.Connection = [new_port]
        new_nic_data.ElementName = vm_name + ' nic'
        new_nic_data.Address = mac
        new_nic_data.StaticMacAddress = 'True'
        new_nic_data.VirtualSystemIdentifiers = ['{' + str(uuid.uuid4()) + '}']
        #Add the new nic to the vm.
        new_resources = self._vmutils.add_virt_resource(self._conn,
            new_nic_data, vm)
        if new_resources is None:
            raise vmutils.HyperVException(_('Failed to add nic to VM %s') %
                    vm_name)
        LOG.info(_("Created nic for %s "), vm_name)

    def _find_external_network(self):
        """Find the vswitch that is connected to the physical nic.
           Assumes only one physical nic on the host
        """
        #If there are no physical nics connected to networks, return.
        LOG.debug(_("Attempting to bind NIC to %s ")
                % CONF.vswitch_name)
        if CONF.vswitch_name:
            LOG.debug(_("Attempting to bind NIC to %s ")
                % CONF.vswitch_name)
            bound = self._conn.Msvm_VirtualSwitch(
                ElementName=CONF.vswitch_name)
        else:
            LOG.debug(_("No vSwitch specified, attaching to default"))
            self._conn.Msvm_ExternalEthernetPort(IsBound='TRUE')
        if len(bound) == 0:
            return None
        if CONF.vswitch_name:
            return self._conn.Msvm_VirtualSwitch(
                ElementName=CONF.vswitch_name)[0]\
                .associators(wmi_result_class='Msvm_SwitchPort')[0]\
                .associators(wmi_result_class='Msvm_VirtualSwitch')[0]
        else:
            return self._conn.Msvm_ExternalEthernetPort(IsBound='TRUE')\
                .associators(wmi_result_class='Msvm_SwitchPort')[0]\
                .associators(wmi_result_class='Msvm_VirtualSwitch')[0]

    def reboot(self, instance, network_info, reboot_type):
        instance_name = instance["name"]
        """Reboot the specified instance."""
        vm = self._vmutils.lookup(self._conn, instance_name)
        if vm is None:
            raise exception.InstanceNotFound(instance_id=instance["id"])
        self._set_vm_state(instance_name, 'Reboot')

    def destroy(self, instance, network_info=None, cleanup=True):
        """Destroy the VM. Also destroy the associated VHD disk files"""
        instance_name = instance["name"]
        LOG.debug(_("Got request to destroy vm %s"), instance_name)
        vm = self._vmutils.lookup(self._conn, instance_name)
        if vm is None:
            return
        vm = self._conn.Msvm_ComputerSystem(ElementName=instance_name)[0]
        vs_man_svc = self._conn.Msvm_VirtualSystemManagementService()[0]
        #Stop the VM first.
        self._set_vm_state(instance_name, 'Disabled')
        vmsettings = vm.associators(
                         wmi_result_class='Msvm_VirtualSystemSettingData')
        rasds = vmsettings[0].associators(
                         wmi_result_class='MSVM_ResourceAllocationSettingData')
        disks = [r for r in rasds
                    if r.ResourceSubType == 'Microsoft Virtual Hard Disk']
        disk_files = []
        volumes = [r for r in rasds
                    if r.ResourceSubType == 'Microsoft Physical Disk Drive']
        volumes_drives_list = []
        #collect the volumes information before destroying the VM.
        for volume in volumes:
            hostResources = volume.HostResource
            drive_path = hostResources[0]
            #Appending the Msvm_Disk path
            volumes_drives_list.append(drive_path)
        #Collect disk file information before destroying the VM.
        for disk in disks:
            disk_files.extend([c for c in disk.Connection])
        #Nuke the VM. Does not destroy disks.
        (job, ret_val) = vs_man_svc.DestroyVirtualSystem(vm.path_())
        if ret_val == constants.WMI_JOB_STATUS_STARTED:
            success = self._vmutils.check_job_status(job)
        elif ret_val == 0:
            success = True
        if not success:
            raise vmutils.HyperVException(_('Failed to destroy vm %s') %
                instance_name)
        #Disconnect volumes
        for volume_drive in volumes_drives_list:
            self._volumeops.disconnect_volume(volume_drive)
        #Delete associated vhd disk files.
        for disk in disk_files:
            vhdfile = self._conn_cimv2.query(
            "Select * from CIM_DataFile where Name = '" +
                disk.replace("'", "''") + "'")[0]
            LOG.debug(_("Del: disk %(vhdfile)s vm %(instance_name)s")
                % locals())
            vhdfile.Delete()

    def pause(self, instance):
        """Pause VM instance."""
        LOG.debug(_("Pause instance"), instance=instance)
        self._set_vm_state(instance["name"], 'Paused')

    def unpause(self, instance):
        """Unpause paused VM instance."""
        LOG.debug(_("Unpause instance"), instance=instance)
        self._set_vm_state(instance["name"], 'Enabled')

    def suspend(self, instance):
        """Suspend the specified instance."""
        print instance
        LOG.debug(_("Suspend instance"), instance=instance)
        self._set_vm_state(instance["name"], 'Suspended')

    def resume(self, instance):
        """Resume the suspended VM instance."""
        LOG.debug(_("Resume instance"), instance=instance)
        self._set_vm_state(instance["name"], 'Enabled')

    def power_off(self, instance):
        """Power off the specified instance."""
        LOG.debug(_("Power off instance"), instance=instance)
        self._set_vm_state(instance["name"], 'Disabled')

    def power_on(self, instance):
        """Power on the specified instance"""
        LOG.debug(_("Power on instance"), instance=instance)
        self._set_vm_state(instance["name"], 'Enabled')

    def _set_vm_state(self, vm_name, req_state):
        """Set the desired state of the VM"""
        vms = self._conn.Msvm_ComputerSystem(ElementName=vm_name)
        if len(vms) == 0:
            return False
        (job, ret_val) = vms[0].RequestStateChange(
            constants.REQ_POWER_STATE[req_state])
        success = False
        if ret_val == constants.WMI_JOB_STATUS_STARTED:
            success = self._vmutils.check_job_status(job)
        elif ret_val == 0:
            success = True
        elif ret_val == 32775:
            #Invalid state for current operation. Typically means it is
            #already in the state requested
            success = True
        if success:
            LOG.info(_("Successfully changed vm state of %(vm_name)s"
                    " to %(req_state)s") % locals())
        else:
            msg = _("Failed to change vm state of %(vm_name)s"
                    " to %(req_state)s") % locals()
            LOG.error(msg)
            raise vmutils.HyperVException(msg)

    def _cache_image(self, fn, target, fname, cow=False, Size=None,
        *args, **kwargs):
        """Wrapper for a method that creates an image that caches the image.

        This wrapper will save the image into a common store and create a
        copy for use by the hypervisor.

        The underlying method should specify a kwarg of target representing
        where the image will be saved.

        fname is used as the filename of the base image.  The filename needs
        to be unique to a given image.

        If cow is True, it will make a CoW image instead of a copy.
        """
        @lockutils.synchronized(fname, 'nova-')
        def call_if_not_exists(path, fn, *args, **kwargs):
                if not os.path.exists(path):
                    fn(target=path, *args, **kwargs)

        if not os.path.exists(target):
            LOG.debug(_("use_cow_image:%s"), cow)
            if cow:
                base = self._vmutils.get_base_vhd_path(fname)
                call_if_not_exists(base, fn, *args, **kwargs)

                image_service = self._conn.query(
                    "Select * from Msvm_ImageManagementService")[0]
                (job, ret_val) = \
                    image_service.CreateDifferencingVirtualHardDisk(
                        Path=target, ParentPath=base)
                LOG.debug(
                    "Creating difference disk: JobID=%s, Source=%s, Target=%s",
                    job, base, target)
                if ret_val == constants.WMI_JOB_STATUS_STARTED:
                    success = self._vmutils.check_job_status(job)
                else:
                    success = (ret_val == 0)

                if not success:
                    raise vmutils.HyperVException(
                        _('Failed to create Difference Disk from '
                            '%(base)s to %(target)s') % locals())

            else:
                call_if_not_exists(target, fn, *args, **kwargs)