From mboxrd@z Thu Jan 1 00:00:00 1970 From: =?UTF-8?B?THVrw6HFoSBEb2t0b3I=?= Subject: Re: [PATCH 1/4] [kvm-autotest] cgroup-kvm: add_*_drive / rm_drive Date: Mon, 10 Oct 2011 12:37:50 +0200 Message-ID: <4E92CAFE.4030202@redhat.com> References: <47a34dd7-fbab-4a34-863f-5983df1c9f35@zmail05.collab.prod.int.phx2.redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: autotest@test.kernel.org, kvm@vger.kernel.org, kvm-autotest@redhat.com, akong@redhat.com, lmr@redhat.com To: Jiri Zupka Return-path: Received: from mx1.redhat.com ([209.132.183.28]:62920 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753113Ab1JJKiA (ORCPT ); Mon, 10 Oct 2011 06:38:00 -0400 In-Reply-To: <47a34dd7-fbab-4a34-863f-5983df1c9f35@zmail05.collab.prod.int.phx2.redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: I thought about that. But pci_add is not much stable and it's not=20 supported in QMP (as far as I read) with a note that this way is buggy=20 and should be rewritten completely. So I placed it here to let it=20 develop and then I can move it into utils. Regards, Luk=C3=A1=C5=A1 Dne 10.10.2011 12:26, Jiri Zupka napsal(a): > This is useful function. This function can be in kvm utils. > > ----- Original Message ----- >> * functions for adding and removal of drive to vm using host-file or >> host-scsi_debug device. >> >> Signed-off-by: Lukas Doktor >> --- >> client/tests/kvm/tests/cgroup.py | 125 >> ++++++++++++++++++++++++++++++++----- >> 1 files changed, 108 insertions(+), 17 deletions(-) >> >> diff --git a/client/tests/kvm/tests/cgroup.py >> b/client/tests/kvm/tests/cgroup.py >> index b9a10ea..d6418b5 100644 >> --- a/client/tests/kvm/tests/cgroup.py >> +++ b/client/tests/kvm/tests/cgroup.py >> @@ -17,6 +17,108 @@ def run_cgroup(test, params, env): >> vms =3D None >> tests =3D None >> >> + # Func >> + def get_device_driver(): >> + """ >> + Discovers the used block device driver {ide, scsi, >> virtio_blk} >> + @return: Used block device driver {ide, scsi, virtio} >> + """ >> + if test.tagged_testname.count('virtio_blk'): >> + return "virtio" >> + elif test.tagged_testname.count('scsi'): >> + return "scsi" >> + else: >> + return "ide" >> + >> + >> + def add_file_drive(vm, driver=3Dget_device_driver(), >> host_file=3DNone): >> + """ >> + Hot-add a drive based on file to a vm >> + @param vm: Desired VM >> + @param driver: which driver should be used (default: same a= s >> in test) >> + @param host_file: Which file on host is the image (default: >> create new) >> + @return: Tupple(ret_file, device) >> + ret_file: created file handler (None if not >> created) >> + device: PCI id of the virtual disk >> + """ >> + if not host_file: >> + host_file =3D >> tempfile.NamedTemporaryFile(prefix=3D"cgroup-disk-", >> + suffix=3D".iso") >> + utils.system("dd if=3D/dev/zero of=3D%s bs=3D1M count=3D= 8 >> &>/dev/null" >> + % (host_file.name)) >> + ret_file =3D host_file >> + else: >> + ret_file =3D None >> + >> + out =3D vm.monitor.cmd("pci_add auto storage >> file=3D%s,if=3D%s,snapshot=3Doff," >> + "cache=3Doff" % (host_file.name, drive= r)) >> + dev =3D re.search(r'OK domain (\d+), bus (\d+), slot (\d+), >> function \d+', >> + out) >> + if not dev: >> + raise error.TestFail("Can't add device(%s, %s, %s): %s" >> % (vm, >> + host_file.name, >> driver, out)) >> + device =3D "%s:%s:%s" % dev.groups() >> + return (ret_file, device) >> + >> + >> + def add_scsi_drive(vm, driver=3Dget_device_driver(), >> host_file=3DNone): >> + """ >> + Hot-add a drive based on scsi_debug device to a vm >> + @param vm: Desired VM >> + @param driver: which driver should be used (default: same a= s >> in test) >> + @param host_file: Which dev on host is the image (default: >> create new) >> + @return: Tupple(ret_file, device) >> + ret_file: string of the created dev (None if no= t >> created) >> + device: PCI id of the virtual disk >> + """ >> + if not host_file: >> + if utils.system_output("lsmod | grep scsi_debug -c") =3D= =3D >> 0: >> + utils.system("modprobe scsi_debug dev_size_mb=3D8 >> add_host=3D0") >> + utils.system("echo 1> >> /sys/bus/pseudo/drivers/scsi_debug/add_host") >> + host_file =3D utils.system_output("ls /dev/sd* | tail -= n >> 1") >> + # Enable idling in scsi_debug drive >> + utils.system("echo 1> /sys/block/%s/queue/rotational" = % >> host_file) >> + ret_file =3D host_file >> + else: >> + # Don't remove this device during cleanup >> + # Reenable idling in scsi_debug drive (in case it's not= ) >> + utils.system("echo 1> /sys/block/%s/queue/rotational" = % >> host_file) >> + ret_file =3D None >> + >> + out =3D vm.monitor.cmd("pci_add auto storage >> file=3D%s,if=3D%s,snapshot=3Doff," >> + "cache=3Doff" % (host_file, driver)) >> + dev =3D re.search(r'OK domain (\d+), bus (\d+), slot (\d+), >> function \d+', >> + out) >> + if not dev: >> + raise error.TestFail("Can't add device(%s, %s, %s): %s" >> % (vm, >> + host_file, >> driver, out)) >> + device =3D "%s:%s:%s" % dev.groups() >> + return (ret_file, device) >> + >> + >> + def rm_drive(vm, host_file, device): >> + """ >> + Remove drive from vm and device on disk >> + ! beware to remove scsi devices in reverse order ! >> + """ >> + vm.monitor.cmd("pci_del %s" % device) >> + >> + if isinstance(host_file, file): # file >> + host_file.close() >> + elif isinstance(host_file, str): # scsi device >> + utils.system("echo -1> >> /sys/bus/pseudo/drivers/scsi_debug/add_host") >> + else: # custom file, do nothing >> + pass >> + >> + def get_all_pids(ppid): >> + """ >> + Get all PIDs of children/threads of parent ppid >> + param ppid: parent PID >> + return: list of PIDs of all children/threads of ppid >> + """ >> + return (utils.system_output("ps -L --ppid=3D%d -o lwp" % pp= id) >> + >> .split('\n')= [1:]) >> + >> # Tests >> class _TestBlkioBandwidth: >> """ >> @@ -46,9 +148,8 @@ def run_cgroup(test, params, env): >> """ >> err =3D "" >> try: >> - for i in range (2): >> - vms[i].monitor.cmd("pci_del %s" % >> self.devices[i]) >> - self.files[i].close() >> + for i in range(1, -1, -1): >> + rm_drive(vms[i], self.files[i], self.devices[i]= ) >> except Exception, failure_detail: >> err +=3D "\nCan't remove PCI drive: %s" % >> failure_detail >> try: >> @@ -89,8 +190,7 @@ def run_cgroup(test, params, env): >> if blkio.set_cgroup(self.vms[i].get_shell_pid(), >> pwd[i]): >> raise error.TestError("Could not set cgroup") >> # Move all existing threads into cgroup >> - for tmp in utils.system_output("ps -L --ppid=3D%d -= o >> lwp" >> - % >> self.vms[i].get_shell_pid()).split('\n')[1:]: >> + for tmp in >> get_all_pids(self.vms[i].get_shell_pid()): >> if blkio.set_cgroup(int(tmp), pwd[i]): >> raise error.TestError("Could not set >> cgroup") >> if self.blkio.set_property("blkio.weight", 100, pwd[0]= ): >> @@ -101,18 +201,9 @@ def run_cgroup(test, params, env): >> # Add dummy drives >> # TODO: implement also using QMP. >> for i in range(2): >> - self.files.append(tempfile.NamedTemporaryFile( >> - prefix=3D"cgroup-disk-", >> - suffix=3D".iso")) >> - utils.system("dd if=3D/dev/zero of=3D%s bs=3D1M cou= nt=3D10 >> &>/dev/null" >> - % (self.files[i].name)) >> - out =3D vms[i].monitor.cmd("pci_add auto storage >> file=3D%s," >> - "if=3Dvirtio,snapshot=3Doff,cache=3D= off" >> - % (self.files[i].name)) >> - out =3D re.search(r'OK domain (\d+), bus (\d+), slo= t >> (\d+), ' >> - 'function \d+', out).groups() >> - self.devices.append("%s:%s:%s" % out) >> - >> + (host_file, device) =3D add_file_drive(vms[i], >> "virtio") >> + self.files.append(host_file) >> + self.devices.append(device) >> >> def run(self): >> """ >> -- >> 1.7.6.2 >> >> -- >> To unsubscribe from this list: send the line "unsubscribe kvm" in >> the body of a message to majordomo@vger.kernel.org >> More majordomo info at http://vger.kernel.org/majordomo-info.html >>