]> git.proxmox.com Git - pve-manager.git/blobdiff - www/manager6/window/GuestImport.js
ui: guest import: rework windows virtio-scsi preparation
[pve-manager.git] / www / manager6 / window / GuestImport.js
index 12e7788101219e745ea02d13a5c21f9e4425a182..b846f00eb09cfad4b536aa9bbe8f9f23ef3dd8e8 100644 (file)
@@ -92,6 +92,139 @@ Ext.define('PVE.window.GuestImport', {
            summaryGrid.getStore().setData(Object.entries(values).map(([key, value]) => ({ key, value })));
        },
 
+       calculateAdditionalCDIdx: function() {
+           let me = this;
+
+           let maxIde = me.getMaxControllerId('ide');
+           let maxSata = me.getMaxControllerId('sata');
+           // only ide0 and ide2 can be used reliably for isos (e.g. for q35)
+           if (maxIde < 0) {
+               return 'ide0';
+           }
+           if (maxIde < 2) {
+               return 'ide2';
+           }
+           if (maxSata < PVE.Utils.diskControllerMaxIDs.sata - 1) {
+               return `sata${maxSata+1}`;
+           }
+
+           return '';
+       },
+
+       // assume assigned sata disks indices are continuous, so without holes
+       getMaxControllerId: function(controller) {
+           let me = this;
+           let view = me.getView();
+           if (!controller) {
+               return -1;
+           }
+
+           let max = view[`max${controller}`];
+           if (max !== undefined) {
+               return max;
+           }
+
+           max = -1;
+           for (const key of Object.keys(me.getView().vmConfig)) {
+               if (!key.toLowerCase().startsWith(controller)) {
+                   continue;
+               }
+               let idx = parseInt(key.slice(controller.length), 10);
+               if (idx > max) {
+                   max = idx;
+               }
+           }
+           me.lookup('diskGrid').getStore().each(rec => {
+               if (!rec.data.id.toLowerCase().startsWith(controller)) {
+                   return;
+               }
+               let idx = parseInt(rec.data.id.slice(controller.length), 10);
+               if (idx > max) {
+                   max = idx;
+               }
+           });
+           me.lookup('cdGrid').getStore().each(rec => {
+               if (!rec.data.id.toLowerCase().startsWith(controller) || rec.data.hidden) {
+                   return;
+               }
+               let idx = parseInt(rec.data.id.slice(controller.length), 10);
+               if (idx > max) {
+                   max = idx;
+               }
+           });
+
+           view[`max${controller}`] = max;
+           return max;
+       },
+
+       mapDisk: function(value, metaData) {
+           let me = this;
+           let mapSata = me.lookup('mapSata');
+           if (mapSata.isDisabled() || !mapSata.getValue()) {
+               return value;
+           }
+           if (!value.toLowerCase().startsWith('scsi')) {
+               return value;
+           }
+           let offset = parseInt(value.slice(4), 10);
+           let newIdx = offset + me.getMaxControllerId('sata') + 1;
+           if (me.getViewModel().get('isWindows') && me.getView().additionalCdIdx?.startsWith('sata')) {
+               // additionalCdIdx takes the highest sata port
+               newIdx++;
+           }
+           if (newIdx >= PVE.Utils.diskControllerMaxIDs.sata) {
+               let prefix = '';
+               if (metaData !== undefined) {
+                   // we're in the renderer so put a warning here
+                   let warning = gettext('Too many disks, could not map to SATA.');
+                   prefix = `<i data-qtip="${warning}" class="fa fa-exclamation-triangle warning"></i> `;
+               }
+               return `${prefix}${value}`;
+           }
+           return `sata${newIdx}`;
+       },
+
+       refreshGrids: function() {
+           this.lookup('diskGrid').reconfigure();
+           this.lookup('cdGrid').reconfigure();
+       },
+
+       onOSTypeChange: function(_cb, value) {
+           let me = this;
+           if (!value) {
+               return;
+           }
+           let store = me.lookup('cdGrid').getStore();
+           let collection = store.getData().getSource() ?? store.getData();
+           let rec = collection.find('autogenerated', true);
+
+           let isWindows = (value ?? '').startsWith('win');
+           if (rec) {
+               rec.set('hidden', !isWindows);
+               rec.commit();
+           }
+           let prepareVirtio = me.lookup('mapSata').getValue();
+           me.lookup('scsihw').setValue(prepareVirtio && isWindows ? 'virtio-scsi-single' : me.getView().vmConfig.scsihw);
+
+           me.refreshGrids();
+       },
+
+       onPrepareVirtioChange: function(_cb, value) {
+           let me = this;
+
+           let scsihw = me.lookup('scsihw');
+           scsihw.suspendEvents();
+           scsihw.setValue(value ? 'virtio-scsi-single' : me.getView().vmConfig.scsihw);
+           scsihw.resumeEvents();
+
+           me.refreshGrids();
+       },
+
+       onScsiHwChange: function(_field, value) {
+           let me = this;
+           me.getView().vmConfig.scsihw = value;
+       },
+
        control: {
            'grid field': {
                // update records from widgetcolumns
@@ -113,6 +246,15 @@ Ext.define('PVE.window.GuestImport', {
            'panel[reference=summaryTab]': {
                activate: 'calculateConfig',
            },
+           'proxmoxcheckbox[reference=mapSata]': {
+               change: 'onPrepareVirtioChange',
+           },
+           'combobox[name=ostype]': {
+               change: 'onOSTypeChange',
+           },
+           'pveScsiHwSelector': {
+               change: 'onScsiHwChange',
+           },
        },
     },
 
@@ -120,6 +262,9 @@ Ext.define('PVE.window.GuestImport', {
        data: {
            coreCount: 1,
            socketCount: 1,
+           liveImport: false,
+           os: '',
+           maxCdDrives: false,
            warnings: [],
        },
 
@@ -128,6 +273,9 @@ Ext.define('PVE.window.GuestImport', {
            hideWarnings: get => get('warnings').length === 0,
            warningsText: get => '<ul style="margin: 0; padding-left: 20px;">'
                + get('warnings').map(w => `<li>${w}</li>`).join('') + '</ul>',
+           liveImportNote: get => !get('liveImport') ? ''
+               : gettext('Note: If anything goes wrong during the live-import, new data written by the VM may be lost.'),
+           isWindows: get => (get('os') ?? '').startsWith('win'),
        },
     },
 
@@ -164,11 +312,22 @@ Ext.define('PVE.window.GuestImport', {
                            config.scsi0 = config.scsi0.replace('local:0,', 'local:0,format=qcow2,');
                        }
 
+                       let parsedBoot = PVE.Parser.parsePropertyString(config.boot ?? '');
+                       if (parsedBoot.order) {
+                           parsedBoot.order = parsedBoot.order.split(';');
+                       }
+
                        grid.lookup('diskGrid').getStore().each((rec) => {
                            if (!rec.data.enable) {
                                return;
                            }
-                           let id = rec.data.id;
+                           let id = grid.getController().mapDisk(rec.data.id);
+                           if (id !== rec.data.id && parsedBoot?.order) {
+                               let idx = parsedBoot.order.indexOf(rec.data.id);
+                               if (idx !== -1) {
+                                   parsedBoot.order[idx] = id;
+                               }
+                           }
                            let data = {
                                ...rec.data,
                            };
@@ -186,6 +345,11 @@ Ext.define('PVE.window.GuestImport', {
                            config[id] = PVE.Parser.printQemuDrive(data);
                        });
 
+                       if (parsedBoot.order) {
+                           parsedBoot.order = parsedBoot.order.join(';');
+                       }
+                       config.boot = PVE.Parser.printPropertyString(parsedBoot);
+
                        grid.lookup('netGrid').getStore().each((rec) => {
                            if (!rec.data.enable) {
                                return;
@@ -214,10 +378,19 @@ Ext.define('PVE.window.GuestImport', {
                            config[id] = PVE.Parser.printPropertyString(cd);
                        });
 
+                       config.scsihw = grid.lookup('scsihw').getValue();
+
                        if (grid.lookup('liveimport').getValue()) {
                            config['live-restore'] = 1;
                        }
 
+                       // remove __default__ values
+                       for (const [key, value] of Object.entries(config)) {
+                           if (value === '__default__') {
+                               delete config[key];
+                           }
+                       }
+
                        return config;
                    },
 
@@ -324,6 +497,9 @@ Ext.define('PVE.window.GuestImport', {
                            queryMode: 'local',
                            valueField: 'val',
                            displayField: 'desc',
+                           bind: {
+                               value: '{os}',
+                           },
                            store: {
                                fields: ['desc', 'val'],
                                data: PVE.Utils.kvm_ostypes.Linux,
@@ -344,7 +520,11 @@ Ext.define('PVE.window.GuestImport', {
                            fieldLabel: gettext('Live Import'),
                            reference: 'liveimport',
                            isFormField: false,
-                           boxLabel: gettext('Experimental'),
+                           boxLabelCls: 'pmx-hint black x-form-cb-label',
+                           bind: {
+                               value: '{liveImport}',
+                               boxLabel: '{liveImportNote}',
+                           },
                        },
                        {
                            xtype: 'displayfield',
@@ -370,7 +550,11 @@ Ext.define('PVE.window.GuestImport', {
                {
                    title: gettext('Advanced'),
                    xtype: 'inputpanel',
-                   items: [
+
+                   // the first inputpanel handles all values, so prevent value leakage here
+                   onGetValues: () => ({}),
+
+                   columnT: [
                        {
                            xtype: 'displayfield',
                            fieldLabel: gettext('Disks'),
@@ -379,7 +563,7 @@ Ext.define('PVE.window.GuestImport', {
                        {
                            xtype: 'grid',
                            reference: 'diskGrid',
-                           minHeight: 58,
+                           minHeight: 60,
                            maxHeight: 150,
                            store: {
                                data: [],
@@ -402,6 +586,7 @@ Ext.define('PVE.window.GuestImport', {
                                {
                                    text: gettext('Disk'),
                                    dataIndex: 'id',
+                                   renderer: 'mapDisk',
                                },
                                {
                                    text: gettext('Source'),
@@ -452,21 +637,60 @@ Ext.define('PVE.window.GuestImport', {
                                },
                            ],
                        },
+                   ],
+
+                   column1: [
+                       {
+                           xtype: 'proxmoxcheckbox',
+                           fieldLabel: gettext('Prepare for VirtIO-SCSI'),
+                           labelWidth: 200,
+                           reference: 'mapSata',
+                           isFormField: false,
+                           disabled: true,
+                           bind: {
+                               disabled: '{!isWindows}',
+                           },
+                           autoEl: {
+                               tag: 'div',
+                               'data-qtip': gettext('Maps SCSI disks to SATA and changes the SCSI Controller. Useful for a quicker switch to VirtIO-SCSI attached disks'),
+                           },
+                       },
+                   ],
+
+                   column2: [
+                       {
+                           xtype: 'pveScsiHwSelector',
+                           reference: 'scsihw',
+                           name: 'scsihw',
+                           submitValue: false,
+                           fieldLabel: gettext('SCSI Controller'),
+                       },
+                   ],
+
+                   columnB: [
                        {
                            xtype: 'displayfield',
                            fieldLabel: gettext('CD/DVD Drives'),
                            labelWidth: 200,
+                           style: {
+                               paddingTop: '10px',
+                           },
                        },
                        {
                            xtype: 'grid',
                            reference: 'cdGrid',
-                           minHeight: 58,
+                           minHeight: 60,
                            maxHeight: 150,
                            store: {
                                data: [],
                                sorters: [
                                    'id',
                                ],
+                               filters: [
+                                   function(rec) {
+                                       return !rec.data.hidden;
+                                   },
+                               ],
                            },
                            columns: [
                                {
@@ -520,6 +744,9 @@ Ext.define('PVE.window.GuestImport', {
                            xtype: 'displayfield',
                            fieldLabel: gettext('Network Interfaces'),
                            labelWidth: 200,
+                           style: {
+                               paddingTop: '10px',
+                           },
                        },
                        {
                            xtype: 'grid',
@@ -685,14 +912,29 @@ Ext.define('PVE.window.GuestImport', {
                    }
                    cdroms.push({
                        enable: true,
+                       hidden: false,
                        id,
                    });
                    delete me.vmConfig[id];
                }
+
                me.lookup('diskGrid').getStore().setData(disks);
                me.lookup('netGrid').getStore().setData(nets);
                me.lookup('cdGrid').getStore().setData(cdroms);
 
+               let additionalCdIdx = me.getController().calculateAdditionalCDIdx();
+               if (additionalCdIdx === '') {
+                   me.getViewModel().set('maxCdDrives', true);
+               } else if (cdroms.length === 0) {
+                   me.additionalCdIdx = additionalCdIdx;
+                   me.lookup('cdGrid').getStore().add({
+                       enable: true,
+                       hidden: !(me.vmConfig.ostype ?? '').startsWith('win'),
+                       id: additionalCdIdx,
+                       autogenerated: true,
+                   });
+               }
+
                me.getViewModel().set('warnings', data.warnings.map(w => renderWarning(w)));
 
                let osinfo = PVE.Utils.get_kvm_osinfo(me.vmConfig.ostype ?? '');