virt: Allow more than 26 disks

This commit is contained in:
Jonathan Gold
2017-09-08 00:22:33 +00:00
parent af75696018
commit 69f479b67e
2 changed files with 33 additions and 2 deletions

View File

@@ -998,7 +998,7 @@ func (d *diskDevice) GetXML(idx int) string {
b += "<disk type='file' device='disk'>"
b += fmt.Sprintf("<driver name='qemu' type='%s'/>", d.Type)
b += fmt.Sprintf("<source file='%s'/>", source)
b += fmt.Sprintf("<target dev='vd%s' bus='virtio'/>", (string)(idx+97)) // TODO: 26, 27... should be 'aa', 'ab'...
b += fmt.Sprintf("<target dev='vd%s' bus='virtio'/>", numToAlpha(idx))
b += "</disk>"
return b
}
@@ -1009,7 +1009,7 @@ func (d *cdRomDevice) GetXML(idx int) string {
b += "<disk type='file' device='cdrom'>"
b += fmt.Sprintf("<driver name='qemu' type='%s'/>", d.Type)
b += fmt.Sprintf("<source file='%s'/>", source)
b += fmt.Sprintf("<target dev='hd%s' bus='ide'/>", (string)(idx+97)) // TODO: 26, 27... should be 'aa', 'ab'...
b += fmt.Sprintf("<target dev='hd%s' bus='ide'/>", numToAlpha(idx))
b += "<readonly/>"
b += "</disk>"
return b
@@ -1195,3 +1195,12 @@ func expandHome(p string) (string, error) {
return p, nil
}
func numToAlpha(idx int) string {
var mod = idx % 26
var div = idx / 26
if div > 0 {
return numToAlpha(div-1) + string(rune(mod+int('a')))
}
return string(rune(mod + int('a')))
}

View File

@@ -46,3 +46,25 @@ func TestExpandHome(t *testing.T) {
}
}
}
func TestNumToAlpha(t *testing.T) {
var numToAlphaTests = []struct {
number int
result string
}{
{0, "a"},
{25, "z"},
{26, "aa"},
{27, "ab"},
{702, "aaa"},
{703, "aab"},
{63269, "cool"},
}
for _, test := range numToAlphaTests {
actual := numToAlpha(test.number)
if actual != test.result {
t.Errorf("numToAlpha(%d): expected %s, actual %s", test.number, test.result, actual)
}
}
}