Skip to content

Commit

Permalink
node23.6 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeauchamp committed Jan 8, 2025
1 parent 161dc94 commit 52f23d0
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 63 deletions.
16 changes: 8 additions & 8 deletions @xen-orchestra/disk-transform/src/demo.mts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { getSyncedHandler } from '@xen-orchestra/fs'
import { VhdRemote, RemoteMetadata } from './from/VhdRemote.mjs'
import { FileAccessor } from './file-accessor/FileAccessor.mjs'
import { VhdRemote } from './from/VhdRemote.mts'
import { FileAccessor } from './file-accessor/FileAccessor.mts'
import { writeVhdFileToRemote } from './to/VhdRemote.mts'

async function run() {
const { value: handler } = await getSyncedHandler({ url: 'file:///mnt/ssd/vhdblock' })
const { value: handlerSource } = await getSyncedHandler({ url: 'file:///mnt/ssd/vhdblock' })
const { value: handlerTarget } = await getSyncedHandler({ url: 'file:///mnt/ssd/out' })
const metadataPath = './xo-vm-backups/cbb46b48-12aa-59dc-4039-8a587fdc67d5/20230831T100000Z.json'

const vhd = new VhdRemote({
handler: handler as FileAccessor,
handler: handlerSource as FileAccessor,
metadataPath,
diskUuid: '1282b678-cb12-4b13-ab17-7a4fdac403d8',
})
const { value: iterator } = await vhd.getBlockIterator()
for await (const block of iterator) {
console.log(block)
}

await writeVhdFileToRemote(handlerTarget, 'out.vhd', vhd)
}

run()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FileAccessor, FileDescriptor } from './FileAccessor.mjs'
import { FileAccessor, type FileDescriptor } from './FileAccessor.mts'

// handle file access inside a form
// to transform disks into an acceptable format from the browser
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FileAccessor, FileDescriptor } from './FileAccessor.mjs'
import { FileAccessor, type FileDescriptor } from './FileAccessor.mts'

export class RemoteFileAccessor extends FileAccessor {
getSize: () => Promise<Number>
Expand Down
30 changes: 14 additions & 16 deletions @xen-orchestra/disk-transform/src/from/VhdRemote.mts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { openVhd } from 'vhd-lib'
import {
DiskBlock,
DiskBlockData,
type DiskBlock,
type DiskBlockData,
DiskBlockGenerator,
Disposable,
type Disposable,
PortableDifferencingDisk,
PortableDiskMetadata,
Uuid,
} from '../PortableDifferencingDisk.mjs'
import { FileAccessor } from '../file-accessor/FileAccessor.mjs'
import { dirname, join, relative } from 'path'
type Uuid,
} from '../PortableDifferencingDisk.mts'
import { type FileAccessor } from '../file-accessor/FileAccessor.mts'
import { dirname, join } from 'path'

