@xen-orchestra/backups 0.15.1 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/RemoteAdapter.js CHANGED
@@ -3,19 +3,19 @@ const Disposable = require('promise-toolbox/Disposable.js')
3
3
  const fromCallback = require('promise-toolbox/fromCallback.js')
4
4
  const fromEvent = require('promise-toolbox/fromEvent.js')
5
5
  const pDefer = require('promise-toolbox/defer.js')
6
- const pump = require('pump')
7
- const { basename, dirname, join, normalize, resolve } = require('path')
6
+ const { dirname, join, normalize, resolve } = require('path')
8
7
  const { createLogger } = require('@xen-orchestra/log')
9
- const { createSyntheticStream, mergeVhd, VhdFile } = require('vhd-lib')
8
+ const { VhdAbstract, createVhdDirectoryFromStream } = require('vhd-lib')
10
9
  const { deduped } = require('@vates/disposable/deduped.js')
11
10
  const { execFile } = require('child_process')
12
11
  const { readdir, stat } = require('fs-extra')
12
+ const { v4: uuidv4 } = require('uuid')
13
13
  const { ZipFile } = require('yazl')
14
14
 
15
15
  const { BACKUP_DIR } = require('./_getVmBackupDir.js')
16
16
  const { cleanVm } = require('./_cleanVm.js')
17
17
  const { getTmpDir } = require('./_getTmpDir.js')
18
- const { isMetadataFile, isVhdFile } = require('./_backupType.js')
18
+ const { isMetadataFile } = require('./_backupType.js')
19
19
  const { isValidXva } = require('./_isValidXva.js')
20
20
  const { listPartitions, LVM_PARTITION_TYPE } = require('./_listPartitions.js')
21
21
  const { lvs, pvs } = require('./_lvm.js')
@@ -77,48 +77,6 @@ class RemoteAdapter {
77
77
  return this._handler
78
78
  }
79
79
 
