Skip to content

Commit

Permalink
correctly handle 'bootable' property
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuRA committed Dec 12, 2024
1 parent 4eeebf7 commit cb25c8c
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions packages/xo-server/src/xapi/mixins/vm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const methods = {
}
}

let hasBootableDisk = !!find(vm.$VBDs, 'bootable')
let hasBootableDisk = false

// Inserts the CD if necessary.
if (installMethod === 'cd') {
Expand Down Expand Up @@ -165,16 +165,18 @@ const methods = {
const _vdisToUpdate = []
const _vdisToDestroy = []

vdis.forEach(vdi => {
if (vdi.userdevice === undefined) {
vdis.forEach(({ destroy, ...vdi }) => {
const { userdevice } = vdi

if (userdevice === undefined) {
_vdisToCreate.push(vdi)
return
}

// If the userdevice match no vbd, create the VDI
const vbd = find(vm.$VBDs, { userdevice: vdi.userdevice })
const vbd = find(vm.$VBDs, { userdevice })
if (vbd === undefined) {
if (vdi.destroy) {
if (destroy) {
log.warn('VDI ignored because it is marked as "destroy"', vdi)
return
}
Expand All @@ -183,18 +185,30 @@ const methods = {
return
}

if (vbd.VDI === Ref.EMPTY) {
throw new Error(`VBD with userdevice: ${userdevice} exist but has no VDI`)
}

vdi.$ref = this.getObject(vbd.VDI).$ref
if (vdi.destroy) {
if (destroy) {
_vdisToDestroy.push(vdi)
return
}

_vdisToUpdate.push(vdi)
})

await Promise.all(_vdisToDestroy.map(vdi => this.VDI_destroy(vdi.$ref)))

// Some VBDs may be destroyed with the VDI_destroy. We need to get a fresh VBDs list
const vbds = (await this.getField('VM', vmRef, 'VBDs')).map(vbdRef => this.getObject(vbdRef))

if (!hasBootableDisk) {
hasBootableDisk = vbds.some(vbd => vbd.bootable)
}

// TODO: set vm.suspend_SR
await Promise.all([
..._vdisToDestroy.map(vdi => this.VDI_destroy(vdi.$ref)),
// Creates the user defined VDIs.
..._vdisToCreate.map(async (vdi, i) => {
const vdiRef = await this.VDI_create({
Expand All @@ -205,18 +219,20 @@ const methods = {
})
$defer.onFailure(() => this.VDI_destroy(vdiRef))

// Either the CD or the 1st disk is bootable (only useful for PV VMs)
let bootable = false
if (!hasBootableDisk && i === 0) {
bootable = true
}
await this.VBD_create({
// Either the CD or the 1st disk is bootable (only useful for PV VMs)
bootable: !(hasBootableDisk || i),
bootable,
userdevice: vdi.userdevice,
VDI: vdiRef,
VM: vm.$ref,
})
}),
// Modify existing (previous template) disks if necessary
..._vdisToUpdate.map(async ({ $ref, sr, size, userdevice, ...properties }) => {
delete properties.destroy

await this._setObjectProperties({ $ref, $type: 'VDI' }, properties)

let _vdi = this.getObject($ref)
Expand Down

0 comments on commit cb25c8c

Please sign in to comment.