type VhdBlock = {
id: number
Expand All @@ -26,8 +26,9 @@ export type Vhd = {
readHeaderAndFooter(): Promise<void>
readBlockAllocationTable(): Promise<void>
readBlock(index: number): Promise<VhdBlock>
blocks(): AsyncIterator<VhdBlock>
writeHeaderAndFooter(): Promise<void>
blocks(): AsyncGenerator<VhdBlock>
writeHeader(): Promise<void>
writeFooter(): Promise<void>
writeBlockAllocationTable(): Promise<void>
writeEntireBlock(block: VhdBlock): Promise<void>
}
Expand All @@ -49,15 +50,12 @@ class VhdFileGenerator extends DiskBlockGenerator {
}
async *[Symbol.asyncIterator](): AsyncIterator<DiskBlock> {
const blockIterator = this.#vhd.blocks()
let res: { value: VhdBlock; done?: boolean }
do {
res = await blockIterator.next(this)
this.consumedBlocks++
for await (const { id, data } of blockIterator) {
yield {
index: res.value.id,
data: res.value.data,
index: id,
data: data,
}
} while (!res.done)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion @xen-orchestra/disk-transform/src/from/utils.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PortableDiskMetadata } from '../PortableDifferencingDisk.mjs'
import { type PortableDiskMetadata } from '../PortableDifferencingDisk.mts'

export async function getXapiMetadata(xapi: any, uuid: string): Promise<PortableDiskMetadata> {
return Promise.resolve({
Expand Down
32 changes: 0 additions & 32 deletions @xen-orchestra/disk-transform/src/to/VhdFileRemote.mts

This file was deleted.

52 changes: 52 additions & 0 deletions @xen-orchestra/disk-transform/src/to/VhdRemote.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { VhdFile } from 'vhd-lib'
import { DiskBlockGenerator, PortableDifferencingDisk } from '../PortableDifferencingDisk.mts'
import { Disposable } from 'promise-toolbox'
import { type Vhd } from '../from/VhdRemote.mts'
import { type FileAccessor } from '../file-accessor/FileAccessor.mts'
import { createFooter, createHeader } from 'vhd-lib/_createFooterHeader.js'
import { unpackFooter, unpackHeader } from 'vhd-lib/Vhd/_utils.js'
import { DISK_TYPES, FOOTER_SIZE } from 'vhd-lib/_constants.js'
import _computeGeometryForSize from 'vhd-lib/_computeGeometryForSize.js'

async function writeVhdToRemote(targetVhd: Vhd, disk: PortableDifferencingDisk): Promise<void> {
return Disposable.use(disk.getBlockIterator(), async (blockIterator: DiskBlockGenerator): Promise<void> => {
// @todo : handle differencing disk parent

const metada = await disk.getMetadata()
console.log('metadata', { metada })
targetVhd.footer = unpackFooter(
// length can be smaller than disk capacity due to alignment to head/cylinder/sector
createFooter(
metada.virtualSize,
Math.floor(Date.now() / 1000),
_computeGeometryForSize(metada.virtualSize),
FOOTER_SIZE,
DISK_TYPES.DYNAMIC
)
)
console.log('got footer ', targetVhd.footer, Math.ceil((metada.virtualSize / 2) * 1024 * 1024))
targetVhd.header = unpackHeader(createHeader(Math.ceil(metada.virtualSize / (2 * 1024 * 1024))))
const bitmap = Buffer.alloc(255, 512)

for await (const block of blockIterator) {
await targetVhd.writeEntireBlock({
id: block.index,
bitmap,
data: block.data,
buffer: Buffer.concat([bitmap, block.data]),
})
}
await targetVhd.writeFooter()
await targetVhd.writeHeader()
await targetVhd.writeBlockAllocationTable()
})
}

export async function writeVhdFileToRemote(handler: FileAccessor, path: string, disk: PortableDifferencingDisk) {
return Disposable.use(VhdFile.create(handler, path), async (vhd: Vhd) => {
// @todo : precompute target bat to ensure we can write the block without updating the bat at each block
return writeVhdToRemote(vhd, disk)
})
}

// @todo: vhddirectory
5 changes: 4 additions & 1 deletion @xen-orchestra/disk-transform/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"module": "NodeNext",
"moduleResolution": "nodenext",
"declaration": true, // Generates `.d.ts` files for type safety
"outDir": "dist" // Outputs compiled code to the `dist` directory
"outDir": "dist", // Outputs compiled code to the `dist` directory
"verbatimModuleSyntax": true,
"allowImportingTsExtensions": true,
"emitDeclarationOnly": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
Expand Down
4 changes: 2 additions & 2 deletions packages/vhd-lib/Vhd/VhdNbd.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ exports.VhdNbd = class VhdNbd extends VhdAbstract {

this.#header = unpackHeader(createHeader(Math.ceil(size / DEFAULT_BLOCK_SIZE)))
const geometry = _computeGeometryForSize(size)
// changed block can only be used to compute a differencing disk
const diskType = DISK_TYPES.DIFFERENCING

const diskType = parentUuid !== undefined ? DISK_TYPES.DIFFERENCING : DISK_TYPES.DYNAMIC
this.#footer = unpackFooter(createFooter(size, Math.floor(Date.now() / 1000), geometry, FOOTER_SIZE, diskType))
this.#footer.uuid = packUuid(uuid)
if (parentUuid) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vhd-lib/Vhd/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ exports.BUF_BLOCK_UNUSED = BUF_BLOCK_UNUSED
* @param {Object} footer
* @returns {Object} the parsed header
*/
exports.unpackHeader = (bufHeader, footer) => {
exports.unpackHeader = (bufHeader, footer = undefined) => {
assertChecksum('header', bufHeader, fuHeader)

const header = fuHeader.unpack(bufHeader)
Expand Down

0 comments on commit 52f23d0

Please sign in to comment.