diff --git a/src/api/vm.coffee b/src/api/vm.coffee index f75847723..2b1bce107 100644 --- a/src/api/vm.coffee +++ b/src/api/vm.coffee @@ -842,32 +842,16 @@ exports.import = import_ # FIXME: if position is used, all other disks after this position # should be shifted. attachDisk = $coroutine ({vm, vdi, position, mode, bootable}) -> - xapi = @getXAPI vm - - VBD_ref = $wait xapi.call 'VBD.create', { - VM: vm.ref - VDI: vdi.ref - mode: mode - type: 'Disk' - userdevice: position - bootable: bootable ? false - empty: false - other_config: {} - qos_algorithm_type: '' - qos_algorithm_params: {} - } - - $wait xapi.call 'VBD.plug', VBD_ref - - return true + $wait @getXAPI(vm).attachVdiToVm(vdi.id, vm.id, {bootable, mode, position}) + return attachDisk.params = { bootable: { type: 'boolean' optional: true } - mode: { type: 'string' } - position: { type: 'string' } + mode: { type: 'string', optional: true } + position: { type: 'string', optional: true } vdi: { type: 'string' } vm: { type: 'string' } } diff --git a/src/xapi.js b/src/xapi.js index fce0f88c6..2e4459a6c 100644 --- a/src/xapi.js +++ b/src/xapi.js @@ -496,6 +496,41 @@ export default class Xapi extends XapiBase { ) } + async attachVdiToVm (vdiId, vmId, { + bootable = false, + mode = 'RW', + position + } = {}) { + const vdi = this.getObject(vdiId) + const vm = this.getObject(vmId) + + if (position == null) { + forEach(vm.$VBDs, vbd => { + const curPos = +vbd.userdevice + if (!(position > curPos)) { + position = curPos + } + }) + + position = position == null ? 0 : position + 1 + } + + const vbdRef = await this.call('VBD.create', { + bootable, + empty: false, + mode, + other_config: {}, + qos_algorithm_params: {}, + qos_algorithm_type: '', + type: 'Disk', + userdevice: String(position), + VDI: vdi.$ref, + VM: vm.$ref + }) + + await this.call('VBD.plug', vbdRef) + } + // ================================================================= async createVirtualInterface (vmId, networkId, {