80
- async _deleteVhd(path) {
81
- const handler = this._handler
82
- const vhds = await asyncMapSettled(
83
- await handler.list(dirname(path), {
84
- filter: isVhdFile,
85
- prependDir: true,
86
- }),
87
- async path => {
88
- try {
89
- const vhd = new VhdFile(handler, path)
90
- await vhd.readHeaderAndFooter()
91
- return {
92
- footer: vhd.footer,
93
- header: vhd.header,
94
- path,
95
- }
96
- } catch (error) {
97
- // Do not fail on corrupted VHDs (usually uncleaned temporary files),
98
- // they are probably inconsequent to the backup process and should not
99
- // fail it.
100
- warn(`BackupNg#_deleteVhd ${path}`, { error })
101
- }
102
- }
103
- )
104
- const base = basename(path)
105
- const child = vhds.find(_ => _ !== undefined && _.header.parentUnicodeName === base)
106
- if (child === undefined) {
107
- await handler.unlink(path)
108
- return 0
109
- }
110
-
111
- try {
112
- const childPath = child.path
113
- const mergedDataSize = await mergeVhd(handler, path, handler, childPath)
114
- await handler.rename(path, childPath)
115
- return mergedDataSize
116
- } catch (error) {
117
- handler.unlink(path).catch(warn)
118
- throw error
119
- }
120
- }
121
-
122
80
  async _findPartition(devicePath, partitionId) {
123
81
  const partitions = await listPartitions(devicePath)
124
82
  const partition = partitions.find(_ => _.id === partitionId)
@@ -255,7 +213,7 @@ class RemoteAdapter {
255
213
  const handler = this._handler
256
214
 
257
215
  // unused VHDs will be detected by `cleanVm`
258
- await asyncMapSettled(backups, ({ _filename }) => handler.unlink(_filename))
216
+ await asyncMapSettled(backups, ({ _filename }) => VhdAbstract.unlink(handler, _filename))
259
217
  }
260
218
 
261
219
  async deleteMetadataBackup(backupId) {
@@ -354,6 +312,17 @@ class RemoteAdapter {
354
312
  return yield this._getPartition(devicePath, await this._findPartition(devicePath, partitionId))
355
313
  }
356
314
 
315
+ // this function will be the one where we plug the logic of the storage format by fs type/user settings
316
+
317
+ // if the file is named .vhd => vhd
318
+ // if the file is named alias.vhd => alias to a vhd
319
+ getVhdFileName(baseName) {
320
+ if (this._handler.type === 's3') {
321
+ return `${baseName}.alias.vhd` // we want an alias to a vhddirectory
322
+ }
323
+ return `${baseName}.vhd`
324
+ }
325
+
357
326
  async listAllVmBackups() {
358
327
  const handler = this._handler
359
328
 
@@ -498,6 +467,24 @@ class RemoteAdapter {
498
467
  return backups.sort(compareTimestamp)
499
468
  }
500
469
 
470
+ async writeVhd(path, input, { checksum = true, validator = noop } = {}) {
471
+ const handler = this._handler
472
+ let dataPath = path
473
+
474
+ if (path.endsWith('.alias.vhd')) {
475
+ await createVhdDirectoryFromStream(handler, `${dirname(path)}/data/${uuidv4()}.vhd`, input, {
476
+ concurrency: 16,
477
+ async validator() {
478
+ await input.task
479
+ return validator.apply(this, arguments)
480
+ },
481
+ })
482
+ await VhdAbstract.createAlias(handler, path, dataPath)
483
+ } else {
484
+ await this.outputStream(dataPath, input, { checksum, validator })
485
+ }
486
+ }
487
+
501
488
  async outputStream(path, input, { checksum = true, validator = noop } = {}) {
502
489
  await this._handler.outputStream(path, input, {
503
490
  checksum,
@@ -509,6 +496,21 @@ class RemoteAdapter {
509
496
  })
510
497
  }
511
498
 
499
+ async _createSyntheticStream(handler, paths) {
500
+ // I don't want the vhds to be disposed on return
501
+ // but only when the stream is done ( or failed )
502
+ const disposables = await Disposable.all(paths.map(path => openVhd(handler, path)))
503
+ const vhds = disposables.value
504
+ const synthetic = new VhdSynthetic(vhds)
505
+ await synthetic.readHeaderAndFooter()
506
+ await synthetic.readBlockAllocationTable()
507
+ const stream = await synthetic.stream()
508
+ stream.on('end', () => disposables.dispose())
509
+ stream.on('close', () => disposables.dispose())
510
+ stream.on('error', () => disposables.dispose())
511
+ return stream
512
+ }
513
+
512
514
  async readDeltaVmBackup(metadata) {
513
515
  const handler = this._handler
514
516
  const { vbds, vdis, vhds, vifs, vm } = metadata
@@ -516,7 +518,7 @@ class RemoteAdapter {
516
518
 
517
519
  const streams = {}
518
520
  await asyncMapSettled(Object.keys(vdis), async id => {
519
- streams[`${id}.vhd`] = await createSyntheticStream(handler, join(dir, vhds[id]))
521
+ streams[`${id}.vhd`] = await this._createSyntheticStream(handler, join(dir, vhds[id]))
520
522
  })
521
523
 
522
524
  return {
package/_cleanVm.js CHANGED
@@ -1,13 +1,14 @@
1
1
  const assert = require('assert')
2
2
  const sum = require('lodash/sum')
3
3
  const { asyncMap } = require('@xen-orchestra/async-map')
4
- const { VhdFile, mergeVhd } = require('vhd-lib')
4
+ const { Constants, mergeVhd, openVhd, VhdAbstract, VhdFile } = require('vhd-lib')
5
5
  const { dirname, resolve } = require('path')
6
- const { DISK_TYPE_DIFFERENCING } = require('vhd-lib/dist/_constants.js')
6
+ const { DISK_TYPES } = Constants
7
7
  const { isMetadataFile, isVhdFile, isXvaFile, isXvaSumFile } = require('./_backupType.js')
8
8
  const { limitConcurrency } = require('limit-concurrency-decorator')
9
9
 
10
10
  const { Task } = require('./Task.js')
11
+ const { Disposable } = require('promise-toolbox')
11
12
 
12
13
  // chain is an array of VHDs from child to parent
13
14
  //
@@ -65,12 +66,12 @@ async function mergeVhdChain(chain, { handler, onLog, remove, merge }) {
65
66
  clearInterval(handle)
66
67
 
67
68
  await Promise.all([
68
- handler.rename(parent, child),
69
+ VhdAbstract.rename(handler, parent, child),
69
70
  asyncMap(children.slice(0, -1), child => {
70
71
  onLog(`the VHD ${child} is unused`)
71
72
  if (remove) {
72
73
  onLog(`deleting unused VHD ${child}`)
73
- return handler.unlink(child)
74
+ return VhdAbstract.unlink(handler, child)
74
75
  }
75
76
  }),
76
77
  ])
@@ -137,53 +138,55 @@ exports.cleanVm = async function cleanVm(
137
138
  // remove broken VHDs
138
139
  await asyncMap(vhdsList.vhds, async path => {
139
140
  try {
140
- const vhd = new VhdFile(handler, path)
141
- await vhd.readHeaderAndFooter(!vhdsList.interruptedVhds.has(path))
142
- vhds.add(path)
143
- if (vhd.footer.diskType === DISK_TYPE_DIFFERENCING) {
144
- const parent = resolve('/', dirname(path), vhd.header.parentUnicodeName)
145
- vhdParents[path] = parent
146
- if (parent in vhdChildren) {
147
- const error = new Error('this script does not support multiple VHD children')
148
- error.parent = parent
149
- error.child1 = vhdChildren[parent]
150
- error.child2 = path
151
- throw error // should we throw?
141
+ await Disposable.use(openVhd(handler, path, { checkSecondFooter: !vhdsList.interruptedVhds.has(path) }), vhd => {
142
+ vhds.add(path)
143
+ if (vhd.footer.diskType === DISK_TYPES.DIFFERENCING) {
144
+ const parent = resolve('/', dirname(path), vhd.header.parentUnicodeName)
145
+ vhdParents[path] = parent
146
+ if (parent in vhdChildren) {
147
+ const error = new Error('this script does not support multiple VHD children')
148
+ error.parent = parent
149
+ error.child1 = vhdChildren[parent]
150
+ error.child2 = path
151
+ throw error // should we throw?
152
+ }
153
+ vhdChildren[parent] = path
152
154
  }
153
- vhdChildren[parent] = path
154
- }
155
+ })
155
156
  } catch (error) {
156
157
  onLog(`error while checking the VHD with path ${path}`, { error })
157
158
  if (error?.code === 'ERR_ASSERTION' && remove) {
158
159
  onLog(`deleting broken ${path}`)
159
- await handler.unlink(path)
160
+ return VhdAbstract.unlink(handler, path)
160
161
  }
161
162
  }
162
163
  })
163
164
 
165
+ // @todo : add check for data folder of alias not referenced in a valid alias
166
+
164
167
  // remove VHDs with missing ancestors
165
168
  {
166
169
  const deletions = []
167
170
 
168
171
  // return true if the VHD has been deleted or is missing
169
- const deleteIfOrphan = vhd => {
170
- const parent = vhdParents[vhd]
172
+ const deleteIfOrphan = vhdPath => {
173
+ const parent = vhdParents[vhdPath]
171
174
  if (parent === undefined) {
172
175
  return
173
176
  }
174
177
 
175
178
  // no longer needs to be checked
176
- delete vhdParents[vhd]
179
+ delete vhdParents[vhdPath]
177
180
 
178
181
  deleteIfOrphan(parent)
179
182
 
180
183
  if (!vhds.has(parent)) {
181
- vhds.delete(vhd)
184
+ vhds.delete(vhdPath)
182
185
 
183
- onLog(`the parent ${parent} of the VHD ${vhd} is missing`)
186
+ onLog(`the parent ${parent} of the VHD ${vhdPath} is missing`)
184
187
  if (remove) {
185
- onLog(`deleting orphan VHD ${vhd}`)
186
- deletions.push(handler.unlink(vhd))
188
+ onLog(`deleting orphan VHD ${vhdPath}`)
189
+ deletions.push(VhdAbstract.unlink(handler, vhdPath))
187
190
  }
188
191
  }
189
192
  }
@@ -253,15 +256,26 @@ exports.cleanVm = async function cleanVm(
253
256
  const { vhds } = metadata
254
257
  return Object.keys(vhds).map(key => resolve('/', vmDir, vhds[key]))
255
258
  })()
256
-
257
259
  // FIXME: find better approach by keeping as much of the backup as
258
260
  // possible (existing disks) even if one disk is missing
259
261
  if (linkedVhds.every(_ => vhds.has(_))) {
260
262
  linkedVhds.forEach(_ => unusedVhds.delete(_))
261
263
 
262
- size = await asyncMap(linkedVhds, vhd => handler.getSize(vhd)).then(sum, error => {
263
- onLog(`failed to get size of ${json}`, { error })
264
- })
264
+ // checking the size of a vhd directory is costly
265
+ // 1 Http Query per 1000 blocks
266
+ // we only check size of all the vhd are VhdFiles
267
+
268
+ const shouldComputeSize = linkedVhds.every(vhd => vhd instanceof VhdFile)
269
+ if (shouldComputeSize) {
270
+ try {
271
+ await Disposable.use(Disposable.all(linkedVhds.map(vhdPath => openVhd(handler, vhdPath))), async vhds => {
272
+ const sizes = await asyncMap(vhds, vhd => vhd.getSize())
273
+ size = sum(sizes)
274
+ })
275
+ } catch (error) {
276
+ onLog(`failed to get size of ${json}`, { error })
277
+ }
278
+ }
265
279
  } else {
266
280
  onLog(`Some VHDs linked to the metadata ${json} are missing`)
267
281
  if (remove) {
@@ -324,7 +338,7 @@ exports.cleanVm = async function cleanVm(
324
338
  onLog(`the VHD ${vhd} is unused`)
325
339
  if (remove) {
326
340
  onLog(`deleting unused VHD ${vhd}`)
327
- unusedVhdsDeletion.push(handler.unlink(vhd))
341
+ unusedVhdsDeletion.push(VhdAbstract.unlink(handler, vhd))
328
342
  }
329
343
  }
330
344
 
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "type": "git",
9
9
  "url": "https://github.com/vatesfr/xen-orchestra.git"
10
10
  },
11
- "version": "0.15.1",
11
+ "version": "0.16.0",
12
12
  "engines": {
13
13
  "node": ">=14.6"
14
14
  },
@@ -20,10 +20,10 @@
20
20
  "@vates/disposable": "^0.1.1",
21
21
  "@vates/parse-duration": "^0.1.1",
22
22
  "@xen-orchestra/async-map": "^0.1.2",
23
- "@xen-orchestra/fs": "^0.18.0",
23
+ "@xen-orchestra/fs": "^0.19.1",
24
24
  "@xen-orchestra/log": "^0.3.0",
25
25
  "@xen-orchestra/template": "^0.1.0",
26
- "compare-versions": "^3.6.0",
26
+ "compare-versions": "^4.0.1",
27
27
  "d3-time-format": "^3.0.0",
28
28
  "end-of-stream": "^1.4.4",
29
29
  "fs-extra": "^10.0.0",
@@ -35,7 +35,8 @@
35
35
  "promise-toolbox": "^0.20.0",
36
36
  "proper-lockfile": "^4.1.2",
37
37
  "pump": "^3.0.0",
38
- "vhd-lib": "^1.3.0",
38
+ "uuid": "^8.3.2",
39
+ "vhd-lib": "^2.0.0",
39
40
  "yazl": "^2.5.1"
40
41
  },
41
42
  "peerDependencies": {
@@ -3,7 +3,7 @@ const map = require('lodash/map.js')
3
3
  const mapValues = require('lodash/mapValues.js')
4
4
  const ignoreErrors = require('promise-toolbox/ignoreErrors.js')
5
5
  const { asyncMap } = require('@xen-orchestra/async-map')
6
- const { chainVhd, checkVhdChain, VhdFile } = require('vhd-lib')
6
+ const { chainVhd, checkVhdChain, openVhd, VhdAbstract } = require('vhd-lib')
7
7
  const { createLogger } = require('@xen-orchestra/log')
8
8
  const { dirname } = require('path')
9
9
 
@@ -16,6 +16,7 @@ const { MixinBackupWriter } = require('./_MixinBackupWriter.js')
16
16
  const { AbstractDeltaWriter } = require('./_AbstractDeltaWriter.js')
17
17
  const { checkVhd } = require('./_checkVhd.js')
18
18
  const { packUuid } = require('./_packUuid.js')
19
+ const { Disposable } = require('promise-toolbox')
19
20
 
20
21
  const { warn } = createLogger('xo:backups:DeltaBackupWriter')
21
22
 
@@ -37,13 +38,13 @@ exports.DeltaBackupWriter = class DeltaBackupWriter extends MixinBackupWriter(Ab
37
38
  await asyncMap(vhds, async path => {
38
39
  try {
39
40
  await checkVhdChain(handler, path)
40
-
41
- const vhd = new VhdFile(handler, path)
42
- await vhd.readHeaderAndFooter()
43
- found = found || vhd.footer.uuid.equals(packUuid(baseUuid))
41
+ await Disposable.use(
42
+ openVhd(handler, path),
43
+ vhd => (found = found || vhd.footer.uuid.equals(packUuid(baseUuid)))
44
+ )
44
45
  } catch (error) {
45
46
  warn('checkBaseVdis', { error })
46
- await ignoreErrors.call(handler.unlink(path))
47
+ await ignoreErrors.call(VhdAbstract.unlink(handler, path))
47
48
  }
48
49
  })
49
50
  } catch (error) {
@@ -144,7 +145,7 @@ exports.DeltaBackupWriter = class DeltaBackupWriter extends MixinBackupWriter(Ab
144
145
  // don't do delta for it
145
146
  vdi.uuid
146
147
  : vdi.$snapshot_of$uuid
147
- }/${basename}.vhd`
148
+ }/${adapter.getVhdFileName(basename)}`
148
149
  )
149
150
 
150
151
  const metadataFilename = `${backupDir}/${basename}.json`
@@ -188,7 +189,7 @@ exports.DeltaBackupWriter = class DeltaBackupWriter extends MixinBackupWriter(Ab
188
189
  await checkVhd(handler, parentPath)
189
190
  }
190
191
 
191
- await adapter.outputStream(path, deltaExport.streams[`${id}.vhd`], {
192
+ await adapter.writeVhd(path, deltaExport.streams[`${id}.vhd`], {
192
193
  // no checksum for VHDs, because they will be invalidated by
193
194
  // merges and chainings
194
195
  checksum: false,
@@ -200,11 +201,11 @@ exports.DeltaBackupWriter = class DeltaBackupWriter extends MixinBackupWriter(Ab
200
201
  }
201
202
 
202
203
  // set the correct UUID in the VHD
203
- const vhd = new VhdFile(handler, path)
204
- await vhd.readHeaderAndFooter()
205
- vhd.footer.uuid = packUuid(vdi.uuid)
206
- await vhd.readBlockAllocationTable() // required by writeFooter()
207
- await vhd.writeFooter()
204
+ await Disposable.use(openVhd(handler, path), async vhd => {
205
+ vhd.footer.uuid = packUuid(vdi.uuid)
206
+ await vhd.readBlockAllocationTable() // required by writeFooter()
207
+ await vhd.writeFooter()
208
+ })
208
209
  })
209
210
  )
210
211
  return {
@@ -1,5 +1,6 @@
1
- const Vhd = require('vhd-lib').VhdFile
1
+ const openVhd = require('vhd-lib').openVhd
2
+ const Disposable = require('promise-toolbox/Disposable')
2
3
 
3
4
  exports.checkVhd = async function checkVhd(handler, path) {
4
- await new Vhd(handler, path).readHeaderAndFooter()
5
+ await Disposable.use(openVhd(handler, path), () => {})
5
6
  }