def test_delete_path_good(self): self.mox.StubOutWithMock(ovz_utils, 'execute') ovz_utils.execute( 'rmdir', CONF.ovz_ve_private_dir, run_as_root=True).AndReturn(('', '')) self.mox.ReplayAll() self.assertTrue(ovz_utils.delete_path(CONF.ovz_ve_private_dir))
def _partition_disk(self): """ Openvz requires that we pass in not only the root device but if the user is to access partitions on that disk we need also to pass in the major/minor numbers for each partition. Given that requirement we're making the assumption for now that volumes are going to be presented as one partition. This will partition the root device so we can later get the information on both the major and minor numbers. If the disk contains a valid partition table then we just return :return: """ # We need potentially need to give the device time to settle self._let_disk_settle() dev = self._find_device() part_table = self._list_partition_table() LOG.debug(_('Partition table for %(dev)s: %(part_table)s') % locals()) # If the volume is partitioned we're assuming this is a reconnect of # a volume and we won't partition anything. if not part_table: commands = ['o,w', 'n,p,1,,,t,1,83,w'] for command in commands: command = command.replace(',', '\n') ovz_utils.execute( 'fdisk', dev, process_input=command, run_as_root=True) time.sleep(3)
def test_get_cpuunits_capability(self): self.mox.StubOutWithMock(ovz_utils, 'execute') ovz_utils.execute( 'vzcpucheck', run_as_root=True).AndReturn('') self.mox.ReplayAll() self.assertRaises( exception.InvalidCPUInfo, ovz_utils.get_cpuunits_capability)
def quota_init(self): """ Initialize quotas for instance """ LOG.debug(_('Initializing quotas for %s') % self.instance['id']) ovz_utils.execute('vzctl', 'quotainit', self.instance['id']) LOG.debug(_('Initialized quotas for %s') % self.instance['id'])
def test_set_permissions(self): perms = 755 filename = '/tmp/testfile' self.mox.StubOutWithMock(ovz_utils, 'execute') ovz_utils.execute('chmod', perms, filename, run_as_root=True) self.mox.ReplayAll() ovz_utils.set_permissions(filename, perms)
def quota_on(self): """ Turn on quotas for instance """ LOG.debug(_('Turning on quotas for %s') % self.instance['id']) ovz_utils.execute('vzctl', 'quotaon', self.instance['id'], run_as_root=True) LOG.debug(_('Turned on quotas for %s') % self.instance['id'])
def test_mkfs_uuid(self): fs_uuid = uuid.uuid4() path = '/dev/sdgg' self.mox.StubOutWithMock(ovz_utils, 'execute') ovz_utils.execute( 'mkfs', '-F', '-t', 'ext3', '-U', fs_uuid, path, run_as_root=True) self.mox.ReplayAll() ovz_utils.mkfs(path, 'ext3', fs_uuid)
def quotadump(self): """ Dump the quotas for containers """ LOG.debug(_('Dumping quotas for %s') % self.instance['id']) ovz_utils.execute('vzdqdump', self.instance['id'], '-U', '-G', '-T', '>', self.qdumpfile, run_as_root=True) LOG.debug(_('Dumped quotas for %s') % self.instance['id'])
def test_execute_process_execution_error_no_raise_on_error(self): self.mox.StubOutWithMock(ovz_utils.utils, 'execute') ovz_utils.utils.execute( 'cat', '/proc/cpuinfo', run_as_root=False).AndRaise( processutils.ProcessExecutionError) self.mox.ReplayAll() ovz_utils.execute( 'cat', '/proc/cpuinfo', run_as_root=False, raise_on_error=False)
def quotaenable(self): """ enable quotas for a given container """ LOG.debug(_('Enabling quotas for %s') % self.instance['id']) ovz_utils.execute('vzquota', 'reload2', self.instance['id'], run_as_root=True) LOG.debug(_('Enabled quotas for %s') % self.instance['id'])
def test_delete_path_bad(self): self.mox.StubOutWithMock(ovz_utils, 'execute') ovz_utils.execute( 'rmdir', CONF.ovz_ve_private_dir, run_as_root=True).AndRaise(exception.InstanceUnacceptable( fakes.ERRORMSG)) self.mox.ReplayAll() self.assertFalse(ovz_utils.delete_path(CONF.ovz_ve_private_dir))
def quotaload(self): """ Load quotas from quota file """ LOG.debug(_('Loading quotas for %s') % self.instance['id']) ovz_utils.execute('vzdqload', self.instance['id'], '-U', '-G', '-T', '<', self.qdumpfile, run_as_root=True) LOG.debug(_('Loaded quotas for %s') % self.instance['id'])
def test_set_perms_success(self): self.mox.StubOutWithMock(ovz_utils, 'execute') ovz_utils.execute( 'chmod', 755, fakes.TEMPFILE, run_as_root=True).AndReturn( ('', fakes.ERRORMSG)) self.mox.ReplayAll() fh = ovzfile.OVZFile(fakes.TEMPFILE, 755) fh.set_permissions(755)
def resume(self): """ Resume a container from an undumped migration """ LOG.debug(_('Resuming instance %s') % self.instance['id']) ovz_utils.execute('vzctl', 'restore', self.instance['id'], '--resume', run_as_root=True) LOG.debug(_('Resumed instance %s') % self.instance['id'])
def kill(self): """ This is used to stop a container once it's suspended without having to resume it to properly destroy it """ LOG.debug(_('Killing instance %s') % self.instance['id']) ovz_utils.execute('vzctl', 'chkpnt', self.instance['id'], '--kill', run_as_root=True) LOG.debug(_('Killed instance %s') % self.instance['id'])
def test_get_fs_uuid_failure(self): dev = '/dev/sdgg' self.mox.StubOutWithMock(ovz_utils, 'execute') ovz_utils.execute( 'blkid', '-o', 'value', '-s', 'UUID', dev, raise_on_error=False, run_as_root=True).AndReturn('\n') self.mox.ReplayAll() fs_uuid = ovz_utils.get_fs_uuid(dev) self.assertFalse(fs_uuid)
def test_get_fs_uuid_success(self): dev = '/dev/sdgg' self.mox.StubOutWithMock(ovz_utils, 'execute') ovz_utils.execute( 'blkid', '-o', 'value', '-s', 'UUID', dev, raise_on_error=False, run_as_root=True).AndReturn(fakes.BLKID) self.mox.ReplayAll() fs_uuid = ovz_utils.get_fs_uuid(dev) self.assertEqual(fs_uuid, fakes.BLKID.strip())
def test_set_perms_failure(self): self.mox.StubOutWithMock(ovz_utils, 'execute') ovz_utils.execute( 'chmod', 755, fakes.TEMPFILE, run_as_root=True).AndRaise( exception.InstanceUnacceptable(fakes.ERRORMSG)) self.mox.ReplayAll() fh = ovzfile.OVZFile(fakes.TEMPFILE, 755) self.assertRaises( exception.InstanceUnacceptable, fh.set_permissions, 755)
def test_mkfs_label(self): path = '/dev/sdgg' fs_label = 'STORAGE' self.mox.StubOutWithMock(ovz_utils, 'execute') ovz_utils.execute( 'mkfs', '-F', '-t', 'ext3', '-U', mox.IgnoreArg(), '-L', fs_label, path, run_as_root=True) self.mox.ReplayAll() ovz_utils.mkfs(path, 'ext3', None, fs_label)
def test_touch_file_success(self): fh = ovzfile.OVZFile(fakes.TEMPFILE, 755) self.mox.StubOutWithMock(fh, 'make_path') fh.make_path() self.mox.StubOutWithMock(ovz_utils, 'execute') ovz_utils.execute( 'touch', fakes.TEMPFILE, run_as_root=True).AndReturn( ('', fakes.ERRORMSG)) self.mox.ReplayAll() fh.touch()
def _rsync(self, src_path, dest_path): """ Copy a path from one place to another using rsync """ dest = "%s@%s:%s" % (self.user, self.dest_host, os.path.abspath(dest_path)) counter = 1 while counter <= CONF.ovz_rsync_iterations: LOG.debug(_("RSyncing %(src_path)s, attempt: %(counter)s") % locals()) ovz_utils.execute("rsync", "-qavz", src_path, dest, run_as_root=True) counter += 1
def test_touch_file_failure(self): fh = ovzfile.OVZFile(fakes.TEMPFILE, 755) self.mox.StubOutWithMock(fh, 'make_path') fh.make_path() self.mox.StubOutWithMock(ovz_utils, 'execute') ovz_utils.execute( 'touch', fakes.TEMPFILE, run_as_root=True).AndRaise( exception.InstanceUnacceptable(fakes.ERRORMSG)) self.mox.ReplayAll() self.assertRaises(exception.InstanceUnacceptable, fh.touch)
def test_make_path_and_dir_success(self): self.mox.StubOutWithMock(ovz_utils, 'execute') ovz_utils.execute( 'mkdir', '-p', mox.IgnoreArg(), run_as_root=True).AndReturn( ('', fakes.ERRORMSG)) self.mox.StubOutWithMock(openvz_conn.os.path, 'exists') openvz_conn.os.path.exists(mox.IgnoreArg()).AndReturn(False) self.mox.ReplayAll() fh = ovzfile.OVZFile(fakes.TEMPFILE, 755) fh.make_path()
def undump(self): """ Restore a VZ from a dump file """ LOG.debug(_('Undumping instance %s') % self.instance['id']) ovz_utils.execute('vzctl', 'restore', self.instance['id'], '--undump', '--dumpfile', self.dumpfile, '--skip_arpdetect', run_as_root=True) LOG.debug(_('Undumped instance %(instance_id)s from %(dumpfile)s') % {'instance_id': self.instance['id'], 'dumpfile': self.dumpfile})
def _let_disk_settle(self): """ Check to see if the device_name is available in /dev/disk/by-path. If it isn't then we'll wait a bit and retry. Disks don't just instantly show up in /dev so this gives a little bit of wait time for things to show up. :return: """ ovz_utils.execute( 'ls', self.device_name(), run_as_root=True, attempts=CONF.ovz_volume_settle_num_tries, delay_on_retry=True)
def _detach_raw_device(self, major, minor): """ Remove perms from the container for a device based on major and minor numbers. :param major: :param minor: :return: """ ovz_utils.execute( 'vzctl', 'set', self.instance_id, '--devices', 'b:%s:%s:none' % (major, minor), run_as_root=True)
def test_get_memory_mb_used_instance(self): self.mox.StubOutWithMock(ovz_utils, 'execute') ovz_utils.execute( 'vzlist', '-H', '-o', 'ctid,privvmpages.l', str(fakes.INSTANCE['id']), raise_on_error=False, run_as_root=True).AndReturn( fakes.PRIVVMPAGES_2048) self.mox.ReplayAll() memory_used = ((int( fakes.PRIVVMPAGES_2048.strip().split()[1]) * 4096) / 1024 ** 2) result = ovz_utils.get_memory_mb_used(fakes.INSTANCE['id']) self.assertEqual(memory_used, result)
def dump(self): """ Create a vz dump file from a container. This is the file that we transfer to do a full migration """ LOG.debug(_('Dumping instance %s') % self.instance['id']) ovz_utils.execute('vzctl', 'chkpnt', self.instance['id'], '--dump', '--dumpfile', self.dumpfile, run_as_root=True) LOG.debug(_('Dumped instance %(instance_id)s to %(dumpfile)s') % {'instance_id': self.instance['id'], 'dumpfile': self.dumpfile})
def _check_device_exists(self, device_path): """Check that the device path exists. Verify that the device path has actually been created and can report it's size, only then can it be available for formatting, retry num_tries to account for the time lag. """ try: ovz_utils.execute('blockdev', '--getsize64', device_path, attempts=CONF.ovz_system_num_tries, run_as_root=True) except processutils.ProcessExecutionError: raise exception.InvalidDevicePath(path=device_path)
def _set_nameserver(self, instance_id, dns): """ Get the nameserver for the assigned network and set it using OpenVz's tools. Run the command: vzctl set <ctid> --save --nameserver <nameserver> If this fails to run an exception is raised as this will indicate the container's inability to do name resolution. """ ovz_utils.execute('vzctl', 'set', instance_id, '--save', '--nameserver', dns, run_as_root=True)