From a9c791b5b86b93745454a159eb6d5945fb4ae5c1 Mon Sep 17 00:00:00 2001 From: Martin Kletzander Date: Wed, 12 Feb 2014 15:44:40 +0100 Subject: [PATCH] Add target_to_num method Opposite to num_to_target. It will be used in future patch. Signed-off-by: Martin Kletzander --- tests/xmlconfig.py | 13 +++++++++++++ virtinst/devicedisk.py | 16 +++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/xmlconfig.py b/tests/xmlconfig.py index c88573898..9db51cb4c 100644 --- a/tests/xmlconfig.py +++ b/tests/xmlconfig.py @@ -1027,6 +1027,16 @@ class TestXMLConfig(unittest.TestCase): self.assertEquals("zz", VirtualDisk.num_to_target(27 * 26)) self.assertEquals("aaa", VirtualDisk.num_to_target(27 * 26 + 1)) + self.assertEquals(VirtualDisk.target_to_num("hda"), 1) + self.assertEquals(VirtualDisk.target_to_num("hdb"), 2) + self.assertEquals(VirtualDisk.target_to_num("sdz"), 26) + self.assertEquals(VirtualDisk.target_to_num("sdaa"), 27) + self.assertEquals(VirtualDisk.target_to_num("vdab"), 28) + self.assertEquals(VirtualDisk.target_to_num("vdaz"), 52) + self.assertEquals(VirtualDisk.target_to_num("xvdba"), 53) + self.assertEquals(VirtualDisk.target_to_num("xvdzz"), 27 * 26) + self.assertEquals(VirtualDisk.target_to_num("xvdaaa"), 27 * 26 + 1) + disk = virtinst.VirtualDisk(utils.get_conn()) disk.bus = "ide" @@ -1035,6 +1045,9 @@ class TestXMLConfig(unittest.TestCase): self.assertEquals("hdc", disk.generate_target(["hdb", "sda"])) self.assertEquals("hdb", disk.generate_target(["hda", "hdd"])) + disk.bus = "scsi" + self.assertEquals("sdh", disk.generate_target(["sda", "sdg"], 1)) + def testFedoraTreeinfo(self): i = utils.make_distro_installer( location="tests/cli-test-xml/fakefedoratree") diff --git a/virtinst/devicedisk.py b/virtinst/devicedisk.py index ce038dedc..e69a0e929 100644 --- a/virtinst/devicedisk.py +++ b/virtinst/devicedisk.py @@ -1,7 +1,7 @@ # # Classes for building disk device xml # -# Copyright 2006-2008, 2012-2013 Red Hat, Inc. +# Copyright 2006-2008, 2012-2014 Red Hat, Inc. # Jeremy Katz # # This program is free software; you can redistribute it and/or modify @@ -474,6 +474,20 @@ class VirtualDisk(VirtualDevice): return gen_t + @staticmethod + def target_to_num(tgt): + """ + Convert disk /dev number (like hda, hdb, hdaa, etc.) to an index + """ + num = 0 + if tgt[0] == 'x': + # This case is here for 'xvda' + tgt = tgt[1:] + for i, c in enumerate(reversed(tgt[2:])): + num += (ord(c) - ord('a') + 1) * (26 ** i) + return num + + _XML_PROP_ORDER = [ "type", "device", "driver_name", "driver_type",