[pve-devel] [PATCH manager v2 2/2] When adding a new hard disk, use the most used controller as suggested value

Emmanuel Kasper e.kasper at proxmox.com
Wed Oct 5 14:24:40 CEST 2016


This is a complementary fix for #1105 (Create Linux VM Wizard: use scsi
as default bus/device) and add some logic to the list of controllers
presented in the ControllerSelector combo box

Since we can have IDE, SCSI, Virtio(blk) as a controller during installation,
based on OS detection and personal preferences, we can reasonably assume
on 80 % of cases it will be the same controller we want to use for the
next time we add a hardisk.

This allows backward compatibility for Linux guests which were proposed a
virtio-blk as first choice, and also helps newly created Linux VMs by proposing
SCSI.
---
 www/manager6/form/ControllerSelector.js | 50 +++++++++++++++++++++++++++++++--
 1 file changed, 48 insertions(+), 2 deletions(-)

diff --git a/www/manager6/form/ControllerSelector.js b/www/manager6/form/ControllerSelector.js
index 1b2946a..3ed68ea 100644
--- a/www/manager6/form/ControllerSelector.js
+++ b/www/manager6/form/ControllerSelector.js
@@ -17,6 +17,50 @@ Ext.define('PVE.form.ControllerSelector', {
 
     vmconfig: {}, // used to check for existing devices
 
+    sortByPreviousUsage: function(vmconfig, controllerList) {
+	var sortedList = [];
+
+	var usedControllers = Ext.clone(PVE.form.ControllerSelector.maxIds);
+
+	var type;
+	for (type in usedControllers) {
+	    if(usedControllers.hasOwnProperty(type)) {
+		usedControllers[type] = 0;
+	    }
+	}
+
+	var property;
+	for (property in vmconfig) {
+	    if (vmconfig.hasOwnProperty(property)) {
+		/*global bus_match */
+		if (property.match(bus_match) && !vmconfig[property].match(/media=cdrom/)) {
+		    var foundController = property.match(bus_match)[1];
+		    usedControllers[foundController]++;
+		    }
+		}
+	}
+
+	var arrayControllers = [
+	    {name:'ide', count:usedControllers.ide},
+	    {name:'sata', count:usedControllers.sata},
+	    {name:'virtio', count:usedControllers.virtio},
+	    {name:'scsi', count:usedControllers.scsi}
+	].sort(function compare(a, b){
+	    return b.count - a.count;
+	});
+
+	if (arrayControllers[0].count > arrayControllers[1].count ) {
+	    // we have a favourite !
+	    var favourite = arrayControllers[0].name;
+	    sortedList = Ext.Array.remove(controllerList, favourite);
+	    sortedList.unshift(favourite);
+	    return sortedList;
+	}
+	else {
+	    return controllerList;
+	}
+    },
+
     setVMConfig: function(vmconfig, autoSelect) {
 	var me = this;
 
@@ -30,8 +74,10 @@ Ext.define('PVE.form.ControllerSelector', {
 		    me.down('field[name=deviceid]').setValue(2);
 		    return;
 		}
-	    } else if (me.vmconfig.ostype === 'l26') {
-		clist = ['virtio', 'ide', 'scsi', 'sata'];
+	    } else  {
+		// in most cases we want to add a disk to the same controller
+		// we previously used
+		clist = me.sortByPreviousUsage(me.vmconfig, clist);
 	    }
 
 	    Ext.Array.each(clist, function(controller) {
-- 
2.1.4





More information about the pve-devel mailing list