chore(xo-server): use @xen-orchestra/xapi/VDI_create

This commit is contained in:
Julien Fontanet 2022-06-02 16:10:16 +02:00
parent 0008f2845c
commit 5063a6982a
6 changed files with 66 additions and 95 deletions

View File

@ -30,7 +30,7 @@ class Vdi {
other_config = {},
read_only = false,
sharable = false,
SR,
SR = this.pool.default_SR,
tags,
type = 'user',
virtual_size,

View File

@ -26,5 +26,6 @@
<!--packages-start-->
- @xen-orchestra/xapi minor
- xo-server patch
<!--packages-end-->

View File

@ -10,6 +10,7 @@ import { peekFooterFromVhdStream } from 'vhd-lib'
import { vmdkToVhd } from 'xo-vmdk-to-vhd'
import { VDI_FORMAT_VHD, VDI_FORMAT_RAW } from '../xapi/index.mjs'
import { parseSize } from '../utils.mjs'
const log = createLogger('xo:disk')
@ -40,11 +41,13 @@ export const create = defer(async function ($defer, { name, size, sr, vm, bootab
} while (false)
const xapi = this.getXapi(sr)
const vdi = await xapi.createVdi({
name_label: name,
size,
sr: sr._xapiId,
})
const vdi = await xapi._getOrWaitObject(
await xapi.VDI_create({
name_label: name,
SR: sr._xapiRef,
virtual_size: parseSize(size),
})
)
$defer.onFailure(() => vdi.$destroy())
if (attach) {
@ -211,12 +214,14 @@ async function handleImport(req, res, { type, name, description, vmdkData, srId,
throw new JsonRpcError(`Unknown disk type, expected "iso", "vhd" or "vmdk", got ${type}`)
}
const vdi = await xapi.createVdi({
name_description: description,
name_label: name,
size,
sr: srId,
})
const vdi = await this._getOrWaitObject(
await xapi.VDI_create({
name_description: description,
name_label: name,
SR: xapi.getObject(srId, 'SR').$ref,
virtual_size: parseSize(size),
})
)
try {
await xapi.importVdiContent(vdi, vhdStream, { format: diskFormat })
res.end(format.response(0, vdi.$id))

View File

@ -734,14 +734,16 @@ async function createNewDisk(xapi, sr, vm, diskSize) {
const vdiMax = 2040 * 1024 * 1024 * 1024
const createVdiSize = Math.min(vdiMax, diskSize)
const extensionSize = diskSize - createVdiSize
const newDisk = await xapi.createVdi(
{
name_label: 'xosan_data',
name_description: 'Created by XO',
size: createVdiSize,
sr,
},
{ sm_config: { type: 'raw' } }
const newDisk = await xapi._getOrWaitObject(
await xapi.VDI_create(
{
name_description: 'Created by XO',
name_label: 'xosan_data',
SR: sr.$ref,
virtual_size: createVdiSize,
},
{ sm_config: { type: 'raw' } }
)
)
if (extensionSize > 0) {
const { type, uuid: srUuid, $PBDs } = xapi.getObject(sr)

View File

@ -31,7 +31,7 @@ import { Ref } from 'xen-api'
import { synchronized } from 'decorator-synchronized'
import fatfsBuffer, { init as fatfsBufferInit } from '../fatfs-buffer.mjs'
import { camelToSnakeCase, forEach, map, parseSize, pDelay, promisifyAll } from '../utils.mjs'
import { camelToSnakeCase, forEach, map, pDelay, promisifyAll } from '../utils.mjs'
import mixins from './mixins/index.mjs'
import OTHER_CONFIG_TEMPLATE from './other-config-template.mjs'
@ -839,12 +839,14 @@ export default class Xapi extends XapiBase {
}
await Promise.all(
map(disks, async disk => {
const vdi = (vdis[disk.path] = await this.createVdi({
name_description: disk.descriptionLabel,
name_label: disk.nameLabel,
size: disk.capacity,
sr: sr.$ref,
}))
const vdi = (vdis[disk.path] = await this._getOrWaitObject(
await this.VDI_create({
name_description: disk.descriptionLabel,
name_label: disk.nameLabel,
SR: sr.$ref,
virtual_size: disk.capacity,
})
))
$defer.onFailure(() => vdi.$destroy())
compression[disk.path] = disk.compression
return this.createVbd({
@ -1176,51 +1178,6 @@ export default class Xapi extends XapiBase {
return this.callAsync('VDI.clone', vdi.$ref).then(extractOpaqueRef)
}
async createVdi(
{
name_description,
name_label,
other_config = {},
read_only = false,
sharable = false,
sm_config,
SR,
tags,
type = 'user',
virtual_size,
xenstore_data,
size,
sr = Ref.isNotEmpty(SR) ? SR : this.pool.default_SR,
},
{
// blindly copying `sm_config` from another VDI can create problems,
// therefore it is ignored by default by this method
//
// see https://github.com/vatesfr/xen-orchestra/issues/4482
setSmConfig = false,
} = {}
) {
sr = this.getObject(sr)
log.debug(`Creating VDI ${name_label} on ${sr.name_label}`)
return this._getOrWaitObject(
await this.callAsync('VDI.create', {
name_description,
name_label,
other_config,
read_only: Boolean(read_only),
sharable: Boolean(sharable),
SR: sr.$ref,
tags,
type,
sm_config: setSmConfig ? sm_config : undefined,
virtual_size: size !== undefined ? parseSize(size) : virtual_size,
xenstore_data,
}).then(extractOpaqueRef)
)
}
async moveVdi(vdiId, srId) {
const vdi = this.getObject(vdiId)
const sr = this.getObject(srId)
@ -1625,11 +1582,13 @@ export default class Xapi extends XapiBase {
// First, create a small VDI (10MB) which will become the ConfigDrive
const buffer = fatfsBufferInit({ label: 'cidata ' })
const vdi = await this.createVdi({
name_label: 'XO CloudConfigDrive',
size: buffer.length,
sr: sr.$ref,
})
const vdi = await this._getOrWaitObject(
await this.VDI_create({
name_label: 'XO CloudConfigDrive',
SR: sr.$ref,
virtual_size: buffer.length,
})
)
$defer.onFailure(() => vdi.$destroy())
// Then, generate a FAT fs
@ -1667,12 +1626,14 @@ export default class Xapi extends XapiBase {
@decorateWith(deferrable)
async createTemporaryVdiOnSr($defer, stream, sr, name_label, name_description) {
const vdi = await this.createVdi({
name_description,
name_label,
size: stream.length,
sr: sr.$ref,
})
const vdi = await this._getOrWaitObject(
await this.VDI_create({
name_description,
name_label,
SR: sr.$ref,
virtual_size: stream.length,
})
)
$defer.onFailure(() => vdi.$destroy())
await this.importVdiContent(vdi.$id, stream, { format: VDI_FORMAT_RAW })

View File

@ -163,21 +163,23 @@ export default {
const devices = await this.call('VM.get_allowed_VBD_devices', vm.$ref)
await Promise.all(
mapToArray(vdis, (vdiDescription, i) =>
this.createVdi({
this.VDI_create({
name_description: vdiDescription.name_description,
name_label: vdiDescription.name_label,
size: vdiDescription.size,
sr: vdiDescription.sr || vdiDescription.SR,
}).then(vdi =>
this.createVbd({
// Either the CD or the 1st disk is bootable (only useful for PV VMs)
bootable: !(hasBootableDisk || i),
virtual_size: vdiDescription.size,
SR: this.getObject(vdiDescription.sr || vdiDescription.SR, 'SR').$ref,
})
.then(vdiRef => this._getOrWaitObject(vdiRef))
.then(vdi =>
this.createVbd({
// Either the CD or the 1st disk is bootable (only useful for PV VMs)
bootable: !(hasBootableDisk || i),
userdevice: devices[i],
vdi,
vm,
})
)
userdevice: devices[i],
vdi,
vm,
})
)
)
)
}