mirror of
https://github.com/vatesfr/xen-orchestra.git
synced 2026-11-30 10:48:06 -06:00
Compare commits
161 Commits
vmk-rest-a
...
stale-obje
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
86cdc3d973 | ||
|
|
208a3d6e4f | ||
|
|
6b775fb52c | ||
|
|
e41e31ef44 | ||
|
|
3cd440dbdf | ||
|
|
94f44bfc0e | ||
|
|
8f62424762 | ||
|
|
f37ffc34ca | ||
|
|
3ac17cbb6b | ||
|
|
1ac84b488c | ||
|
|
8fe26d1c2c | ||
|
|
3c6d3e66e4 | ||
|
|
c8d016d8bd | ||
|
|
8dbf5a5ace | ||
|
|
067b1ac2ef | ||
|
|
1dd7cb4319 | ||
|
|
8fb6a9d8e5 | ||
|
|
932794389d | ||
|
|
1f03e87932 | ||
|
|
0857694312 | ||
|
|
641b13e593 | ||
|
|
9866e0c47c | ||
|
|
6166c9321f | ||
|
|
8b4fd15a86 | ||
|
|
3eb48bdbdb | ||
|
|
83b1ce297f | ||
|
|
30d30a66c7 | ||
|
|
79219f2e1c | ||
|
|
67ed8f7eb3 | ||
|
|
2783e60933 | ||
|
|
7679bb0d44 | ||
|
|
b9f5b0724e | ||
|
|
7d5c818efb | ||
|
|
152365f984 | ||
|
|
83c3e344ba | ||
|
|
56ffcdba29 | ||
|
|
1d3e1fce39 | ||
|
|
4cd316d23b | ||
|
|
6e7a6659ab | ||
|
|
2e67209418 | ||
|
|
03e93498fd | ||
|
|
50166fe3a4 | ||
|
|
724402733b | ||
|
|
abbe24631d | ||
|
|
8e81250ca3 | ||
|
|
c8c11ae63c | ||
|
|
8df236a512 | ||
|
|
8cc3384850 | ||
|
|
e4cc8df323 | ||
|
|
43a7e3d3ac | ||
|
|
2e586f0369 | ||
|
|
93dc079f71 | ||
|
|
9c6a59bb89 | ||
|
|
12aa9576b9 | ||
|
|
85541ce5d7 | ||
|
|
56a33c1639 | ||
|
|
7f6ea616ca | ||
|
|
5601a64703 | ||
|
|
a50776cb7d | ||
|
|
b477167b41 | ||
|
|
909fa94161 | ||
|
|
79e88245da | ||
|
|
4802815bc3 | ||
|
|
7985244daf | ||
|
|
406f4436c6 | ||
|
|
1609d2f825 | ||
|
|
a953ca748d | ||
|
|
5385f9f229 | ||
|
|
092133f811 | ||
|
|
54c52e8f24 | ||
|
|
9e1fc78f84 | ||
|
|
e2ca66130c | ||
|
|
d618707ac3 | ||
|
|
2685f3b95c | ||
|
|
4cfe9e81bd | ||
|
|
3359eb898f | ||
|
|
1ce24b85bd | ||
|
|
c234800468 | ||
|
|
a846ae68dd | ||
|
|
666a99706e | ||
|
|
5dd65197bb | ||
|
|
c2bda04112 | ||
|
|
0be905e7cf | ||
|
|
fd5b54f972 | ||
|
|
248a6e025c | ||
|
|
d4dd0791f2 | ||
|
|
1329b7bc69 | ||
|
|
b9fa292b9d | ||
|
|
767fc2cb76 | ||
|
|
d2ffe1ad56 | ||
|
|
a508ca7897 | ||
|
|
96e789c35e | ||
|
|
f013adf7e4 | ||
|
|
3923756ff5 | ||
|
|
988d466cba | ||
|
|
e0c9112429 | ||
|
|
61f3cbc9ce | ||
|
|
eac0b1e2c9 | ||
|
|
5a384d60c4 | ||
|
|
bc927fdffb | ||
|
|
7c3f861a98 | ||
|
|
0ebc92b50f | ||
|
|
57282c0457 | ||
|
|
ee54d78259 | ||
|
|
10208cbfe0 | ||
|
|
6f5caa409c | ||
|
|
2233bad7a7 | ||
|
|
b630456835 | ||
|
|
c77d22f422 | ||
|
|
191317630d | ||
|
|
d32c9879b1 | ||
|
|
ab4d372ce2 | ||
|
|
5cc6500def | ||
|
|
8a70c4abc0 | ||
|
|
1639a5f6a6 | ||
|
|
d525a7d5bc | ||
|
|
776993e119 | ||
|
|
8ae4ea25b2 | ||
|
|
eecba81f22 | ||
|
|
05aeff101b | ||
|
|
b7af59b6ed | ||
|
|
12e1a10394 | ||
|
|
954a2034e6 | ||
|
|
b819ba1527 | ||
|
|
ff90a80e99 | ||
|
|
cfa75ef652 | ||
|
|
a350c62cc3 | ||
|
|
bb5a79e6c6 | ||
|
|
e4e7c05104 | ||
|
|
52c68a28cc | ||
|
|
635f02bcae | ||
|
|
db5cb01fc9 | ||
|
|
928408fe8e | ||
|
|
db98f15bc1 | ||
|
|
decdf61919 | ||
|
|
6115c74bbe | ||
|
|
025f6eb533 | ||
|
|
3e74189479 | ||
|
|
0695c6b44c | ||
|
|
0bb2f12b2b | ||
|
|
865b6a84c7 | ||
|
|
6b82340fdc | ||
|
|
d846e00dff | ||
|
|
f93bb67409 | ||
|
|
51bb88a1ce | ||
|
|
11335b465b | ||
|
|
295b8838a6 | ||
|
|
e0b0c75682 | ||
|
|
286dafa267 | ||
|
|
b08ad1669f | ||
|
|
4731cbd629 | ||
|
|
1c4890be10 | ||
|
|
4abb9d80bc | ||
|
|
87fe4e2b53 | ||
|
|
6058f07813 | ||
|
|
d451d5c35b | ||
|
|
375b9fe91b | ||
|
|
d53294f15c | ||
|
|
40e5ca9a73 | ||
|
|
cf760d26cd | ||
|
|
d4b85421fe |
@@ -53,6 +53,12 @@ module.exports = {
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
files: ['@xen-orchestra/web-core/**/*.unit.{test,spec}.ts'],
|
||||
rules: {
|
||||
'n/no-unpublished-import': 'off',
|
||||
},
|
||||
},
|
||||
{
|
||||
files: ['@xen-orchestra/{web-core,lite,web}/**/*.{vue,ts}'],
|
||||
parser: 'vue-eslint-parser',
|
||||
|
||||
11
.github/workflows/vectura.yml
vendored
11
.github/workflows/vectura.yml
vendored
@@ -45,8 +45,8 @@ jobs:
|
||||
- name: Hold the coverage floor
|
||||
run: cargo test --locked --test coverage -- --ignored
|
||||
|
||||
# builds the binary as a release does, then checks it and the one in git,
|
||||
# which xo-server runs
|
||||
# builds the binary as release.sh does, checks it, then checks the one in git,
|
||||
# which xo-server runs, is that build
|
||||
binary:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
@@ -65,4 +65,9 @@ jobs:
|
||||
|
||||
- run: scripts/build.sh "$RUNNER_TEMP/build"
|
||||
- run: scripts/check-binary.sh "$RUNNER_TEMP/build/vectura"
|
||||
- run: scripts/check-binary.sh vectura
|
||||
- name: The binary in git is the build of these sources
|
||||
run: |
|
||||
cmp "$RUNNER_TEMP/build/vectura" vectura || {
|
||||
echo '::error::vectura is not the build of these sources: run scripts/build.sh . in vectura/ and commit the binary'
|
||||
exit 1
|
||||
}
|
||||
|
||||
@@ -19,12 +19,12 @@
|
||||
},
|
||||
"main": "./index.mjs",
|
||||
"dependencies": {
|
||||
"@xen-orchestra/fs": "^4.9.5",
|
||||
"@xen-orchestra/fs": "^4.10.0",
|
||||
"@xen-orchestra/log": "^0.7.2",
|
||||
"fuse-native": "^2.2.6",
|
||||
"lru-cache": "^7.14.0",
|
||||
"promise-toolbox": "^0.21.0",
|
||||
"vhd-lib": "^4.16.2"
|
||||
"vhd-lib": "^4.17.0"
|
||||
},
|
||||
"bin": {
|
||||
"xo-fuse-vhd": "./cli.mjs"
|
||||
|
||||
@@ -15,7 +15,7 @@ The target drives login negotiation onto a single, fixed code path:
|
||||
It implements SendTargets discovery, login (`AuthMethod=None` or one-way
|
||||
**CHAP** — see [Authentication](#authentication-chap)), the ~10 SCSI
|
||||
commands a Linux initiator issues to attach and use a block device
|
||||
(`INQUIRY` + VPD `0x00`/`0x80`/`0x83`, `REPORT LUNS`, `READ CAPACITY 10/16`,
|
||||
(`INQUIRY` + VPD `0x00`/`0x80`/`0x83`/`0xb0`, `REPORT LUNS`, `READ CAPACITY 10/16`,
|
||||
`READ`/`WRITE 10/16`, `TEST UNIT READY`, `REQUEST SENSE`, `MODE SENSE`,
|
||||
`SYNCHRONIZE CACHE`), the full R2T / Data-Out write path, NOP keepalive replies,
|
||||
and Logout. Commands may be outstanding concurrently (Data-Out PDUs are routed
|
||||
@@ -28,6 +28,19 @@ The package also ships a userspace **initiator** (`IscsiInitiator`) and an
|
||||
Not implemented (yet): mutual (two-way) CHAP, Task Management (abort/reset),
|
||||
target-initiated NOP keepalives, multiple connections/LUNs.
|
||||
|
||||
What a peer can make the target do is bounded:
|
||||
|
||||
- connections that have not logged in yet are limited in time (`loginTimeoutMs`)
|
||||
and number (`maxPendingConnections`), and `allowedInitiatorAddresses` (the
|
||||
source addresses of the expected initiators) closes the other ones as soon as
|
||||
they are accepted, reporting them to `onConnectionRefused`
|
||||
- a PDU data segment is at most 8 KiB during login, then the declared
|
||||
`MaxRecvDataSegmentLength` (256 KiB), checked before it is read
|
||||
- a single `READ`/`WRITE` is at most `maxTransferLength` (8 MiB by default),
|
||||
advertised in the block limits VPD page so that initiators split their I/O
|
||||
- a `BlockDevice` with `readOnly: true` is reported write-protected, and `WRITE`
|
||||
is refused with `DATA PROTECT` before any data is solicited
|
||||
|
||||
### CLI
|
||||
|
||||
The package ships a `vates-iscsi` binary that serves a file as a LUN:
|
||||
@@ -139,6 +152,8 @@ class RandomAccessDiskLun {
|
||||
}
|
||||
return out
|
||||
}
|
||||
// reported write-protected: WRITE is refused before reaching write()
|
||||
readOnly = true
|
||||
write() {
|
||||
throw new Error('read-only LUN')
|
||||
}
|
||||
|
||||
@@ -18,14 +18,14 @@ The package has two independent roles with very different maturity:
|
||||
|
||||
## Session and connection
|
||||
|
||||
| Aspect | Target | Initiator |
|
||||
| ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------- |
|
||||
| Connections per session | `MaxConnections=1`, enforced | opens exactly one |
|
||||
| Session reinstatement | **no** — no ISID tracking; a new connection always tears down and replaces whatever connection currently exists, rather than RFC 7143's ISID-matched reinstatement (see `index.mts`'s class doc for the reasoning: safe here because the target is ephemeral, single-consumer, and CHAP-guarded) | n/a (single connection) |
|
||||
| Multiple LUNs | **no** — exactly one LUN, always LUN 0 | reads are hardcoded to LUN 0 |
|
||||
| `ErrorRecoveryLevel` | `0` only | requests `0` |
|
||||
| Header/data digests | `HeaderDigest=None`, `DataDigest=None` — no CRC32C on the wire | same |
|
||||
| Markers (`IFMarker`/`OFMarker`) | not offered (RFC-deprecated) | not offered |
|
||||
| Aspect | Target | Initiator |
|
||||
| ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------- |
|
||||
| Connections per session | `MaxConnections=1`, enforced | opens exactly one |
|
||||
| Session reinstatement | **no** — no ISID tracking; once a new connection has established a Normal session (CHAP included), it tears down and replaces whatever connection currently exists, rather than RFC 7143's ISID-matched reinstatement (see `index.mts`'s class doc for the reasoning: safe here because the target is ephemeral, single-consumer, and CHAP-guarded). Until then it does not affect the current one, and is bounded by `loginTimeoutMs` and `maxPendingConnections` | n/a (single connection) |
|
||||
| Multiple LUNs | **no** — exactly one LUN, always LUN 0 | reads are hardcoded to LUN 0 |
|
||||
| `ErrorRecoveryLevel` | `0` only | requests `0` |
|
||||
| Header/data digests | `HeaderDigest=None`, `DataDigest=None` — no CRC32C on the wire | same |
|
||||
| Markers (`IFMarker`/`OFMarker`) | not offered (RFC-deprecated) | not offered |
|
||||
|
||||
## Login / text negotiation
|
||||
|
||||
@@ -72,17 +72,17 @@ keys if it declines transit on the first exchange.
|
||||
Decoded explicitly (`types.mts#decodeCdb`); anything else maps to `unsupported` → `CHECK CONDITION` /
|
||||
`ILLEGAL_REQUEST` / `INVALID COMMAND OPERATION CODE`:
|
||||
|
||||
| Command | Variants |
|
||||
| ------------------- | ---------------------------------------------------------------- |
|
||||
| `INQUIRY` | standard + EVPD (vendor/product/revision/serial from `identity`) |
|
||||
| `REPORT LUNS` | |
|
||||
| `READ CAPACITY` | `(10)` and `(16)` |
|
||||
| `READ` | `(10)` and `(16)` |
|
||||
| `WRITE` | `(10)` and `(16)` |
|
||||
| `TEST UNIT READY` | |
|
||||
| `REQUEST SENSE` | |
|
||||
| `MODE SENSE` | `(6)` and `(10)` |
|
||||
| `SYNCHRONIZE CACHE` | `(10)` and `(16)` — flushes the backing `BlockDevice` |
|
||||
| Command | Variants |
|
||||
| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `INQUIRY` | standard + EVPD `0x00`/`0x80`/`0x83` (vendor/product/revision/serial from `identity`) and `0xb0` (block limits: `maxTransferLength`) |
|
||||
| `REPORT LUNS` | |
|
||||
| `READ CAPACITY` | `(10)` and `(16)` |
|
||||
| `READ` | `(10)` and `(16)`; longer than `maxTransferLength` → `INVALID FIELD IN CDB` |
|
||||
| `WRITE` | `(10)` and `(16)`; longer than `maxTransferLength` → `INVALID FIELD IN CDB`; on a `readOnly` LUN → `DATA PROTECT` / `WRITE PROTECTED`, without soliciting data |
|
||||
| `TEST UNIT READY` | |
|
||||
| `REQUEST SENSE` | |
|
||||
| `MODE SENSE` | `(6)` and `(10)`; `WP` set on a `readOnly` LUN |
|
||||
| `SYNCHRONIZE CACHE` | `(10)` and `(16)` — flushes the backing `BlockDevice` |
|
||||
|
||||
**Not implemented**: persistent reservations, VAAI/thin-provisioning primitives (`UNMAP`, `WRITE SAME`, `COMPARE
|
||||
AND WRITE`, ...), any vendor-specific commands — anything beyond the ~10 commands a Linux initiator actually
|
||||
|
||||
@@ -33,7 +33,7 @@ The target drives login negotiation onto a single, fixed code path:
|
||||
It implements SendTargets discovery, login (`AuthMethod=None` or one-way
|
||||
**CHAP** — see [Authentication](#authentication-chap)), the ~10 SCSI
|
||||
commands a Linux initiator issues to attach and use a block device
|
||||
(`INQUIRY` + VPD `0x00`/`0x80`/`0x83`, `REPORT LUNS`, `READ CAPACITY 10/16`,
|
||||
(`INQUIRY` + VPD `0x00`/`0x80`/`0x83`/`0xb0`, `REPORT LUNS`, `READ CAPACITY 10/16`,
|
||||
`READ`/`WRITE 10/16`, `TEST UNIT READY`, `REQUEST SENSE`, `MODE SENSE`,
|
||||
`SYNCHRONIZE CACHE`), the full R2T / Data-Out write path, NOP keepalive replies,
|
||||
and Logout. Commands may be outstanding concurrently (Data-Out PDUs are routed
|
||||
@@ -46,6 +46,19 @@ The package also ships a userspace **initiator** (`IscsiInitiator`) and an
|
||||
Not implemented (yet): mutual (two-way) CHAP, Task Management (abort/reset),
|
||||
target-initiated NOP keepalives, multiple connections/LUNs.
|
||||
|
||||
What a peer can make the target do is bounded:
|
||||
|
||||
- connections that have not logged in yet are limited in time (`loginTimeoutMs`)
|
||||
and number (`maxPendingConnections`), and `allowedInitiatorAddresses` (the
|
||||
source addresses of the expected initiators) closes the other ones as soon as
|
||||
they are accepted, reporting them to `onConnectionRefused`
|
||||
- a PDU data segment is at most 8 KiB during login, then the declared
|
||||
`MaxRecvDataSegmentLength` (256 KiB), checked before it is read
|
||||
- a single `READ`/`WRITE` is at most `maxTransferLength` (8 MiB by default),
|
||||
advertised in the block limits VPD page so that initiators split their I/O
|
||||
- a `BlockDevice` with `readOnly: true` is reported write-protected, and `WRITE`
|
||||
is refused with `DATA PROTECT` before any data is solicited
|
||||
|
||||
### CLI
|
||||
|
||||
The package ships a `vates-iscsi` binary that serves a file as a LUN:
|
||||
@@ -157,6 +170,8 @@ class RandomAccessDiskLun {
|
||||
}
|
||||
return out
|
||||
}
|
||||
// reported write-protected: WRITE is refused before reaching write()
|
||||
readOnly = true
|
||||
write() {
|
||||
throw new Error('read-only LUN')
|
||||
}
|
||||
|
||||
@@ -35,6 +35,11 @@ VM boot mechanics, and deployment specifics live elsewhere and aren't reproduced
|
||||
- Re-verified end-to-end after deploying the fix: same S3-backed boot scenario completed successfully
|
||||
(~5 min total, vs ~20-25s for the equivalent local-storage boot — S3 itself is slow, but the mount no
|
||||
longer wedges).
|
||||
- Refined afterwards: the replacement used to happen as soon as the TCP connection was accepted, so any
|
||||
peer reaching the port (a port scan, a failed CHAP login, a Discovery session) interrupted the session in
|
||||
use. It now only happens once the new connection has established a Normal session; until then,
|
||||
connections are bounded by `loginTimeoutMs` and `maxPendingConnections`. Regression tests:
|
||||
`loopback.integ.mts`, `describe('connection admission', ...)`.
|
||||
|
||||
### Independently landed, cross-referenced against this pass's coverage
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@vates/iscsi",
|
||||
"version": "0.2.0",
|
||||
"version": "0.4.0",
|
||||
"main": "dist/index.mjs",
|
||||
"types": "dist/index.d.mts",
|
||||
"bin": {
|
||||
@@ -23,7 +23,7 @@
|
||||
"dependencies": {
|
||||
"@vates/async-each": "^1.0.3",
|
||||
"@vates/read-chunk": "^1.2.1",
|
||||
"@xen-orchestra/disk-transform": "^1.3.0",
|
||||
"@xen-orchestra/disk-transform": "^1.3.4",
|
||||
"@xen-orchestra/log": "^0.7.2",
|
||||
"promise-toolbox": "^0.21.0"
|
||||
},
|
||||
|
||||
@@ -102,10 +102,10 @@ export class DiskBlockDevice implements BlockDevice {
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Always throws: the source disk is read-only. The target answers CHECK
|
||||
* CONDITION / MEDIUM ERROR, which initiators surface as an I/O error.
|
||||
*/
|
||||
// the target refuses WRITE commands before they reach `write()`
|
||||
readonly readOnly = true
|
||||
|
||||
/** Always throws: the source disk is read-only. */
|
||||
async write(): Promise<void> {
|
||||
throw new Error('DiskBlockDevice is read-only')
|
||||
}
|
||||
|
||||
129
@vates/iscsi/src/backend.integ.mts
Normal file
129
@vates/iscsi/src/backend.integ.mts
Normal file
@@ -0,0 +1,129 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { execFile, execFileSync } from 'node:child_process'
|
||||
import { mkdtemp, rm, truncate, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { promisify } from 'node:util'
|
||||
import { after, before, describe, it } from 'node:test'
|
||||
|
||||
import { FileBlockDevice } from './index.mjs'
|
||||
|
||||
const execFileP = promisify(execFile)
|
||||
|
||||
const BLOCK_SIZE = 512
|
||||
const SIZE = 4 * 1024 * 1024 // 4 MiB
|
||||
|
||||
/**
|
||||
* Why this cannot run here, or `false` if it can. A real block device is the
|
||||
* whole point, and the cheapest one to make is a loop device — which needs root
|
||||
* and `losetup`.
|
||||
*/
|
||||
function skipReason(): string | false {
|
||||
if (process.getuid === undefined || process.getuid() !== 0) {
|
||||
return 'requires root to attach a loop device'
|
||||
}
|
||||
try {
|
||||
execFileSync('losetup', ['--version'], { stdio: 'ignore' })
|
||||
} catch {
|
||||
return 'requires util-linux (losetup not found)'
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
const bytePattern = (i: number) => (i * 31 + 11) & 0xff
|
||||
|
||||
describe('FileBlockDevice on a loop device', { skip: skipReason() }, () => {
|
||||
let dir: string
|
||||
let devicePath: string
|
||||
|
||||
before(async () => {
|
||||
dir = await mkdtemp(join(tmpdir(), 'vates-iscsi-loop-'))
|
||||
const backing = join(dir, 'backing.img')
|
||||
await writeFile(backing, '')
|
||||
await truncate(backing, SIZE)
|
||||
const { stdout } = await execFileP('losetup', ['--find', '--show', backing])
|
||||
devicePath = stdout.trim()
|
||||
})
|
||||
|
||||
after(async () => {
|
||||
if (devicePath !== undefined) {
|
||||
await execFileP('losetup', ['--detach', devicePath])
|
||||
}
|
||||
await rm(dir, { force: true, recursive: true })
|
||||
})
|
||||
|
||||
const withDevice = async (fn: (device: FileBlockDevice) => Promise<void>): Promise<void> => {
|
||||
const device = new FileBlockDevice({ path: devicePath, size: SIZE })
|
||||
await device.open()
|
||||
try {
|
||||
await fn(device)
|
||||
} finally {
|
||||
await device.close()
|
||||
}
|
||||
}
|
||||
|
||||
it('accepts the device node and reports the capacity it was given', async () => {
|
||||
await withDevice(async device => {
|
||||
assert.equal(device.getSize(), SIZE)
|
||||
assert.equal(device.getBlockSize(), BLOCK_SIZE)
|
||||
})
|
||||
})
|
||||
|
||||
it('round-trips block-aligned data', async () => {
|
||||
await withDevice(async device => {
|
||||
const payload = Buffer.alloc(8 * BLOCK_SIZE)
|
||||
for (let i = 0; i < payload.length; i++) {
|
||||
payload[i] = bytePattern(i)
|
||||
}
|
||||
await device.write(16 * BLOCK_SIZE, payload)
|
||||
assert.deepEqual(await device.read(16 * BLOCK_SIZE, payload.length), payload)
|
||||
})
|
||||
})
|
||||
|
||||
it('round-trips data straddling block boundaries, at an unaligned offset', async () => {
|
||||
await withDevice(async device => {
|
||||
const payload = Buffer.from('a payload that is not a whole number of sectors long')
|
||||
const offset = 3 * BLOCK_SIZE + 17
|
||||
await device.write(offset, payload)
|
||||
assert.deepEqual(await device.read(offset, payload.length), payload)
|
||||
// the surrounding sector was read-modify-written by the kernel, not clobbered
|
||||
assert.deepEqual(await device.read(3 * BLOCK_SIZE, 17), Buffer.alloc(17))
|
||||
})
|
||||
})
|
||||
|
||||
it('survives a reopen, which is what makes it a cache', async () => {
|
||||
const payload = Buffer.alloc(BLOCK_SIZE, 0x5a)
|
||||
await withDevice(device => device.write(2 * BLOCK_SIZE, payload))
|
||||
await withDevice(async device => {
|
||||
await device.flush()
|
||||
assert.deepEqual(await device.read(2 * BLOCK_SIZE, BLOCK_SIZE), payload)
|
||||
})
|
||||
})
|
||||
|
||||
it('reads a large range in one call', async () => {
|
||||
await withDevice(async device => {
|
||||
const payload = Buffer.alloc(SIZE / 2)
|
||||
for (let i = 0; i < payload.length; i++) {
|
||||
payload[i] = bytePattern(i + 7)
|
||||
}
|
||||
await device.write(0, payload)
|
||||
assert.deepEqual(await device.read(0, payload.length), payload)
|
||||
})
|
||||
})
|
||||
|
||||
it('throws on a read running past the end, instead of zero-filling it', async () => {
|
||||
// the behaviour that sets the block device mode apart: a short read on a
|
||||
// device of a known size is an I/O error, never a sparse tail
|
||||
await withDevice(async device => {
|
||||
await assert.rejects(device.read(SIZE, BLOCK_SIZE), /ended after 0 bytes/)
|
||||
await assert.rejects(device.read(SIZE - BLOCK_SIZE, 2 * BLOCK_SIZE), /ended after 512 bytes/)
|
||||
})
|
||||
})
|
||||
|
||||
it('flushes without error', async () => {
|
||||
await withDevice(async device => {
|
||||
await device.write(0, Buffer.alloc(BLOCK_SIZE, 0xff))
|
||||
await device.flush()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -16,6 +16,11 @@ export interface BlockDevice {
|
||||
getSize(): number
|
||||
/** Logical block size in bytes (typically 512). */
|
||||
getBlockSize(): number
|
||||
/**
|
||||
* When true, the LUN is reported as write-protected and WRITE commands are
|
||||
* refused up front (DATA PROTECT), without soliciting their data.
|
||||
*/
|
||||
readonly readOnly?: boolean
|
||||
/** Read exactly `length` bytes starting at byte `offset`. */
|
||||
read(offset: number, length: number): Promise<Buffer>
|
||||
/** Write `data` starting at byte `offset`. */
|
||||
@@ -29,31 +34,60 @@ export interface BlockDevice {
|
||||
const DEFAULT_BLOCK_SIZE = 512
|
||||
|
||||
export interface FileBlockDeviceOptions {
|
||||
/** Path to the backing file. It must already exist and be sized to capacity. */
|
||||
/**
|
||||
* Path to the backing file. It must already exist and be sized to capacity —
|
||||
* or be a block device node, see {@link FileBlockDeviceOptions.size}.
|
||||
*/
|
||||
readonly path: string
|
||||
/** Logical block size in bytes. Defaults to 512. */
|
||||
readonly blockSize?: number
|
||||
/**
|
||||
* Capacity in bytes, for a block device node (e.g. a VDI hot-plugged onto the
|
||||
* appliance's VM, `/dev/xvdX`), whose inode reports a size of 0.
|
||||
*
|
||||
* Setting it switches to the block device mode:
|
||||
* - `path` must be a block device: a typo naming the wrong node is the
|
||||
* difference between a cache and a destroyed disk;
|
||||
* - a short read is an I/O error, not a sparse tail: zero-filling it would hand
|
||||
* the initiator fabricated data.
|
||||
*/
|
||||
readonly size?: number
|
||||
}
|
||||
|
||||
/**
|
||||
* A {@link BlockDevice} backed by a regular (sparse) file opened read/write.
|
||||
* A {@link BlockDevice} backed by a regular (sparse) file, or a block device
|
||||
* node, opened read/write.
|
||||
*
|
||||
* The file must already exist and be sized to the desired capacity (e.g. via
|
||||
* `fs.truncate`); this keeps the device a pure data store with no provisioning
|
||||
* policy of its own.
|
||||
*
|
||||
* I/O is buffered, never `O_DIRECT`, which would need buffers aligned to the
|
||||
* logical block size that Node cannot allocate.
|
||||
*/
|
||||
export class FileBlockDevice implements BlockDevice {
|
||||
readonly #path: string
|
||||
readonly #blockSize: number
|
||||
readonly #isBlockDevice: boolean
|
||||
#handle?: FileHandle
|
||||
#size = 0
|
||||
|
||||
constructor({ path, blockSize = DEFAULT_BLOCK_SIZE }: FileBlockDeviceOptions) {
|
||||
constructor({ path, blockSize = DEFAULT_BLOCK_SIZE, size }: FileBlockDeviceOptions) {
|
||||
if (!Number.isInteger(blockSize) || blockSize <= 0) {
|
||||
throw new Error(`blockSize must be a positive integer, got ${blockSize}`)
|
||||
}
|
||||
if (size !== undefined) {
|
||||
if (!Number.isInteger(size) || size <= 0) {
|
||||
throw new Error(`size must be a positive integer, got ${size}`)
|
||||
}
|
||||
if (size % blockSize !== 0) {
|
||||
throw new Error(`size (${size}) is not a multiple of block size (${blockSize})`)
|
||||
}
|
||||
this.#size = size
|
||||
}
|
||||
this.#path = path
|
||||
this.#blockSize = blockSize
|
||||
this.#isBlockDevice = size !== undefined
|
||||
}
|
||||
|
||||
/** Open the backing file and read its current size. Must be called before any I/O. */
|
||||
@@ -63,12 +97,18 @@ export class FileBlockDevice implements BlockDevice {
|
||||
}
|
||||
const handle = await open(this.#path, 'r+')
|
||||
try {
|
||||
const { size } = await handle.stat()
|
||||
if (size % this.#blockSize !== 0) {
|
||||
throw new Error(`backing file size (${size}) is not a multiple of block size (${this.#blockSize})`)
|
||||
const stats = await handle.stat()
|
||||
if (this.#isBlockDevice) {
|
||||
if (!stats.isBlockDevice()) {
|
||||
throw new Error(`${this.#path} is not a block device`)
|
||||
}
|
||||
} else {
|
||||
if (stats.size % this.#blockSize !== 0) {
|
||||
throw new Error(`backing file size (${stats.size}) is not a multiple of block size (${this.#blockSize})`)
|
||||
}
|
||||
this.#size = stats.size
|
||||
}
|
||||
this.#handle = handle
|
||||
this.#size = size
|
||||
} catch (error) {
|
||||
await handle.close()
|
||||
throw error
|
||||
@@ -99,6 +139,10 @@ export class FileBlockDevice implements BlockDevice {
|
||||
while (read < length) {
|
||||
const { bytesRead } = await handle.read(buffer, read, length - read, offset + read)
|
||||
if (bytesRead === 0) {
|
||||
if (this.#isBlockDevice) {
|
||||
// the device is exactly as long as the caller said: the I/O failed
|
||||
throw new Error(`read of ${length} bytes at ${offset} on ${this.#path} ended after ${read} bytes`)
|
||||
}
|
||||
// Reading past EOF (sparse tail): zero-fill the remainder.
|
||||
buffer.fill(0, read)
|
||||
break
|
||||
|
||||
173
@vates/iscsi/src/backend.test.mts
Normal file
173
@vates/iscsi/src/backend.test.mts
Normal file
@@ -0,0 +1,173 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { describe, it } from 'node:test'
|
||||
import { mkdtemp, rm, truncate, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
|
||||
import { FileBlockDevice } from './backend.mjs'
|
||||
|
||||
const BLOCK_SIZE = 512
|
||||
|
||||
/** A temporary directory removed whatever the test does. */
|
||||
const withTmpDir = async (fn: (dir: string) => Promise<void>): Promise<void> => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'vates-iscsi-backend-'))
|
||||
try {
|
||||
await fn(dir)
|
||||
} finally {
|
||||
await rm(dir, { force: true, recursive: true })
|
||||
}
|
||||
}
|
||||
|
||||
/** A sparse file of `size` bytes, as the class requires: existing and pre-sized. */
|
||||
const makeFile = async (dir: string, size: number, name = 'lun.img'): Promise<string> => {
|
||||
const path = join(dir, name)
|
||||
await writeFile(path, '')
|
||||
await truncate(path, size)
|
||||
return path
|
||||
}
|
||||
|
||||
describe('FileBlockDevice', () => {
|
||||
it('rejects a non-positive-integer block size', () => {
|
||||
assert.throws(() => new FileBlockDevice({ path: '/nope', blockSize: 0 }), /positive integer/)
|
||||
assert.throws(() => new FileBlockDevice({ path: '/nope', blockSize: -1 }), /positive integer/)
|
||||
assert.throws(() => new FileBlockDevice({ path: '/nope', blockSize: 1.5 }), /positive integer/)
|
||||
})
|
||||
|
||||
it('reads the capacity from the backing file', async () => {
|
||||
await withTmpDir(async dir => {
|
||||
const device = new FileBlockDevice({ path: await makeFile(dir, 4 * BLOCK_SIZE) })
|
||||
await device.open()
|
||||
try {
|
||||
assert.equal(device.getSize(), 4 * BLOCK_SIZE)
|
||||
assert.equal(device.getBlockSize(), BLOCK_SIZE)
|
||||
} finally {
|
||||
await device.close()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('refuses a file whose size is not a multiple of the block size', async () => {
|
||||
await withTmpDir(async dir => {
|
||||
const device = new FileBlockDevice({ path: await makeFile(dir, BLOCK_SIZE + 1) })
|
||||
await assert.rejects(device.open(), /not a multiple of block size/)
|
||||
})
|
||||
})
|
||||
|
||||
it('throws on I/O before open()', async () => {
|
||||
const device = new FileBlockDevice({ path: '/nope' })
|
||||
await assert.rejects(device.read(0, BLOCK_SIZE), /must be called before I\/O/)
|
||||
await assert.rejects(device.write(0, Buffer.alloc(BLOCK_SIZE)), /must be called before I\/O/)
|
||||
await assert.rejects(device.flush(), /must be called before I\/O/)
|
||||
})
|
||||
|
||||
it('round-trips a write, at an offset and unaligned', async () => {
|
||||
await withTmpDir(async dir => {
|
||||
const device = new FileBlockDevice({ path: await makeFile(dir, 4 * BLOCK_SIZE) })
|
||||
await device.open()
|
||||
try {
|
||||
const payload = Buffer.from('hello, lun')
|
||||
await device.write(BLOCK_SIZE + 3, payload)
|
||||
assert.deepEqual(await device.read(BLOCK_SIZE + 3, payload.length), payload)
|
||||
|
||||
// the bytes around it are untouched
|
||||
assert.deepEqual(await device.read(BLOCK_SIZE, 3), Buffer.alloc(3))
|
||||
} finally {
|
||||
await device.close()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('zero-fills the sparse tail of a read past EOF', async () => {
|
||||
await withTmpDir(async dir => {
|
||||
// a hole: nothing was ever written, so the whole file reads as zeroes
|
||||
const device = new FileBlockDevice({ path: await makeFile(dir, 2 * BLOCK_SIZE) })
|
||||
await device.open()
|
||||
try {
|
||||
assert.deepEqual(await device.read(0, 2 * BLOCK_SIZE), Buffer.alloc(2 * BLOCK_SIZE))
|
||||
|
||||
// past the end of the file, `fh.read` reports 0 bytes read: the remainder is
|
||||
// zero-filled rather than being reported as an error
|
||||
assert.deepEqual(await device.read(2 * BLOCK_SIZE, BLOCK_SIZE), Buffer.alloc(BLOCK_SIZE))
|
||||
} finally {
|
||||
await device.close()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('reads back a large buffer in full', async () => {
|
||||
await withTmpDir(async dir => {
|
||||
const size = 1024 * BLOCK_SIZE
|
||||
const device = new FileBlockDevice({ path: await makeFile(dir, size) })
|
||||
await device.open()
|
||||
try {
|
||||
const payload = Buffer.alloc(size)
|
||||
for (let i = 0; i < payload.length; i++) {
|
||||
payload[i] = (i * 31 + 11) & 0xff
|
||||
}
|
||||
await device.write(0, payload)
|
||||
assert.deepEqual(await device.read(0, size), payload)
|
||||
} finally {
|
||||
await device.close()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('flushes and closes, and tolerates being opened or closed twice', async () => {
|
||||
await withTmpDir(async dir => {
|
||||
const device = new FileBlockDevice({ path: await makeFile(dir, BLOCK_SIZE) })
|
||||
await device.open()
|
||||
await device.open() // no-op
|
||||
await device.write(0, Buffer.alloc(BLOCK_SIZE, 0xaa))
|
||||
await device.flush()
|
||||
await device.close()
|
||||
await device.close() // no-op
|
||||
await assert.rejects(device.read(0, BLOCK_SIZE), /must be called before I\/O/)
|
||||
})
|
||||
})
|
||||
|
||||
it('leaves no handle behind when open() fails', async () => {
|
||||
await withTmpDir(async dir => {
|
||||
const device = new FileBlockDevice({ path: await makeFile(dir, BLOCK_SIZE + 1) })
|
||||
await assert.rejects(device.open())
|
||||
// the handle opened before the size check was closed again
|
||||
await assert.rejects(device.read(0, BLOCK_SIZE), /must be called before I\/O/)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// A real device node cannot be created without root, so what is covered here is
|
||||
// everything around the I/O: the guards, the capacity, and the lifecycle. The
|
||||
// I/O itself is covered by `backend.integ.mts`, against a loop device.
|
||||
describe('FileBlockDevice with a size, over a block device', () => {
|
||||
it('rejects a non-positive-integer size', () => {
|
||||
for (const size of [0, -BLOCK_SIZE, 1.5]) {
|
||||
assert.throws(() => new FileBlockDevice({ path: '/dev/null', size }), /size must be a positive integer/)
|
||||
}
|
||||
})
|
||||
|
||||
it('rejects a size that is not a multiple of the block size', () => {
|
||||
assert.throws(
|
||||
() => new FileBlockDevice({ path: '/dev/null', size: BLOCK_SIZE + 1 }),
|
||||
/is not a multiple of block size/
|
||||
)
|
||||
})
|
||||
|
||||
it('reports the capacity it was given, never the inode', () => {
|
||||
// nothing may be read from the inode, which for a real device node reports 0
|
||||
const device = new FileBlockDevice({ path: '/dev/null', size: 4 * BLOCK_SIZE })
|
||||
assert.equal(device.getSize(), 4 * BLOCK_SIZE)
|
||||
assert.equal(device.getBlockSize(), BLOCK_SIZE)
|
||||
})
|
||||
|
||||
it('refuses to open a path that is not a block device', async () => {
|
||||
await withTmpDir(async dir => {
|
||||
const path = await makeFile(dir, 4 * BLOCK_SIZE)
|
||||
const device = new FileBlockDevice({ path, size: 4 * BLOCK_SIZE })
|
||||
// the guard against a misconfigured path naming, say, a real disk's
|
||||
// filesystem instead of the cache device
|
||||
await assert.rejects(device.open(), new RegExp(`${path} is not a block device`))
|
||||
// and the handle it opened to check was closed again
|
||||
await assert.rejects(device.read(0, BLOCK_SIZE), /must be called before I\/O/)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -24,7 +24,13 @@ import {
|
||||
TEXT_FLAG_FINAL,
|
||||
} from './constants.mjs'
|
||||
import { buildFixedSense, handleScsiCommand, type ScsiIdentity } from './scsi.mjs'
|
||||
import { buildSendTargetsResponse, LoginNegotiator, parseTextKeys } from './login.mjs'
|
||||
import {
|
||||
buildSendTargetsResponse,
|
||||
LOGIN_MAX_DATA_SEGMENT_LENGTH,
|
||||
LoginNegotiator,
|
||||
OUR_MAX_RECV_DATA_SEGMENT_LENGTH,
|
||||
parseTextKeys,
|
||||
} from './login.mjs'
|
||||
import { allocBhs, assemblePdu, type IncomingPdu, readPdu, writePdu } from './pdu.mjs'
|
||||
import {
|
||||
decodeCdb,
|
||||
@@ -81,6 +87,16 @@ export interface ConnectionDeps {
|
||||
allocateTsih(): number
|
||||
/** When set, require one-way CHAP: the target challenges and verifies the initiator. */
|
||||
readonly chap?: ChapCredentials
|
||||
/**
|
||||
* Time allowed to establish a Normal session, in ms (0 or unset disables):
|
||||
* past it, the connection is closed. Also bounds Discovery sessions, which
|
||||
* never become Normal ones.
|
||||
*/
|
||||
readonly loginTimeoutMs?: number
|
||||
/** Called once this connection has established a Normal session. */
|
||||
onSessionEstablished?(connection: Connection): void
|
||||
/** Max length of a single READ or WRITE, in bytes. */
|
||||
readonly maxTransferLength?: number
|
||||
}
|
||||
|
||||
type Phase = 'login' | 'fullFeature' | 'closed'
|
||||
@@ -123,6 +139,8 @@ export class Connection implements CommandContext {
|
||||
readonly #pendingWrites = new Map<number, PendingWrite>()
|
||||
#nextTargetTransferTag = 1
|
||||
|
||||
#loginTimer?: ReturnType<typeof setTimeout>
|
||||
|
||||
constructor(socket: Socket, deps: ConnectionDeps) {
|
||||
this.#socket = socket
|
||||
this.#deps = deps
|
||||
@@ -142,9 +160,19 @@ export class Connection implements CommandContext {
|
||||
|
||||
/** Read and service PDUs until the peer logs out or the connection drops. */
|
||||
async serve(): Promise<void> {
|
||||
const { loginTimeoutMs = 0 } = this.#deps
|
||||
if (loginTimeoutMs > 0) {
|
||||
this.#loginTimer = setTimeout(() => {
|
||||
log.warn('no session established in time, closing the connection', { remote: this.#socket.remoteAddress })
|
||||
this.destroy()
|
||||
}, loginTimeoutMs).unref()
|
||||
}
|
||||
try {
|
||||
for (;;) {
|
||||
const pdu = await readPdu(this.#socket)
|
||||
const pdu = await readPdu(
|
||||
this.#socket,
|
||||
this.#phase === 'login' ? LOGIN_MAX_DATA_SEGMENT_LENGTH : OUR_MAX_RECV_DATA_SEGMENT_LENGTH
|
||||
)
|
||||
if (pdu === null) {
|
||||
break // peer closed the connection
|
||||
}
|
||||
@@ -156,6 +184,7 @@ export class Connection implements CommandContext {
|
||||
} catch (error) {
|
||||
this.#fail(error)
|
||||
} finally {
|
||||
clearTimeout(this.#loginTimer)
|
||||
this.#phase = 'closed'
|
||||
this.#socket.destroy()
|
||||
}
|
||||
@@ -299,6 +328,13 @@ export class Connection implements CommandContext {
|
||||
bhs[37] = statusDetail
|
||||
await this.#send(assemblePdu(bhs, data))
|
||||
|
||||
// destroyed while the response was being written (login timeout, target
|
||||
// closing): the write still succeeds, but this connection must not go on
|
||||
// and replace the current session
|
||||
if (this.#phase === 'closed') {
|
||||
return true
|
||||
}
|
||||
|
||||
if (statusClass !== LoginStatusClass.SUCCESS) {
|
||||
// RFC 7143 §6.3: the failure response goes out first (above), then the
|
||||
// target closes the connection.
|
||||
@@ -316,6 +352,10 @@ export class Connection implements CommandContext {
|
||||
sessionType: this.#sessionType,
|
||||
target: this.#login.targetName,
|
||||
})
|
||||
if (this.#sessionType === 'Normal') {
|
||||
clearTimeout(this.#loginTimer)
|
||||
this.#deps.onSessionEstablished?.(this)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -389,13 +429,13 @@ export class Connection implements CommandContext {
|
||||
// Not awaited: the read loop must move on immediately rather than block on
|
||||
// this `lun.read()`. See the class doc for why no extra ordering is needed.
|
||||
void this.#readGate
|
||||
.run(() => handleScsiCommand(cdb, pdu.itt, this, this.#deps.identity))
|
||||
.run(() => handleScsiCommand(cdb, pdu.itt, this, this.#deps.identity, this.#deps.maxTransferLength))
|
||||
.catch(error => {
|
||||
this.#fail(error)
|
||||
})
|
||||
return
|
||||
}
|
||||
await handleScsiCommand(cdb, pdu.itt, this, this.#deps.identity)
|
||||
await handleScsiCommand(cdb, pdu.itt, this, this.#deps.identity, this.#deps.maxTransferLength)
|
||||
}
|
||||
|
||||
async #sendReject(pdu: IncomingPdu, reason: number): Promise<void> {
|
||||
|
||||
@@ -166,6 +166,7 @@ export const SenseKey = {
|
||||
HARDWARE_ERROR: 0x04,
|
||||
ILLEGAL_REQUEST: 0x05,
|
||||
UNIT_ATTENTION: 0x06,
|
||||
DATA_PROTECT: 0x07,
|
||||
} as const
|
||||
|
||||
/** Additional Sense Code / Qualifier pairs we may emit. */
|
||||
@@ -174,6 +175,7 @@ export const Asc = {
|
||||
INVALID_COMMAND_OPERATION_CODE: { asc: 0x20, ascq: 0x00 },
|
||||
INVALID_FIELD_IN_CDB: { asc: 0x24, ascq: 0x00 },
|
||||
LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE: { asc: 0x21, ascq: 0x00 },
|
||||
WRITE_PROTECTED: { asc: 0x27, ascq: 0x00 },
|
||||
} as const
|
||||
|
||||
/** Reject reason codes (Reject PDU byte 2). */
|
||||
|
||||
@@ -3,7 +3,7 @@ import { createLogger, type Logger } from '@xen-orchestra/log'
|
||||
|
||||
import type { BlockDevice } from './backend.mjs'
|
||||
import { Connection, type ConnectionDeps } from './connection.mjs'
|
||||
import type { ScsiIdentity } from './scsi.mjs'
|
||||
import { DEFAULT_MAX_TRANSFER_LENGTH, type ScsiIdentity } from './scsi.mjs'
|
||||
import type { ChapCredentials } from './types.mjs'
|
||||
|
||||
export { FileBlockDevice } from './backend.mjs'
|
||||
@@ -25,6 +25,8 @@ const DEFAULT_PORT = 3260
|
||||
const DEFAULT_WRITE_TIMEOUT_MS = 30_000
|
||||
const DEFAULT_CMD_WINDOW = 64
|
||||
const DEFAULT_READ_CONCURRENCY = 16
|
||||
const DEFAULT_LOGIN_TIMEOUT_MS = 15_000
|
||||
const DEFAULT_MAX_PENDING_CONNECTIONS = 8
|
||||
|
||||
const DEFAULT_IDENTITY: ScsiIdentity = {
|
||||
vendor: 'VATES',
|
||||
@@ -61,18 +63,62 @@ export interface IscsiTargetOptions {
|
||||
* open-iscsi `node.session.auth` username/password). Omit for no authentication.
|
||||
*/
|
||||
readonly chap?: ChapCredentials
|
||||
/**
|
||||
* Time a new connection has to establish a Normal session, in ms (0
|
||||
* disables). Also bounds Discovery sessions. Defaults to 15000.
|
||||
*/
|
||||
readonly loginTimeoutMs?: number
|
||||
/**
|
||||
* Max number of connections that have not established a Normal session yet;
|
||||
* past it, new ones are refused. Defaults to 8.
|
||||
*/
|
||||
readonly maxPendingConnections?: number
|
||||
/**
|
||||
* Source addresses the initiators may connect from. When set, connections
|
||||
* from any other address are closed as soon as they are accepted, before any
|
||||
* login: only these initiators can take a pending connection slot.
|
||||
* IPv4-mapped IPv6 addresses (`::ffff:a.b.c.d`) match their IPv4 form.
|
||||
*/
|
||||
readonly allowedInitiatorAddresses?: Iterable<string>
|
||||
/**
|
||||
* Called with the source address of each connection closed because it is
|
||||
* not in `allowedInitiatorAddresses`, e.g. to tell why an initiator behind a
|
||||
* NAT cannot connect.
|
||||
*/
|
||||
readonly onConnectionRefused?: (address: string) => void
|
||||
/**
|
||||
* Max length of a single READ or WRITE, in bytes, advertised in the block
|
||||
* limits VPD page; longer commands are refused. At least one logical block.
|
||||
* Defaults to 8 MiB.
|
||||
*/
|
||||
readonly maxTransferLength?: number
|
||||
}
|
||||
|
||||
const IPV4_MAPPED_PREFIX = '::ffff:'
|
||||
|
||||
function normalizeAddress(address: string): string {
|
||||
return address.startsWith(IPV4_MAPPED_PREFIX) && address.includes('.')
|
||||
? address.slice(IPV4_MAPPED_PREFIX.length)
|
||||
: address
|
||||
}
|
||||
|
||||
/**
|
||||
* A minimal iSCSI target exposing exactly one read/write LUN.
|
||||
*
|
||||
* Single initiator, single connection (`MaxConnections=1`),
|
||||
* `ErrorRecoveryLevel=0`, no digests. A new connection always replaces the
|
||||
* current one rather than being refused (see `#onConnection`): there is no
|
||||
* way to tell a stale, abandoned connection (initiator gave up without
|
||||
* closing the socket) from a healthy one, so refusing would let one stuck
|
||||
* connection wedge the target forever. Not RFC 7143 session reinstatement (no
|
||||
* ISID matching) — safe here since the target is ephemeral, CHAP-guarded, and
|
||||
* `ErrorRecoveryLevel=0`, no digests.
|
||||
*
|
||||
* A new connection replaces the current one rather than being refused (see
|
||||
* `#onSessionEstablished`): there is no way to tell a stale, abandoned
|
||||
* connection (initiator gave up without closing the socket) from a healthy
|
||||
* one, so refusing would let one stuck connection wedge the target forever.
|
||||
* The replacement only happens once the new connection has established a
|
||||
* Normal session, CHAP included when configured, so that a port scan, a failed
|
||||
* login or a Discovery session cannot interrupt the current one. Until then,
|
||||
* connections are bounded in time (`loginTimeoutMs`) and in number
|
||||
* (`maxPendingConnections`); with `allowedInitiatorAddresses`, only the expected
|
||||
* initiators can take those slots. Not RFC 7143 session reinstatement (no ISID
|
||||
* matching) — safe here since the target is ephemeral, CHAP-guarded, and
|
||||
* single-consumer.
|
||||
*/
|
||||
export class IscsiTarget {
|
||||
@@ -85,9 +131,17 @@ export class IscsiTarget {
|
||||
readonly #cmdWindow: number
|
||||
readonly #readConcurrency: number
|
||||
readonly #chap?: ChapCredentials
|
||||
readonly #loginTimeoutMs: number
|
||||
readonly #maxPendingConnections: number
|
||||
readonly #allowedInitiatorAddresses?: ReadonlySet<string>
|
||||
readonly #onConnectionRefused?: (address: string) => void
|
||||
readonly #maxTransferLength: number
|
||||
|
||||
#server?: Server
|
||||
// the connection with an established Normal session, serving the LUN
|
||||
#connection?: Connection
|
||||
// every open connection, so that close() can drop them all
|
||||
readonly #connections = new Set<Connection>()
|
||||
#tsih = 0
|
||||
|
||||
constructor(options: IscsiTargetOptions) {
|
||||
@@ -99,6 +153,16 @@ export class IscsiTarget {
|
||||
this.#cmdWindow = options.cmdWindow ?? DEFAULT_CMD_WINDOW
|
||||
this.#readConcurrency = options.readConcurrency ?? DEFAULT_READ_CONCURRENCY
|
||||
this.#chap = options.chap
|
||||
this.#loginTimeoutMs = options.loginTimeoutMs ?? DEFAULT_LOGIN_TIMEOUT_MS
|
||||
this.#maxPendingConnections = options.maxPendingConnections ?? DEFAULT_MAX_PENDING_CONNECTIONS
|
||||
if (options.allowedInitiatorAddresses !== undefined) {
|
||||
this.#allowedInitiatorAddresses = new Set(Array.from(options.allowedInitiatorAddresses, normalizeAddress))
|
||||
}
|
||||
this.#onConnectionRefused = options.onConnectionRefused
|
||||
this.#maxTransferLength = options.maxTransferLength ?? DEFAULT_MAX_TRANSFER_LENGTH
|
||||
if (this.#maxTransferLength < options.lun.getBlockSize()) {
|
||||
throw new RangeError('maxTransferLength must be at least one logical block')
|
||||
}
|
||||
this.#identity = {
|
||||
...DEFAULT_IDENTITY,
|
||||
// Default the serial to the IQN so the LUN has a stable, unique identity.
|
||||
@@ -122,28 +186,44 @@ export class IscsiTarget {
|
||||
readConcurrency: this.#readConcurrency,
|
||||
allocateTsih: () => this.#allocateTsih(),
|
||||
chap: this.#chap,
|
||||
loginTimeoutMs: this.#loginTimeoutMs,
|
||||
onSessionEstablished: connection => this.#onSessionEstablished(connection),
|
||||
maxTransferLength: this.#maxTransferLength,
|
||||
}
|
||||
}
|
||||
|
||||
#onConnection(socket: Socket): void {
|
||||
const existing = this.#connection
|
||||
if (existing !== undefined) {
|
||||
// Replaces a possibly-stale connection rather than refusing; see the class doc.
|
||||
log.warn('replacing existing connection with a new one', { remote: socket.remoteAddress })
|
||||
existing.destroy()
|
||||
const allowed = this.#allowedInitiatorAddresses
|
||||
const remote = normalizeAddress(socket.remoteAddress ?? '')
|
||||
if (allowed !== undefined && !allowed.has(remote)) {
|
||||
log.warn('connection from an address that is not allowed, closing it', { remote })
|
||||
socket.destroy()
|
||||
this.#onConnectionRefused?.(remote)
|
||||
return
|
||||
}
|
||||
socket.setNoDelay(true)
|
||||
const connection = new Connection(socket, this.#deps())
|
||||
this.#connection = connection
|
||||
this.#connections.add(connection)
|
||||
// Long-lived per-connection task; serve() handles its own errors and never
|
||||
// rejects, so detaching it here is safe.
|
||||
void connection.serve().finally(() => {
|
||||
this.#connections.delete(connection)
|
||||
if (this.#connection === connection) {
|
||||
this.#connection = undefined
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#onSessionEstablished(connection: Connection): void {
|
||||
const existing = this.#connection
|
||||
this.#connection = connection
|
||||
if (existing !== undefined) {
|
||||
// Replaces a possibly-stale connection rather than refusing; see the class doc.
|
||||
log.warn('replacing existing connection with a newly logged in one')
|
||||
existing.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
/** Open the LUN (if needed) and start accepting connections. */
|
||||
async listen(): Promise<void> {
|
||||
if (this.#server !== undefined) {
|
||||
@@ -152,6 +232,11 @@ export class IscsiTarget {
|
||||
await this.#lun.open?.()
|
||||
|
||||
const server = createServer(socket => this.#onConnection(socket))
|
||||
// + 1: the connection serving the LUN does not take a pending slot
|
||||
server.maxConnections = this.#maxPendingConnections + 1
|
||||
server.on('drop', data => {
|
||||
log.warn('too many connections without a session, refusing a new one', { remote: data?.remoteAddress })
|
||||
})
|
||||
this.#server = server
|
||||
try {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
@@ -175,17 +260,20 @@ export class IscsiTarget {
|
||||
return address !== null && typeof address === 'object' ? address : undefined
|
||||
}
|
||||
|
||||
/** Stop accepting connections, drop the active connection, and close the LUN. */
|
||||
/** Stop accepting connections, drop every connection, and close the LUN. */
|
||||
async close(): Promise<void> {
|
||||
const server = this.#server
|
||||
const connection = this.#connection
|
||||
const connections = [...this.#connections]
|
||||
this.#server = undefined
|
||||
this.#connection = undefined
|
||||
this.#connections.clear()
|
||||
if (server !== undefined) {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server.close(error => (error ? reject(error) : resolve()))
|
||||
// Drop the live connection so server.close() can complete.
|
||||
connection?.destroy()
|
||||
// Drop the live connections so server.close() can complete.
|
||||
for (const connection of connections) {
|
||||
connection.destroy()
|
||||
}
|
||||
})
|
||||
}
|
||||
await this.#lun.close()
|
||||
|
||||
@@ -35,9 +35,12 @@ const log: Logger = createLogger('vates:iscsi:login')
|
||||
// Operational values the target imposes. The target drives negotiation, so these
|
||||
// are pinned to a single code path (see plan): all writes are R2T-solicited
|
||||
// (InitialR2T=Yes, ImmediateData=No), no digests, one connection.
|
||||
const OUR_MAX_RECV_DATA_SEGMENT_LENGTH = 262144
|
||||
export const OUR_MAX_RECV_DATA_SEGMENT_LENGTH = 262144
|
||||
const OUR_MAX_BURST_LENGTH = 262144
|
||||
const DEFAULT_INITIATOR_MAX_RECV_DATA_SEGMENT_LENGTH = 8192 // RFC 7143 default
|
||||
// a declared MaxRecvDataSegmentLength only applies once logged in: until then,
|
||||
// both sides keep to the 8192-byte default (RFC 7143 §13.12)
|
||||
export const LOGIN_MAX_DATA_SEGMENT_LENGTH = 8192
|
||||
|
||||
/** Parse a NUL-delimited iSCSI text key=value data segment. */
|
||||
export function parseTextKeys(data: Buffer): Map<string, string> {
|
||||
|
||||
@@ -4,6 +4,7 @@ import { connect, type Socket } from 'node:net'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { once } from 'node:events'
|
||||
import { setTimeout as delay } from 'node:timers/promises'
|
||||
import { after, before, describe, it } from 'node:test'
|
||||
|
||||
import {
|
||||
@@ -642,6 +643,200 @@ describe('connection replacement', () => {
|
||||
})
|
||||
})
|
||||
|
||||
// A connection only takes over once it has itself logged in: until then, it
|
||||
// must not disturb the session in use, whatever it does or fails to do.
|
||||
describe('connection admission', () => {
|
||||
const CHAP = { user: 'alice', secret: 's3cr3t-s3cr3t' }
|
||||
|
||||
let dir: string
|
||||
let backingPath: string
|
||||
|
||||
before(async () => {
|
||||
dir = await mkdtemp(join(tmpdir(), 'vates-iscsi-admission-'))
|
||||
backingPath = join(dir, 'lun.img')
|
||||
await writeFile(backingPath, Buffer.alloc(LUN_SIZE, 0x5a))
|
||||
})
|
||||
|
||||
after(async () => {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
async function withTarget(
|
||||
options: {
|
||||
loginTimeoutMs?: number
|
||||
maxPendingConnections?: number
|
||||
allowedInitiatorAddresses?: string[]
|
||||
onConnectionRefused?: (address: string) => void
|
||||
},
|
||||
fn: (port: number) => Promise<void>
|
||||
): Promise<void> {
|
||||
const target = new IscsiTarget({
|
||||
iqn: IQN,
|
||||
host: '127.0.0.1',
|
||||
port: 0,
|
||||
lun: new FileBlockDevice({ path: backingPath, blockSize: BLOCK_SIZE }),
|
||||
chap: CHAP,
|
||||
...options,
|
||||
})
|
||||
await target.listen()
|
||||
try {
|
||||
const address = target.address()
|
||||
assert.ok(address !== undefined)
|
||||
await fn(address.port)
|
||||
} finally {
|
||||
await target.close()
|
||||
}
|
||||
}
|
||||
|
||||
async function rawConnect(port: number): Promise<Socket> {
|
||||
const socket = connect(port, '127.0.0.1')
|
||||
await once(socket, 'connect')
|
||||
return socket
|
||||
}
|
||||
|
||||
async function assertStillServing(initiator: IscsiInitiator): Promise<void> {
|
||||
// leave the target time to act on whatever just happened
|
||||
await delay(100)
|
||||
assert.deepEqual(await initiator.read(0, BLOCK_SIZE), Buffer.alloc(BLOCK_SIZE, 0x5a))
|
||||
}
|
||||
|
||||
it('a connection that sends nothing does not interrupt the session', async () => {
|
||||
await withTarget({}, async port => {
|
||||
const initiator = new IscsiInitiator({ host: '127.0.0.1', port, targetIqn: IQN, chap: CHAP })
|
||||
await initiator.connect()
|
||||
const probe = await rawConnect(port)
|
||||
try {
|
||||
await assertStillServing(initiator)
|
||||
} finally {
|
||||
probe.destroy()
|
||||
await initiator.close()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('a failed CHAP login does not interrupt the session', async () => {
|
||||
await withTarget({}, async port => {
|
||||
const initiator = new IscsiInitiator({ host: '127.0.0.1', port, targetIqn: IQN, chap: CHAP })
|
||||
await initiator.connect()
|
||||
try {
|
||||
const intruder = new IscsiInitiator({
|
||||
host: '127.0.0.1',
|
||||
port,
|
||||
targetIqn: IQN,
|
||||
chap: { user: CHAP.user, secret: 'wrong-wrong-wrong' },
|
||||
})
|
||||
await assert.rejects(intruder.connect(), /login rejected/)
|
||||
await assertStillServing(initiator)
|
||||
} finally {
|
||||
await initiator.close()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('a Discovery session does not interrupt the session', async () => {
|
||||
await withTarget({}, async port => {
|
||||
const initiator = new IscsiInitiator({ host: '127.0.0.1', port, targetIqn: IQN, chap: CHAP })
|
||||
await initiator.connect()
|
||||
const socket = await rawConnect(port)
|
||||
try {
|
||||
const discovery = new MiniInitiator(socket)
|
||||
await discovery.login('Discovery')
|
||||
assert.equal((await discovery.sendTargets()).get('TargetName'), IQN)
|
||||
await assertStillServing(initiator)
|
||||
await discovery.logout()
|
||||
} finally {
|
||||
socket.destroy()
|
||||
await initiator.close()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('an authenticated login replaces the session', async () => {
|
||||
await withTarget({}, async port => {
|
||||
const stale = new IscsiInitiator({ host: '127.0.0.1', port, targetIqn: IQN, chap: CHAP })
|
||||
await stale.connect()
|
||||
const initiator = new IscsiInitiator({ host: '127.0.0.1', port, targetIqn: IQN, chap: CHAP })
|
||||
await initiator.connect()
|
||||
try {
|
||||
await assert.rejects(stale.read(0, BLOCK_SIZE), /connection closed/)
|
||||
await assertStillServing(initiator)
|
||||
} finally {
|
||||
await stale.close().catch(() => {})
|
||||
await initiator.close()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('closes a connection that does not log in in time', async () => {
|
||||
await withTarget({ loginTimeoutMs: 100 }, async port => {
|
||||
const probe = await rawConnect(port)
|
||||
const closed = once(probe, 'close')
|
||||
try {
|
||||
await Promise.race([closed, delay(5000).then(() => assert.fail('the connection should have been closed'))])
|
||||
} finally {
|
||||
probe.destroy()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('closes connections from an address that is not allowed, and reports it', async () => {
|
||||
const refused: string[] = []
|
||||
await withTarget(
|
||||
{ allowedInitiatorAddresses: ['192.0.2.1'], onConnectionRefused: address => refused.push(address) },
|
||||
async port => {
|
||||
const probe = await rawConnect(port)
|
||||
try {
|
||||
await Promise.race([
|
||||
once(probe, 'close'),
|
||||
delay(5000).then(() => assert.fail('the connection should have been closed')),
|
||||
])
|
||||
} finally {
|
||||
probe.destroy()
|
||||
}
|
||||
}
|
||||
)
|
||||
assert.deepEqual(refused, ['127.0.0.1'])
|
||||
})
|
||||
|
||||
it('serves an allowed address, also in its IPv4-mapped IPv6 form', async () => {
|
||||
for (const allowed of ['127.0.0.1', '::ffff:127.0.0.1']) {
|
||||
await withTarget({ allowedInitiatorAddresses: [allowed] }, async port => {
|
||||
const initiator = new IscsiInitiator({ host: '127.0.0.1', port, targetIqn: IQN, chap: CHAP })
|
||||
await initiator.connect()
|
||||
try {
|
||||
await assertStillServing(initiator)
|
||||
} finally {
|
||||
await initiator.close()
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
it('refuses connections without a session past the limit', async () => {
|
||||
await withTarget({ maxPendingConnections: 2 }, async port => {
|
||||
const initiator = new IscsiInitiator({ host: '127.0.0.1', port, targetIqn: IQN, chap: CHAP })
|
||||
await initiator.connect()
|
||||
const probes = [await rawConnect(port), await rawConnect(port)]
|
||||
try {
|
||||
const extra = await rawConnect(port)
|
||||
probes.push(extra)
|
||||
await Promise.race([
|
||||
once(extra, 'close'),
|
||||
delay(5000).then(() => assert.fail('the connection should have been refused')),
|
||||
])
|
||||
assert.equal(probes[0].destroyed, false)
|
||||
assert.equal(probes[1].destroyed, false)
|
||||
await assertStillServing(initiator)
|
||||
} finally {
|
||||
for (const probe of probes) {
|
||||
probe.destroy()
|
||||
}
|
||||
await initiator.close()
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// One loopback that exercises BOTH one-way CHAP roles at once: our target is the
|
||||
// authenticator (challenges + verifies) and our initiator is the responder
|
||||
// (answers the challenge). This is the same code path each production role uses
|
||||
|
||||
@@ -86,8 +86,12 @@ export class IncomingPdu {
|
||||
* No per-read timeout, intentionally — a target may sit idle between commands
|
||||
* indefinitely. Liveness is a transport concern (socket timeout / NOP
|
||||
* keepalives), handled elsewhere.
|
||||
*
|
||||
* A PDU announcing a data segment longer than `maxDataSegmentLength` is
|
||||
* rejected before its payload is read, so a peer cannot make us buffer up to
|
||||
* the 16 MiB the 24-bit length field allows.
|
||||
*/
|
||||
export async function readPdu(stream: Readable): Promise<IncomingPdu | null> {
|
||||
export async function readPdu(stream: Readable, maxDataSegmentLength = Infinity): Promise<IncomingPdu | null> {
|
||||
const bhs = await readChunk(stream, BHS_LENGTH)
|
||||
if (bhs === null) {
|
||||
return null // clean EOF between PDUs
|
||||
@@ -98,6 +102,9 @@ export async function readPdu(stream: Readable): Promise<IncomingPdu | null> {
|
||||
|
||||
const totalAhsLength = bhs.readUInt8(4)
|
||||
const dataSegmentLength = bhs.readUIntBE(5, 3)
|
||||
if (dataSegmentLength > maxDataSegmentLength) {
|
||||
throw new Error(`iSCSI data segment too long (${dataSegmentLength} > ${maxDataSegmentLength} bytes)`)
|
||||
}
|
||||
const ahsBytes = totalAhsLength * 4
|
||||
const dataBytesPadded = roundUp4(dataSegmentLength)
|
||||
|
||||
|
||||
@@ -56,6 +56,24 @@ describe('assemblePdu / readPdu round-trip', () => {
|
||||
})
|
||||
|
||||
describe('readPdu framing', () => {
|
||||
it('rejects a data segment longer than the limit before reading it', async () => {
|
||||
const bhs = allocBhs(InitiatorOpcode.SCSI_COMMAND)
|
||||
bhs.writeUIntBE(16 * 1024 * 1024 - 1, 5, 3) // announced data segment length
|
||||
const stream = new PassThrough()
|
||||
stream.write(bhs) // the announced payload is never sent
|
||||
await assert.rejects(readPdu(stream, 8192), /data segment too long/)
|
||||
})
|
||||
|
||||
it('accepts a data segment of exactly the limit', async () => {
|
||||
const data = Buffer.alloc(8192, 0x5a)
|
||||
const pdu = await (async () => {
|
||||
const stream = new PassThrough()
|
||||
stream.end(buildScsiCommandPdu(1, 1, Buffer.alloc(16), data))
|
||||
return readPdu(stream, 8192)
|
||||
})()
|
||||
assert.deepEqual(pdu?.data, data)
|
||||
})
|
||||
|
||||
it('reassembles a PDU split across multiple TCP chunks', async () => {
|
||||
const data = Buffer.from('hello world!!') // 13 bytes -> padded to 16
|
||||
const wire = buildScsiCommandPdu(7, 7, Buffer.alloc(16), data)
|
||||
|
||||
@@ -40,13 +40,20 @@ export function buildStandardInquiry(identity: ScsiIdentity): Buffer {
|
||||
return buffer
|
||||
}
|
||||
|
||||
const SUPPORTED_VPD_PAGES = [0x00, 0x80, 0x83]
|
||||
const SUPPORTED_VPD_PAGES = [0x00, 0x80, 0x83, 0xb0]
|
||||
|
||||
/** Default max length of a single READ or WRITE, in bytes. */
|
||||
export const DEFAULT_MAX_TRANSFER_LENGTH = 8 * 1024 * 1024
|
||||
|
||||
/**
|
||||
* Build an INQUIRY EVPD page, or `undefined` for an unsupported page code (the
|
||||
* caller then returns CHECK CONDITION / INVALID FIELD IN CDB).
|
||||
*/
|
||||
export function buildVpd(pageCode: number, identity: ScsiIdentity): Buffer | undefined {
|
||||
export function buildVpd(
|
||||
pageCode: number,
|
||||
identity: ScsiIdentity,
|
||||
maxTransferBlocks = DEFAULT_MAX_TRANSFER_LENGTH / 512
|
||||
): Buffer | undefined {
|
||||
switch (pageCode) {
|
||||
case 0x00: {
|
||||
// Supported VPD pages.
|
||||
@@ -83,6 +90,15 @@ export function buildVpd(pageCode: number, identity: ScsiIdentity): Buffer | und
|
||||
descriptor.copy(buffer, 4)
|
||||
return buffer
|
||||
}
|
||||
case 0xb0: {
|
||||
// Block limits: initiators split their I/O to the max transfer length
|
||||
// instead of seeing it refused.
|
||||
const buffer = Buffer.alloc(64)
|
||||
buffer[1] = 0xb0
|
||||
buffer.writeUInt16BE(0x3c, 2)
|
||||
buffer.writeUInt32BE(maxTransferBlocks, 8) // maximum transfer length
|
||||
return buffer
|
||||
}
|
||||
default:
|
||||
return undefined
|
||||
}
|
||||
@@ -125,15 +141,21 @@ export function buildFixedSense({ key, asc, ascq }: SenseInfo): Buffer {
|
||||
return buffer
|
||||
}
|
||||
|
||||
// WP bit of the device-specific parameter in the mode parameter header
|
||||
const MODE_SENSE_WRITE_PROTECT = 0x80
|
||||
|
||||
/** Minimal MODE SENSE parameter data: a header with no block descriptor or pages. */
|
||||
export function buildModeSense(long: boolean): Buffer {
|
||||
export function buildModeSense(long: boolean, writeProtected = false): Buffer {
|
||||
const deviceSpecific = writeProtected ? MODE_SENSE_WRITE_PROTECT : 0
|
||||
if (long) {
|
||||
const buffer = Buffer.alloc(8)
|
||||
buffer.writeUInt16BE(6, 0) // mode data length (8 - 2)
|
||||
buffer[3] = deviceSpecific
|
||||
return buffer
|
||||
}
|
||||
const buffer = Buffer.alloc(4)
|
||||
buffer[0] = 3 // mode data length (4 - 1)
|
||||
buffer[2] = deviceSpecific
|
||||
return buffer
|
||||
}
|
||||
|
||||
@@ -150,6 +172,7 @@ const ILLEGAL_REQUEST_LBA_OUT_OF_RANGE: SenseInfo = {
|
||||
...Asc.LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE,
|
||||
}
|
||||
const MEDIUM_ERROR_NO_ADDITIONAL_SENSE: SenseInfo = { key: SenseKey.MEDIUM_ERROR, ...Asc.NO_ADDITIONAL_SENSE }
|
||||
const DATA_PROTECT_WRITE_PROTECTED: SenseInfo = { key: SenseKey.DATA_PROTECT, ...Asc.WRITE_PROTECTED }
|
||||
|
||||
function checkCondition(ctx: CommandContext, itt: number, sense: SenseInfo): Promise<void> {
|
||||
return ctx.sendScsiResponse(itt, ScsiStatus.CHECK_CONDITION, { sense: buildFixedSense(sense) })
|
||||
@@ -161,16 +184,22 @@ function checkCondition(ctx: CommandContext, itt: number, sense: SenseInfo): Pro
|
||||
* `cdb` is the 16-byte CDB from the SCSI Command PDU and `itt` its Initiator
|
||||
* Task Tag. Read commands stream Data-In; WRITE solicits Data-Out via R2T;
|
||||
* everything else returns status (with sense on error).
|
||||
*
|
||||
* A READ or WRITE longer than `maxTransferLength` bytes, advertised in the
|
||||
* block limits VPD page, is refused with INVALID FIELD IN CDB: it also bounds
|
||||
* the buffer a WRITE allocates.
|
||||
*/
|
||||
export async function handleScsiCommand(
|
||||
cdb: Buffer,
|
||||
itt: number,
|
||||
ctx: CommandContext,
|
||||
identity: ScsiIdentity
|
||||
identity: ScsiIdentity,
|
||||
maxTransferLength = DEFAULT_MAX_TRANSFER_LENGTH
|
||||
): Promise<void> {
|
||||
const { lun } = ctx
|
||||
const blockSize = lun.getBlockSize()
|
||||
const blockCount = lun.getSize() / blockSize
|
||||
const maxTransferBlocks = Math.floor(maxTransferLength / blockSize)
|
||||
const request = decodeCdb(cdb)
|
||||
log.debug('scsi command', { itt, ...request })
|
||||
|
||||
@@ -183,7 +212,7 @@ export async function handleScsiCommand(
|
||||
}
|
||||
return ctx.sendReadData(itt, buildStandardInquiry(identity), request.allocationLength)
|
||||
}
|
||||
const page = buildVpd(request.pageCode, identity)
|
||||
const page = buildVpd(request.pageCode, identity, maxTransferBlocks)
|
||||
if (page === undefined) {
|
||||
return checkCondition(ctx, itt, ILLEGAL_REQUEST_INVALID_FIELD)
|
||||
}
|
||||
@@ -203,6 +232,9 @@ export async function handleScsiCommand(
|
||||
if (request.lba + request.blocks > blockCount) {
|
||||
return checkCondition(ctx, itt, ILLEGAL_REQUEST_LBA_OUT_OF_RANGE)
|
||||
}
|
||||
if (request.blocks > maxTransferBlocks) {
|
||||
return checkCondition(ctx, itt, ILLEGAL_REQUEST_INVALID_FIELD)
|
||||
}
|
||||
const byteLength = request.blocks * blockSize
|
||||
if (byteLength === 0) {
|
||||
return ctx.sendReadData(itt, Buffer.alloc(0), 0)
|
||||
@@ -221,9 +253,15 @@ export async function handleScsiCommand(
|
||||
}
|
||||
|
||||
case 'write': {
|
||||
if (lun.readOnly === true) {
|
||||
return checkCondition(ctx, itt, DATA_PROTECT_WRITE_PROTECTED)
|
||||
}
|
||||
if (request.lba + request.blocks > blockCount) {
|
||||
return checkCondition(ctx, itt, ILLEGAL_REQUEST_LBA_OUT_OF_RANGE)
|
||||
}
|
||||
if (request.blocks > maxTransferBlocks) {
|
||||
return checkCondition(ctx, itt, ILLEGAL_REQUEST_INVALID_FIELD)
|
||||
}
|
||||
const byteLength = request.blocks * blockSize
|
||||
if (byteLength === 0) {
|
||||
return ctx.sendScsiResponse(itt, ScsiStatus.GOOD)
|
||||
@@ -243,7 +281,7 @@ export async function handleScsiCommand(
|
||||
}
|
||||
|
||||
case 'modeSense':
|
||||
return ctx.sendReadData(itt, buildModeSense(request.long), request.allocationLength)
|
||||
return ctx.sendReadData(itt, buildModeSense(request.long, lun.readOnly === true), request.allocationLength)
|
||||
|
||||
case 'syncCache':
|
||||
await lun.flush()
|
||||
|
||||
@@ -145,9 +145,17 @@ describe('response encoders', () => {
|
||||
it('advertises supported VPD pages and rejects unknown ones', () => {
|
||||
const page00 = buildVpd(0x00, IDENTITY)
|
||||
assert.ok(page00 !== undefined)
|
||||
assert.deepEqual([...page00.subarray(4)], [0x00, 0x80, 0x83])
|
||||
assert.deepEqual([...page00.subarray(4)], [0x00, 0x80, 0x83, 0xb0])
|
||||
assert.equal(buildVpd(0x55, IDENTITY), undefined)
|
||||
})
|
||||
|
||||
it('advertises the max transfer length in the block limits page', () => {
|
||||
const page = buildVpd(0xb0, IDENTITY, 2048)
|
||||
assert.ok(page !== undefined)
|
||||
assert.equal(page[1], 0xb0)
|
||||
assert.equal(page.readUInt16BE(2), 0x3c)
|
||||
assert.equal(page.readUInt32BE(8), 2048)
|
||||
})
|
||||
})
|
||||
|
||||
describe('handleScsiCommand', () => {
|
||||
@@ -234,4 +242,61 @@ describe('handleScsiCommand', () => {
|
||||
assert.equal(ctx.responses[0].status, ScsiStatus.CHECK_CONDITION)
|
||||
assert.equal(ctx.responses[0].options?.sense?.[12], 0x20) // INVALID COMMAND OPERATION CODE
|
||||
})
|
||||
|
||||
for (const [name, opcode] of [
|
||||
['READ(10)', 0x28],
|
||||
['WRITE(10)', 0x2a],
|
||||
] as const) {
|
||||
it(`refuses a ${name} longer than the max transfer length, without reading or soliciting data`, async () => {
|
||||
let reads = 0
|
||||
const lun = new MemoryBlockDevice(8192)
|
||||
const read = lun.read.bind(lun)
|
||||
lun.read = (offset, length) => {
|
||||
reads++
|
||||
return read(offset, length)
|
||||
}
|
||||
const ctx = new FakeContext(lun)
|
||||
const buffer = Buffer.alloc(16)
|
||||
buffer[0] = opcode
|
||||
buffer.writeUInt16BE(3, 7) // 3 blocks, the limit is 2
|
||||
await handleScsiCommand(buffer, 1, ctx, IDENTITY, 1024)
|
||||
assert.equal(ctx.responses[0].status, ScsiStatus.CHECK_CONDITION)
|
||||
assert.equal(ctx.responses[0].options?.sense?.[2], SenseKey.ILLEGAL_REQUEST)
|
||||
assert.equal(ctx.responses[0].options?.sense?.[12], 0x24) // INVALID FIELD IN CDB
|
||||
assert.equal(reads, 0)
|
||||
assert.equal(ctx.writes.length, 0)
|
||||
})
|
||||
}
|
||||
|
||||
describe('read-only LUN', () => {
|
||||
class ReadOnlyBlockDevice extends MemoryBlockDevice {
|
||||
readonly readOnly = true
|
||||
}
|
||||
|
||||
it('refuses WRITE(10) with DATA PROTECT, without soliciting its data', async () => {
|
||||
const ctx = new FakeContext(new ReadOnlyBlockDevice(4096))
|
||||
const buffer = Buffer.alloc(16)
|
||||
buffer[0] = 0x2a
|
||||
buffer.writeUInt16BE(1, 7)
|
||||
await handleScsiCommand(buffer, 1, ctx, IDENTITY)
|
||||
assert.equal(ctx.writes.length, 0)
|
||||
assert.equal(ctx.responses[0].status, ScsiStatus.CHECK_CONDITION)
|
||||
assert.equal(ctx.responses[0].options?.sense?.[2], SenseKey.DATA_PROTECT)
|
||||
assert.equal(ctx.responses[0].options?.sense?.[12], 0x27) // WRITE PROTECTED
|
||||
})
|
||||
|
||||
it('reports write protection in MODE SENSE(6) and MODE SENSE(10)', async () => {
|
||||
const ctx = new FakeContext(new ReadOnlyBlockDevice(4096))
|
||||
await handleScsiCommand(cdb(0x1a, 0x00, 0x3f, 0x00, 255), 1, ctx, IDENTITY)
|
||||
await handleScsiCommand(cdb(0x5a, 0x00, 0x3f, 0, 0, 0, 0, 0x01, 0x00), 2, ctx, IDENTITY)
|
||||
assert.equal(ctx.reads[0].data[2] & 0x80, 0x80)
|
||||
assert.equal(ctx.reads[1].data[3] & 0x80, 0x80)
|
||||
})
|
||||
|
||||
it('does not report a writable LUN as write-protected', async () => {
|
||||
const ctx = new FakeContext(new MemoryBlockDevice(4096))
|
||||
await handleScsiCommand(cdb(0x1a, 0x00, 0x3f, 0x00, 255), 1, ctx, IDENTITY)
|
||||
assert.equal(ctx.reads[0].data[2] & 0x80, 0)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -56,17 +56,21 @@ function parseAttachedDisk(sessionOutput: string): string | undefined {
|
||||
}
|
||||
|
||||
/**
|
||||
* `stat()`ing the device node only proves udev created it, not that the SCSI
|
||||
* layer has finished attaching it — `open()` right after can still fail with
|
||||
* ENXIO. `/sys/class/block/<name>/size` reporting nonzero is what actually
|
||||
* means ready, the same check `_cache.mjs`'s `openLocalDevice` uses for the
|
||||
* same reason on XAPI-plugged devices.
|
||||
* Neither the device node nor a nonzero `/sys/class/block/<name>/size` means
|
||||
* the disk can be opened: the sd driver sets the size before
|
||||
* `device_add_disk()`, which creates the sysfs entry, then the `/dev` node, then
|
||||
* registers the block device: until then `open()` fails with ENOENT or ENXIO.
|
||||
* Only an `open()` that succeeds means ready.
|
||||
*/
|
||||
async function isDeviceReady(name: string): Promise<boolean> {
|
||||
try {
|
||||
return Number.parseInt(await readFile(`/sys/class/block/${name}/size`, 'utf8'), 10) > 0
|
||||
} catch {
|
||||
return false
|
||||
await (await openFile(`/dev/${name}`, 'r')).close()
|
||||
return true
|
||||
} catch (error) {
|
||||
if (error instanceof Error && 'code' in error && (error.code === 'ENXIO' || error.code === 'ENOENT')) {
|
||||
return false
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { Socket } from 'node:net'
|
||||
import { once } from 'node:events'
|
||||
import { request as httpRequest } from 'node:http'
|
||||
import { request as httpsRequest } from 'node:https'
|
||||
import { isIPv6, Socket } from 'node:net'
|
||||
import { connect } from 'node:tls'
|
||||
import { spawn } from 'node:child_process'
|
||||
import AbstractNbdClient from './AbstractNbdClient.mjs'
|
||||
@@ -11,6 +14,9 @@ import { NBD_DEFAULT_PORT, NBD_OPT_STARTTLS } from './constants.mjs'
|
||||
* @extends {AbstractNbdClient}
|
||||
*/
|
||||
export default class NbdTcpClient extends AbstractNbdClient {
|
||||
#httpProxy
|
||||
#proxyRejectUnauthorized
|
||||
#proxyTimeout
|
||||
#serverAddress
|
||||
#serverCert
|
||||
#serverPort
|
||||
@@ -21,18 +27,103 @@ export default class NbdTcpClient extends AbstractNbdClient {
|
||||
* @param {number} [settings.port]
|
||||
* @param {string} [settings.exportname]
|
||||
* @param {string} [settings.cert] - PEM certificate, enables TLS when set
|
||||
* @param {string} [settings.httpProxy] - URL of an HTTP(S) proxy, the connection is tunneled through it with `CONNECT`
|
||||
* @param {boolean} [settings.proxyRejectUnauthorized=true] - whether to check the certificate of an HTTPS proxy
|
||||
* @param {number} [settings.proxyTimeout=6e4] - delay in ms after which an unanswered `CONNECT` is aborted
|
||||
* @param {object} [options] - see {@link AbstractNbdClient}
|
||||
*/
|
||||
constructor({ address, port = NBD_DEFAULT_PORT, exportname, cert }, options) {
|
||||
constructor(
|
||||
{
|
||||
address,
|
||||
port = NBD_DEFAULT_PORT,
|
||||
exportname,
|
||||
cert,
|
||||
httpProxy,
|
||||
proxyRejectUnauthorized = true,
|
||||
proxyTimeout = 6e4,
|
||||
},
|
||||
options
|
||||
) {
|
||||
super({ exportname }, options)
|
||||
// other proxy protocols (e.g. SOCKS) are not supported for now
|
||||
if (httpProxy !== undefined) {
|
||||
const { protocol } = new URL(httpProxy)
|
||||
if (protocol !== 'http:' && protocol !== 'https:') {
|
||||
const error = new Error(`unsupported proxy protocol ${protocol}, only http: and https: are supported`)
|
||||
error.code = 'NBD_PROXY_UNSUPPORTED_PROTOCOL'
|
||||
throw error
|
||||
}
|
||||
}
|
||||
this.#serverAddress = address
|
||||
this.#serverPort = port
|
||||
this.#serverCert = cert
|
||||
this.#httpProxy = httpProxy
|
||||
this.#proxyRejectUnauthorized = proxyRejectUnauthorized
|
||||
this.#proxyTimeout = proxyTimeout
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a raw TCP tunnel to the NBD server through an HTTP proxy
|
||||
*
|
||||
* https://datatracker.ietf.org/doc/html/rfc9110#section-9.3.6
|
||||
*
|
||||
* @returns {Promise<import('node:net').Socket>}
|
||||
*/
|
||||
async #connectThroughHttpProxy() {
|
||||
const proxy = new URL(this.#httpProxy)
|
||||
const isHttps = proxy.protocol === 'https:'
|
||||
|
||||
const address = this.#serverAddress
|
||||
const target = `${isIPv6(address) ? `[${address}]` : address}:${this.#serverPort}`
|
||||
const headers = { host: target }
|
||||
if (proxy.username !== '' || proxy.password !== '') {
|
||||
const credentials = `${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`
|
||||
headers['proxy-authorization'] = `Basic ${Buffer.from(credentials).toString('base64')}`
|
||||
}
|
||||
|
||||
const req = (isHttps ? httpsRequest : httpRequest)({
|
||||
agent: false,
|
||||
headers,
|
||||
// URL keeps the brackets around IPv6 addresses
|
||||
hostname: proxy.hostname.replace(/^\[(.*)\]$/, '$1'),
|
||||
method: 'CONNECT',
|
||||
path: target,
|
||||
port: proxy.port,
|
||||
rejectUnauthorized: this.#proxyRejectUnauthorized,
|
||||
timeout: this.#proxyTimeout,
|
||||
})
|
||||
// `timeout` only emits an event, the request must be aborted explicitly
|
||||
req.on('timeout', () => {
|
||||
const error = new Error(`HTTP proxy did not answer the CONNECT to ${target} in time`)
|
||||
error.code = 'NBD_PROXY_CONNECT_TIMEOUT'
|
||||
req.destroy(error)
|
||||
})
|
||||
req.end()
|
||||
|
||||
// the NBD server may already have sent some data, it is in `head`
|
||||
const [res, socket, head] = await once(req, 'connect')
|
||||
// the tunnel is established, NBD has its own timeouts
|
||||
socket.setTimeout(0)
|
||||
if (res.statusCode !== 200) {
|
||||
socket.destroy()
|
||||
const error = new Error(`HTTP proxy refused to connect to ${target}: ${res.statusCode} ${res.statusMessage}`)
|
||||
error.code = 'NBD_PROXY_CONNECT_FAILED'
|
||||
throw error
|
||||
}
|
||||
if (head.length !== 0) {
|
||||
socket.unshift(head)
|
||||
}
|
||||
return socket
|
||||
}
|
||||
|
||||
// mandatory , at least to start the handshake: the connection always starts
|
||||
// unsecured, and is upgraded to TLS during the handshake
|
||||
async _openTransport() {
|
||||
if (this.#httpProxy !== undefined) {
|
||||
const socket = await this.#connectThroughHttpProxy()
|
||||
return { readable: socket, writable: socket }
|
||||
}
|
||||
|
||||
const socket = new Socket()
|
||||
await new Promise((resolve, reject) => {
|
||||
socket.connect(this.#serverPort, this.#serverAddress)
|
||||
|
||||
107
@vates/nbd-client/NbdTcpClient.test.mjs
Normal file
107
@vates/nbd-client/NbdTcpClient.test.mjs
Normal file
@@ -0,0 +1,107 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { after, before, describe, it } from 'node:test'
|
||||
import { createServer as createHttpServer } from 'node:http'
|
||||
import { connect, createServer as createTcpServer } from 'node:net'
|
||||
import { once } from 'node:events'
|
||||
|
||||
import NbdTcpClient from './NbdTcpClient.mjs'
|
||||
import { serveNbd } from './tests/fake-nbd-server.mjs'
|
||||
|
||||
const BLOCK_SIZE = 64 * 1024
|
||||
const DATA = Buffer.alloc(4 * BLOCK_SIZE)
|
||||
for (let i = 0; i < DATA.length; i++) {
|
||||
DATA[i] = i % 251
|
||||
}
|
||||
const CREDENTIALS = 'Basic ' + Buffer.from('user:p@ss').toString('base64')
|
||||
|
||||
describe('NbdTcpClient through an HTTP proxy', () => {
|
||||
let nbdServer, proxyServer, nbdPort, proxyPort
|
||||
const connectRequests = []
|
||||
|
||||
before(async () => {
|
||||
nbdServer = createTcpServer(socket => {
|
||||
serveNbd({ readable: socket, writable: socket, data: DATA }).catch(() => {})
|
||||
})
|
||||
nbdServer.listen(0, '127.0.0.1')
|
||||
await once(nbdServer, 'listening')
|
||||
nbdPort = nbdServer.address().port
|
||||
|
||||
// minimal CONNECT proxy, similar to the XO Proxy one
|
||||
proxyServer = createHttpServer()
|
||||
proxyServer.on('connect', (req, clientSocket, head) => {
|
||||
connectRequests.push(req.url)
|
||||
if (req.headers['proxy-authorization'] !== CREDENTIALS) {
|
||||
clientSocket.end('HTTP/1.1 407 Proxy Authentication Required\r\n\r\n')
|
||||
return
|
||||
}
|
||||
const { hostname, port } = new URL('http://' + req.url)
|
||||
const serverSocket = connect(port, hostname, () => {
|
||||
clientSocket.write('HTTP/1.1 200 Connection Established\r\n\r\n')
|
||||
serverSocket.write(head)
|
||||
serverSocket.pipe(clientSocket).pipe(serverSocket)
|
||||
})
|
||||
serverSocket.on('error', () => clientSocket.destroy())
|
||||
clientSocket.on('error', () => serverSocket.destroy())
|
||||
})
|
||||
proxyServer.listen(0, '127.0.0.1')
|
||||
await once(proxyServer, 'listening')
|
||||
proxyPort = proxyServer.address().port
|
||||
})
|
||||
|
||||
after(() => {
|
||||
proxyServer.closeAllConnections()
|
||||
proxyServer.close()
|
||||
nbdServer.close()
|
||||
})
|
||||
|
||||
it('tunnels the NBD connection with CONNECT', async () => {
|
||||
const client = new NbdTcpClient({
|
||||
address: '127.0.0.1',
|
||||
port: nbdPort,
|
||||
httpProxy: `http://user:p%40ss@127.0.0.1:${proxyPort}`,
|
||||
})
|
||||
await client.connect()
|
||||
try {
|
||||
assert.equal(client.exportSize, BigInt(DATA.length))
|
||||
const block = await client.readBlock(2, BLOCK_SIZE)
|
||||
assert.deepEqual(block, DATA.subarray(2 * BLOCK_SIZE, 3 * BLOCK_SIZE))
|
||||
} finally {
|
||||
await client.disconnect()
|
||||
}
|
||||
assert.equal(connectRequests.at(-1), `127.0.0.1:${nbdPort}`)
|
||||
})
|
||||
|
||||
it('fails when the proxy refuses the connection', async () => {
|
||||
const client = new NbdTcpClient({
|
||||
address: '127.0.0.1',
|
||||
port: nbdPort,
|
||||
httpProxy: `http://user:wrong@127.0.0.1:${proxyPort}`,
|
||||
})
|
||||
await assert.rejects(client.connect(), { code: 'NBD_PROXY_CONNECT_FAILED' })
|
||||
await client.disconnect().catch(() => {})
|
||||
})
|
||||
|
||||
it('aborts the CONNECT when the proxy does not answer', async () => {
|
||||
// accepts the TCP connection but never answers
|
||||
const silentProxy = createTcpServer(() => {})
|
||||
silentProxy.listen(0, '127.0.0.1')
|
||||
await once(silentProxy, 'listening')
|
||||
try {
|
||||
const client = new NbdTcpClient({
|
||||
address: '127.0.0.1',
|
||||
port: nbdPort,
|
||||
httpProxy: `http://127.0.0.1:${silentProxy.address().port}`,
|
||||
proxyTimeout: 50,
|
||||
})
|
||||
await assert.rejects(client.connect(), { code: 'NBD_PROXY_CONNECT_TIMEOUT' })
|
||||
} finally {
|
||||
silentProxy.close()
|
||||
}
|
||||
})
|
||||
|
||||
it('rejects unsupported proxy protocols', () => {
|
||||
assert.throws(() => new NbdTcpClient({ address: '127.0.0.1', httpProxy: 'socks5://127.0.0.1:1080' }), {
|
||||
code: 'NBD_PROXY_UNSUPPORTED_PROTOCOL',
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -13,7 +13,7 @@
|
||||
"url": "https://vates.fr"
|
||||
},
|
||||
"license": "ISC",
|
||||
"version": "3.6.0",
|
||||
"version": "3.7.0",
|
||||
"engines": {
|
||||
"node": ">=14.0"
|
||||
},
|
||||
@@ -22,10 +22,10 @@
|
||||
"@vates/async-each": "^1.0.3",
|
||||
"@vates/read-chunk": "^1.2.1",
|
||||
"@xen-orchestra/async-map": "^0.1.5",
|
||||
"@xen-orchestra/disk-transform": "^1.3.3",
|
||||
"@xen-orchestra/disk-transform": "^1.3.4",
|
||||
"@xen-orchestra/log": "^0.7.2",
|
||||
"promise-toolbox": "^0.21.0",
|
||||
"xen-api": "^5.0.2"
|
||||
"xen-api": "^5.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tap": "^18.7.0",
|
||||
|
||||
@@ -44,3 +44,39 @@ obfuscated.token = 'qux'
|
||||
merge(obfuscated, original)
|
||||
// → { username: 'foo', password: 'bar', token: 'qux' }
|
||||
```
|
||||
|
||||
### `redact(value, [isSensitive], [options])`
|
||||
|
||||
> Recursively replace sensitive entries with the same special tag as `obfuscate()`, whatever their type.
|
||||
>
|
||||
> Unlike `obfuscate()`, it uses a broader list of sensitive keys (`isExportSensitiveParam`) because redacted values are never merged back: it is meant for data leaving XO, such as support dumps or audit records. Keys are kept so that the structure stays usable.
|
||||
|
||||
Options:
|
||||
|
||||
- `replacement`: the special tag of `obfuscate()` by default
|
||||
- `urls`: whether to also apply `redactUrls()` to the other strings, `false` by default
|
||||
|
||||
```js
|
||||
import { redact } from '@vates/obfuscate'
|
||||
|
||||
redact({ username: 'foo', password: 'bar', cluster_token: { value: 'baz' } })
|
||||
// → {
|
||||
// username: 'foo',
|
||||
// password: 'obfuscated-q3oi6d9X8uenGvdLnHk2',
|
||||
// cluster_token: 'obfuscated-q3oi6d9X8uenGvdLnHk2',
|
||||
// }
|
||||
|
||||
redact({ error: 'cannot mount nfs://user:pass@host/path' }, undefined, { replacement: '*', urls: true })
|
||||
// → { error: 'cannot mount nfs://*@host/path' }
|
||||
```
|
||||
|
||||
### `redactUrls(string, [replacement])`
|
||||
|
||||
> Replace the credentials and the sensitive query parameters (e.g. `encryptionKey`) of the URLs in a string, wherever they are (e.g. in an error message).
|
||||
|
||||
```js
|
||||
import { redactUrls } from '@vates/obfuscate'
|
||||
|
||||
redactUrls('s3://KEY:secret@s3.lan/bucket?encryptionKey=foo', '*')
|
||||
// → 's3://*@s3.lan/bucket?encryptionKey=*'
|
||||
```
|
||||
|
||||
@@ -61,6 +61,42 @@ merge(obfuscated, original)
|
||||
// → { username: 'foo', password: 'bar', token: 'qux' }
|
||||
```
|
||||
|
||||
### `redact(value, [isSensitive], [options])`
|
||||
|
||||
> Recursively replace sensitive entries with the same special tag as `obfuscate()`, whatever their type.
|
||||
>
|
||||
> Unlike `obfuscate()`, it uses a broader list of sensitive keys (`isExportSensitiveParam`) because redacted values are never merged back: it is meant for data leaving XO, such as support dumps or audit records. Keys are kept so that the structure stays usable.
|
||||
|
||||
Options:
|
||||
|
||||
- `replacement`: the special tag of `obfuscate()` by default
|
||||
- `urls`: whether to also apply `redactUrls()` to the other strings, `false` by default
|
||||
|
||||
```js
|
||||
import { redact } from '@vates/obfuscate'
|
||||
|
||||
redact({ username: 'foo', password: 'bar', cluster_token: { value: 'baz' } })
|
||||
// → {
|
||||
// username: 'foo',
|
||||
// password: 'obfuscated-q3oi6d9X8uenGvdLnHk2',
|
||||
// cluster_token: 'obfuscated-q3oi6d9X8uenGvdLnHk2',
|
||||
// }
|
||||
|
||||
redact({ error: 'cannot mount nfs://user:pass@host/path' }, undefined, { replacement: '*', urls: true })
|
||||
// → { error: 'cannot mount nfs://*@host/path' }
|
||||
```
|
||||
|
||||
### `redactUrls(string, [replacement])`
|
||||
|
||||
> Replace the credentials and the sensitive query parameters (e.g. `encryptionKey`) of the URLs in a string, wherever they are (e.g. in an error message).
|
||||
|
||||
```js
|
||||
import { redactUrls } from '@vates/obfuscate'
|
||||
|
||||
redactUrls('s3://KEY:secret@s3.lan/bucket?encryptionKey=foo', '*')
|
||||
// → 's3://*@s3.lan/bucket?encryptionKey=*'
|
||||
```
|
||||
|
||||
## Contributions
|
||||
|
||||
Contributions are _very_ welcomed, either on the documentation or on
|
||||
|
||||
@@ -57,3 +57,74 @@ function replace(value, replacement) {
|
||||
return helper(value)
|
||||
}
|
||||
exports.replace = replace
|
||||
|
||||
// broader than SENSITIVE_PARAMS because redacted values are never merged back:
|
||||
// meant for data leaving XO (e.g. support dumps, audit records), not for display
|
||||
const EXPORT_SENSITIVE_PARAMS = [
|
||||
/api[-_]?key/i, // e.g. `apikey` in the XenServer credentials of user preferences
|
||||
'encryptionKey',
|
||||
'privateKey',
|
||||
'pw_hash',
|
||||
/passphrase/i,
|
||||
/password/i,
|
||||
/secret/i,
|
||||
/token/i,
|
||||
]
|
||||
const isExportSensitiveParam = name =>
|
||||
EXPORT_SENSITIVE_PARAMS.some(pattern => (typeof pattern === 'string' ? pattern === name : pattern.test(name)))
|
||||
exports.isExportSensitiveParam = isExportSensitiveParam
|
||||
|
||||
// SMB URLs (e.g. of backup repositories) contain the raw password, which can
|
||||
// contain any character (e.g. `smb://user:p@ss/w@domain\\\\host\0path`), so the
|
||||
// credentials are everything up to the last `@` before the path
|
||||
const SMB_CREDENTIALS_RE = /^(smb:\/\/)[^\0]*@/i
|
||||
|
||||
// other URLs, anywhere in the string (e.g. an error message): credentials are
|
||||
// URL encoded and end at the first `@`, see https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1
|
||||
const URL_CREDENTIALS_RE = /([a-z][a-z\d+.-]*:\/\/)[^\s/?#@]*@/gi
|
||||
|
||||
// e.g. `?encryptionKey=` in a backup repository URL, same names as SENSITIVE_PARAMS
|
||||
const SENSITIVE_QUERY_PARAM_RE = /([?&](?:encryptionKey|passphrase|token|[^=&#\s]*password[^=&#\s]*)=)[^&#\s]*/gi
|
||||
|
||||
// replaces the credentials and the sensitive query parameters of the URLs in a string
|
||||
function redactUrls(string, replacement = OBFUSCATED_VALUE) {
|
||||
if (!string.includes('://')) {
|
||||
return string
|
||||
}
|
||||
return string
|
||||
.replace(SMB_CREDENTIALS_RE, (_, scheme) => `${scheme}${replacement}@`)
|
||||
.replace(URL_CREDENTIALS_RE, (_, scheme) => `${scheme}${replacement}@`)
|
||||
.replace(SENSITIVE_QUERY_PARAM_RE, (_, param) => `${param}${replacement}`)
|
||||
}
|
||||
exports.redactUrls = redactUrls
|
||||
|
||||
// unlike replace(), also replaces non-string entries (e.g. a whole object under
|
||||
// a `secret` key), keys are kept so that the structure stays usable
|
||||
//
|
||||
// options:
|
||||
// - `replacement`: OBFUSCATED_VALUE by default
|
||||
// - `urls`: whether to apply redactUrls() to the other strings, false by default
|
||||
function redact(value, isSensitive = isExportSensitiveParam, { replacement = OBFUSCATED_VALUE, urls = false } = {}) {
|
||||
function helper(value) {
|
||||
if (typeof value === 'string') {
|
||||
return urls ? redactUrls(value, replacement) : value
|
||||
}
|
||||
|
||||
if (typeof value !== 'object' || value === null) {
|
||||
return value
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(helper)
|
||||
}
|
||||
|
||||
const result = {}
|
||||
for (const key of Object.keys(value)) {
|
||||
result[key] = isSensitive(key) ? replacement : helper(value[key])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
return helper(value)
|
||||
}
|
||||
exports.redact = redact
|
||||
|
||||
94
@vates/obfuscate/index.test.js
Normal file
94
@vates/obfuscate/index.test.js
Normal file
@@ -0,0 +1,94 @@
|
||||
'use strict'
|
||||
|
||||
const { describe, it } = require('node:test')
|
||||
const assert = require('node:assert/strict')
|
||||
|
||||
const { obfuscate, OBFUSCATED_VALUE, redact, redactUrls } = require('./')
|
||||
|
||||
describe('redact()', () => {
|
||||
it('replaces sensitive entries at any depth, whatever their type, and keeps the keys', () => {
|
||||
const remote = {
|
||||
name: 'nas',
|
||||
url: 's3://key@host/bucket',
|
||||
encryptionKey: 'aes-key',
|
||||
proxy: { authenticationToken: 'proxy-token' },
|
||||
credentials: [{ username: 'admin', password: 'pass', secret: { value: 'chap' } }],
|
||||
}
|
||||
|
||||
assert.deepEqual(redact(remote), {
|
||||
name: 'nas',
|
||||
url: 's3://key@host/bucket',
|
||||
encryptionKey: OBFUSCATED_VALUE,
|
||||
proxy: { authenticationToken: OBFUSCATED_VALUE },
|
||||
credentials: [{ username: 'admin', password: OBFUSCATED_VALUE, secret: OBFUSCATED_VALUE }],
|
||||
})
|
||||
})
|
||||
|
||||
it('catches keys missed by obfuscate()', () => {
|
||||
const plugin = { apiKey: 'k', cluster_token: 't', secretAccessKey: 's', pw_hash: 'h' }
|
||||
|
||||
assert.deepEqual(redact(plugin), {
|
||||
apiKey: OBFUSCATED_VALUE,
|
||||
cluster_token: OBFUSCATED_VALUE,
|
||||
secretAccessKey: OBFUSCATED_VALUE,
|
||||
pw_hash: OBFUSCATED_VALUE,
|
||||
})
|
||||
assert.equal(obfuscate(plugin).apiKey, 'k')
|
||||
})
|
||||
|
||||
it('accepts a custom predicate and replacement', () => {
|
||||
assert.deepEqual(
|
||||
redact({ a: 1, b: { a: 2, c: 3 } }, key => key === 'a', { replacement: '*' }),
|
||||
{
|
||||
a: '*',
|
||||
b: { a: '*', c: 3 },
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
it('redacts the URLs of the other strings with `urls`', () => {
|
||||
const data = {
|
||||
params: { url: 'smb://admin:p@ss@nas\\\\share' },
|
||||
error: 'mount of nfs://user:pass@host/path failed',
|
||||
}
|
||||
|
||||
assert.deepEqual(redact(data, undefined, { replacement: '*', urls: true }), {
|
||||
params: { url: 'smb://*@nas\\\\share' },
|
||||
error: 'mount of nfs://*@host/path failed',
|
||||
})
|
||||
assert.deepEqual(redact(data), data)
|
||||
})
|
||||
|
||||
it('returns other values as is', () => {
|
||||
assert.equal(redact('password'), 'password')
|
||||
assert.equal(redact(null), null)
|
||||
})
|
||||
})
|
||||
|
||||
describe('redactUrls()', () => {
|
||||
it('replaces the credentials of URLs, anywhere in the string', () => {
|
||||
assert.equal(
|
||||
redactUrls('failed to connect to https://user:p%40ss@host/ and s3://KEY:secret@s3.lan/bucket'),
|
||||
`failed to connect to https://${OBFUSCATED_VALUE}@host/ and s3://${OBFUSCATED_VALUE}@s3.lan/bucket`
|
||||
)
|
||||
})
|
||||
|
||||
it('replaces the credentials of SMB URLs up to the last @ before the path', () => {
|
||||
assert.equal(redactUrls('smb://user:p@ss/w@domain\\\\host\0path', '*'), 'smb://*@domain\\\\host\0path')
|
||||
})
|
||||
|
||||
it('replaces sensitive query parameters', () => {
|
||||
assert.equal(
|
||||
redactUrls('s3://host/bucket?encryptionKey=k®ion=eu&adminPassword=p', '*'),
|
||||
's3://host/bucket?encryptionKey=*®ion=eu&adminPassword=*'
|
||||
)
|
||||
})
|
||||
|
||||
it('handles replacements containing $', () => {
|
||||
assert.equal(redactUrls('http://u:p@h/', '$1'), 'http://$1@h/')
|
||||
})
|
||||
|
||||
it('returns strings without URLs as is', () => {
|
||||
assert.equal(redactUrls('user:pass@host'), 'user:pass@host')
|
||||
})
|
||||
})
|
||||
@@ -18,6 +18,7 @@
|
||||
"node": ">=8.10"
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public"
|
||||
"postversion": "npm publish --access public",
|
||||
"test": "node --test"
|
||||
}
|
||||
}
|
||||
|
||||
48
@vates/syslog/.USAGE.md
Normal file
48
@vates/syslog/.USAGE.md
Normal file
@@ -0,0 +1,48 @@
|
||||
```js
|
||||
import { createClient } from '@vates/syslog'
|
||||
|
||||
const client = createClient('tls://syslog.company.lan', {
|
||||
appName: 'my-app',
|
||||
facility: 'authpriv',
|
||||
})
|
||||
|
||||
await client.send('something happened', { msgid: 'event', severity: 'warning' })
|
||||
|
||||
// closes the connection once the messages already sent are delivered, the client reconnects if it is used again
|
||||
client.close()
|
||||
```
|
||||
|
||||
### `createClient(uri, options)`
|
||||
|
||||
`uri`: `udp://host[:port]`, `tcp://host[:port]` or `tls://host[:port]`, the default port is `514`, or `6514` for TLS.
|
||||
|
||||
Options:
|
||||
|
||||
- `appName`: application name of the messages, `node` by default
|
||||
- `facility`: one of `FACILITIES` (e.g. `user`, `authpriv`, `local0`), `user` by default
|
||||
- `format`: `rfc5424` (default) or `rfc3164`
|
||||
- `framing` (TCP and TLS): `lf` (default, each message followed by a line feed, its own line breaks written as `\n` and `\r`) or `octet-counting` (each message prefixed by its length, required by some servers for TLS, see [RFC 5425](https://datatracker.ietf.org/doc/html/rfc5425#section-4.3))
|
||||
- `hostname`: host name of the messages, `os.hostname()` by default
|
||||
- `ca` (TLS): contents of PEM certificate authorities to trust, in addition to the system ones
|
||||
- `allowUnauthorized` (TLS): do not check the certificate of the server, `false` by default
|
||||
- `closeTimeout`: in milliseconds, how long `close()` waits for the messages already sent, `10e3` by default
|
||||
- `connectTimeout`: in milliseconds, `10e3` by default
|
||||
- `maxPendingBytes`: messages are rejected when more than this is waiting to be sent (e.g. the server is unreachable or does not read), `1 MiB` by default
|
||||
|
||||
A client uses at most one connection, opened when needed.
|
||||
|
||||
### `client.send(message, options)`
|
||||
|
||||
Returns a promise which resolves when the message has been handed to the system. Syslog has no acknowledgement: it does not mean that the server received it.
|
||||
|
||||
Options:
|
||||
|
||||
- `msgid`: message ID (RFC 5424 only)
|
||||
- `severity`: one of `SEVERITIES` (e.g. `error`, `warning`, `informational`), `informational` by default
|
||||
- `timestamp`: `Date` of the message, now by default
|
||||
|
||||
The promise rejects if the message cannot be sent, with the cause of the failure (e.g. connection refused, invalid certificate, too much data waiting).
|
||||
|
||||
### `client.close()`
|
||||
|
||||
Closes the connection, including a connection which is being established. The messages already sent are still delivered first (e.g. while the connection is being established or the host name resolved), for at most `closeTimeout`: after that, those not sent fail.
|
||||
36
@vates/syslog/.npmignore
Normal file
36
@vates/syslog/.npmignore
Normal file
@@ -0,0 +1,36 @@
|
||||
.*
|
||||
|
||||
/benchmark/
|
||||
/benchmarks/
|
||||
*.bench.{,c,m}js
|
||||
*.bench.{,c,m}js.map
|
||||
|
||||
/coverage/
|
||||
|
||||
/examples/
|
||||
example.{,c,m}js
|
||||
example.{,c,m}js.map
|
||||
*.example.{,c,m}js
|
||||
*.example.{,c,m}js.map
|
||||
|
||||
/fixture/
|
||||
/fixtures/
|
||||
*.fixture.{,c,m}js
|
||||
*.fixture.{,c,m}js.map
|
||||
*.fixtures.{,c,m}js
|
||||
*.fixtures.{,c,m}js.map
|
||||
|
||||
/test/
|
||||
/tests/
|
||||
*.integ.{,c,m}js
|
||||
*.integ.{,c,m}js.map
|
||||
*.spec.{,c,m}js
|
||||
*.spec.{,c,m}js.map
|
||||
*.test.{,c,m}js
|
||||
*.test.{,c,m}js.map
|
||||
|
||||
__snapshots__/
|
||||
|
||||
/docs/
|
||||
/src/
|
||||
eslint.config.mjs
|
||||
81
@vates/syslog/README.md
Normal file
81
@vates/syslog/README.md
Normal file
@@ -0,0 +1,81 @@
|
||||
<!-- DO NOT EDIT MANUALLY, THIS FILE HAS BEEN GENERATED -->
|
||||
|
||||
# @vates/syslog
|
||||
|
||||
[](https://npmjs.org/package/@vates/syslog)  [](https://bundlephobia.com/result?p=@vates/syslog) [](https://npmjs.org/package/@vates/syslog)
|
||||
|
||||
> Syslog client: RFC 5424 messages over UDP, TCP or TLS, with a bounded buffer
|
||||
|
||||
## Install
|
||||
|
||||
Installation of the [npm package](https://npmjs.org/package/@vates/syslog):
|
||||
|
||||
```sh
|
||||
npm install --save @vates/syslog
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import { createClient } from '@vates/syslog'
|
||||
|
||||
const client = createClient('tls://syslog.company.lan', {
|
||||
appName: 'my-app',
|
||||
facility: 'authpriv',
|
||||
})
|
||||
|
||||
await client.send('something happened', { msgid: 'event', severity: 'warning' })
|
||||
|
||||
// closes the connection once the messages already sent are delivered, the client reconnects if it is used again
|
||||
client.close()
|
||||
```
|
||||
|
||||
### `createClient(uri, options)`
|
||||
|
||||
`uri`: `udp://host[:port]`, `tcp://host[:port]` or `tls://host[:port]`, the default port is `514`, or `6514` for TLS.
|
||||
|
||||
Options:
|
||||
|
||||
- `appName`: application name of the messages, `node` by default
|
||||
- `facility`: one of `FACILITIES` (e.g. `user`, `authpriv`, `local0`), `user` by default
|
||||
- `format`: `rfc5424` (default) or `rfc3164`
|
||||
- `framing` (TCP and TLS): `lf` (default, each message followed by a line feed, its own line breaks written as `\n` and `\r`) or `octet-counting` (each message prefixed by its length, required by some servers for TLS, see [RFC 5425](https://datatracker.ietf.org/doc/html/rfc5425#section-4.3))
|
||||
- `hostname`: host name of the messages, `os.hostname()` by default
|
||||
- `ca` (TLS): contents of PEM certificate authorities to trust, in addition to the system ones
|
||||
- `allowUnauthorized` (TLS): do not check the certificate of the server, `false` by default
|
||||
- `closeTimeout`: in milliseconds, how long `close()` waits for the messages already sent, `10e3` by default
|
||||
- `connectTimeout`: in milliseconds, `10e3` by default
|
||||
- `maxPendingBytes`: messages are rejected when more than this is waiting to be sent (e.g. the server is unreachable or does not read), `1 MiB` by default
|
||||
|
||||
A client uses at most one connection, opened when needed.
|
||||
|
||||
### `client.send(message, options)`
|
||||
|
||||
Returns a promise which resolves when the message has been handed to the system. Syslog has no acknowledgement: it does not mean that the server received it.
|
||||
|
||||
Options:
|
||||
|
||||
- `msgid`: message ID (RFC 5424 only)
|
||||
- `severity`: one of `SEVERITIES` (e.g. `error`, `warning`, `informational`), `informational` by default
|
||||
- `timestamp`: `Date` of the message, now by default
|
||||
|
||||
The promise rejects if the message cannot be sent, with the cause of the failure (e.g. connection refused, invalid certificate, too much data waiting).
|
||||
|
||||
### `client.close()`
|
||||
|
||||
Closes the connection, including a connection which is being established. The messages already sent are still delivered first (e.g. while the connection is being established or the host name resolved), for at most `closeTimeout`: after that, those not sent fail.
|
||||
|
||||
## Contributions
|
||||
|
||||
Contributions are _very_ welcomed, either on the documentation or on
|
||||
the code.
|
||||
|
||||
You may:
|
||||
|
||||
- report any [issue](https://github.com/vatesfr/xen-orchestra/issues)
|
||||
you've encountered;
|
||||
- fork and create a pull request.
|
||||
|
||||
## License
|
||||
|
||||
[ISC](https://spdx.org/licenses/ISC) © [Vates SAS](https://vates.fr)
|
||||
311
@vates/syslog/index.js
Normal file
311
@vates/syslog/index.js
Normal file
@@ -0,0 +1,311 @@
|
||||
'use strict'
|
||||
|
||||
const dgram = require('dgram')
|
||||
const net = require('net')
|
||||
const os = require('os')
|
||||
const splitHost = require('split-host')
|
||||
const tls = require('tls')
|
||||
|
||||
// https://datatracker.ietf.org/doc/html/rfc5424#section-6.2.1
|
||||
const FACILITIES = {
|
||||
__proto__: null,
|
||||
|
||||
kern: 0,
|
||||
user: 1,
|
||||
mail: 2,
|
||||
daemon: 3,
|
||||
auth: 4,
|
||||
syslog: 5,
|
||||
lpr: 6,
|
||||
news: 7,
|
||||
uucp: 8,
|
||||
cron: 9,
|
||||
authpriv: 10,
|
||||
ftp: 11,
|
||||
ntp: 12,
|
||||
audit: 13,
|
||||
alert: 14,
|
||||
clock: 15,
|
||||
local0: 16,
|
||||
local1: 17,
|
||||
local2: 18,
|
||||
local3: 19,
|
||||
local4: 20,
|
||||
local5: 21,
|
||||
local6: 22,
|
||||
local7: 23,
|
||||
}
|
||||
exports.FACILITIES = Object.keys(FACILITIES)
|
||||
|
||||
const SEVERITIES = {
|
||||
__proto__: null,
|
||||
|
||||
emergency: 0,
|
||||
alert: 1,
|
||||
critical: 2,
|
||||
error: 3,
|
||||
warning: 4,
|
||||
notice: 5,
|
||||
informational: 6,
|
||||
debug: 7,
|
||||
}
|
||||
exports.SEVERITIES = Object.keys(SEVERITIES)
|
||||
|
||||
const DEFAULT_PORTS = {
|
||||
tcp: 514,
|
||||
// https://datatracker.ietf.org/doc/html/rfc5425#section-4.1
|
||||
tls: 6514,
|
||||
udp: 514,
|
||||
}
|
||||
|
||||
// header fields must be printable ASCII characters without spaces
|
||||
const toHeaderField = (value, maxLength) =>
|
||||
String(value)
|
||||
.replace(/[^\x21-\x7e]/g, '_')
|
||||
.slice(0, maxLength) || '-'
|
||||
|
||||
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
||||
const pad2 = n => String(n).padStart(2, '0')
|
||||
|
||||
const FORMATTERS = {
|
||||
// https://datatracker.ietf.org/doc/html/rfc5424#section-6
|
||||
rfc5424: ({ appName, hostname, message, msgid, pri, timestamp }) =>
|
||||
`<${pri}>1 ${timestamp.toISOString()} ${hostname} ${appName} ${process.pid} ${msgid} - ${message}`,
|
||||
|
||||
// https://datatracker.ietf.org/doc/html/rfc3164#section-4.1, local time without year
|
||||
rfc3164: ({ appName, hostname, message, pri, timestamp }) =>
|
||||
`<${pri}>${MONTHS[timestamp.getMonth()]} ${String(timestamp.getDate()).padStart(2, ' ')} ${pad2(timestamp.getHours())}:${pad2(timestamp.getMinutes())}:${pad2(timestamp.getSeconds())} ${hostname} ${appName}[${process.pid}]: ${message}`,
|
||||
}
|
||||
|
||||
const FRAMERS = {
|
||||
// https://datatracker.ietf.org/doc/html/rfc6587#section-3.4.2
|
||||
//
|
||||
// a line feed ends the message: the line breaks it contains are written as
|
||||
// `\n` (and `\r`) so that it is received as one message, use octet-counting
|
||||
// to keep them
|
||||
lf: message => message.replace(/\r/g, '\\r').replace(/\n/g, '\\n') + '\n',
|
||||
|
||||
// https://datatracker.ietf.org/doc/html/rfc6587#section-3.4.1, required by RFC 5425 (TLS)
|
||||
'octet-counting': message => `${Buffer.byteLength(message)} ${message}`,
|
||||
}
|
||||
|
||||
const noop = Function.prototype
|
||||
|
||||
// connecting to a host name with several addresses (e.g. IPv4 and IPv6) fails
|
||||
// with an AggregateError without message, use the messages of its errors
|
||||
function withMessage(error) {
|
||||
if (error.message === '' && Array.isArray(error.errors) && error.errors.length !== 0) {
|
||||
error.message = error.errors.map(error => error.message).join(', ')
|
||||
|
||||
// the first line of the stack has been computed with the empty message
|
||||
if (typeof error.stack === 'string') {
|
||||
const i = error.stack.indexOf('\n')
|
||||
error.stack = `${error.name}: ${error.message}${i === -1 ? '' : error.stack.slice(i)}`
|
||||
}
|
||||
}
|
||||
return error
|
||||
}
|
||||
|
||||
// UDP sockets: callbacks of the datagrams not yet sent (e.g. host name being
|
||||
// resolved), the socket is closed once they are all called
|
||||
const pendingDatagrams = new WeakMap()
|
||||
|
||||
function closeUdpSocket(socket, timeout) {
|
||||
const pending = pendingDatagrams.get(socket)
|
||||
if (pending.size === 0) {
|
||||
socket.close()
|
||||
return
|
||||
}
|
||||
pending.closing = true
|
||||
// the datagrams which are still not sent then fail, so that no send() stays pending
|
||||
setTimeout(() => {
|
||||
if (pending.size !== 0) {
|
||||
const callbacks = Array.from(pending)
|
||||
pending.clear()
|
||||
socket.close()
|
||||
const error = new Error('the syslog client was closed before the message could be sent')
|
||||
callbacks.forEach(cb => cb(error))
|
||||
}
|
||||
}, timeout).unref()
|
||||
}
|
||||
|
||||
class SyslogClient {
|
||||
constructor(
|
||||
uri,
|
||||
{
|
||||
appName = 'node',
|
||||
ca,
|
||||
closeTimeout = 10e3,
|
||||
connectTimeout = 10e3,
|
||||
facility = 'user',
|
||||
format = 'rfc5424',
|
||||
framing = 'lf',
|
||||
allowUnauthorized = false,
|
||||
hostname = os.hostname(),
|
||||
maxPendingBytes = 1024 * 1024,
|
||||
} = {}
|
||||
) {
|
||||
const matches = /^(tcp|tls|udp):\/\/(.+)$/.exec(String(uri).trim())
|
||||
const protocol = matches?.[1]
|
||||
const { host, port = DEFAULT_PORTS[protocol] } = (matches !== null && splitHost(matches[2])) || {}
|
||||
if (!host) {
|
||||
throw new Error(
|
||||
`invalid syslog URI: ${uri}, it should be like udp://host[:port], tcp://host[:port] or tls://host[:port]`
|
||||
)
|
||||
}
|
||||
if (!(facility in FACILITIES)) {
|
||||
throw new Error(`invalid syslog facility: ${facility}`)
|
||||
}
|
||||
if (!(format in FORMATTERS)) {
|
||||
throw new Error(`invalid syslog format: ${format}`)
|
||||
}
|
||||
if (!(framing in FRAMERS)) {
|
||||
throw new Error(`invalid syslog framing: ${framing}`)
|
||||
}
|
||||
|
||||
this._appName = toHeaderField(appName, 48)
|
||||
this._closeTimeout = closeTimeout
|
||||
this._connectTimeout = connectTimeout
|
||||
this._facility = FACILITIES[facility]
|
||||
this._format = FORMATTERS[format]
|
||||
this._frame = FRAMERS[framing]
|
||||
this._host = host
|
||||
this._hostname = toHeaderField(hostname, 255)
|
||||
this._maxPendingBytes = maxPendingBytes
|
||||
this._port = port
|
||||
this._send = protocol === 'udp' ? this._sendUdp : this._sendStream
|
||||
this._tlsOptions =
|
||||
protocol === 'tls'
|
||||
? {
|
||||
// the `ca` option replaces the system certificate authorities
|
||||
ca: ca === undefined ? undefined : [...tls.rootCertificates, ...ca],
|
||||
rejectUnauthorized: !allowUnauthorized,
|
||||
}
|
||||
: undefined
|
||||
|
||||
// at most one socket at a time, created when needed
|
||||
this._socket = undefined
|
||||
}
|
||||
|
||||
// the messages already sent are still delivered (e.g. a connection which is
|
||||
// being established, or a host name which is being resolved), then the socket
|
||||
// is closed, at most closeTimeout later
|
||||
//
|
||||
// the client can still be used afterwards, it will reconnect
|
||||
close() {
|
||||
const socket = this._socket
|
||||
if (socket === undefined) {
|
||||
return
|
||||
}
|
||||
this._socket = undefined
|
||||
|
||||
if (socket instanceof net.Socket) {
|
||||
// the pending writes are flushed before the connection is ended
|
||||
socket.end()
|
||||
const timeout = setTimeout(() => socket.destroy(), this._closeTimeout)
|
||||
timeout.unref()
|
||||
socket.once('close', () => clearTimeout(timeout))
|
||||
} else {
|
||||
closeUdpSocket(socket, this._closeTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
// resolves when the message has been handed to the system, which does not
|
||||
// mean that the server received it (syslog has no acknowledgement)
|
||||
send(message, { msgid = '-', severity = 'informational', timestamp = new Date() } = {}) {
|
||||
if (!(severity in SEVERITIES)) {
|
||||
return Promise.reject(new Error(`invalid syslog severity: ${severity}`))
|
||||
}
|
||||
|
||||
const formatted = this._format({
|
||||
appName: this._appName,
|
||||
hostname: this._hostname,
|
||||
message: String(message),
|
||||
msgid: toHeaderField(msgid, 32),
|
||||
pri: this._facility * 8 + SEVERITIES[severity],
|
||||
timestamp,
|
||||
})
|
||||
return new Promise((resolve, reject) => {
|
||||
this._send(formatted, error => (error == null ? resolve() : reject(error)))
|
||||
})
|
||||
}
|
||||
|
||||
// TCP and TLS
|
||||
_sendStream(message, cb) {
|
||||
let socket = this._socket
|
||||
if (socket === undefined) {
|
||||
const tlsOptions = this._tlsOptions
|
||||
const options = { host: this._host, port: this._port }
|
||||
socket = this._socket =
|
||||
tlsOptions === undefined ? net.connect(options) : tls.connect({ ...tlsOptions, ...options })
|
||||
|
||||
// a connection attempt that never completes (e.g. packets dropped by a
|
||||
// firewall, or a TLS handshake without answer) must not stay around
|
||||
//
|
||||
// not socket.setTimeout() which is an inactivity timeout, postponed by
|
||||
// each write during a TLS handshake
|
||||
const connectTimeout = setTimeout(() => socket.destroy(new Error('connection timed out')), this._connectTimeout)
|
||||
connectTimeout.unref()
|
||||
socket.on(tlsOptions === undefined ? 'connect' : 'secureConnect', () => {
|
||||
clearTimeout(connectTimeout)
|
||||
|
||||
// detect a server which disappeared without closing the connection
|
||||
socket.setKeepAlive(true, 60e3)
|
||||
})
|
||||
|
||||
// errors are passed to the callbacks of the pending writes
|
||||
socket.on('error', noop)
|
||||
socket.on('close', () => {
|
||||
clearTimeout(connectTimeout)
|
||||
if (this._socket === socket) {
|
||||
this._socket = undefined
|
||||
}
|
||||
})
|
||||
|
||||
// do not prevent the process from stopping
|
||||
socket.unref()
|
||||
}
|
||||
|
||||
// messages are dropped when too much is waiting to be sent (e.g. the server
|
||||
// is unreachable or no longer reads), instead of filling the memory, this
|
||||
// also covers the messages written while the connection is being established
|
||||
if (socket.writableLength > this._maxPendingBytes) {
|
||||
cb(new Error('too much data waiting to be sent to the syslog server'))
|
||||
return
|
||||
}
|
||||
|
||||
// pending writes fail with a generic error when the connection fails, report its cause instead
|
||||
socket.write(this._frame(message), error => cb(error == null ? error : withMessage(socket.errored ?? error)))
|
||||
}
|
||||
|
||||
// one message per datagram, see https://datatracker.ietf.org/doc/html/rfc5426
|
||||
_sendUdp(message, cb) {
|
||||
let socket = this._socket
|
||||
if (socket === undefined) {
|
||||
socket = this._socket = dgram.createSocket(net.isIPv6(this._host) ? 'udp6' : 'udp4')
|
||||
pendingDatagrams.set(socket, new Set())
|
||||
|
||||
// errors are passed to the callbacks of send(), a failed socket is replaced
|
||||
socket.on('error', () => {
|
||||
if (this._socket === socket) {
|
||||
this.close()
|
||||
}
|
||||
})
|
||||
socket.unref()
|
||||
}
|
||||
|
||||
const pending = pendingDatagrams.get(socket)
|
||||
pending.add(cb)
|
||||
socket.send(message, this._port, this._host, error => {
|
||||
// otherwise, already failed by closeUdpSocket()
|
||||
if (pending.delete(cb)) {
|
||||
if (pending.size === 0 && pending.closing) {
|
||||
socket.close()
|
||||
}
|
||||
cb(error)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
exports.createClient = (uri, opts) => new SyslogClient(uri, opts)
|
||||
286
@vates/syslog/index.test.mjs
Normal file
286
@vates/syslog/index.test.mjs
Normal file
@@ -0,0 +1,286 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import dgram from 'node:dgram'
|
||||
import fs from 'node:fs'
|
||||
import net from 'node:net'
|
||||
import tls from 'node:tls'
|
||||
import { after, describe, it } from 'node:test'
|
||||
import { setTimeout as wait } from 'node:timers/promises'
|
||||
|
||||
import { createClient, FACILITIES, SEVERITIES } from './index.js'
|
||||
|
||||
const fixture = name => fs.readFileSync(new URL(`./tests/${name}`, import.meta.url), 'utf8')
|
||||
const CA = fixture('ca.pem')
|
||||
|
||||
const servers = []
|
||||
after(() => servers.forEach(server => server.close()))
|
||||
|
||||
const listen = server =>
|
||||
new Promise(resolve => {
|
||||
servers.push(server)
|
||||
server.listen(0, '127.0.0.1', () => resolve(server.address().port))
|
||||
})
|
||||
|
||||
// stream server collecting the received data
|
||||
async function createStreamServer(createServer = net.createServer) {
|
||||
const server = { data: '', sockets: new Set() }
|
||||
server.port = await listen(
|
||||
createServer(socket => {
|
||||
server.sockets.add(socket)
|
||||
socket.on('close', () => server.sockets.delete(socket))
|
||||
socket.on('data', data => (server.data += data))
|
||||
})
|
||||
)
|
||||
return server
|
||||
}
|
||||
|
||||
async function createUdpServer() {
|
||||
const messages = []
|
||||
const socket = dgram.createSocket('udp4').on('message', message => messages.push(message.toString()))
|
||||
servers.push(socket)
|
||||
await new Promise(resolve => socket.bind(0, '127.0.0.1', resolve))
|
||||
return { messages, port: socket.address().port }
|
||||
}
|
||||
|
||||
describe('createClient()', () => {
|
||||
it('rejects invalid URIs and options', () => {
|
||||
for (const uri of ['syslog.lan', 'http://syslog.lan', 'udp://', 'udp://:514', undefined]) {
|
||||
assert.throws(() => createClient(uri), /invalid syslog URI/, String(uri))
|
||||
}
|
||||
assert.throws(() => createClient('udp://h', { facility: 'foo' }), /invalid syslog facility/)
|
||||
assert.throws(() => createClient('udp://h', { format: 'foo' }), /invalid syslog format/)
|
||||
assert.throws(() => createClient('udp://h', { framing: 'foo' }), /invalid syslog framing/)
|
||||
})
|
||||
|
||||
it('exposes the facility and severity names', () => {
|
||||
assert(FACILITIES.includes('authpriv'))
|
||||
assert.equal(SEVERITIES[4], 'warning')
|
||||
})
|
||||
})
|
||||
|
||||
describe('send()', () => {
|
||||
it('formats RFC 5424 messages', async () => {
|
||||
const server = await createUdpServer()
|
||||
const client = createClient(` udp://127.0.0.1:${server.port} `, {
|
||||
appName: 'my app',
|
||||
facility: 'authpriv',
|
||||
hostname: 'host',
|
||||
})
|
||||
await client.send('{"foo":"bar"}', {
|
||||
msgid: 'some event',
|
||||
severity: 'warning',
|
||||
timestamp: new Date('2026-01-02T03:04:05.678Z'),
|
||||
})
|
||||
await wait(50)
|
||||
client.close()
|
||||
|
||||
// header fields are sanitized: no spaces
|
||||
assert.deepEqual(server.messages, [
|
||||
`<84>1 2026-01-02T03:04:05.678Z host my_app ${process.pid} some_event - {"foo":"bar"}`,
|
||||
])
|
||||
})
|
||||
|
||||
it('formats RFC 3164 messages', async () => {
|
||||
const server = await createUdpServer()
|
||||
const client = createClient(`udp://127.0.0.1:${server.port}`, {
|
||||
appName: 'app',
|
||||
format: 'rfc3164',
|
||||
hostname: 'host',
|
||||
})
|
||||
await client.send('message', { timestamp: new Date(2026, 0, 2, 3, 4, 5) })
|
||||
await wait(50)
|
||||
client.close()
|
||||
|
||||
assert.deepEqual(server.messages, [`<14>Jan 2 03:04:05 host app[${process.pid}]: message`])
|
||||
})
|
||||
|
||||
it('rejects an invalid severity', async () => {
|
||||
await assert.rejects(
|
||||
createClient('udp://127.0.0.1').send('message', { severity: 'foo' }),
|
||||
/invalid syslog severity/
|
||||
)
|
||||
})
|
||||
|
||||
it('frames TCP messages with a line feed by default', async () => {
|
||||
const server = await createStreamServer()
|
||||
const client = createClient(`tcp://127.0.0.1:${server.port}`)
|
||||
await client.send('foo')
|
||||
await client.send('bar')
|
||||
await wait(50)
|
||||
client.close()
|
||||
|
||||
assert.match(server.data, /^<14>1 [^\n]+ - foo\n<14>1 [^\n]+ - bar\n$/)
|
||||
})
|
||||
|
||||
it('escapes the line breaks of a message framed with a line feed', async () => {
|
||||
const server = await createStreamServer()
|
||||
const client = createClient(`tcp://127.0.0.1:${server.port}`)
|
||||
await client.send('foo\r\nbar\nbaz')
|
||||
await wait(50)
|
||||
client.close()
|
||||
|
||||
assert.match(server.data, /^<14>1 [^\n]+ - foo\\r\\nbar\\nbaz\n$/)
|
||||
})
|
||||
|
||||
it('frames TCP messages with octet counting', async () => {
|
||||
const server = await createStreamServer()
|
||||
const client = createClient(`tcp://127.0.0.1:${server.port}`, { framing: 'octet-counting' })
|
||||
await client.send('foo')
|
||||
await client.send('b\när')
|
||||
await wait(50)
|
||||
client.close()
|
||||
|
||||
// each message is prefixed by its length in bytes
|
||||
const buffer = Buffer.from(server.data)
|
||||
const messages = []
|
||||
for (let offset = 0; offset < buffer.length; ) {
|
||||
const space = buffer.indexOf(' ', offset)
|
||||
const length = Number(buffer.toString('utf8', offset, space))
|
||||
messages.push(buffer.toString('utf8', space + 1, space + 1 + length))
|
||||
offset = space + 1 + length
|
||||
}
|
||||
assert.equal(messages.length, 2)
|
||||
assert.match(messages[0], /^<14>1 .+ - foo$/)
|
||||
assert.match(messages[1], /^<14>1 .+ - b\när$/s)
|
||||
})
|
||||
|
||||
it('reconnects when the server closes the connection', async () => {
|
||||
const server = await createStreamServer()
|
||||
const client = createClient(`tcp://127.0.0.1:${server.port}`)
|
||||
await client.send('first')
|
||||
await wait(50)
|
||||
server.sockets.forEach(socket => socket.destroy())
|
||||
await wait(50)
|
||||
await client.send('second')
|
||||
await wait(50)
|
||||
client.close()
|
||||
|
||||
assert.match(server.data, /first\n.*second\n$/s)
|
||||
})
|
||||
|
||||
it('reports why the connection failed', async () => {
|
||||
await assert.rejects(createClient('tcp://127.0.0.1:1').send('message'), { code: 'ECONNREFUSED' })
|
||||
|
||||
// several addresses (::1 and 127.0.0.1) fail with an AggregateError, which has no message by itself
|
||||
await assert.rejects(createClient('tcp://localhost:1').send('message'), error => {
|
||||
assert.match(error.message, /ECONNREFUSED/)
|
||||
assert(error.stack.startsWith(`${error.name}: ${error.message}\n`))
|
||||
return true
|
||||
})
|
||||
})
|
||||
|
||||
it('stops connecting after connectTimeout, with a single socket', async () => {
|
||||
// accepts the TCP connection but never answers the TLS handshake
|
||||
const port = await listen(net.createServer(socket => socket.resume()))
|
||||
const client = createClient(`tls://127.0.0.1:${port}`, { connectTimeout: 200 })
|
||||
const sockets = new Set()
|
||||
|
||||
const start = Date.now()
|
||||
const result = client.send('message').catch(error => error)
|
||||
sockets.add(client._socket)
|
||||
client.send('message').catch(Function.prototype)
|
||||
sockets.add(client._socket)
|
||||
|
||||
assert.equal((await result).message, 'connection timed out')
|
||||
assert(Date.now() - start < 1e3)
|
||||
assert.equal(sockets.size, 1)
|
||||
})
|
||||
|
||||
it('drops messages when too much is waiting to be sent', async () => {
|
||||
// never reads
|
||||
const port = await listen(net.createServer(socket => socket.pause()))
|
||||
const client = createClient(`tcp://127.0.0.1:${port}`, { closeTimeout: 200, maxPendingBytes: 64 * 1024 })
|
||||
|
||||
const message = 'x'.repeat(1024)
|
||||
const results = []
|
||||
for (let i = 0; i < 10e3; ++i) {
|
||||
results.push(
|
||||
client.send(message).then(
|
||||
() => 'sent',
|
||||
() => 'dropped'
|
||||
)
|
||||
)
|
||||
}
|
||||
await wait(500)
|
||||
client.close()
|
||||
|
||||
assert(client._socket === undefined)
|
||||
assert((await Promise.all(results)).includes('dropped'))
|
||||
})
|
||||
|
||||
it('delivers the messages of a connection which is being established, then closes it', async () => {
|
||||
const server = await createStreamServer()
|
||||
const client = createClient(`tcp://127.0.0.1:${server.port}`)
|
||||
const sent = client.send('message')
|
||||
const socket = client._socket
|
||||
client.close()
|
||||
|
||||
await sent
|
||||
await new Promise(resolve => (socket.closed ? resolve() : socket.once('close', resolve)))
|
||||
assert.match(server.data, /message\n$/)
|
||||
assert(socket.destroyed)
|
||||
assert.equal(client._socket, undefined)
|
||||
})
|
||||
|
||||
it('delivers the datagrams whose host name is being resolved, then closes the socket', async () => {
|
||||
const server = await createUdpServer()
|
||||
const client = createClient(`udp://localhost:${server.port}`)
|
||||
const sent = client.send('message')
|
||||
client.close()
|
||||
|
||||
await sent
|
||||
await wait(100)
|
||||
assert.equal(server.messages.length, 1)
|
||||
assert.match(server.messages[0], /message$/)
|
||||
})
|
||||
|
||||
it('stops waiting for the pending writes after closeTimeout', async () => {
|
||||
// never reads
|
||||
const port = await listen(net.createServer(socket => socket.pause()))
|
||||
const client = createClient(`tcp://127.0.0.1:${port}`, { closeTimeout: 200, maxPendingBytes: Infinity })
|
||||
const results = []
|
||||
for (let i = 0; i < 1e3; ++i) {
|
||||
results.push(client.send('x'.repeat(64 * 1024)).then(Function.prototype, error => error))
|
||||
}
|
||||
const socket = client._socket
|
||||
client.close()
|
||||
|
||||
const start = Date.now()
|
||||
await Promise.all(results)
|
||||
assert(Date.now() - start < 2e3)
|
||||
assert(socket.destroyed)
|
||||
})
|
||||
})
|
||||
|
||||
describe('TLS', () => {
|
||||
const createTlsServer = () =>
|
||||
createStreamServer(listener =>
|
||||
tls.createServer({ cert: fixture('server.pem'), key: fixture('server.key') }, listener)
|
||||
)
|
||||
|
||||
it('trusts additional certificate authorities', async () => {
|
||||
const server = await createTlsServer()
|
||||
const client = createClient(`tls://localhost:${server.port}`, { ca: [CA] })
|
||||
await client.send('message')
|
||||
await wait(50)
|
||||
client.close()
|
||||
|
||||
assert.match(server.data, / - message\n$/)
|
||||
})
|
||||
|
||||
it('rejects an unknown certificate authority, unless allowUnauthorized', async () => {
|
||||
const server = await createTlsServer()
|
||||
await assert.rejects(createClient(`tls://localhost:${server.port}`).send('message'), {
|
||||
code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE',
|
||||
})
|
||||
|
||||
const client = createClient(`tls://localhost:${server.port}`, { allowUnauthorized: true })
|
||||
await client.send('message')
|
||||
client.close()
|
||||
})
|
||||
|
||||
it('defaults to port 6514', () => {
|
||||
assert.equal(createClient('tls://localhost')._port, 6514)
|
||||
assert.equal(createClient('tcp://[::1]')._port, 514)
|
||||
assert.equal(createClient('tcp://[::1]')._host, '::1')
|
||||
})
|
||||
})
|
||||
33
@vates/syslog/package.json
Normal file
33
@vates/syslog/package.json
Normal file
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"private": false,
|
||||
"name": "@vates/syslog",
|
||||
"description": "Syslog client: RFC 5424 messages over UDP, TCP or TLS, with a bounded buffer",
|
||||
"keywords": [
|
||||
"rfc5424",
|
||||
"syslog",
|
||||
"tls"
|
||||
],
|
||||
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@vates/syslog",
|
||||
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
|
||||
"repository": {
|
||||
"directory": "@vates/syslog",
|
||||
"type": "git",
|
||||
"url": "https://github.com/vatesfr/xen-orchestra.git"
|
||||
},
|
||||
"author": {
|
||||
"name": "Vates SAS",
|
||||
"url": "https://vates.fr"
|
||||
},
|
||||
"license": "ISC",
|
||||
"version": "0.1.0",
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"dependencies": {
|
||||
"split-host": "^2.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public",
|
||||
"test": "node --test"
|
||||
}
|
||||
}
|
||||
19
@vates/syslog/tests/ca.pem
Normal file
19
@vates/syslog/tests/ca.pem
Normal file
@@ -0,0 +1,19 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIDITCCAgmgAwIBAgIUGikUHbKIfotmlkV7oGy7YubSDn8wDQYJKoZIhvcNAQEL
|
||||
BQAwHzEdMBsGA1UEAwwUdmF0ZXMtc3lzbG9nIHRlc3QgQ0EwIBcNMjYxMDE4MTAx
|
||||
MjI1WhgPMjEyNjA5MjQxMDEyMjVaMB8xHTAbBgNVBAMMFHZhdGVzLXN5c2xvZyB0
|
||||
ZXN0IENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn/lGtdLEE8tf
|
||||
gb+hSPe8xLOlGh7ZzRiGcKjkcCbCNFNnjjOa29Lki8ksFD7Su2BWIHpxAzPhEnlQ
|
||||
9xq7+M9dayM2CslQPuOfKEEyQIBWG9OggXT3kyTn0BVHNFfiGzJlyRz6fIfLf/sU
|
||||
e2d9AWZhP9I53HpKaNMHyxwuzEBWFxmpv1t7LRVwhrcQjsnoZqxlukpsGyKkT4gs
|
||||
TNH5/hFhZzmnvfFPhTzQvOGSvFQqgUgd5uxidDmImoRoGA9Ip3bqwMkT99euNrgc
|
||||
jsxnXeV8pdtHmz4kjQRp+/YIIoh7pA73389OeBx7LC9wpZo1z+3AVKlCAGZ8V9Il
|
||||
Izk3e7FXTQIDAQABo1MwUTAdBgNVHQ4EFgQUTx7dqoC2XIwpwW0MYcZsoXHqztww
|
||||
HwYDVR0jBBgwFoAUTx7dqoC2XIwpwW0MYcZsoXHqztwwDwYDVR0TAQH/BAUwAwEB
|
||||
/zANBgkqhkiG9w0BAQsFAAOCAQEARYUd0q9VwgmZGdhhqks1xCipBuW1eWwBa0jk
|
||||
Y0jZjhxeq9F0KBMVoazRjjKoL6cwpjvUKytarnN8+kj/7U4dMPLX+/bcpEjwo1gW
|
||||
DI7MtBI+e1+IohWujDtCqyAiPYpWlwlTTC4kMdfjDH4fbYNY6UHUnedPdSwN3HAp
|
||||
ZtMb1RoT+gtMedpbnQJKTwxAwH5cBMjTgKeuxeHo6WWjB6evyaLjQINNGLDgLxUC
|
||||
i4+o0rNYCPtJV3uqw9egDkgoAtMI9mY6snMUxaHGz2JhGzT1wu/UXPrkbFYxCYpm
|
||||
BGxY36taw81Mr+6n0V1ScWhnGEgPOe3BJCYtoB964YDYfvcFLA==
|
||||
-----END CERTIFICATE-----
|
||||
28
@vates/syslog/tests/server.key
Normal file
28
@vates/syslog/tests/server.key
Normal file
@@ -0,0 +1,28 @@
|
||||
-----BEGIN PRIVATE KEY-----
|
||||
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCL0wuMNV3AkIhP
|
||||
gt4eQ9IPbOkbZiyNOgQWanaRr1P5XeEz4wbWH03MNf9gUDbR2ToGJLfxU68upFYo
|
||||
uQRCRHRySdbzvhc5+OYbQ2/QvucGKaMnYuerxvENgnh2eLg5QG+Z50M8+IK+jK4t
|
||||
YJjECe5nefjdtGKQf1h4xgVJp2CQW2zKyfOBqjgcB6bOISDni9+1ssWDkcgVnqyB
|
||||
8o/zA11CFfO1AL0OBbpD6Fk9HZ29hODCQLOq01vIrfpV2H8S/hsik4tUtMgM10y5
|
||||
7LNckDzd8LbZQiBUJ/Q63pWDN19SqAW9lsonE3aKJANvew2s/NRtU7EKIeKLvf3a
|
||||
HPJH83QBAgMBAAECggEABrXXOphrTlj1XL0A5kqOkchSF9s3gUPW80Ey3Wcg2GFo
|
||||
s9f18ZfdIMM45T02Q0wJtoajuAKCUFKJUsEd1uUQglVgOjB9zn2/I9DwxF+sCEn+
|
||||
Yy8SQ5Z7pjCEMdnVbAR3UqdUTRBXXHgVihaWIEbifM9wCcpM1mbRJvMoaO5uIkFZ
|
||||
ztlYElhFC+ITpm8Pjs/fQPNlA8DPZO3xHRSx+M0+tNw3ba7SaFvYTA2o/sMOx1Yq
|
||||
pyWf7yq6kPDE3vO6NfO8uYrfkw/eNioQ3jZh9tlH/87hm1ucXFOHKxdCpabv2COJ
|
||||
YIOqIcXlRbXlzVugDY6IOdBcCvUM6DdB+iyOx2m6UQKBgQDEyHoCCJZhLf7ynsmu
|
||||
onmonWGojX6EwDgpvoey2Lhbr9j7gkCkx7TF+CNRN4AM4o+K9x+m8rF2aJEXKgof
|
||||
GNwYPpBaUwuV2y+A9wiYtM+o9D4pTy2JPzlfyj7zWIQtOzrIkgSz9BH7QYRA51it
|
||||
L0d9O6myUs2IILcXdb7gTePA0QKBgQC15qgJ4DeDq+VygQi1VW6Xn2DhiAy1frDY
|
||||
etG3We+PIKYOc9XN79hI7cSXAy9DYwX7KTQJ13YDYDKNuEEGlijatxMruJv2Zvdm
|
||||
5CfgNzw+QjFfOM0AXsp9/F4xiiSiUAvLJeZzN7BVPQ/7KUz6kMxtr8RorP66NSFt
|
||||
zplrq6XMMQKBgQCP+EyraDT9bHV2dI4B4oU3w81LUBocbbWpdOg7BSv8iK8rc6Rv
|
||||
6lxr6puMNBrPUoLKnPrfXfhQNTjRxcQZ9EoywrNCIjf7r50Vsabscnzrk6ZW+sLK
|
||||
AyOq3YEo8L3jic38KDigoBT3yntEMP5D1q9rBFenS8VrruPFmjPIT7QtwQKBgQCd
|
||||
To/bmo/nyuxujbBeQTeW4DoEUIfNHXcKf4yFp71IZc+1h+9KXTUKPLVac2KYQDaX
|
||||
8hYSvMZOPQ77CHETpSBHpTNrCAe4m2sxC/aALMRogw33b0O6Ij+znGBJL1JnHBpW
|
||||
4LvewDY6QMhlSF5OApMH+I0+iPzxQ6lZMOX0+0gywQKBgEGcCeJBvqpnSOyMdOFu
|
||||
MIcLhGa2SIttij1jz7eS9zD+VeONHjmn3Atn7MRagddrYeIGy6ZHUFFsSWBbVqwB
|
||||
cfT6o0GOeNx0gQn185SUBxB3Oxfg4xWBA0q0GVNFqaScKiXk1IvTj/GTuXM7rehi
|
||||
HjK6iamDr8lIJE/JFdn2K8ge
|
||||
-----END PRIVATE KEY-----
|
||||
20
@vates/syslog/tests/server.pem
Normal file
20
@vates/syslog/tests/server.pem
Normal file
@@ -0,0 +1,20 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIDMzCCAhugAwIBAgIUSGpRtJFiD61hQZBOp49sq7GLkIswDQYJKoZIhvcNAQEL
|
||||
BQAwHzEdMBsGA1UEAwwUdmF0ZXMtc3lzbG9nIHRlc3QgQ0EwIBcNMjYxMDE4MTAx
|
||||
MjI1WhgPMjEyNjA5MjQxMDEyMjVaMBQxEjAQBgNVBAMMCWxvY2FsaG9zdDCCASIw
|
||||
DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAIvTC4w1XcCQiE+C3h5D0g9s6Rtm
|
||||
LI06BBZqdpGvU/ld4TPjBtYfTcw1/2BQNtHZOgYkt/FTry6kVii5BEJEdHJJ1vO+
|
||||
Fzn45htDb9C+5wYpoydi56vG8Q2CeHZ4uDlAb5nnQzz4gr6Mri1gmMQJ7md5+N20
|
||||
YpB/WHjGBUmnYJBbbMrJ84GqOBwHps4hIOeL37WyxYORyBWerIHyj/MDXUIV87UA
|
||||
vQ4FukPoWT0dnb2E4MJAs6rTW8it+lXYfxL+GyKTi1S0yAzXTLnss1yQPN3wttlC
|
||||
IFQn9DrelYM3X1KoBb2WyicTdookA297Daz81G1TsQoh4ou9/doc8kfzdAECAwEA
|
||||
AaNwMG4wLAYDVR0RBCUwI4IJbG9jYWxob3N0hwR/AAABhxAAAAAAAAAAAAAAAAAA
|
||||
AAABMB0GA1UdDgQWBBTCMy+a1aPNwrF3QYPIAIR4iDazWTAfBgNVHSMEGDAWgBRP
|
||||
Ht2qgLZcjCnBbQxhxmyhcerO3DANBgkqhkiG9w0BAQsFAAOCAQEAPWBHmtiuPwFb
|
||||
z0DIdTOA7VvFziMyJzW0EvI2AmB2wSViPGg5OFqVOLq5+H3OJsi5yTYJaUjNpr4A
|
||||
UjpMweJJlMkqwsTt/kBKpPiUQbiN7Ja2+b3mmmMhEfyMUSebcfegLNvglABhJlmv
|
||||
FOwniX8J1LJ8fzxAHfF32HwqxG6vxcZOsP661WqSnkyDSuoHo/jwB4MeCZiljc8h
|
||||
F4MkcaIGouTtAGMvb8V6bGNz1iWgkQVpfYP9uqKGq8eBXzd+/033RmrnqorHjYiz
|
||||
38C1Gf3lhbXBQqmMY3+aEhvjSCJrLCfjMV/twDTwRp8khrkG7C9yuEqmXtbTcF1e
|
||||
/+rsiTEv9A==
|
||||
-----END CERTIFICATE-----
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@vates/types",
|
||||
"private": false,
|
||||
"version": "2.3.0",
|
||||
"version": "2.5.0",
|
||||
"main": "./dist/index.mjs",
|
||||
"exports": {
|
||||
".": {
|
||||
@@ -11,6 +11,10 @@
|
||||
"./xo": "./dist/xo.d.mts",
|
||||
"./xo-app": "./dist/xo-app.d.mts",
|
||||
"./xen-api": "./dist/xen-api.d.mts",
|
||||
"./backup-expression-context": {
|
||||
"default": "./dist/backup-expression-context.mjs",
|
||||
"types": "./dist/backup-expression-context.d.mts"
|
||||
},
|
||||
"./common": {
|
||||
"default": "./dist/common.mjs",
|
||||
"types": "./dist/common.d.mts"
|
||||
@@ -34,7 +38,7 @@
|
||||
"typescript": "~5.6.3",
|
||||
"typescript-eslint": "^8.61.0",
|
||||
"@types/express": "^5.0.0",
|
||||
"@xen-orchestra/disk-transform": "^1.3.3"
|
||||
"@xen-orchestra/disk-transform": "^1.3.4"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
|
||||
38
@vates/types/src/backup-expression-context.mts
Normal file
38
@vates/types/src/backup-expression-context.mts
Normal file
@@ -0,0 +1,38 @@
|
||||
export const BACKUP_EXPRESSION_CONTEXT = {
|
||||
run: {
|
||||
dayOfWeek: { type: 'number' },
|
||||
dayOfMonth: { type: 'number' },
|
||||
hour: { type: 'number' },
|
||||
month: { type: 'number' },
|
||||
year: { type: 'number' },
|
||||
},
|
||||
sr: {
|
||||
name_label: { type: 'string' },
|
||||
type: { type: 'string' },
|
||||
description: { type: 'string' },
|
||||
tags: { type: 'string[]' },
|
||||
},
|
||||
backupRepository: {
|
||||
name: { type: 'string' },
|
||||
type: { type: 'string' },
|
||||
tags: { type: 'string[]' },
|
||||
},
|
||||
vm: {
|
||||
name_label: { type: 'string' },
|
||||
tags: { type: 'string[]' },
|
||||
power_state: { type: 'string' },
|
||||
},
|
||||
chainLength: { type: 'number' },
|
||||
} as const
|
||||
|
||||
export type BACKUP_EXPRESSION_CONTEXT = (typeof BACKUP_EXPRESSION_CONTEXT)[keyof typeof BACKUP_EXPRESSION_CONTEXT]
|
||||
|
||||
export const BACKUP_SETTING_CONTEXT = {
|
||||
healthCheckVmsWithTags: ['vm', 'run'],
|
||||
fullInterval: ['vm', 'run', 'chainLength'],
|
||||
vmFilter: ['vm', 'run'],
|
||||
srs: ['sr', 'run'],
|
||||
remotes: ['backupRepository', 'run'],
|
||||
} as const
|
||||
|
||||
export type BACKUP_SETTING_CONTEXT = (typeof BACKUP_SETTING_CONTEXT)[keyof typeof BACKUP_SETTING_CONTEXT]
|
||||
@@ -663,6 +663,19 @@ export const SUPPORTED_VDI_FORMAT = {
|
||||
|
||||
export type SUPPORTED_VDI_FORMAT = (typeof SUPPORTED_VDI_FORMAT)[keyof typeof SUPPORTED_VDI_FORMAT]
|
||||
|
||||
/**
|
||||
* Formats a VDI can be exported to by XO: the XAPI ones, plus the ones XO generates
|
||||
* itself from the blocks of the disk.
|
||||
*/
|
||||
export const SUPPORTED_VDI_EXPORT_FORMAT = {
|
||||
raw: 'raw',
|
||||
vhd: 'vhd',
|
||||
qcow2: 'qcow2',
|
||||
vmdk: 'vmdk',
|
||||
} as const
|
||||
|
||||
export type SUPPORTED_VDI_EXPORT_FORMAT = (typeof SUPPORTED_VDI_EXPORT_FORMAT)[keyof typeof SUPPORTED_VDI_EXPORT_FORMAT]
|
||||
|
||||
// ----- XAPI Stats
|
||||
|
||||
type XapiStatsResponse<T> = {
|
||||
@@ -759,6 +772,7 @@ export type RawTrafficRule = {
|
||||
ipRange: string
|
||||
direction: TrafficRuleDirection
|
||||
port?: string
|
||||
priority?: number
|
||||
}
|
||||
|
||||
type BaseTrafficRule = RawTrafficRule & {
|
||||
|
||||
@@ -4,5 +4,8 @@ export * from './xo.mjs'
|
||||
export * from './xo-app.mjs'
|
||||
export * from './lib/xen-orchestra-xapi.mjs'
|
||||
export * from './lib/xen-orchestra-acl.mjs'
|
||||
export * from './lib/xen-orchestra-plugins.mjs'
|
||||
export * from './lib/xen-orchestra-v2v.mjs'
|
||||
export * from './lib/complex-matcher.mjs'
|
||||
export * from './lib/rest-api.mjs'
|
||||
export * from './lib/xen-orchestra-vmware-explorer.mjs'
|
||||
|
||||
1261
@vates/types/src/lib/xen-orchestra-plugins.mts
Normal file
1261
@vates/types/src/lib/xen-orchestra-plugins.mts
Normal file
File diff suppressed because it is too large
Load Diff
219
@vates/types/src/lib/xen-orchestra-v2v.mts
Normal file
219
@vates/types/src/lib/xen-orchestra-v2v.mts
Normal file
@@ -0,0 +1,219 @@
|
||||
import type { Branded } from '../common.mjs'
|
||||
import type { XoNetwork, XoSr, XoVmTemplate } from '../xo.mjs'
|
||||
|
||||
// Stateless API: requests needing the source carry the connection in a POST body (never in URLs or access logs) and
|
||||
// xo-server stores nothing it reads from the source.
|
||||
|
||||
export const V2V_PLATFORM = {
|
||||
VMWARE: 'vmware',
|
||||
} as const
|
||||
|
||||
export type V2V_PLATFORM = (typeof V2V_PLATFORM)[keyof typeof V2V_PLATFORM]
|
||||
|
||||
// 422 rather than 401 for invalid source credentials: a 401 would open the browser Basic authentication prompt
|
||||
export const V2V_ERROR_CODE = {
|
||||
INVALID_CREDENTIALS: 'V2V_INVALID_CREDENTIALS',
|
||||
} as const
|
||||
|
||||
export type V2V_ERROR_CODE = (typeof V2V_ERROR_CODE)[keyof typeof V2V_ERROR_CODE]
|
||||
|
||||
// Clones, restores and replicas inherit `other_config`: the imported VM records its own UUID to be told apart from
|
||||
// them, and the source identity because a moref is only unique within one vCenter. The source identity is the
|
||||
// inventory `instanceUuid`, or `<lowercased host>:<port>` for a standalone ESXi which has none.
|
||||
export const V2V_VM_OTHER_CONFIG = {
|
||||
SOURCE_VM_ID: 'sourceVmId',
|
||||
SOURCE_SNAPSHOT_ID: 'sourceSnapshotId',
|
||||
IMPORTED_VM_UUID: 'xo:v2v:importedVmUuid',
|
||||
SOURCE_ID: 'xo:v2v:sourceId',
|
||||
SOURCE_VM_INSTANCE_UUID: 'xo:v2v:sourceVmInstanceUuid',
|
||||
} as const
|
||||
|
||||
export type V2V_VM_OTHER_CONFIG = (typeof V2V_VM_OTHER_CONFIG)[keyof typeof V2V_VM_OTHER_CONFIG]
|
||||
|
||||
export type XoV2vVmOtherConfig = Partial<Record<V2V_VM_OTHER_CONFIG, string>>
|
||||
|
||||
// The disk of the source chain an imported VDI last received, one of the `chainPaths` of its source disk: the CID
|
||||
// cannot tell a fresh snapshot delta from its parent, the path can
|
||||
export const V2V_VDI_OTHER_CONFIG = {
|
||||
DISK_PATH: 'esxi_diskPath',
|
||||
} as const
|
||||
|
||||
export type V2V_VDI_OTHER_CONFIG = (typeof V2V_VDI_OTHER_CONFIG)[keyof typeof V2V_VDI_OTHER_CONFIG]
|
||||
|
||||
export const V2V_START_BLOCKED_REASON = 'Esxi migration in progress...'
|
||||
|
||||
export const V2V_PARTIAL_IMPORT_TAG = 'xo:no-bak=V2V partial import'
|
||||
|
||||
export const V2V_SNAPSHOT_NAME = {
|
||||
AFTER_PARTIAL_IMPORT: 'after partial import from V2V',
|
||||
AFTER_COMPLETE_IMPORT: 'after complete import from V2V',
|
||||
COMPLETE_IMPORT: 'complete import from V2V',
|
||||
} as const
|
||||
|
||||
export type V2V_SNAPSHOT_NAME = (typeof V2V_SNAPSHOT_NAME)[keyof typeof V2V_SNAPSHOT_NAME]
|
||||
|
||||
// Set on the task of a start so the front can tell which source VMs are being imported
|
||||
export const V2V_TASK_PROPERTY = {
|
||||
SOURCE_VM_IDS: 'sourceVmIds',
|
||||
} as const
|
||||
|
||||
export type XoV2vConnection = {
|
||||
host: string
|
||||
port: number
|
||||
username: string
|
||||
password: string
|
||||
skipSslCheck: boolean
|
||||
}
|
||||
|
||||
export type XoV2vStatus = {
|
||||
vectura: { status: 'success' } | { status: 'error'; error: string }
|
||||
}
|
||||
|
||||
export type XoV2vInventoryPayload = {
|
||||
platform: V2V_PLATFORM
|
||||
connection: XoV2vConnection
|
||||
}
|
||||
|
||||
export type XoV2vDatacenter = {
|
||||
id: Branded<'v2v-datacenter'>
|
||||
name: string
|
||||
}
|
||||
|
||||
export type XoV2vCluster = {
|
||||
id: Branded<'v2v-cluster'>
|
||||
name: string
|
||||
datacenterId: XoV2vDatacenter['id']
|
||||
}
|
||||
|
||||
export type XoV2vHost = {
|
||||
id: Branded<'v2v-host'>
|
||||
name: string
|
||||
clusterId?: XoV2vCluster['id']
|
||||
datacenterId: XoV2vDatacenter['id']
|
||||
}
|
||||
|
||||
// Sizes are in bytes, as in the rest of XO, not in the KB/MB VMware returns
|
||||
export type XoV2vDatastore = {
|
||||
id: Branded<'v2v-datastore'>
|
||||
name: string
|
||||
type: string
|
||||
capacity: number
|
||||
free: number
|
||||
hostIds: XoV2vHost['id'][]
|
||||
}
|
||||
|
||||
export type XoV2vPortGroup = {
|
||||
id: Branded<'v2v-port-group'>
|
||||
name: string
|
||||
vlan?: number
|
||||
}
|
||||
|
||||
export const V2V_DISK_MODE = {
|
||||
PERSISTENT: 'persistent',
|
||||
NON_PERSISTENT: 'nonpersistent',
|
||||
INDEPENDENT_PERSISTENT: 'independent_persistent',
|
||||
INDEPENDENT_NON_PERSISTENT: 'independent_nonpersistent',
|
||||
UNDOABLE: 'undoable',
|
||||
APPEND: 'append',
|
||||
} as const
|
||||
|
||||
export type V2V_DISK_MODE = (typeof V2V_DISK_MODE)[keyof typeof V2V_DISK_MODE]
|
||||
|
||||
// `key` is the vmx device node (`scsi0:0`, `ethernet0`…), the identifier the import matches the start maps against
|
||||
export type XoV2vDisk = {
|
||||
key: Branded<'v2v-disk'>
|
||||
label: string
|
||||
datastoreId: XoV2vDatastore['id']
|
||||
capacity: number
|
||||
mode: V2V_DISK_MODE
|
||||
isRdm: boolean
|
||||
isEncrypted: boolean
|
||||
// `dir/disk.vmdk` in its datastore (no `[datastore]` prefix) of the base disk, then of each snapshot delta down to
|
||||
// the active disk: the chain the import walks for this vmx node
|
||||
chainPaths: string[]
|
||||
}
|
||||
|
||||
// No port group for opaque (NSX) or missing backings
|
||||
export type XoV2vNic = {
|
||||
key: Branded<'v2v-nic'>
|
||||
label: string
|
||||
portGroupId?: XoV2vPortGroup['id']
|
||||
macAddress: string
|
||||
}
|
||||
|
||||
export const V2V_VM_POWER_STATE = {
|
||||
POWERED_ON: 'poweredOn',
|
||||
POWERED_OFF: 'poweredOff',
|
||||
SUSPENDED: 'suspended',
|
||||
} as const
|
||||
|
||||
export type V2V_VM_POWER_STATE = (typeof V2V_VM_POWER_STATE)[keyof typeof V2V_VM_POWER_STATE]
|
||||
|
||||
export const V2V_FIRMWARE = {
|
||||
BIOS: 'bios',
|
||||
UEFI: 'uefi',
|
||||
} as const
|
||||
|
||||
export type V2V_FIRMWARE = (typeof V2V_FIRMWARE)[keyof typeof V2V_FIRMWARE]
|
||||
|
||||
export type XoV2vVm = {
|
||||
id: Branded<'v2v-vm'>
|
||||
instanceUuid: string
|
||||
name: string
|
||||
description: string
|
||||
powerState: V2V_VM_POWER_STATE
|
||||
hostId: XoV2vHost['id']
|
||||
nCpus: number
|
||||
memory: number
|
||||
firmware: V2V_FIRMWARE
|
||||
secureBoot: boolean
|
||||
datastoreIds: XoV2vDatastore['id'][]
|
||||
snapshotCount: number
|
||||
// Compared with `other_config.sourceSnapshotId` of a partial import: a delta is only possible while they match
|
||||
currentSnapshotId?: string
|
||||
disks: XoV2vDisk[]
|
||||
nics: XoV2vNic[]
|
||||
}
|
||||
|
||||
export type XoV2vInventory = {
|
||||
product: string
|
||||
version: string
|
||||
// Absent on a standalone ESXi, identified by its connection address instead
|
||||
instanceUuid?: string
|
||||
datacenters: XoV2vDatacenter[]
|
||||
clusters: XoV2vCluster[]
|
||||
hosts: XoV2vHost[]
|
||||
datastores: XoV2vDatastore[]
|
||||
portGroups: XoV2vPortGroup[]
|
||||
vms: XoV2vVm[]
|
||||
}
|
||||
|
||||
export type XoV2vStartOptions = {
|
||||
stopSource: boolean
|
||||
startVm: boolean
|
||||
cleanup: boolean
|
||||
}
|
||||
|
||||
// A resumed import sends no disk or NIC map: the back end reads its mapping from the existing XCP-ng VM
|
||||
export type XoV2vStartPayloadVm = Partial<XoV2vStartOptions> & {
|
||||
id: XoV2vVm['id']
|
||||
disks?: Record<XoV2vDisk['key'], XoSr['id']>
|
||||
nics?: Record<XoV2vNic['key'], XoNetwork['id']>
|
||||
template?: XoVmTemplate['id']
|
||||
tags: string[]
|
||||
keepMac: boolean
|
||||
}
|
||||
|
||||
// Body of `POST /pools/{id}/actions/import_vms_from_vmware`: the pool is in the path. Checked against the body of the
|
||||
// route when the REST API is built, which refuses any extra key
|
||||
export type XoV2vStartPayload = {
|
||||
connection: XoV2vConnection
|
||||
defaults: XoV2vStartOptions & {
|
||||
sr: XoSr['id']
|
||||
network: XoNetwork['id']
|
||||
template: XoVmTemplate['id']
|
||||
}
|
||||
vms: XoV2vStartPayloadVm[]
|
||||
stopOnError: boolean
|
||||
concurrency: number
|
||||
}
|
||||
103
@vates/types/src/lib/xen-orchestra-vmware-explorer.mts
Normal file
103
@vates/types/src/lib/xen-orchestra-vmware-explorer.mts
Normal file
@@ -0,0 +1,103 @@
|
||||
// shape of `Esxi#getInventory()` in `@xen-orchestra/vmware-explorer`
|
||||
// the objects reference each other through their VMware managed object references (moref)
|
||||
|
||||
export type VmwareDatacenter = {
|
||||
id: string
|
||||
name: string
|
||||
}
|
||||
|
||||
export type VmwareCluster = {
|
||||
id: string
|
||||
name: string
|
||||
datacenter?: string
|
||||
}
|
||||
|
||||
export type VmwareHost = {
|
||||
id: string
|
||||
name: string
|
||||
/** a cluster, or the compute resource of a standalone host */
|
||||
parent?: string
|
||||
datacenter?: string
|
||||
}
|
||||
|
||||
export type VmwareDatastore = {
|
||||
id: string
|
||||
name?: string
|
||||
type?: string
|
||||
capacity?: number
|
||||
freeSpace?: number
|
||||
/** the hosts which mount the datastore and can reach it */
|
||||
hosts: string[]
|
||||
}
|
||||
|
||||
export type VmwareVlan = number | { start: number; end: number }[]
|
||||
|
||||
export type VmwareStandardNetwork = {
|
||||
id: string
|
||||
name: string
|
||||
distributed: false
|
||||
}
|
||||
|
||||
export type VmwareDistributedPortgroup = {
|
||||
id: string
|
||||
name: string
|
||||
distributed: true
|
||||
vlan?: VmwareVlan
|
||||
}
|
||||
|
||||
export type VmwareNetwork = VmwareStandardNetwork | VmwareDistributedPortgroup
|
||||
|
||||
export type VmwareVmDisk = {
|
||||
/** node of the disk, as the vmx names it, e.g. `scsi0:0` */
|
||||
key: string
|
||||
label: string
|
||||
capacity: number
|
||||
datastore?: string
|
||||
diskMode?: string
|
||||
rdm: boolean
|
||||
encrypted: boolean
|
||||
/** `dir/disk.vmdk` in its datastore of the base disk, then of each snapshot delta down to the active disk */
|
||||
chainPaths: string[]
|
||||
}
|
||||
|
||||
export type VmwareVmNic = {
|
||||
/** device key of the card */
|
||||
key: string
|
||||
label: string
|
||||
macAddress?: string
|
||||
addressType?: string
|
||||
network?: string
|
||||
}
|
||||
|
||||
export type VmwareVm = {
|
||||
id: string
|
||||
name?: string
|
||||
annotation?: string
|
||||
instanceUuid?: string
|
||||
firmware?: string
|
||||
secureBoot: boolean
|
||||
isTemplate: boolean
|
||||
cpus?: number
|
||||
memory?: number
|
||||
powerState?: 'poweredOff' | 'poweredOn' | 'suspended'
|
||||
host?: string
|
||||
datastores: string[]
|
||||
currentSnapshot?: string
|
||||
snapshotCount: number
|
||||
disks: VmwareVmDisk[]
|
||||
nics: VmwareVmNic[]
|
||||
}
|
||||
|
||||
export type VmwareInventory = {
|
||||
/** e.g. `VMware vCenter Server`, `VMware ESXi` */
|
||||
product?: string
|
||||
version?: string
|
||||
/** only set by a vCenter */
|
||||
instanceUuid?: string
|
||||
datacenters: VmwareDatacenter[]
|
||||
clusters: VmwareCluster[]
|
||||
hosts: VmwareHost[]
|
||||
datastores: VmwareDatastore[]
|
||||
networks: VmwareNetwork[]
|
||||
vms: VmwareVm[]
|
||||
}
|
||||
@@ -130,6 +130,8 @@ export type XsPatches = {
|
||||
uuid?: string
|
||||
}
|
||||
|
||||
type WrappedRecordOfType<Type extends WrappedXenApiRecord['$type']> = Extract<WrappedXenApiRecord, { $type: Type }>
|
||||
|
||||
export interface Xapi {
|
||||
status: string
|
||||
pool?: { uuid: string }
|
||||
@@ -147,6 +149,14 @@ export interface Xapi {
|
||||
ref: T['$ref'],
|
||||
field: K
|
||||
): Promise<T[K]>
|
||||
// the record is inferred from `type`, for a JS caller which cannot spell it out as a type argument
|
||||
getField<Type extends WrappedXenApiRecord['$type'], Field extends keyof WrappedRecordOfType<Type>>(
|
||||
type: Type,
|
||||
ref: WrappedRecordOfType<Type>['$ref'],
|
||||
field: Field
|
||||
): Promise<WrappedRecordOfType<Type>[Field]>
|
||||
/** Synchronous: the record as the object cache currently holds it. */
|
||||
getObjectByRef<Ref extends WrappedXenApiRecord['$ref']>(ref: Ref): Extract<WrappedXenApiRecord, { $ref: Ref }>
|
||||
getObject: <
|
||||
XoRecord extends XapiXoRecord,
|
||||
WrappedRecord extends WrappedXenApiRecord = Extract<WrappedXenApiRecord, { $type: XoRecord['type'] }>,
|
||||
@@ -283,6 +293,42 @@ export interface Xapi {
|
||||
}
|
||||
): Promise<XenApiVdi['$ref']>
|
||||
SR_reclaimSpace(ref: XenApiSr['$ref']): Promise<void>
|
||||
VBD_create(params: {
|
||||
bootable?: boolean
|
||||
currently_attached?: boolean
|
||||
device?: string
|
||||
empty?: boolean
|
||||
mode?: VBD_MODE
|
||||
other_config?: Record<string, string>
|
||||
qos_algorithm_params?: Record<string, string>
|
||||
qos_algorithm_type?: string
|
||||
/** a plug failure, only logged otherwise, is thrown: the VBD exists all the same */
|
||||
throwVbdPlug?: boolean
|
||||
type?: VBD_TYPE
|
||||
unpluggable?: boolean
|
||||
userdevice?: string
|
||||
VDI?: XenApiVdi['$ref']
|
||||
VM: XenApiVm['$ref']
|
||||
}): Promise<XenApiVbd['$ref']>
|
||||
/** unplugs the VBD first, if needed */
|
||||
VBD_destroy(ref: XenApiVbd['$ref']): Promise<void>
|
||||
VDI_create(
|
||||
params: {
|
||||
name_description?: string
|
||||
name_label: string
|
||||
other_config?: Record<string, string>
|
||||
read_only?: boolean
|
||||
sharable?: boolean
|
||||
SR?: XenApiSr['$ref']
|
||||
tags?: string[]
|
||||
type?: VDI_TYPE
|
||||
virtual_size: number
|
||||
xenstore_data?: Record<string, string>
|
||||
},
|
||||
opts?: { sm_config?: Record<string, string> }
|
||||
): Promise<XenApiVdi['$ref']>
|
||||
/** a VDI which no longer exists counts as destroyed */
|
||||
VDI_destroy(ref: XenApiVdi['$ref']): Promise<void>
|
||||
editVm(
|
||||
id: XoVm['id'],
|
||||
props: EditVmProps,
|
||||
|
||||
@@ -824,7 +824,10 @@ export interface XenApiSr {
|
||||
VDIs: XenApiVdi['$ref'][]
|
||||
virtual_allocation: number
|
||||
}
|
||||
export type XenApiSrWrapped = WrapperXenApi<XenApiSr, 'SR', XenApiSrCallMethods>
|
||||
export type XenApiSrWrapped = WrapperXenApi<XenApiSr, 'SR', XenApiSrCallMethods> & {
|
||||
// resolved by xen-api from `PBDs`
|
||||
$PBDs: XenApiPbdWrapped[]
|
||||
}
|
||||
|
||||
export interface XenApiSrStat {
|
||||
$ref: Branded<'sr_stat'>
|
||||
|
||||
@@ -1,27 +1,39 @@
|
||||
import { EventEmitter, Readable } from 'node:stream'
|
||||
import type { EventEmitter, Readable } from 'node:stream'
|
||||
import type {
|
||||
AnyXoJob,
|
||||
AnyXoLog,
|
||||
NonXapiXoRecord,
|
||||
XoBackupDiskPartition,
|
||||
XapiXoRecord,
|
||||
XoAuthenticationToken,
|
||||
XoBackupRepository,
|
||||
XoBrowserMediaSession,
|
||||
XoConfigBackupArchive,
|
||||
XoGroup,
|
||||
XoHost,
|
||||
XoMetadataBackupJob,
|
||||
XoMirrorBackupJob,
|
||||
XoNetwork,
|
||||
XoPlugin,
|
||||
XoPool,
|
||||
XoPoolBackupArchive,
|
||||
XoProxy,
|
||||
XoSchedule,
|
||||
XoServer,
|
||||
XoSr,
|
||||
XoTask,
|
||||
XoUser,
|
||||
XoVdi,
|
||||
XoVif,
|
||||
XoVm,
|
||||
XoVmBackupArchive,
|
||||
XoVmBackupJob,
|
||||
XoVmTemplate,
|
||||
} from './xo.mjs'
|
||||
import { VatesTask } from './lib/vates-task.mjs'
|
||||
import type { PluginRestRouteDefinition } from './lib/rest-api.mjs'
|
||||
import type { VmwareInventory } from './lib/xen-orchestra-vmware-explorer.mjs'
|
||||
import type { XoV2vStatus } from './lib/xen-orchestra-v2v.mjs'
|
||||
import type { RPU_RECOVERY_STEP_NAME } from './common.mjs'
|
||||
import {
|
||||
Xapi,
|
||||
@@ -52,7 +64,6 @@ import {
|
||||
XoGroupRole,
|
||||
XoUserRole,
|
||||
} from './index.mjs'
|
||||
import { IncomingHttpHeaders } from 'node:http'
|
||||
|
||||
type FeatureCode =
|
||||
| 'BACKUP.DELTA'
|
||||
@@ -176,13 +187,13 @@ export type PoolRollingUpdateRecoveryBlocked = {
|
||||
|
||||
export type PoolRollingUpdateRecovery = PoolRollingUpdateRecoveryRun | PoolRollingUpdateRecoveryBlocked
|
||||
|
||||
/** A disk of a backup archive currently served as a read-only iSCSI LUN */
|
||||
/** A disk of a backup archive currently served as an iSCSI LUN */
|
||||
export type BackupArchiveDiskMount = {
|
||||
/** Handle to pass to `unmountBackupArchiveDisk` */
|
||||
id: string
|
||||
/** UUID of the SR introduced on the host */
|
||||
srUuid: string
|
||||
/** UUID of the read-only VDI exposing the backup disk */
|
||||
/** UUID of the VDI exposing the backup disk, read-only unless it is cached */
|
||||
vdiUuid: string
|
||||
/** IQN of the target serving the disk */
|
||||
iqn: string
|
||||
@@ -192,7 +203,25 @@ export type BackupArchiveDiskMount = {
|
||||
port: number
|
||||
}
|
||||
|
||||
export type XoApp = {
|
||||
type XoStoreRangeOptions = {
|
||||
gt?: string
|
||||
gte?: string
|
||||
lt?: string
|
||||
lte?: string
|
||||
limit?: number
|
||||
reverse?: boolean
|
||||
}
|
||||
|
||||
// `@xen-orchestra/vmware-explorer` is not typed
|
||||
export type EsxiConnectionParams = {
|
||||
/** `host` or `host:port` */
|
||||
host: string
|
||||
user: string
|
||||
password: string
|
||||
sslVerify: boolean
|
||||
}
|
||||
|
||||
export type XoApp = EventEmitter & {
|
||||
hooks: EventEmitter
|
||||
_redis: {
|
||||
get(key: string): Promise<string | null>
|
||||
@@ -234,12 +263,6 @@ export type XoApp = {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* the VM backup archives of the backup repositories, as a collection: `add`, `update` and
|
||||
* `remove` events carrying the archive and its previous value
|
||||
*/
|
||||
vmBackupArchives: EventEmitter
|
||||
|
||||
tasks: EventEmitter & {
|
||||
abort(id: XoTask['id'], reason?: string): Promise<void>
|
||||
clearLogs(): Promise<void>
|
||||
@@ -253,6 +276,7 @@ export type XoApp = {
|
||||
user?: XoUser
|
||||
permission?: XoUser['permission'] | 'none' | null
|
||||
}
|
||||
backupLogsEe: EventEmitter
|
||||
|
||||
// methods ------------
|
||||
allocIpAddresses(vifId: XoVif['id'], addAddresses?: string[], removeAddresses?: string[]): Promise<void>
|
||||
@@ -277,6 +301,8 @@ export type XoApp = {
|
||||
backupGuard(objectId: XapiXoRecord['id'], opts?: { bypassBackupCheck?: boolean; operation: string }): Promise<void>
|
||||
/* Throw if no authorization */
|
||||
checkFeatureAuthorization(featureCode: FeatureCode): Promise<void>
|
||||
/* whether this machine can run the tools of an import from VMware */
|
||||
checkEsxiDependencies(): Promise<XoV2vStatus>
|
||||
/* validate, apply and persist the configuration of a plugin */
|
||||
configurePlugin(id: string, configuration: unknown, mergeWithExisting?: boolean): Promise<void>
|
||||
/* connect a server (XCP-ng/XenServer) */
|
||||
@@ -297,6 +323,24 @@ export type XoApp = {
|
||||
description?: XoAclRole['description']
|
||||
privileges?: Omit<XoAclBasePrivilege, 'id' | 'roleId'>[]
|
||||
}): Promise<XoAclRole>
|
||||
createApiConnection(remoteAddress: string): EventEmitter & {
|
||||
close(): void
|
||||
get<T = unknown>(key: string, defaultValue: unknown): T
|
||||
has(key: string): boolean
|
||||
set(key: string, value: unknown): void
|
||||
unset(key: string): void
|
||||
}
|
||||
createBackupNgJob<T extends XoVmBackupJob | XoMirrorBackupJob>(
|
||||
type: T['type'],
|
||||
props: Omit<T, 'id' | 'type'>,
|
||||
/** keyed by temporary IDs, which can be used as keys in `props.settings` */
|
||||
schedules?: Record<string, Pick<XoSchedule, 'cron' | 'enabled' | 'name' | 'timezone'>>
|
||||
): Promise<T>
|
||||
createMetadataBackupJob(
|
||||
props: Omit<XoMetadataBackupJob, 'id' | 'type'>,
|
||||
/** keyed by temporary IDs, which can be used as keys in `props.settings` */
|
||||
schedules: Record<string, Pick<XoSchedule, 'cron' | 'enabled' | 'name' | 'timezone'>>
|
||||
): Promise<XoMetadataBackupJob>
|
||||
createAuthenticationToken(opts: {
|
||||
client?: {
|
||||
id?: string
|
||||
@@ -312,6 +356,14 @@ export type XoApp = {
|
||||
proxy?: XoProxy['id']
|
||||
url: string
|
||||
}): Promise<XoBackupRepository>
|
||||
createSchedule(params: {
|
||||
cron: XoSchedule['cron']
|
||||
enabled: XoSchedule['enabled']
|
||||
jobId: XoSchedule['jobId']
|
||||
name?: XoSchedule['name']
|
||||
timezone?: XoSchedule['timezone']
|
||||
userId?: XoUser['id']
|
||||
}): Promise<XoSchedule>
|
||||
createUser(params: {
|
||||
firstname?: string
|
||||
lastname?: string
|
||||
@@ -328,11 +380,14 @@ export type XoApp = {
|
||||
deleteAclV2Privilege(privilegeId: XoAclBasePrivilege['id'], options?: { force?: boolean }): Promise<boolean>
|
||||
deleteAclV2Role(roleId: XoAclRole['id'], options?: { force?: boolean }): Promise<boolean>
|
||||
deleteGroup(id: XoGroup['id']): Promise<void>
|
||||
deleteSchedule(id: XoSchedule['id']): Promise<void>
|
||||
deleteUser(id: XoUser['id']): Promise<void>
|
||||
detachHostFromPool(hostId: XoHost['id']): Promise<void>
|
||||
createGroup(params: { name: string; provider?: string; providerGroup?: string }): Promise<XoGroup>
|
||||
disablePluginAutoload(id: XoPlugin['id']): Promise<void>
|
||||
/* disconnect a server (XCP-ng/XenServer) */
|
||||
disconnectXenServer(id: XoServer['id']): Promise<void>
|
||||
enablePluginAutoload(id: XoPlugin['id']): Promise<void>
|
||||
findEnabledScheduleSequenceFromSchedule(id: XoSchedule['id']): Promise<XoSchedule | undefined>
|
||||
getAclV2Privilege(id: XoAclBasePrivilege['id']): Promise<XoAclBasePrivilege>
|
||||
getAclV2Privileges(): Promise<XoAclBasePrivilege[]>
|
||||
@@ -343,6 +398,7 @@ export type XoApp = {
|
||||
roleId: XoAclRole['id'],
|
||||
opts?: { bypassAuthorization?: boolean }
|
||||
): Promise<boolean>
|
||||
deleteSchedule(id: XoSchedule['id']): Promise<void>
|
||||
getAclV2GroupRoles(
|
||||
groupId: XoGroup['id'],
|
||||
opts?: { bypassAuthorization?: boolean }
|
||||
@@ -357,10 +413,14 @@ export type XoApp = {
|
||||
getAllProxies(): Promise<XoProxy[]>
|
||||
getAllJobs<T extends AnyXoJob['type']>(type: T): Promise<Extract<AnyXoJob, { type: T }>[]>
|
||||
getAllJobs(type?: string): Promise<AnyXoJob[]>
|
||||
getPlugin(id: XoPlugin['id']): Promise<XoPlugin>
|
||||
getPlugins(): Promise<XoPlugin[]>
|
||||
getProxy(id: XoProxy['id']): Promise<XoProxy>
|
||||
getRemote(id: XoBackupRepository['id']): Promise<XoBackupRepository>
|
||||
getAllRemotes(): Promise<XoBackupRepository[]>
|
||||
removeRemote(id: XoBackupRepository['id']): Promise<void>
|
||||
/** Also deletes the schedules of the job */
|
||||
removeJob(id: AnyXoJob['id']): Promise<void>
|
||||
getAllRemotesInfo(): Promise<
|
||||
Record<
|
||||
XoBackupRepository['id'],
|
||||
@@ -377,6 +437,23 @@ export type XoApp = {
|
||||
>
|
||||
>
|
||||
getAllSchedules(): Promise<XoSchedule[]>
|
||||
/** Every browser media session, whoever owns it; empty when the feature is disabled */
|
||||
getAllBrowserMediaSessions(): XoBrowserMediaSession[]
|
||||
getBrowserMediaSession(id: XoBrowserMediaSession['id']): XoBrowserMediaSession
|
||||
/**
|
||||
* Insert the ISO of a browser media session in the CD drive of a VM. Undone by `ejectBrowserMedia`,
|
||||
* or when the browser disconnects.
|
||||
*
|
||||
* `assertBrowserMediaInsertable` reports the predictable failures synchronously, e.g. before
|
||||
* starting a task.
|
||||
*/
|
||||
insertBrowserMedia(params: {
|
||||
id: XoBrowserMediaSession['id']
|
||||
vmId: XoVm['id']
|
||||
}): Promise<{ vdiId: XoVdi['id'] }>
|
||||
assertBrowserMediaInsertable(params: { id: XoBrowserMediaSession['id']; vmId: XoVm['id'] }): void
|
||||
/** Eject the ISO of a browser media session, release its storage, and disconnect the browser */
|
||||
ejectBrowserMedia(id: XoBrowserMediaSession['id']): Promise<void>
|
||||
getAllUsers(opts?: { obfuscatePassword?: boolean }): Promise<XoUser[]>
|
||||
getAllXenServers(): Promise<XoServer[]>
|
||||
getAuthenticationTokensForUser(userId: XoUser['id']): Promise<XoAuthenticationToken[]>
|
||||
@@ -389,6 +466,10 @@ export type XoApp = {
|
||||
}
|
||||
getBackupNgLogs(): Promise<Record<string, AnyXoLog>>
|
||||
getBackupNgLogs(id: AnyXoLog['id']): Promise<AnyXoLog>
|
||||
/** Please use `REMOVE_CACHE_ENTRY` const to clear the cache */
|
||||
getBackupNgLogs(removeCacheEntry: {}): void
|
||||
/** Please use `REMOVE_CACHE_ENTRY` const to clear a cache entry */
|
||||
getBackupNgLogs(removeCacheEntry: {}, id: AnyXoLog['id']): void
|
||||
getBackupNgLogsSorted(opts: {
|
||||
after?: number
|
||||
before?: number
|
||||
@@ -406,8 +487,21 @@ export type XoApp = {
|
||||
) => Record<T['id'], T> | undefined
|
||||
getTotalBackupSizeOnRemote(id: XoBackupRepository['id']): Promise<{ onDisk: number }>
|
||||
getSchedule(id: XoSchedule['id']): Promise<XoSchedule>
|
||||
getStore(namespace: string): Promise<{
|
||||
/** rejects with an error having `notFound: true` when the key does not exist */
|
||||
get(key: string): Promise<unknown>
|
||||
has(key: string): Promise<boolean>
|
||||
put(key: string, value: unknown): Promise<void>
|
||||
del(key: string): Promise<void>
|
||||
del(key: string, cb: (error?: Error | null) => void): void
|
||||
/** object mode stream of `{ key, value }` */
|
||||
createReadStream(opts?: XoStoreRangeOptions): Readable
|
||||
/** object mode stream of keys */
|
||||
createKeyStream(opts?: XoStoreRangeOptions): Readable
|
||||
}>
|
||||
getUser: (id: XoUser['id'], opts?: { obfuscatePassword?: boolean }) => Promise<XoUser>
|
||||
getUserIdentityFields(): string[]
|
||||
getEsxiInventory(params: EsxiConnectionParams): Promise<VmwareInventory>
|
||||
getXapi(maybeId: XapiXoRecord['id'] | XapiXoRecord): Xapi
|
||||
getXapiHostStats: (hostId: XoHost['id'], granularity?: XapiStatsGranularity) => Promise<XapiHostStats>
|
||||
getXapiObject: <T extends XapiXoRecord>(
|
||||
@@ -417,23 +511,29 @@ export type XoApp = {
|
||||
getXapiPoolStats(poolId: XoPool['id'], granularity?: XapiStatsGranularity): Promise<XapiPoolStats>
|
||||
getXapiVmStats: (vmId: XoVm['id'], granularity?: XapiStatsGranularity) => Promise<XapiVmStats>
|
||||
getXenServer(id: XoServer['id']): Promise<XoServer>
|
||||
/** the connection of the pool holding this object, which may not be connected */
|
||||
getXenServerIdByObject<T extends XapiXoRecord>(maybeId: T['id'] | T, type?: T['type'] | T['type'][]): XoServer['id']
|
||||
getXenServerWithCredentials(id: XoServer['id']): Promise<XoServer & { password: string }>
|
||||
getXoEventEmitterByType(type: string): EventEmitter
|
||||
hasFeatureAuthorization(featureCode: string): Promise<boolean>
|
||||
hasObject<T extends XapiXoRecord>(id: T['id'], type: T['type']): boolean
|
||||
httpRequest(
|
||||
url: string,
|
||||
opts: {
|
||||
auth?: string
|
||||
body?: string | NodeJS.ArrayBufferView | Readable | null
|
||||
bypassStatusCheck?: boolean
|
||||
headers?: IncomingHttpHeaders
|
||||
method: string
|
||||
rejectUnauthorized?: boolean
|
||||
bodyTimeout?: number
|
||||
headersTimeout?: number
|
||||
signal?: AbortSignal
|
||||
}
|
||||
): Promise<Response>
|
||||
listBackupNgDiskPartitions(remoteId: XoBackupRepository['id'], diskId: string): Promise<XoBackupDiskPartition[]>
|
||||
listBackupNgPartitionFiles(
|
||||
remoteId: XoBackupRepository['id'],
|
||||
diskId: string,
|
||||
partitionId: string | undefined,
|
||||
path: string
|
||||
// keys ending in "/" are directories; files carry size (bytes) and mtime (epoch ms).
|
||||
// Values may be empty ({}) for directories or when served by an older proxy.
|
||||
): Promise<Record<string, { size?: number; mtime?: number }>>
|
||||
fetchBackupNgPartitionFiles(
|
||||
remoteId: XoBackupRepository['id'],
|
||||
diskId: string,
|
||||
partitionId: string | undefined,
|
||||
paths: string[],
|
||||
format: string
|
||||
): Promise<Readable>
|
||||
loadPlugin(id: XoPlugin['id']): Promise<void>
|
||||
listMetadataBackups(backupRepositoryIds: XoBackupRepository['id'][]): Promise<{
|
||||
xo: Record<XoBackupRepository['id'], XoConfigBackupArchive[]>
|
||||
pool: Record<XoBackupRepository['id'], Record<XoPool['id'], XoPoolBackupArchive[]>>
|
||||
@@ -443,19 +543,41 @@ export type XoApp = {
|
||||
backupRepositoryIds: XoBackupRepository['id'][],
|
||||
opts?: { _forceRefresh?: boolean; vmId?: XoVm['id'] }
|
||||
): Promise<Record<XoBackupRepository['id'], Record<XoVm['id'], XoVmBackupArchive[]> | null>>
|
||||
/** imports one VM from VMware, resuming a previous partial import when possible, returns the UUID of the XCP-ng VM */
|
||||
migrationfromEsxi(
|
||||
params: EsxiConnectionParams & {
|
||||
sr: XoSr['id']
|
||||
network: XoNetwork['id']
|
||||
/** overrides `sr`, by disk node (e.g. `scsi0:0`) */
|
||||
disks?: Record<string, XoSr['id']>
|
||||
/** overrides `network`, by NIC device key (e.g. `4000`) */
|
||||
nics?: Record<string, XoNetwork['id']>
|
||||
template: XoVmTemplate['id']
|
||||
stopSource: boolean
|
||||
/** VMware moref of the VM */
|
||||
vm: string
|
||||
}
|
||||
): Promise<XoVm['uuid']>
|
||||
/**
|
||||
* Serve one disk of a backup archive as a read-only iSCSI LUN and attach it to
|
||||
* `host` as an SR. Undone by `unmountBackupArchiveDisk`.
|
||||
* Serve one disk of a backup archive as an iSCSI LUN and attach it to `host`
|
||||
* as an SR, read-only unless `cacheSrId` is set. Undone by `unmountBackupArchiveDisk`.
|
||||
*
|
||||
* The LUN is served by whoever can read the backup repository: this appliance, or the proxy the
|
||||
* repository is linked to.
|
||||
*/
|
||||
mountBackupArchiveDisk(params: {
|
||||
archiveId: XoVmBackupArchive['id']
|
||||
/**
|
||||
* SR of a VDI of the serving appliance, which the disk is materialized into as it is read and
|
||||
* which holds the writes; unset, nothing is cached and the mount is read-only. On a local SR,
|
||||
* the serving appliance must not be migrated while the mount lasts: the VDI cannot follow it
|
||||
*/
|
||||
cacheSrId?: XoSr['id']
|
||||
/** One of the archive's `disks[].id` */
|
||||
diskId: string
|
||||
hostId: XoHost['id']
|
||||
}): Promise<BackupArchiveDiskMount>
|
||||
purgePluginConfiguration(id: XoPlugin['id']): Promise<void>
|
||||
pingRemote(id: XoBackupRepository['id']): Promise<{ success: true }>
|
||||
/**
|
||||
* Record the live mounts a proxy created by itself, while running a restore: they never went
|
||||
@@ -496,6 +618,7 @@ export type XoApp = {
|
||||
removeUserFromGroup(userId: XoUser['id'], id: XoGroup['id']): Promise<void>
|
||||
runJob(job: AnyXoJob, schedule: XoSchedule): void
|
||||
runWithApiContext: (user: XoUser | undefined, fn: () => void) => Promise<unknown>
|
||||
testPlugin(id: XoPlugin['id'], data?: unknown): Promise<void>
|
||||
testRemote(
|
||||
id: XoBackupRepository['id']
|
||||
): Promise<
|
||||
@@ -505,12 +628,26 @@ export type XoApp = {
|
||||
touchXoObject(type: string, id: NonXapiXoRecord['id']): Promise<void>
|
||||
/** Detach a disk mounted by `mountBackupArchiveDisk` and stop serving it */
|
||||
unmountBackupArchiveDisk(id: BackupArchiveDiskMount['id']): Promise<void>
|
||||
unloadPlugin(id: XoPlugin['id']): Promise<void>
|
||||
reclaimSpace(
|
||||
remoteId: XoBackupRepository['id'],
|
||||
opts?: { vmUuid?: XoVm['id']; merge?: boolean; remove?: boolean }
|
||||
): Promise<{ vmUuid: string; success: boolean; merge?: boolean; size?: number; error?: string }[]>
|
||||
/** Remove a server from the DB (XCP-ng/XenServer) */
|
||||
unregisterXenServer(id: XoServer['id']): Promise<void>
|
||||
/**
|
||||
* Shallow merge of the properties into the existing job, a `null` value removes the property.
|
||||
*/
|
||||
updateJob(job: { id: AnyXoJob['id']; [key: string]: unknown }, merge?: boolean): Promise<void>
|
||||
updateSchedule(params: {
|
||||
id: XoSchedule['id']
|
||||
cron?: XoSchedule['cron']
|
||||
enabled?: XoSchedule['enabled']
|
||||
jobId?: XoSchedule['jobId']
|
||||
name?: XoSchedule['name']
|
||||
timezone?: XoSchedule['timezone']
|
||||
userId?: XoUser['id']
|
||||
}): Promise<void>
|
||||
updateUser(
|
||||
id: XoUser['id'],
|
||||
updates: {
|
||||
|
||||
@@ -21,6 +21,29 @@ import type {
|
||||
import type * as CMType from './lib/complex-matcher.mjs'
|
||||
import { XoAclBasePrivilege, XoAclRole } from './lib/xen-orchestra-acl.mjs'
|
||||
import type { XenApiHost, XenApiPool } from './xen-api.mjs'
|
||||
import type {
|
||||
XoAuditPlugin,
|
||||
XoAuthGithubPlugin,
|
||||
XoAuthGooglePlugin,
|
||||
XoAuthLdapPlugin,
|
||||
XoAuthOidcPlugin,
|
||||
XoAuthSamlPlugin,
|
||||
XoBackupReportsPlugin,
|
||||
XoIpmiSensorsPlugin,
|
||||
XoLoadBalancerPlugin,
|
||||
XoNetboxPlugin,
|
||||
XoOpenmetricsPlugin,
|
||||
XoPerfAlertPlugin,
|
||||
XoSdnControllerPlugin,
|
||||
XoTestPluginPlugin,
|
||||
XoTransportEmailPlugin,
|
||||
XoTransportIcinga2Plugin,
|
||||
XoTransportNagiosPlugin,
|
||||
XoTransportSlackPlugin,
|
||||
XoTransportXmppPlugin,
|
||||
XoUsageReportPlugin,
|
||||
XoWebHooksPlugin,
|
||||
} from './lib/xen-orchestra-plugins.mjs'
|
||||
|
||||
type BaseXapiXo = {
|
||||
$pool: XoPool['id']
|
||||
@@ -29,6 +52,12 @@ type BaseXapiXo = {
|
||||
*/
|
||||
$poolId: XoPool['id']
|
||||
|
||||
/**
|
||||
* Only present while the pool is disconnected: the object is kept but may be
|
||||
* outdated, this is the last time (in milliseconds) the pool was reached
|
||||
*/
|
||||
$lastSeen?: number
|
||||
|
||||
_xapiRef: string
|
||||
|
||||
uuid: string
|
||||
@@ -118,7 +147,7 @@ type BaseXoVm = BaseXapiXo & {
|
||||
/**
|
||||
* @deprecated use pvDriversVersion instead
|
||||
*/
|
||||
xentools?:
|
||||
xenTools?:
|
||||
| false
|
||||
| {
|
||||
major: null | number
|
||||
@@ -157,6 +186,15 @@ export type XoBackupLog = BaseXoLog & {
|
||||
export type XoRestoreLog = BaseXoLog & {
|
||||
message: 'restore'
|
||||
}
|
||||
export type XoBackupDiskPartition = {
|
||||
id: Branded<'xo-backup-disk-partition'>
|
||||
name?: string
|
||||
size: number
|
||||
start: number
|
||||
/** number for MBR, string for GPT partition */
|
||||
type: string | number
|
||||
}
|
||||
|
||||
export type XoVmBackupArchive = {
|
||||
id: Branded<'vm-backup-archive'>
|
||||
type: 'xo-vm-backup'
|
||||
@@ -208,6 +246,7 @@ export type XoBackupRepositoryBenchmark = {
|
||||
}
|
||||
|
||||
export type XoBackupRepository = {
|
||||
available?: number
|
||||
benchmarks?: XoBackupRepositoryBenchmark[]
|
||||
enabled: boolean
|
||||
error?: Record<string, unknown>
|
||||
@@ -215,7 +254,9 @@ export type XoBackupRepository = {
|
||||
name: string
|
||||
options?: string
|
||||
proxy?: XoProxy['id']
|
||||
size?: number
|
||||
url: string
|
||||
used?: number
|
||||
}
|
||||
|
||||
export type XoGpuGroup = BaseXapiXo & {
|
||||
@@ -457,6 +498,91 @@ export type XoPool = BaseXapiXo & {
|
||||
zstdSupported: boolean
|
||||
}
|
||||
|
||||
export type XoPlugin =
|
||||
| XoAuditPlugin
|
||||
| XoAuthGithubPlugin
|
||||
| XoAuthGooglePlugin
|
||||
| XoAuthLdapPlugin
|
||||
| XoAuthOidcPlugin
|
||||
| XoAuthSamlPlugin
|
||||
| XoBackupReportsPlugin
|
||||
| XoIpmiSensorsPlugin
|
||||
| XoLoadBalancerPlugin
|
||||
| XoNetboxPlugin
|
||||
| XoOpenmetricsPlugin
|
||||
| XoPerfAlertPlugin
|
||||
| XoSdnControllerPlugin
|
||||
| XoTestPluginPlugin
|
||||
| XoTransportEmailPlugin
|
||||
| XoTransportIcinga2Plugin
|
||||
| XoTransportNagiosPlugin
|
||||
| XoTransportSlackPlugin
|
||||
| XoTransportXmppPlugin
|
||||
| XoUsageReportPlugin
|
||||
| XoWebHooksPlugin
|
||||
|
||||
/**
|
||||
* An ISO selected in a browser and streamed from it, which can be inserted in the CD drive of a VM
|
||||
* while the browser stays connected
|
||||
*/
|
||||
export type XoBrowserMediaSession = {
|
||||
id: Branded<'browser-media'>
|
||||
/** File name of the ISO, as selected in the browser */
|
||||
name: string
|
||||
/** Size of the ISO, in bytes */
|
||||
size: number
|
||||
/** User whose browser serves the ISO */
|
||||
userId: XoUser['id']
|
||||
/** Timestamp of the browser connection */
|
||||
createdAt: number
|
||||
/** Total of the bytes read from the browser */
|
||||
bytesRead: number
|
||||
/** Timestamp of the last read from the browser */
|
||||
lastReadAt?: number
|
||||
} & (
|
||||
| {
|
||||
/** Not inserted in any VM */
|
||||
status: 'connected'
|
||||
vmId?: undefined
|
||||
vdiId?: undefined
|
||||
insertedAt?: undefined
|
||||
/** Why the last insertion failed */
|
||||
error?: string
|
||||
}
|
||||
| {
|
||||
/** Being inserted in a VM */
|
||||
status: 'inserting'
|
||||
/** VM the ISO is being inserted in */
|
||||
vmId: XoVm['id']
|
||||
vdiId?: undefined
|
||||
insertedAt?: undefined
|
||||
error?: undefined
|
||||
}
|
||||
| {
|
||||
/** Inserted in the CD drive of a VM */
|
||||
status: 'inserted'
|
||||
/** VM the ISO is inserted in */
|
||||
vmId: XoVm['id']
|
||||
/** VDI inserted in the CD drive of the VM */
|
||||
vdiId: XoVdi['id']
|
||||
/** Timestamp of the end of the insertion */
|
||||
insertedAt: number
|
||||
error?: undefined
|
||||
}
|
||||
| {
|
||||
/** Being ejected, or a failed insertion being undone */
|
||||
status: 'ejecting'
|
||||
/** VM the ISO is ejected from */
|
||||
vmId: XoVm['id']
|
||||
/** VDI inserted in the CD drive of the VM, unless the insertion failed before */
|
||||
vdiId?: XoVdi['id']
|
||||
/** Timestamp of the end of the insertion, unless it failed */
|
||||
insertedAt?: number
|
||||
/** Why the insertion failed */
|
||||
error?: string
|
||||
}
|
||||
)
|
||||
|
||||
export type XoProxy = {
|
||||
id: Branded<'proxy'>
|
||||
url: string
|
||||
@@ -481,15 +607,22 @@ type XoBackupJobGeneralSettings = {
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps the last backup of each of the last `retention` days/weeks/months/years, in addition to `exportRetention`.
|
||||
*
|
||||
* Requires the `timezone` setting.
|
||||
*/
|
||||
export type XoBackupJobLongTermRetention = {
|
||||
daily?: { retention: number; settings: Record<string, unknown> }
|
||||
weekly?: { retention: number; settings: Record<string, unknown> }
|
||||
monthly?: { retention: number; settings: Record<string, unknown> }
|
||||
yearly?: { retention: number; settings: Record<string, unknown> }
|
||||
}
|
||||
|
||||
export type XoVmBackupJobGeneralSettings = XoBackupJobGeneralSettings & {
|
||||
cbtDestroySnapshotData?: boolean
|
||||
concurrency?: number
|
||||
longTermRetention?: {
|
||||
daily?: { retention: number; settings: Record<string, unknown> }
|
||||
weekly?: { retention: number; settings: Record<string, unknown> }
|
||||
monthly?: { retention: number; settings: Record<string, unknown> }
|
||||
yearly?: { retention: number; settings: Record<string, unknown> }
|
||||
}
|
||||
longTermRetention?: XoBackupJobLongTermRetention
|
||||
checkpointSnapshot?: boolean
|
||||
offlineSnapshot?: boolean
|
||||
maxExportRate?: number
|
||||
@@ -503,8 +636,8 @@ export type XoVmBackupJobGeneralSettings = XoBackupJobGeneralSettings & {
|
||||
}
|
||||
export type XoVmBackupJobScheduleSettings = {
|
||||
exportRetention?: number
|
||||
healthCheckVmsWithTags?: XoVm['tags']
|
||||
fullInterval?: number
|
||||
healthCheckVmsWithTags?: XoVm['tags'] | string
|
||||
fullInterval?: number | string
|
||||
copyRetention?: number
|
||||
snapshotRetention?: number
|
||||
cbtDestroySnapshotData?: boolean
|
||||
@@ -515,9 +648,9 @@ export type XoVmBackupJob = BaseXoJob & {
|
||||
compression?: 'native' | 'zstd' | ''
|
||||
proxy?: XoProxy['id']
|
||||
mode: 'full' | 'delta'
|
||||
remotes?: CMType.IdOr<XoBackupRepository['id']>
|
||||
remotes?: CMType.IdOr<XoBackupRepository['id']> | string
|
||||
vms: CMType.IdOr<XoVm['id']> | Record<string, unknown>
|
||||
srs?: CMType.IdOr<XoSr['id']>
|
||||
srs?: CMType.IdOr<XoSr['id']> | string
|
||||
type: 'backup'
|
||||
settings: {
|
||||
'': XoVmBackupJobGeneralSettings
|
||||
@@ -546,16 +679,17 @@ export type XoMetadataBackupJob = BaseXoJob & {
|
||||
|
||||
export type XoMirrorBackupGeneralSettings = XoBackupJobGeneralSettings & {
|
||||
concurrency?: number
|
||||
longTermRetention?: XoBackupJobLongTermRetention
|
||||
nRetriesVmBackupFailures?: number
|
||||
mergeBackupsSynchronously?: boolean
|
||||
timeout?: number
|
||||
timezone?: string
|
||||
maxExportRate?: number
|
||||
backupReportTpl?: 'compactMjml'
|
||||
reportWhen: 'failure'
|
||||
}
|
||||
export type XoMirrorBackupScheduleSettings = {
|
||||
exportRetention?: number
|
||||
healthCheckVmsWithTags?: XoVm['tags']
|
||||
healthCheckVmsWithTags?: XoVm['tags'] | string
|
||||
vmFilter?: string
|
||||
healthCheckSr?: XoSr['id']
|
||||
[key: string]: unknown
|
||||
}
|
||||
@@ -609,6 +743,7 @@ export type XoServer = {
|
||||
label?: string
|
||||
master?: XoHost['id']
|
||||
poolId?: XoPool['id']
|
||||
poolMembersAddresses?: string[]
|
||||
poolNameDescription?: string
|
||||
poolNameLabel?: string
|
||||
readOnly: boolean
|
||||
@@ -696,7 +831,9 @@ export type XoTask = {
|
||||
| 'backup-job'
|
||||
| 'backup-log'
|
||||
| 'backup-repository'
|
||||
| 'browser-media'
|
||||
| 'group'
|
||||
| 'plugin'
|
||||
| 'proxy'
|
||||
| 'restore'
|
||||
| 'restore-log'
|
||||
@@ -911,7 +1048,9 @@ export type NonXapiXoRecord =
|
||||
| AnyXoJob
|
||||
| AnyXoLog
|
||||
| XoAuthenticationToken
|
||||
| XoBrowserMediaSession
|
||||
| XoGroup
|
||||
| XoPlugin
|
||||
| XoProxy
|
||||
| XoJob
|
||||
| XoBackupRepository
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
"main": "dist/index.mjs",
|
||||
"name": "@xen-orchestra/acl",
|
||||
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@xen-orchestra/acl",
|
||||
"version": "1.7.0",
|
||||
"version": "1.8.0",
|
||||
"license": "AGPL-3.0-or-later",
|
||||
"private": false,
|
||||
"type": "module",
|
||||
@@ -30,7 +30,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.19.0",
|
||||
"@vates/types": "^2.3.0",
|
||||
"@vates/types": "^2.5.0",
|
||||
"rimraf": "^6.0.1",
|
||||
"typescript": "~5.6",
|
||||
"typescript-eslint": "^8.61.0"
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
export default {
|
||||
read: true,
|
||||
export: true, // can export the content ( full or file by file )
|
||||
read: true, // can read the metadata of an archive
|
||||
mount: true, // can mount the archive for file level restore
|
||||
}
|
||||
|
||||
@@ -25,4 +25,14 @@ export default {
|
||||
update: {
|
||||
tags: true,
|
||||
},
|
||||
probe: {
|
||||
nfs: true,
|
||||
zfs: true,
|
||||
hba: true,
|
||||
iscsiiqn: true,
|
||||
iscsilun: true,
|
||||
'iscsi-exists': true,
|
||||
'hba-exists': true,
|
||||
'nfs-exists': true,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -8,14 +8,13 @@ import { default as backupRepository } from './backup-repository.mjs'
|
||||
import { default as gpuGroup } from './gpuGroup.mjs'
|
||||
import { default as group } from './group.mjs'
|
||||
import { default as host } from './host.mjs'
|
||||
import { default as kubernetesCluster } from './kubernetes-cluster.mjs'
|
||||
import { default as kubernetesNode } from './kubernetes-node.mjs'
|
||||
import { default as message } from './message.mjs'
|
||||
import { default as network } from './network.mjs'
|
||||
import { default as pbd } from './pbd.mjs'
|
||||
import { default as pci } from './pci.mjs'
|
||||
import { default as pgpu } from './pgpu.mjs'
|
||||
import { default as pif } from './pif.mjs'
|
||||
import { default as plugin } from './plugin.mjs'
|
||||
import { default as pool } from './pool.mjs'
|
||||
import { default as proxy } from './proxy.mjs'
|
||||
import { default as restoreLog } from './restore-log.mjs'
|
||||
@@ -52,14 +51,13 @@ export const SUPPORTED_ACTIONS_BY_RESOURCE = {
|
||||
group,
|
||||
gpuGroup,
|
||||
host,
|
||||
'kubernetes-cluster': kubernetesCluster,
|
||||
'kubernetes-node': kubernetesNode,
|
||||
message,
|
||||
network,
|
||||
pbd,
|
||||
pci,
|
||||
pgpu,
|
||||
pif,
|
||||
plugin,
|
||||
pool,
|
||||
proxy,
|
||||
'restore-log': restoreLog,
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
export default {
|
||||
create: true,
|
||||
delete: true,
|
||||
read: true,
|
||||
'read-config': true,
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export default {
|
||||
read: true,
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
export default {
|
||||
read: true,
|
||||
update: {
|
||||
dns: true,
|
||||
gateway: true,
|
||||
ip: true,
|
||||
management: true,
|
||||
mode: true,
|
||||
netmask: true,
|
||||
},
|
||||
}
|
||||
|
||||
10
@xen-orchestra/acl/src/actions/plugin.mts
Normal file
10
@xen-orchestra/acl/src/actions/plugin.mts
Normal file
@@ -0,0 +1,10 @@
|
||||
export default {
|
||||
load: true,
|
||||
read: true,
|
||||
test: true,
|
||||
unload: true,
|
||||
update: {
|
||||
autoload: true,
|
||||
configuration: true,
|
||||
},
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
import assert from 'node:assert'
|
||||
import type { XoUser } from '@vates/types/xo'
|
||||
|
||||
import { SUPPORTED_ACTIONS_BY_RESOURCE } from './actions/index.mjs'
|
||||
@@ -42,10 +41,21 @@ export function hasPrivilegeOn<T extends SupportedResource>({
|
||||
}) {
|
||||
// Function that will be called outside of the module
|
||||
// We cannot be sure types are respected
|
||||
assert.strictEqual(typeof user?.permission, 'string')
|
||||
assert.strictEqual(typeof action, 'string')
|
||||
assert.strictEqual(typeof resource, 'string')
|
||||
assert.strictEqual(objects === undefined, false)
|
||||
if (typeof user?.permission !== 'string') {
|
||||
throw new TypeError('user.permission must be a string')
|
||||
}
|
||||
|
||||
if (typeof action !== 'string') {
|
||||
throw new TypeError('action must be a string')
|
||||
}
|
||||
|
||||
if (typeof resource !== 'string') {
|
||||
throw new TypeError('resource must be a string')
|
||||
}
|
||||
|
||||
if (objects === undefined) {
|
||||
throw new TypeError('objects must be defined')
|
||||
}
|
||||
|
||||
CPrivilege.checkActionIsValid(resource, action as SupportedActions<typeof resource>)
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@xen-orchestra/backup-archive",
|
||||
"version": "1.1.3",
|
||||
"version": "1.2.0",
|
||||
"description": "Helper to handle generic backup archives",
|
||||
"license": "ISC",
|
||||
"author": "",
|
||||
@@ -21,18 +21,18 @@
|
||||
"tar-stream": "^3.1.8",
|
||||
"@types/tar-stream": "^3.1.4",
|
||||
"tmp": "^0.2.1",
|
||||
"@vates/types": "^2.3.0"
|
||||
"@vates/types": "^2.5.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@xen-orchestra/log": "^0.7.1",
|
||||
"@xen-orchestra/fs": "^4.9.5",
|
||||
"@xen-orchestra/disk-transform": "^1.3.3",
|
||||
"@xen-orchestra/fs": "^4.10.0",
|
||||
"@xen-orchestra/disk-transform": "^1.3.4",
|
||||
"@vates/task": "0.7.0",
|
||||
"golike-defer": "0.5.1",
|
||||
"uuid": "^9.0.0",
|
||||
"promise-toolbox": "^0.21.0",
|
||||
"@vates/async-each": "^1.0.1",
|
||||
"vhd-lib": "^4.16.2"
|
||||
"vhd-lib": "^4.17.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "yarn run clean && tsc",
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
* @typedef {import('vhd-lib/Vhd/VhdFile.js').VhdFile} VhdFile
|
||||
* @typedef {import('vhd-lib/_createFooterHeader.js').VhdFooter} VhdFooter
|
||||
* @typedef {import('@xen-orchestra/disk-transform').DiskBlock} DiskBlock
|
||||
* @typedef {import('vhd-lib/disk-consumer/compressedBlock.mjs').CompressedDiskBlock} CompressedDiskBlock
|
||||
* @typedef {import('@xen-orchestra/fs').RemoteHandlerAbstract} RemoteHandlerAbstract
|
||||
*
|
||||
|
||||
@@ -45,26 +46,36 @@ export class RemoteVhdDisk extends RemoteDisk {
|
||||
*/
|
||||
#blockSize = 2 * 1024 * 1024
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
#bitmapSize = 512
|
||||
|
||||
/**
|
||||
* @type {() => any}
|
||||
*/
|
||||
#dispose = () => {}
|
||||
|
||||
/**
|
||||
* @type {string | undefined}
|
||||
*/
|
||||
#rawBlocksCompression
|
||||
|
||||
/**
|
||||
* set in init(): this disk is a VHD directory using #rawBlocksCompression
|
||||
* @type {boolean}
|
||||
*/
|
||||
#rawBlocks = false
|
||||
|
||||
/**
|
||||
* @param {Object} params
|
||||
* @param {RemoteHandlerAbstract} params.handler
|
||||
* @param {string} params.path
|
||||
* @param {string} [params.rawBlocksCompression] if this disk is a VHD directory using this compression, its blocks
|
||||
* are read as stored (see CompressedDiskBlock), to be written as is in a VHD directory using the same compression.
|
||||
* Otherwise (plain VHD, other compression), they are read normally
|
||||
*/
|
||||
constructor({ handler, path }) {
|
||||
constructor({ handler, path, rawBlocksCompression }) {
|
||||
super()
|
||||
// @todo : ensure this is the full path from the root of the remote
|
||||
this.#path = path
|
||||
this.#handler = handler
|
||||
this.#rawBlocksCompression = rawBlocksCompression
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,6 +98,9 @@ export class RemoteVhdDisk extends RemoteDisk {
|
||||
this.#vhd = undefined
|
||||
throw Object.assign(new Error("Can't init vhd directory without using alias"), { code: 'NOT_SUPPORTED' })
|
||||
}
|
||||
// the disk may predate a change of the remote configuration: only use its raw blocks if they match
|
||||
this.#rawBlocks =
|
||||
this.#rawBlocksCompression !== undefined && this.#getCompressionType() === this.#rawBlocksCompression
|
||||
|
||||
if (!options.ignoreBlockIndexes) {
|
||||
await this.#vhd.readBlockAllocationTable()
|
||||
@@ -263,10 +277,7 @@ export class RemoteVhdDisk extends RemoteDisk {
|
||||
if (this.#vhd === undefined) {
|
||||
throw new Error(`can't call readBlock of a RemoteVhdDisk before init`)
|
||||
}
|
||||
await this.#vhd.writeEntireBlock({
|
||||
id: diskBlock.index,
|
||||
buffer: Buffer.concat([Buffer.alloc(this.#bitmapSize, 255), diskBlock.data]),
|
||||
})
|
||||
await this.#vhd.writeBlockData(diskBlock.index, diskBlock.data)
|
||||
|
||||
return this.getBlockSize()
|
||||
}
|
||||
@@ -274,12 +285,22 @@ export class RemoteVhdDisk extends RemoteDisk {
|
||||
/**
|
||||
* Reads a specific block from the VHD.
|
||||
* @param {number} index
|
||||
* @returns {Promise<DiskBlock>} diskBlock
|
||||
* @returns {Promise<DiskBlock | CompressedDiskBlock>} diskBlock
|
||||
*/
|
||||
async readBlock(index) {
|
||||
if (this.#vhd === undefined) {
|
||||
throw new Error(`can't call readBlock of a RemoteVhdDisk before init`)
|
||||
}
|
||||
if (this.#rawBlocks) {
|
||||
/** @type {CompressedDiskBlock} */
|
||||
const block = {
|
||||
index,
|
||||
// only a VHD directory has a compression, checked in init()
|
||||
data: await /** @type {VhdDirectory} */ (this.#vhd).readCompressedBlock(index),
|
||||
compression: /** @type {string} */ (this.#rawBlocksCompression),
|
||||
}
|
||||
return block
|
||||
}
|
||||
const { data } = await this.#vhd.readBlock(index)
|
||||
return {
|
||||
index,
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
"dependencies": {
|
||||
"@vates/async-each": "^1.0.3",
|
||||
"@xen-orchestra/async-map": "^0.1.5",
|
||||
"@xen-orchestra/backups": "^0.76.1",
|
||||
"@xen-orchestra/fs": "^4.9.5",
|
||||
"@xen-orchestra/backups": "^0.78.0",
|
||||
"@xen-orchestra/fs": "^4.10.0",
|
||||
"filenamify": "^6.0.0",
|
||||
"getopts": "^2.2.5",
|
||||
"lodash": "^4.18.0",
|
||||
@@ -28,7 +28,7 @@
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public"
|
||||
},
|
||||
"version": "1.1.16",
|
||||
"version": "1.1.18",
|
||||
"license": "AGPL-3.0-or-later",
|
||||
"author": {
|
||||
"name": "Vates SAS",
|
||||
|
||||
@@ -2,6 +2,7 @@ import assert from 'node:assert'
|
||||
|
||||
import { formatFilenameDate } from './_filenameDate.mjs'
|
||||
import { importIncrementalVm } from './_incrementalVm.mjs'
|
||||
import { liveMountXapiLabels } from './liveMountXapiLabels.mjs'
|
||||
import { watchStreamSize } from './_watchStreamSize.mjs'
|
||||
import { decorateClass } from '@vates/decorate-with'
|
||||
import { Task } from '@vates/task'
|
||||
@@ -42,7 +43,8 @@ export class ImportVmBackup {
|
||||
* @param {object} [params.liveMount] - how to serve a disk from the backup repository instead of
|
||||
* copying it, injected by the caller since a live mount outlives the restore and cannot be run
|
||||
* from this package
|
||||
* @param {({ diskPath, hostId }) => Promise<{ id: string, vdiUuid: string }>} params.liveMount.mountDisk
|
||||
* @param {({ cacheSrUuid, diskPath, hostId, xapiLabels }) => Promise<{ id: string, vdiUuid: string }>} params.liveMount.mountDisk
|
||||
* `xapiLabels` are those of {@link liveMountXapiLabels}, `cacheSrUuid` makes the mount read/write
|
||||
* @param {(id: string) => Promise<void>} params.liveMount.unmountDisk
|
||||
* @param {object} [params.settings]
|
||||
*/
|
||||
@@ -356,11 +358,23 @@ export class ImportVmBackup {
|
||||
}
|
||||
|
||||
const diskPath = join(metadataDir, metadata.vhds[vdiRef])
|
||||
const mount = await liveMount.mountDisk({ diskPath, hostId })
|
||||
const cacheSrUuid = this._vdiRestoreTargets.get(vdi.uuid).cacheSr
|
||||
const readWrite = cacheSrUuid !== undefined
|
||||
const xapiLabels = liveMountXapiLabels({
|
||||
readWrite,
|
||||
timestamp: metadata.timestamp,
|
||||
vdiNameLabel: vdi.name_label,
|
||||
vmNameLabel: metadata.vm.name_label,
|
||||
})
|
||||
const mount = await liveMount.mountDisk({ cacheSrUuid, diskPath, hostId, xapiLabels })
|
||||
this.#liveMounts.push({ ...mount, hostId })
|
||||
info('disk live mounted', { diskPath, hostId, mountId: mount.id, vdiUuid: vdi.uuid })
|
||||
info('disk live mounted', { cacheSrUuid, diskPath, hostId, mountId: mount.id, vdiUuid: vdi.uuid })
|
||||
|
||||
const record = { ...vdi, liveMountedVdiRef: await xapi.call('VDI.get_by_uuid', mount.vdiUuid) }
|
||||
const record = {
|
||||
...vdi,
|
||||
liveMountedVdiRef: await xapi.call('VDI.get_by_uuid', mount.vdiUuid),
|
||||
liveMountedReadOnly: !readWrite,
|
||||
}
|
||||
// it is attached as is: there is nothing to clone it from, nothing to transfer into it, and
|
||||
// no SR to create it on — `SR` still holds the ref it had on the backed up pool
|
||||
delete record.baseVdi
|
||||
|
||||
@@ -141,7 +141,20 @@ describe('ImportVmBackup#_decorateIncrementalVmMetadata()', () => {
|
||||
|
||||
const backup = await importer._decorateIncrementalVmMetadata()
|
||||
|
||||
assert.deepEqual(liveMount.mounted, [{ diskPath: `/xo-vm-backups/${VM_UUID}/${vhdPath(DATA)}`, hostId: HOST }])
|
||||
assert.deepEqual(liveMount.mounted, [
|
||||
{
|
||||
// not cached: read only
|
||||
cacheSrUuid: undefined,
|
||||
diskPath: `/xo-vm-backups/${VM_UUID}/${vhdPath(DATA)}`,
|
||||
hostId: HOST,
|
||||
// named after the backed up VM and disk, not after the file on the backup repository
|
||||
xapiLabels: {
|
||||
srNameLabel: '[XO live mount] a vm (20250801T080832Z)',
|
||||
vdiNameLabel: 'data',
|
||||
vdiNameDescription: 'Read-only live mount of a vm (20250801T080832Z), removed on unmount.',
|
||||
},
|
||||
},
|
||||
])
|
||||
// no data is transferred for it
|
||||
assert.deepEqual(Object.keys(backup.disks), [SYSTEM.ref])
|
||||
})
|
||||
@@ -153,6 +166,7 @@ describe('ImportVmBackup#_decorateIncrementalVmMetadata()', () => {
|
||||
|
||||
const vdi = backup.vdis[DATA.ref]
|
||||
assert.equal(vdi.liveMountedVdiRef, 'VDI_REF:mounted-vdi-1')
|
||||
assert.equal(vdi.liveMountedReadOnly, true)
|
||||
assert.equal(vdi.SR, undefined)
|
||||
assert.equal(vdi.uuid, DATA.uuid)
|
||||
// the other disk is restored as usual
|
||||
@@ -160,6 +174,19 @@ describe('ImportVmBackup#_decorateIncrementalVmMetadata()', () => {
|
||||
assert.equal(backup.vdis[SYSTEM.ref].liveMountedVdiRef, undefined)
|
||||
})
|
||||
|
||||
it('mounts a cached disk read/write, and says so in its names', async () => {
|
||||
const { importer, liveMount } = makeImporter({
|
||||
mapVdisSrs: { [DATA.uuid]: { type: 'live-mount', host: HOST, cacheSr: OTHER_SR } },
|
||||
})
|
||||
|
||||
const backup = await importer._decorateIncrementalVmMetadata()
|
||||
|
||||
const [mounted] = liveMount.mounted
|
||||
assert.equal(mounted.cacheSrUuid, OTHER_SR)
|
||||
assert.equal(mounted.xapiLabels.srNameLabel, '[XO live restore] a vm (20250801T080832Z)')
|
||||
assert.equal(backup.vdis[DATA.ref].liveMountedReadOnly, false)
|
||||
})
|
||||
|
||||
it('rejects live mounts spread over several hosts', async () => {
|
||||
const { importer } = makeImporter({
|
||||
mapVdisSrs: {
|
||||
|
||||
@@ -40,10 +40,10 @@ afterEach(async () => {
|
||||
const uniqueId = () => uuid.v1()
|
||||
const uniqueIdBuffer = () => uuid.v1({}, Buffer.alloc(16))
|
||||
|
||||
async function getAdapter({ useVhdDirectory = false, vhdDirectoryCompression, remoteCompressionType } = {}) {
|
||||
async function getAdapter({ useVhdDirectory = false, remoteCompressionType } = {}) {
|
||||
handler = getHandler({ url: `file://${tempDir}`, useVhdDirectory, compressionType: remoteCompressionType })
|
||||
await handler.sync()
|
||||
return new RemoteAdapter(handler, { vhdDirectoryCompression })
|
||||
return new RemoteAdapter(handler)
|
||||
}
|
||||
|
||||
const listVmDir = () => handler.list(rootPath, { ignoreMissing: true })
|
||||
@@ -145,7 +145,8 @@ describe('RemoteAdapter#isMergeableParent', { concurrency: 1 }, () => {
|
||||
|
||||
test(`diskIsDirectory=${diskIsDirectory} useVhdDirectory=${useVhdDirectory} compressionMatches=${compressionMatches} uuidMatches=${uuidMatches} -> ${expected}`, async () => {
|
||||
const targetCompression = 'brotli'
|
||||
const adapter = await getAdapter({ useVhdDirectory, vhdDirectoryCompression: targetCompression })
|
||||
// no compressionType on the remote: brotli
|
||||
const adapter = await getAdapter({ useVhdDirectory })
|
||||
const targetUuid = uniqueIdBuffer()
|
||||
|
||||
await generateVhd(`${basePath}/disk.vhd`, {
|
||||
@@ -164,37 +165,32 @@ describe('RemoteAdapter#isMergeableParent', { concurrency: 1 }, () => {
|
||||
})
|
||||
|
||||
describe('RemoteAdapter#isMergeableParent compression resolution', { concurrency: 1 }, () => {
|
||||
test('the per-remote compressionType override wins over the adapter default', async () => {
|
||||
const adapter = await getAdapter({
|
||||
useVhdDirectory: true,
|
||||
vhdDirectoryCompression: 'gzip', // config.toml-style default
|
||||
remoteCompressionType: 'brotli', // per-remote override, should be the one actually compared against
|
||||
})
|
||||
test('the compressionType of the remote is the one compared against', async () => {
|
||||
const adapter = await getAdapter({ useVhdDirectory: true, remoteCompressionType: 'gzip' })
|
||||
const targetUuid = uniqueIdBuffer()
|
||||
await generateVhd(`${basePath}/disk.vhd`, { mode: 'directory', compression: 'gzip', uuid: targetUuid })
|
||||
|
||||
assert.equal(await adapter.isMergeableParent(targetUuid, `${basePath}/disk.vhd.alias.vhd`), true)
|
||||
})
|
||||
|
||||
test('a disk compressed with another codec than the remote is rejected', async () => {
|
||||
const adapter = await getAdapter({ useVhdDirectory: true, remoteCompressionType: 'gzip' })
|
||||
const targetUuid = uniqueIdBuffer()
|
||||
await generateVhd(`${basePath}/disk.vhd`, { mode: 'directory', compression: 'brotli', uuid: targetUuid })
|
||||
|
||||
assert.equal(await adapter.isMergeableParent(targetUuid, `${basePath}/disk.vhd.alias.vhd`), false)
|
||||
})
|
||||
|
||||
test('a remote without compressionType uses brotli', async () => {
|
||||
const adapter = await getAdapter({ useVhdDirectory: true })
|
||||
const targetUuid = uniqueIdBuffer()
|
||||
await generateVhd(`${basePath}/disk.vhd`, { mode: 'directory', compression: 'brotli', uuid: targetUuid })
|
||||
|
||||
assert.equal(await adapter.isMergeableParent(targetUuid, `${basePath}/disk.vhd.alias.vhd`), true)
|
||||
})
|
||||
|
||||
test('a disk compressed with the adapter default (not the override) is rejected', async () => {
|
||||
const adapter = await getAdapter({
|
||||
useVhdDirectory: true,
|
||||
vhdDirectoryCompression: 'gzip',
|
||||
remoteCompressionType: 'brotli',
|
||||
})
|
||||
const targetUuid = uniqueIdBuffer()
|
||||
// disk uses the config default, not the remote override that's actually in effect
|
||||
await generateVhd(`${basePath}/disk.vhd`, { mode: 'directory', compression: 'gzip', uuid: targetUuid })
|
||||
|
||||
assert.equal(await adapter.isMergeableParent(targetUuid, `${basePath}/disk.vhd.alias.vhd`), false)
|
||||
})
|
||||
|
||||
test("a remote-level 'none' override means no compression, ignoring the adapter default", async () => {
|
||||
const adapter = await getAdapter({
|
||||
useVhdDirectory: true,
|
||||
vhdDirectoryCompression: 'brotli',
|
||||
remoteCompressionType: 'none',
|
||||
})
|
||||
test("a remote-level 'none' means no compression", async () => {
|
||||
const adapter = await getAdapter({ useVhdDirectory: true, remoteCompressionType: 'none' })
|
||||
const targetUuid = uniqueIdBuffer()
|
||||
// no compression option => VhdDirectory's own getCompressionType() is undefined, matching 'none'
|
||||
await generateVhd(`${basePath}/disk.vhd`, { mode: 'directory', uuid: targetUuid })
|
||||
@@ -323,3 +319,105 @@ describe('RemoteAdapter#writeVhd validated by checkDisk', { concurrency: 1 }, ()
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// IncrementalRemote runner when source and targets use the same compression: the blocks are copied as stored
|
||||
describe('RemoteAdapter raw blocks copy', { concurrency: 1 }, () => {
|
||||
// copy a disk the way the IncrementalRemote runner does, and check the target content
|
||||
async function copyAndCheck(adapter, sourcePath, { rawBlocksCompression, expectedCompression }) {
|
||||
const targetPath = `${basePath}/${adapter.getVhdFileName('target')}`
|
||||
const source = await adapter._createVhdDisk(handler, sourcePath, { useChain: false, rawBlocksCompression })
|
||||
await adapter.writeVhd(targetPath, source, {
|
||||
validator: tmpPath => checkDisk(handler, tmpPath),
|
||||
uuid: uniqueIdBuffer(),
|
||||
})
|
||||
|
||||
await Disposable.use(openVhd(handler, targetPath), async vhd => {
|
||||
assert.equal(vhd.compressionType, expectedCompression)
|
||||
await vhd.readBlockAllocationTable()
|
||||
assert.ok(vhd.containsBlock(0))
|
||||
assert.ok(!vhd.containsBlock(1))
|
||||
const { data } = await vhd.readBlock(2)
|
||||
assert.ok(
|
||||
data.every(byte => byte === 0x42),
|
||||
'block 2 should contain the source disk data'
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
for (const compression of ['brotli', 'gzip', 'none']) {
|
||||
test(`${compression}: a disk read with the same compression is copied as stored, readable and identical`, async () => {
|
||||
const adapter = await getAdapter({ useVhdDirectory: true, remoteCompressionType: compression })
|
||||
const sourcePath = `${basePath}/${adapter.getVhdFileName('source')}`
|
||||
await adapter.writeVhd(sourcePath, new MockDisk(3, [0, 2], 0x42), { uuid: uniqueIdBuffer() })
|
||||
|
||||
const source = await adapter._createVhdDisk(handler, sourcePath, {
|
||||
useChain: false,
|
||||
rawBlocksCompression: compression,
|
||||
})
|
||||
const block = await source.readBlock(2)
|
||||
await source.close()
|
||||
assert.equal(block.compression, compression)
|
||||
if (compression !== 'none') {
|
||||
assert.ok(block.data.length < 2 * 1024 * 1024, 'the block should be read compressed')
|
||||
}
|
||||
|
||||
await copyAndCheck(adapter, sourcePath, { rawBlocksCompression: compression, expectedCompression: compression })
|
||||
})
|
||||
}
|
||||
|
||||
test('a disk written before a compression change is read normally and copied', async () => {
|
||||
const gzipAdapter = await getAdapter({ useVhdDirectory: true, remoteCompressionType: 'gzip' })
|
||||
const sourcePath = `${basePath}/${gzipAdapter.getVhdFileName('source')}`
|
||||
await gzipAdapter.writeVhd(sourcePath, new MockDisk(3, [0, 2], 0x42), { uuid: uniqueIdBuffer() })
|
||||
await handler.forget()
|
||||
|
||||
const brotliAdapter = await getAdapter({ useVhdDirectory: true, remoteCompressionType: 'brotli' })
|
||||
const source = await brotliAdapter._createVhdDisk(handler, sourcePath, {
|
||||
useChain: false,
|
||||
rawBlocksCompression: 'brotli',
|
||||
})
|
||||
const block = await source.readBlock(2)
|
||||
await source.close()
|
||||
assert.equal(block.compression, undefined)
|
||||
assert.equal(block.data.length, 2 * 1024 * 1024)
|
||||
|
||||
await copyAndCheck(brotliAdapter, sourcePath, { rawBlocksCompression: 'brotli', expectedCompression: 'brotli' })
|
||||
})
|
||||
|
||||
test('a VHD file written before the switch to VHD directories is read normally and copied', async () => {
|
||||
const fileAdapter = await getAdapter({ useVhdDirectory: false })
|
||||
const sourcePath = `${basePath}/${fileAdapter.getVhdFileName('source')}`
|
||||
await fileAdapter.writeVhd(sourcePath, new MockDisk(3, [0, 2], 0x42), { uuid: uniqueIdBuffer() })
|
||||
await handler.forget()
|
||||
|
||||
const directoryAdapter = await getAdapter({ useVhdDirectory: true, remoteCompressionType: 'brotli' })
|
||||
await copyAndCheck(directoryAdapter, sourcePath, { rawBlocksCompression: 'brotli', expectedCompression: 'brotli' })
|
||||
})
|
||||
|
||||
// blocks as read from a VHD directory using `compression`, without their real content: only the checks are tested
|
||||
class MockCompressedDisk extends MockDisk {
|
||||
#compression
|
||||
constructor(compression, ...args) {
|
||||
super(...args)
|
||||
this.#compression = compression
|
||||
}
|
||||
|
||||
async readBlock(index) {
|
||||
return { ...(await super.readBlock(index)), compression: this.#compression }
|
||||
}
|
||||
}
|
||||
|
||||
test('a VHD directory target refuses blocks compressed differently', async () => {
|
||||
const adapter = await getAdapter({ useVhdDirectory: true, remoteCompressionType: 'brotli' })
|
||||
await assert.rejects(
|
||||
adapter.writeVhd(
|
||||
`${basePath}/${adapter.getVhdFileName('target')}`,
|
||||
new MockCompressedDisk('gzip', 1, [0], 0x42),
|
||||
{
|
||||
uuid: uniqueIdBuffer(),
|
||||
}
|
||||
),
|
||||
/can't write a gzip compressed block 0/
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -3,6 +3,7 @@ import { asyncMap, asyncMapSettled } from '@xen-orchestra/async-map'
|
||||
import { createLogger } from '@xen-orchestra/log'
|
||||
import { stringify } from 'uuid'
|
||||
import { decorateMethodsWith } from '@vates/decorate-with'
|
||||
import assert from 'node:assert'
|
||||
import { basename, dirname, join, resolve } from 'node:path'
|
||||
import { normalize } from '@xen-orchestra/fs/path'
|
||||
import { synchronized } from 'decorator-synchronized'
|
||||
@@ -26,6 +27,7 @@ import { formatFilenameDate } from './_filenameDate.mjs'
|
||||
import { isMetadataFile } from './_backupType.mjs'
|
||||
import {
|
||||
isKnownJournalEvent,
|
||||
purgeBackupJournal,
|
||||
readBackupJournal,
|
||||
writeBackupJournalEntries,
|
||||
writeBackupJournalEntry,
|
||||
@@ -64,6 +66,10 @@ export class RemoteAdapter {
|
||||
this._handler = handler
|
||||
this._readCacheListVmBackups = synchronized.withKey()(this._readCacheListVmBackups)
|
||||
this._useGetDiskLegacy = useGetDiskLegacy
|
||||
|
||||
// Cache of the partition list per diskId, populated by listPartitions and evicted by
|
||||
// getDisk when the disk is unmounted
|
||||
this._partitionsCache = new Map()
|
||||
}
|
||||
|
||||
get handler() {
|
||||
@@ -317,6 +323,14 @@ export class RemoteAdapter {
|
||||
return this.handler.getConfig('useVhdDirectory')
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {string | undefined} the compression of the VHD directories written on this remote ('none' if not
|
||||
* compressed), undefined if this remote writes plain VHD files
|
||||
*/
|
||||
getVhdDirectoryCompression() {
|
||||
return this.useVhdDirectory() ? (this.handler.getConfig('compressionType') ?? 'brotli') : undefined // compatibility layer
|
||||
}
|
||||
|
||||
#useAlias() {
|
||||
return this.useVhdDirectory()
|
||||
}
|
||||
@@ -546,6 +560,14 @@ export class RemoteAdapter {
|
||||
return readBackupJournal(this._handler, cursor, opts)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} before timestamp in ms, the journal days before its own are removed
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async purgeBackupJournal(before) {
|
||||
return purgeBackupJournal(this._handler, before)
|
||||
}
|
||||
|
||||
// Same as `readBackupJournal()`, with the current metadata of the added and changed backups
|
||||
// attached, so that a listing can be brought up to date from the result alone, in a single
|
||||
// round-trip for a caller which is not on this host.
|
||||
@@ -676,7 +698,7 @@ export class RemoteAdapter {
|
||||
path,
|
||||
concurrency: writeBlockConcurrency,
|
||||
validator,
|
||||
compression: handler.getConfig('compressionType') ?? 'brotli', // compatibility layer
|
||||
compression: this.getVhdDirectoryCompression(),
|
||||
uuid,
|
||||
parentUuid,
|
||||
parentPath,
|
||||
@@ -716,19 +738,24 @@ export class RemoteAdapter {
|
||||
}
|
||||
|
||||
// open the hierarchy of ancestors until we find a full one
|
||||
async _createVhdDisk(handler, path, { useChain }) {
|
||||
async _createVhdDisk(handler, path, { useChain, rawBlocksCompression }) {
|
||||
let disk
|
||||
if (useChain) {
|
||||
assert.strictEqual(rawBlocksCompression, undefined, `can't read raw blocks of a disk chain`)
|
||||
disk = await openDiskChain({ handler, path })
|
||||
} else {
|
||||
disk = new RemoteVhdDisk({ handler, path })
|
||||
disk = new RemoteVhdDisk({ handler, path, rawBlocksCompression })
|
||||
await disk.init()
|
||||
}
|
||||
disk = new ReadAhead(disk)
|
||||
return disk
|
||||
}
|
||||
|
||||
async readIncrementalVmBackup(metadata, ignoredVdis, { useChain = true } = {}) {
|
||||
/**
|
||||
* `rawBlocksCompression`: the blocks of the VHD directories using this compression are read as stored (see
|
||||
* RemoteVhdDisk), to be written as is in a remote using the same compression
|
||||
*/
|
||||
async readIncrementalVmBackup(metadata, ignoredVdis, { useChain = true, rawBlocksCompression } = {}) {
|
||||
const handler = this._handler
|
||||
const { vbds, vhds, vifs, vm, vmSnapshot, vtpms } = metadata
|
||||
const dir = dirname(metadata._filename)
|
||||
@@ -736,7 +763,7 @@ export class RemoteAdapter {
|
||||
const disks = {}
|
||||
await asyncMapSettled(Object.keys(vdis), async ref => {
|
||||
delete vdis[ref].baseVdi
|
||||
disks[ref] = await this._createVhdDisk(handler, join(dir, vhds[ref]), { useChain })
|
||||
disks[ref] = await this._createVhdDisk(handler, join(dir, vhds[ref]), { useChain, rawBlocksCompression })
|
||||
})
|
||||
|
||||
return {
|
||||
|
||||
@@ -393,6 +393,47 @@ describe('readBackupJournal()', { concurrency: 1 }, () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('purgeBackupJournal()', { concurrency: 1 }, () => {
|
||||
const DAY = 24 * 60 * 60 * 1e3
|
||||
const HOUR = 60 * 60 * 1e3
|
||||
|
||||
// midnight of the given number of days ago, so that the tests do not depend on the time they run at
|
||||
const daysAgo = n => (Math.floor(Date.now() / DAY) - n) * DAY
|
||||
|
||||
const writeEntryAt = timestamp =>
|
||||
writeJournalEntryAt(timestamp, { event: 'add', vmUuid, filename: `/${rootPath}/${timestamp}.json` })
|
||||
|
||||
test('removes the days before the one of `before`, and keeps that day whole', async () => {
|
||||
for (const n of [3, 2]) {
|
||||
await writeEntryAt(daysAgo(n) + HOUR)
|
||||
}
|
||||
|
||||
// after the entry of its own day: that entry is kept all the same
|
||||
await adapter.purgeBackupJournal(daysAgo(2) + 2 * HOUR)
|
||||
|
||||
assert.deepEqual(await listJournalDays(), [formatJournalDay(daysAgo(2))])
|
||||
})
|
||||
|
||||
test('skips a day which cannot be removed, and removes the next ones', async t => {
|
||||
for (const n of [3, 2]) {
|
||||
await writeEntryAt(daysAgo(n))
|
||||
}
|
||||
const lockedDay = formatJournalDay(daysAgo(3))
|
||||
|
||||
const rmtree = handler.rmtree.bind(handler)
|
||||
t.mock.method(handler, 'rmtree', async path => {
|
||||
if (path === `/${BACKUP_JOURNAL_DIR}/${lockedDay}`) {
|
||||
throw Object.assign(new Error('EPERM: operation not permitted'), { code: 'EPERM' })
|
||||
}
|
||||
return rmtree(path)
|
||||
})
|
||||
|
||||
await adapter.purgeBackupJournal(Date.now())
|
||||
|
||||
assert.deepEqual(await listJournalDays(), [lockedDay])
|
||||
})
|
||||
})
|
||||
|
||||
describe('readBackupJournalEvents()', { concurrency: 1 }, () => {
|
||||
// records the paths whose metadata is read from now on, to check a replay does not re-read the
|
||||
// same backup once per event
|
||||
|
||||
@@ -309,3 +309,47 @@ export async function readBackupJournal(handler, cursor, { mustExist = false } =
|
||||
|
||||
return entries
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the journal entries of the days before the one of `before`.
|
||||
*
|
||||
* Whole days are removed, entries are never removed one by one: the day of `before` is kept entirely,
|
||||
* whatever the time of its entries.
|
||||
*
|
||||
* A day which cannot be removed is skipped, e.g. its files are immutable (`EPERM` on a local
|
||||
* repository, `AccessDenied` from the policy of a bucket): it must not keep the later days from being
|
||||
* removed, and the next purge tries it again. Only a failure to list the journal rejects.
|
||||
*
|
||||
* A purge warns once, with its first failure and their count: where deletes are refused, every day
|
||||
* before `before` fails again on each purge, one more each day.
|
||||
*
|
||||
* @param {RemoteHandler} handler
|
||||
* @param {number} before timestamp in ms
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function purgeBackupJournal(handler, before) {
|
||||
const minDay = formatJournalDay(before)
|
||||
const days = await handler.list(`/${BACKUP_JOURNAL_DIR}`, {
|
||||
filter: name => isDayName(name) && name < minDay,
|
||||
ignoreMissing: true,
|
||||
})
|
||||
let firstFailure
|
||||
let nFailures = 0
|
||||
// one day at a time: this runs in the background, it must not compete with the backups for the
|
||||
// remote
|
||||
for (const day of days) {
|
||||
const path = `/${BACKUP_JOURNAL_DIR}/${day}`
|
||||
try {
|
||||
await handler.rmtree(path)
|
||||
debug('journal day purged', { path })
|
||||
} catch (error) {
|
||||
if (firstFailure === undefined) {
|
||||
firstFailure = { error, path }
|
||||
}
|
||||
++nFailures
|
||||
}
|
||||
}
|
||||
if (firstFailure !== undefined) {
|
||||
warn('failed to purge some journal days', { ...firstFailure, nFailures })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +75,6 @@ class BackupWorker {
|
||||
yield new RemoteAdapter(handler, {
|
||||
debounceResource: this.debounceResource,
|
||||
dirMode: this.#config.dirMode,
|
||||
vhdDirectoryCompression: this.#config.vhdDirectoryCompression,
|
||||
})
|
||||
} finally {
|
||||
await handler.forget()
|
||||
|
||||
111
@xen-orchestra/backups/_buildContext.mjs
Normal file
111
@xen-orchestra/backups/_buildContext.mjs
Normal file
@@ -0,0 +1,111 @@
|
||||
// @ts-check
|
||||
|
||||
import { createLogger } from '@xen-orchestra/log'
|
||||
import moment from 'moment-timezone'
|
||||
import { parse } from 'xo-remote-parser'
|
||||
|
||||
const { warn } = createLogger('xo:backups:buildContext')
|
||||
|
||||
/**
|
||||
* @typedef {import('@vates/types/xen-api').XenApiVm} XenApiVm
|
||||
* @typedef {import('@vates/types/xo').XoSr} XoSr
|
||||
* @typedef {import('@vates/types/xo').XoBackupRepository} XoBackupRepository
|
||||
*
|
||||
* @typedef {object} RunContext
|
||||
* @property {number} dayOfWeek
|
||||
* @property {number} dayOfMonth
|
||||
* @property {number} hour
|
||||
* @property {number} month
|
||||
* @property {number} year
|
||||
*
|
||||
* @typedef {object} VmContext
|
||||
* @property {Record<string, unknown>} vm
|
||||
* @property {RunContext} run
|
||||
* @property {number} [chainLength]
|
||||
*
|
||||
* @typedef {object} SrContext
|
||||
* @property {{name_label: string, type: string, description: string, tags: string[]}} sr
|
||||
* @property {RunContext} run
|
||||
*
|
||||
* @typedef {object} BackupRepositoryContext
|
||||
* @property {{name: string, type: string | undefined, tags: string[]}} backupRepository
|
||||
* @property {RunContext} run
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {Date} date
|
||||
* @param {string | undefined} timezone
|
||||
* @returns {RunContext}
|
||||
*/
|
||||
export function buildRunContext(date, timezone) {
|
||||
const momentDate = timezone ? moment.tz(date, timezone) : moment(date)
|
||||
return {
|
||||
dayOfWeek: momentDate.day(),
|
||||
dayOfMonth: momentDate.date(),
|
||||
hour: momentDate.hour(),
|
||||
month: momentDate.month() + 1,
|
||||
year: momentDate.year(),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {XenApiVm} vm
|
||||
* @param {RunContext} run
|
||||
* @param {number} [chainLength]
|
||||
* @returns {VmContext}
|
||||
*/
|
||||
export function buildVmContext(vm, run, chainLength) {
|
||||
/** @type {Record<string, unknown>} */
|
||||
const vmObject = {}
|
||||
for (const [key, value] of Object.entries(vm)) {
|
||||
if (!key.startsWith('$') && typeof value !== 'function') {
|
||||
vmObject[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
if (!Array.isArray(vmObject.tags)) {
|
||||
vmObject.tags = []
|
||||
}
|
||||
|
||||
return { vm: vmObject, run, chainLength }
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {XoSr} sr
|
||||
* @param {RunContext} run
|
||||
* @returns {SrContext}
|
||||
*/
|
||||
export function buildSrContext(sr, run) {
|
||||
return {
|
||||
sr: {
|
||||
name_label: sr.name_label,
|
||||
type: sr.SR_type,
|
||||
description: sr.name_description,
|
||||
tags: Array.isArray(sr.tags) ? sr.tags : [],
|
||||
},
|
||||
run,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {XoBackupRepository} backupRepository
|
||||
* @param {RunContext} run
|
||||
* @returns {BackupRepositoryContext}
|
||||
*/
|
||||
export function buildBackupRepositoryContext(backupRepository, run) {
|
||||
let type
|
||||
try {
|
||||
type = parse(backupRepository.url).type
|
||||
} catch (error) {
|
||||
warn('cannot parse backup repository URL', { backupRepositoryId: backupRepository.id, error })
|
||||
}
|
||||
|
||||
return {
|
||||
backupRepository: {
|
||||
name: backupRepository.name,
|
||||
type,
|
||||
tags: [], // Placeholder - Backup repositories don't have tags yet
|
||||
},
|
||||
run,
|
||||
}
|
||||
}
|
||||
147
@xen-orchestra/backups/_buildContext.test.mjs
Normal file
147
@xen-orchestra/backups/_buildContext.test.mjs
Normal file
@@ -0,0 +1,147 @@
|
||||
import { describe, it } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import { buildRunContext, buildVmContext, buildSrContext, buildBackupRepositoryContext } from './_buildContext.mjs'
|
||||
import { BACKUP_EXPRESSION_CONTEXT } from '@vates/types/backup-expression-context'
|
||||
|
||||
describe('buildContext', () => {
|
||||
const date = new Date('2025-01-05T03:00:00Z')
|
||||
const runContext = buildRunContext(date, 'America/New_York')
|
||||
const expectedRunContext = {
|
||||
dayOfWeek: 6,
|
||||
dayOfMonth: 4,
|
||||
hour: 22,
|
||||
month: 1,
|
||||
year: 2025,
|
||||
}
|
||||
|
||||
it('buildRunContext', () => {
|
||||
assert.deepEqual(runContext, expectedRunContext)
|
||||
})
|
||||
|
||||
it('buildVmContext', () => {
|
||||
const vm1 = { name: 'name', $value: '$value', func: () => {} }
|
||||
const vmContext1 = buildVmContext(vm1, runContext, 3)
|
||||
assert.deepEqual(vmContext1, {
|
||||
vm: {
|
||||
name: 'name',
|
||||
tags: [],
|
||||
},
|
||||
run: expectedRunContext,
|
||||
chainLength: 3,
|
||||
})
|
||||
|
||||
const vm2 = { name: 'name', $value: '$value', func: () => {}, tags: ['tag'] }
|
||||
const vmContext2 = buildVmContext(vm2, runContext, undefined)
|
||||
assert.deepEqual(vmContext2, {
|
||||
vm: {
|
||||
name: 'name',
|
||||
tags: ['tag'],
|
||||
},
|
||||
run: expectedRunContext,
|
||||
chainLength: undefined,
|
||||
})
|
||||
|
||||
const vm3 = { name: 'name', $value: '$value', func: () => {}, tags: 'tag' }
|
||||
const vmContext3 = buildVmContext(vm3, runContext, undefined)
|
||||
assert.deepEqual(vmContext3, {
|
||||
vm: {
|
||||
name: 'name',
|
||||
tags: [],
|
||||
},
|
||||
run: expectedRunContext,
|
||||
chainLength: undefined,
|
||||
})
|
||||
})
|
||||
|
||||
it('buildVmContext exposes every field advertised by the schema', () => {
|
||||
// vm is an open context (buildVmContext copies through all real VM fields),
|
||||
// so we assert the schema's curated fields are a subset of the output, not equality.
|
||||
const vm = { name_label: 'name', power_state: 'Running', tags: ['tag'], $ref: 'ref', func: () => {} }
|
||||
const { vm: vmContext } = buildVmContext(vm, runContext)
|
||||
|
||||
for (const field of Object.keys(BACKUP_EXPRESSION_CONTEXT.vm)) {
|
||||
assert.ok(field in vmContext, `vm context is missing schema field: ${field}`)
|
||||
}
|
||||
})
|
||||
|
||||
it('buildSrContext', () => {
|
||||
const sr1 = {
|
||||
name_label: 'name_label',
|
||||
SR_type: 'type',
|
||||
name_description: 'description',
|
||||
ignored: 'ignored',
|
||||
}
|
||||
const srContext1 = buildSrContext(sr1, runContext)
|
||||
assert.deepEqual(srContext1, {
|
||||
sr: {
|
||||
name_label: 'name_label',
|
||||
type: 'type',
|
||||
description: 'description',
|
||||
tags: [],
|
||||
},
|
||||
run: expectedRunContext,
|
||||
})
|
||||
assert.deepEqual(Object.keys(srContext1.sr).sort(), Object.keys(BACKUP_EXPRESSION_CONTEXT.sr).sort())
|
||||
|
||||
const sr2 = {
|
||||
name_label: 'name_label',
|
||||
SR_type: 'type',
|
||||
name_description: 'description',
|
||||
tags: ['tag'],
|
||||
ignored: 'ignored',
|
||||
}
|
||||
const srContext2 = buildSrContext(sr2, runContext)
|
||||
assert.deepEqual(srContext2, {
|
||||
sr: {
|
||||
name_label: 'name_label',
|
||||
type: 'type',
|
||||
description: 'description',
|
||||
tags: ['tag'],
|
||||
},
|
||||
run: expectedRunContext,
|
||||
})
|
||||
assert.deepEqual(Object.keys(srContext2.sr).sort(), Object.keys(BACKUP_EXPRESSION_CONTEXT.sr).sort())
|
||||
})
|
||||
|
||||
it('buildBackupRepositoryContext', () => {
|
||||
const backupRepository1 = {
|
||||
name: 'name',
|
||||
url: 'nfs://host/path',
|
||||
ignored: 'ignored',
|
||||
}
|
||||
const backupRepositoryContext1 = buildBackupRepositoryContext(backupRepository1, runContext)
|
||||
assert.deepEqual(backupRepositoryContext1, {
|
||||
backupRepository: {
|
||||
name: 'name',
|
||||
type: 'nfs',
|
||||
tags: [],
|
||||
},
|
||||
run: expectedRunContext,
|
||||
})
|
||||
assert.deepEqual(
|
||||
Object.keys(backupRepositoryContext1.backupRepository).sort(),
|
||||
Object.keys(BACKUP_EXPRESSION_CONTEXT.backupRepository).sort()
|
||||
)
|
||||
|
||||
const backupRepository2 = {
|
||||
name: 'name',
|
||||
url: 'nfs://host/path',
|
||||
tags: [],
|
||||
ignored: 'ignored',
|
||||
}
|
||||
const backupRepositoryContext2 = buildBackupRepositoryContext(backupRepository2, runContext)
|
||||
assert.deepEqual(backupRepositoryContext2, {
|
||||
backupRepository: {
|
||||
name: 'name',
|
||||
type: 'nfs',
|
||||
tags: [],
|
||||
},
|
||||
run: expectedRunContext,
|
||||
})
|
||||
assert.deepEqual(
|
||||
Object.keys(backupRepositoryContext2.backupRepository).sort(),
|
||||
Object.keys(BACKUP_EXPRESSION_CONTEXT.backupRepository).sort()
|
||||
)
|
||||
})
|
||||
})
|
||||
24
@xen-orchestra/backups/_expressionPredicate.mjs
Normal file
24
@xen-orchestra/backups/_expressionPredicate.mjs
Normal file
@@ -0,0 +1,24 @@
|
||||
// @ts-check
|
||||
|
||||
import * as CM from 'complex-matcher'
|
||||
|
||||
/**
|
||||
* Parse and compile a complex-matcher expression into a predicate function.
|
||||
* Throws synchronously if the expression is syntactically invalid.
|
||||
*
|
||||
* @param {string} expression
|
||||
* @returns {(context: object) => boolean}
|
||||
*/
|
||||
export function compileExpression(expression) {
|
||||
return CM.parse(expression).createPredicate()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if value is a non-empty string — i.e. a complex-matcher expression.
|
||||
*
|
||||
* @param {unknown} value
|
||||
* @returns {value is string}
|
||||
*/
|
||||
export function isExpression(value) {
|
||||
return typeof value === 'string' && value.length > 0 && !Number.isFinite(Number(value))
|
||||
}
|
||||
95
@xen-orchestra/backups/_expressionPredicate.test.mjs
Normal file
95
@xen-orchestra/backups/_expressionPredicate.test.mjs
Normal file
@@ -0,0 +1,95 @@
|
||||
import { describe, it } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import { compileExpression, isExpression } from './_expressionPredicate.mjs'
|
||||
|
||||
describe('compileExpression', () => {
|
||||
it('run:dayOfWeek:0', () => {
|
||||
const predicate = compileExpression('run:dayOfWeek:0')
|
||||
|
||||
assert(predicate({ run: { dayOfWeek: 0 } }))
|
||||
assert(!predicate({ run: { dayOfWeek: 1 } }))
|
||||
})
|
||||
|
||||
it('chainLength:>5', () => {
|
||||
const predicate = compileExpression('chainLength:>5')
|
||||
|
||||
assert(predicate({ chainLength: 6 }))
|
||||
assert(!predicate({ chainLength: 5 }))
|
||||
})
|
||||
|
||||
it('vm:tags:prod', () => {
|
||||
const predicate = compileExpression('vm:tags:prod')
|
||||
|
||||
assert(predicate({ vm: { tags: ['prod'] } }))
|
||||
assert(predicate({ vm: { tags: ['prod', 'test'] } }))
|
||||
assert(!predicate({ vm: { tags: ['test'] } }))
|
||||
assert(!predicate({ vm: { tags: [] } }))
|
||||
})
|
||||
|
||||
it('run:dayOfWeek:0 chainLength:>3', () => {
|
||||
const predicate = compileExpression('run:dayOfWeek:0 chainLength:>3')
|
||||
|
||||
assert(predicate({ run: { dayOfWeek: 0 }, chainLength: 6 }))
|
||||
assert(!predicate({ run: { dayOfWeek: 0 }, chainLength: 2 }))
|
||||
assert(!predicate({ run: { dayOfWeek: 1 }, chainLength: 6 }))
|
||||
})
|
||||
|
||||
it('|(run:dayOfWeek:0 chainLength:>6)', () => {
|
||||
const predicate = compileExpression('|(run:dayOfWeek:0 chainLength:>6)')
|
||||
|
||||
assert(predicate({ run: { dayOfWeek: 0 }, chainLength: 7 }))
|
||||
assert(predicate({ run: { dayOfWeek: 1 }, chainLength: 7 }))
|
||||
assert(predicate({ run: { dayOfWeek: 0 }, chainLength: 6 }))
|
||||
assert(!predicate({ run: { dayOfWeek: 1 }, chainLength: 6 }))
|
||||
})
|
||||
|
||||
it('missing field', () => {
|
||||
const predicate = compileExpression('run:dayOfWeek:0')
|
||||
|
||||
assert(predicate({ run: { dayOfWeek: 0 }, chainLength: 6 }))
|
||||
assert(!predicate({ chainLength: 6 }))
|
||||
assert(!predicate({ run: {}, chainLength: 6 }))
|
||||
})
|
||||
|
||||
it('null-checking', () => {
|
||||
const predicate = compileExpression('!vm')
|
||||
|
||||
assert(predicate({}))
|
||||
assert(!predicate({ vm: [{ vm: 'vm' }] }))
|
||||
})
|
||||
|
||||
it('throws on invalid expression', () => {
|
||||
assert.throws(() => compileExpression('vm:('))
|
||||
})
|
||||
})
|
||||
|
||||
describe('isExpression', () => {
|
||||
it('non-empty string', () => {
|
||||
assert(isExpression('vm:tags:test'))
|
||||
})
|
||||
|
||||
it('number', () => {
|
||||
assert(!isExpression(0))
|
||||
})
|
||||
|
||||
it('string[]', () => {
|
||||
assert(!isExpression(['string']))
|
||||
})
|
||||
|
||||
it('undefined', () => {
|
||||
assert(!isExpression(undefined))
|
||||
})
|
||||
|
||||
it('empty string', () => {
|
||||
assert(!isExpression(''))
|
||||
})
|
||||
|
||||
it('number', () => {
|
||||
assert(!isExpression('3'))
|
||||
})
|
||||
|
||||
it('number with spaces', () => {
|
||||
assert(!isExpression(' 5 '))
|
||||
})
|
||||
})
|
||||
@@ -1,7 +1,7 @@
|
||||
import { strict as assert } from 'node:assert'
|
||||
import test from 'node:test'
|
||||
import { execFile } from 'node:child_process'
|
||||
import { copyFile, mkdtemp, rm, writeFile } from 'node:fs/promises'
|
||||
import { copyFile, mkdir, mkdtemp, rm, symlink, writeFile } from 'node:fs/promises'
|
||||
import { Stream } from 'node:stream'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
@@ -9,6 +9,7 @@ import { promisify } from 'node:util'
|
||||
import { randomBytes } from 'node:crypto'
|
||||
|
||||
import Disposable from 'promise-toolbox/Disposable'
|
||||
import * as tar from 'tar'
|
||||
|
||||
import { RemoteAdapter } from './RemoteAdapter.mjs'
|
||||
|
||||
@@ -71,6 +72,83 @@ test('fetchPartitionFiles resolves with a node:stream for every format', async t
|
||||
}
|
||||
})
|
||||
|
||||
async function readAll(stream) {
|
||||
const chunks = []
|
||||
for await (const chunk of stream) {
|
||||
chunks.push(chunk)
|
||||
}
|
||||
return Buffer.concat(chunks)
|
||||
}
|
||||
|
||||
async function listTgzEntries(archive, tmp) {
|
||||
const file = join(tmp, 'archive.tgz')
|
||||
await writeFile(file, archive)
|
||||
const entries = []
|
||||
await tar.t({ file, onReadEntry: entry => entries.push(entry.path.replace(/\/$/, '')) })
|
||||
return entries
|
||||
}
|
||||
|
||||
// The partition is mounted on the XOA/proxy: a guest symlink is resolved against the host root,
|
||||
// so following one would expose host files. Paths through a symlink are refused, and symlinks
|
||||
// met while walking a directory are skipped.
|
||||
test('file restore never follows symbolic links', async t => {
|
||||
const tmp = await mkdtemp(join(tmpdir(), 'xo-flr-symlink-'))
|
||||
t.after(() => rm(tmp, { recursive: true, force: true }))
|
||||
|
||||
// `host` stands for the XOA filesystem, `root` for the mounted guest partition
|
||||
const host = join(tmp, 'host')
|
||||
const root = join(tmp, 'root')
|
||||
await mkdir(host)
|
||||
await writeFile(join(host, 'secret.txt'), 'host secret')
|
||||
await mkdir(join(root, 'dir'), { recursive: true })
|
||||
await writeFile(join(root, 'dir', 'file.txt'), 'guest file')
|
||||
await symlink(host, join(root, 'absolute')) // like `ln -s / foo` in the guest
|
||||
await symlink('../../host', join(root, 'dir', 'relative'))
|
||||
await symlink('file.txt', join(root, 'dir', 'in-tree'))
|
||||
|
||||
const adapter = new RemoteAdapter({})
|
||||
adapter.getPartition = Disposable.factory(async function* () {
|
||||
yield root
|
||||
})
|
||||
|
||||
const refusedPaths = ['/absolute', '/absolute/secret.txt', '/dir/relative', '/dir/relative/host/secret.txt']
|
||||
|
||||
await t.test('listing refuses a path through a symlink', async () => {
|
||||
for (const path of refusedPaths) {
|
||||
await assert.rejects(adapter.listPartitionFiles('disk', 'partition', path), { code: 'ELINK_NOT_SUPPORTED' }, path)
|
||||
}
|
||||
})
|
||||
|
||||
await t.test('listing skips symlinks', async () => {
|
||||
assert.deepEqual(await adapter.listPartitionFiles('disk', 'partition', '/'), { 'dir/': {} })
|
||||
assert.deepEqual(await adapter.listPartitionFiles('disk', 'partition', '/dir'), { 'file.txt': {} })
|
||||
})
|
||||
|
||||
for (const format of ['tgz', 'zip']) {
|
||||
await t.test(`${format}: download refuses a path through a symlink`, async () => {
|
||||
for (const path of refusedPaths) {
|
||||
await assert.rejects(
|
||||
adapter.fetchPartitionFiles('disk', 'partition', ['/dir/file.txt', path], format),
|
||||
{ code: 'ELINK_NOT_SUPPORTED' },
|
||||
path
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
await t.test('tgz: symlinks inside a selected directory are skipped', async () => {
|
||||
const archive = await readAll(await adapter.fetchPartitionFiles('disk', 'partition', ['/dir'], 'tgz'))
|
||||
assert.deepEqual((await listTgzEntries(archive, tmp)).sort(), ['dir', 'dir/file.txt'])
|
||||
})
|
||||
|
||||
await t.test('zip: symlinks inside a selected directory are skipped', async () => {
|
||||
const archive = await readAll(await adapter.fetchPartitionFiles('disk', 'partition', ['/dir'], 'zip'))
|
||||
assert.ok(archive.includes('dir/file.txt'))
|
||||
assert.equal(archive.includes('dir/relative'), false)
|
||||
assert.equal(archive.includes('dir/in-tree'), false)
|
||||
})
|
||||
})
|
||||
|
||||
test('LVM file-restore against real losetup/dmsetup/lvm', { skip }, async t => {
|
||||
const tmp = await mkdtemp(join(tmpdir(), 'xo-flr-'))
|
||||
const adapter = new RemoteAdapter({})
|
||||
|
||||
@@ -66,7 +66,29 @@ export const toLvPartitions = (partitionId, originalVgName, lvItems) =>
|
||||
}))
|
||||
|
||||
const makeRelative = path => resolve('/', path).slice(1)
|
||||
const resolveSubpath = (root, path) => resolve(root, makeRelative(path))
|
||||
|
||||
// The partition is mounted on the XOA/proxy, so the kernel resolves a guest symlink against the
|
||||
// host root: following one (e.g. `ln -s / foo` in the VM, then `/foo/etc`) would read host files.
|
||||
// Symlinks are therefore not supported by file restore: a path going through one is refused,
|
||||
// checked component by component with lstat. The mount is read-only, so no check/use race.
|
||||
export async function resolveWithoutSymlinks(rootPath, path) {
|
||||
const relativePath = makeRelative(path)
|
||||
let currentPath = rootPath
|
||||
for (const name of relativePath.split('/')) {
|
||||
if (name === '') {
|
||||
continue
|
||||
}
|
||||
currentPath = join(currentPath, name)
|
||||
const stats = await lstat(currentPath)
|
||||
if (stats.isSymbolicLink()) {
|
||||
const error = new Error(`symbolic links are not supported by file restore: ${path}`)
|
||||
error.code = 'ELINK_NOT_SUPPORTED'
|
||||
error.path = path
|
||||
throw error
|
||||
}
|
||||
}
|
||||
return relativePath
|
||||
}
|
||||
|
||||
async function addZipEntries(zip, realBasePath, virtualBasePath, relativePaths) {
|
||||
for (const relativePath of relativePaths) {
|
||||
@@ -290,6 +312,12 @@ export const fileRestoreMethods = {
|
||||
const self = this
|
||||
Disposable.use(async function* () {
|
||||
const path = yield self.getPartition(diskId, partitionId)
|
||||
// validated before resolving the promise, so a refused path rejects instead of
|
||||
// producing a truncated archive
|
||||
const relativePaths = []
|
||||
for (const requestedPath of paths) {
|
||||
relativePaths.push(await resolveWithoutSymlinks(path, requestedPath))
|
||||
}
|
||||
let outputStream
|
||||
|
||||
if (format === 'tgz') {
|
||||
@@ -298,7 +326,13 @@ export const fileRestoreMethods = {
|
||||
// simultaneous reads. Those saturate the libuv threadpool and starve
|
||||
// the underlying vhd/CIFS reads NTFS-3g depends on (FUSE-on-FUSE
|
||||
// threadpool deadlock). Serializing keeps a worker free for them.
|
||||
const pack = tar.c({ cwd: path, gzip: true, jobs: 1 }, paths.map(makeRelative))
|
||||
//
|
||||
// symlinks found while walking the selected directories are skipped, like in the zip
|
||||
// format and the file listing
|
||||
const pack = tar.c(
|
||||
{ cwd: path, gzip: true, jobs: 1, filter: (_, stats) => !stats.isSymbolicLink() },
|
||||
relativePaths
|
||||
)
|
||||
|
||||
// node-tar >= 7 returns a Minipass stream: it has `pipe()` but is not a `node:stream`
|
||||
// instance. xo-proxy serves this stream through Koa, which pipes only bodies passing
|
||||
@@ -316,7 +350,7 @@ export const fileRestoreMethods = {
|
||||
// appear as a freeze with no response sent.
|
||||
outputStream = zip.outputStream
|
||||
resolve(zip.outputStream)
|
||||
await addZipEntries(zip, path, '', paths.map(makeRelative))
|
||||
await addZipEntries(zip, path, '', relativePaths)
|
||||
zip.end()
|
||||
} else {
|
||||
throw new Error('unsupported format ' + format)
|
||||
@@ -331,17 +365,23 @@ export const fileRestoreMethods = {
|
||||
},
|
||||
|
||||
async *getDisk(diskId) {
|
||||
if (this._useGetDiskLegacy) {
|
||||
yield* getDiskLegacy(this._handler, diskId)
|
||||
return
|
||||
try {
|
||||
if (this._useGetDiskLegacy) {
|
||||
yield* getDiskLegacy(this._handler, diskId)
|
||||
return
|
||||
}
|
||||
const handler = this._handler
|
||||
// this is a disposable
|
||||
const mountDir = yield getTmpDir()
|
||||
// this is also a disposable
|
||||
yield mount(handler, diskId, mountDir)
|
||||
// this will yield disk path to caller
|
||||
yield `${mountDir}/vhd0`
|
||||
} finally {
|
||||
// Disposable.factory disposes by calling gen.return(), which runs this finally before
|
||||
// the mount/tmpdir disposers — i.e. exactly as the disk is unmounted.
|
||||
this._partitionsCache.delete(diskId)
|
||||
}
|
||||
const handler = this._handler
|
||||
// this is a disposable
|
||||
const mountDir = yield getTmpDir()
|
||||
// this is also a disposable
|
||||
yield mount(handler, diskId, mountDir)
|
||||
// this will yield disk path to caller
|
||||
yield `${mountDir}/vhd0`
|
||||
},
|
||||
|
||||
// partitionId values:
|
||||
@@ -371,7 +411,7 @@ export const fileRestoreMethods = {
|
||||
|
||||
listPartitionFiles(diskId, partitionId, path) {
|
||||
return Disposable.use(this.getPartition(diskId, partitionId), async rootPath => {
|
||||
path = resolveSubpath(rootPath, path)
|
||||
path = join(rootPath, await resolveWithoutSymlinks(rootPath, path))
|
||||
const entriesMap = {}
|
||||
await asyncEach(
|
||||
await readdir(path),
|
||||
@@ -397,7 +437,13 @@ export const fileRestoreMethods = {
|
||||
},
|
||||
|
||||
listPartitions(diskId) {
|
||||
return Disposable.use(this.getDisk(diskId), async devicePath => {
|
||||
const cache = this._partitionsCache
|
||||
const cached = cache.get(diskId)
|
||||
if (cached !== undefined) {
|
||||
return cached
|
||||
}
|
||||
|
||||
const pPartitions = Disposable.use(this.getDisk(diskId), async devicePath => {
|
||||
// partx may return empty on FUSE-backed files (vhd0); a loop device
|
||||
// presents proper block-device semantics that partx reads reliably.
|
||||
// losetup may itself fail if the FUSE mount isn't fully ready yet —
|
||||
@@ -415,11 +461,14 @@ export const fileRestoreMethods = {
|
||||
}
|
||||
}
|
||||
|
||||
// direct listing failed, try with lvm listing
|
||||
if (partitions.length === 0) {
|
||||
debug('direct listing of the volume partition failed, try to read through LVM')
|
||||
try {
|
||||
// handle potential raw LVM physical volume
|
||||
return await this._listLvmLogicalVolumes(devicePath, undefined, partitions)
|
||||
} catch (error) {
|
||||
warn('LVM listing of this volume failed ', error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
@@ -455,6 +504,16 @@ export const fileRestoreMethods = {
|
||||
})
|
||||
return results
|
||||
})
|
||||
|
||||
// The entry is evicted on disk unmount (see getDisk)
|
||||
// a failed probe is not cached so the next call retries.
|
||||
cache.set(diskId, pPartitions)
|
||||
pPartitions.catch(() => {
|
||||
if (cache.get(diskId) === pPartitions) {
|
||||
cache.delete(diskId)
|
||||
}
|
||||
})
|
||||
return pPartitions
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -294,9 +294,9 @@ export const importIncrementalVm = defer(async function importIncrementalVm(
|
||||
await asyncMap(Object.values(vdiVbds), vbd =>
|
||||
xapi.VBD_create({
|
||||
...vbd,
|
||||
// a live mount serves its disk read only: attach it as such, instead of letting the
|
||||
// guest discover it through I/O errors on its first write
|
||||
mode: isLiveMounted ? 'RO' : vbd.mode,
|
||||
// an uncached live mount serves its disk read only: attach it as such, instead of letting
|
||||
// the guest discover it through I/O errors on its first write
|
||||
mode: isLiveMounted && vdi.liveMountedReadOnly ? 'RO' : vbd.mode,
|
||||
VDI: newVdi.$ref,
|
||||
VM: vmRef,
|
||||
})
|
||||
@@ -307,9 +307,16 @@ export const importIncrementalVm = defer(async function importIncrementalVm(
|
||||
})
|
||||
|
||||
// 3.5. Destroy old VDIs that are no longer attached to the VM.
|
||||
// Uses ignoreErrors because some storage backends refuse to destroy a VDI that still has snapshot children;
|
||||
// those VDIs will become truly orphaned once the old snapshot VMs are cleaned up by _deleteOldEntries.
|
||||
await asyncMap([...oldVdiRefs], ref => ignoreErrors.call(xapi.call('VDI.destroy', ref)))
|
||||
// Some storage backends refuse to destroy a VDI that still has snapshot children.
|
||||
// Log it and leave it for later cleanup.
|
||||
await asyncMap([...oldVdiRefs], ref =>
|
||||
xapi.call('VDI.destroy', ref).catch(error =>
|
||||
Task.warning('failed to destroy orphaned VDI', {
|
||||
vdi: xapi.getObject(ref, undefined)?.uuid ?? ref,
|
||||
error,
|
||||
})
|
||||
)
|
||||
)
|
||||
|
||||
// 4. For updates, destroy existing VIFs before recreating them.
|
||||
if (isUpdate) {
|
||||
|
||||
@@ -60,7 +60,7 @@ const parsePartxLine = createParser({
|
||||
export async function listPartitions(devicePath) {
|
||||
const parts = await fromCallback(execFile, 'partx', [
|
||||
'--bytes',
|
||||
'--output=NR,START,SIZE,NAME,UUID,TYPE',
|
||||
'--output=START,SIZE,NAME,UUID,TYPE',
|
||||
'--pairs',
|
||||
devicePath,
|
||||
]).catch(error => {
|
||||
|
||||
@@ -10,6 +10,8 @@ import { FullRemote } from './_vmRunners/FullRemote.mjs'
|
||||
import { IncrementalRemote } from './_vmRunners/IncrementalRemote.mjs'
|
||||
import { Throttle } from '@vates/generator-toolbox'
|
||||
import createStreamThrottle from './_createStreamThrottle.mjs'
|
||||
import { compileExpression, isExpression } from '../_expressionPredicate.mjs'
|
||||
import { buildRunContext, buildVmContext } from '../_buildContext.mjs'
|
||||
|
||||
const noop = Function.prototype
|
||||
|
||||
@@ -60,21 +62,25 @@ export const VmsRemote = class RemoteVmsBackupRunner extends Abstract {
|
||||
return
|
||||
}
|
||||
|
||||
const runContext = job.runContext ?? buildRunContext(new Date(), settings.timezone)
|
||||
const predicate = isExpression(settings.vmFilter) ? compileExpression(settings.vmFilter) : null
|
||||
|
||||
const vmsUuids = await sourceRemoteAdapter.listAllVms()
|
||||
|
||||
const queue = new Set(vmsUuids)
|
||||
|
||||
Task.info('vms', { vms: vmsUuids })
|
||||
const nbVms = vmsUuids.length
|
||||
let nbVms = vmsUuids.length
|
||||
let nbVmsDone = 0
|
||||
|
||||
remoteAdapters = getAdaptersByRemote(remoteAdapters)
|
||||
const allSettings = this._job.settings
|
||||
const baseSettings = this._baseSettings
|
||||
|
||||
const queue = new Set(vmsUuids)
|
||||
const taskByVmId = {}
|
||||
const nTriesByVmId = {}
|
||||
|
||||
const handleVm = vmUuid => {
|
||||
const handleVm = async vmUuid => {
|
||||
if (nTriesByVmId[vmUuid] === undefined) {
|
||||
nTriesByVmId[vmUuid] = 0
|
||||
}
|
||||
@@ -84,12 +90,26 @@ export const VmsRemote = class RemoteVmsBackupRunner extends Abstract {
|
||||
const vmSettings = { ...settings, ...allSettings[vmUuid] }
|
||||
const isLastRun = nTriesByVmId[vmUuid] === vmSettings.nRetriesVmBackupFailures + 1
|
||||
|
||||
const vmBackups = await sourceRemoteAdapter.listVmBackups(vmUuid, ({ mode }) => mode === job.mode)
|
||||
|
||||
// avoiding to create tasks for empty directories
|
||||
if (vmBackups.length === 0) {
|
||||
nbVms--
|
||||
return
|
||||
}
|
||||
|
||||
if (predicate !== null && !predicate(buildVmContext(vmBackups.at(-1).vm, runContext))) {
|
||||
nbVms--
|
||||
return
|
||||
}
|
||||
|
||||
const opts = {
|
||||
baseSettings,
|
||||
config,
|
||||
job,
|
||||
healthCheckSr,
|
||||
remoteAdapters,
|
||||
runContext,
|
||||
schedule,
|
||||
settings: vmSettings,
|
||||
sourceRemoteAdapter,
|
||||
@@ -106,45 +126,39 @@ export const VmsRemote = class RemoteVmsBackupRunner extends Abstract {
|
||||
throw new Error(`Job mode ${job.mode} not implemented for mirror backup`)
|
||||
}
|
||||
|
||||
return sourceRemoteAdapter
|
||||
.listVmBackups(vmUuid, ({ mode }) => mode === job.mode)
|
||||
.then(vmBackups => {
|
||||
// avoiding to create tasks for empty directories
|
||||
if (vmBackups.length > 0) {
|
||||
if (taskByVmId[vmUuid] === undefined) {
|
||||
taskByVmId[vmUuid] = new Task(taskStart)
|
||||
}
|
||||
const task = taskByVmId[vmUuid]
|
||||
// error has to be caught in the task to prevent its failure, but handled outside the task to execute another task.run()
|
||||
let taskError
|
||||
return task
|
||||
.runInside(async () =>
|
||||
vmBackup.run().catch(error => {
|
||||
taskError = error
|
||||
})
|
||||
)
|
||||
.then(result => {
|
||||
if (taskError === undefined) {
|
||||
nbVmsDone++
|
||||
return task.success(result)
|
||||
}
|
||||
if (isLastRun) {
|
||||
nbVmsDone++
|
||||
return task.failure(taskError)
|
||||
}
|
||||
Task.set('progress', Math.round((nbVmsDone * 100) / nbVms))
|
||||
// don't end the task
|
||||
task.warning(`Retry the VM mirror backup due to an error`, {
|
||||
attempt: nTriesByVmId[vmUuid],
|
||||
error: taskError.message,
|
||||
isRetry: true,
|
||||
})
|
||||
queue.add(vmUuid)
|
||||
})
|
||||
.catch(noop)
|
||||
if (taskByVmId[vmUuid] === undefined) {
|
||||
taskByVmId[vmUuid] = new Task(taskStart)
|
||||
}
|
||||
const task = taskByVmId[vmUuid]
|
||||
// error has to be caught in the task to prevent its failure, but handled outside the task to execute another task.run()
|
||||
let taskError
|
||||
return task
|
||||
.runInside(async () =>
|
||||
vmBackup.run().catch(error => {
|
||||
taskError = error
|
||||
})
|
||||
)
|
||||
.then(result => {
|
||||
if (taskError === undefined) {
|
||||
nbVmsDone++
|
||||
return task.success(result)
|
||||
}
|
||||
if (isLastRun) {
|
||||
nbVmsDone++
|
||||
return task.failure(taskError)
|
||||
}
|
||||
Task.set('progress', Math.round((nbVmsDone * 100) / nbVms))
|
||||
// don't end the task
|
||||
task.warning(`Retry the VM mirror backup due to an error`, {
|
||||
attempt: nTriesByVmId[vmUuid],
|
||||
error: taskError.message,
|
||||
isRetry: true,
|
||||
})
|
||||
queue.add(vmUuid)
|
||||
})
|
||||
.catch(noop)
|
||||
}
|
||||
|
||||
const { concurrency } = settings
|
||||
const _handleVm = !concurrency ? handleVm : limitConcurrency(concurrency)(handleVm)
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import { Throttle } from '@vates/generator-toolbox'
|
||||
import { asyncEach } from '@vates/async-each'
|
||||
import createStreamThrottle from './_createStreamThrottle.mjs'
|
||||
import { selectSynchronizedSnapshotVms } from './_selectSynchronizedSnapshotVms.mjs'
|
||||
import { buildRunContext } from '../_buildContext.mjs'
|
||||
|
||||
const noop = Function.prototype
|
||||
|
||||
@@ -68,6 +69,7 @@ export const VmsXapi = class VmsXapiBackupRunner extends Abstract {
|
||||
const getSnapshotNameLabel = this._getSnapshotNameLabel
|
||||
const schedule = this._schedule
|
||||
const settings = this._settings
|
||||
const runContext = job.runContext ?? buildRunContext(new Date(), settings.timezone)
|
||||
|
||||
const throttleGenerator = new Throttle(settings.maxExportRate)
|
||||
const throttleStream = createStreamThrottle(settings.maxExportRate)
|
||||
@@ -151,6 +153,7 @@ export const VmsXapi = class VmsXapiBackupRunner extends Abstract {
|
||||
healthCheckSr,
|
||||
job,
|
||||
remoteAdapters,
|
||||
runContext,
|
||||
schedule,
|
||||
settings: vmSettings,
|
||||
srs,
|
||||
@@ -249,6 +252,7 @@ export const VmsXapi = class VmsXapiBackupRunner extends Abstract {
|
||||
throttleGenerator,
|
||||
throttleStream,
|
||||
vm,
|
||||
runContext,
|
||||
}
|
||||
|
||||
const vmBackup = this._getVmBackup(job.mode, opts)
|
||||
|
||||
@@ -53,7 +53,7 @@ export const FullRemote = class FullRemoteVmBackupRunner extends AbstractRemote
|
||||
'writer.run()'
|
||||
)
|
||||
// for healthcheck
|
||||
this._tags = metadata.vm.tags
|
||||
this._vm = metadata.vm
|
||||
nbTransferredVms++
|
||||
Task.set('progress', Math.round((nbTransferredVms * 100) / nbTransferrableVms))
|
||||
}
|
||||
|
||||
@@ -62,14 +62,33 @@ class IncrementalRemoteVmBackupRunner extends AbstractRemote {
|
||||
})
|
||||
// yeah , let's go
|
||||
}
|
||||
|
||||
/**
|
||||
* the block files can be copied as stored, never decompressed nor recompressed, if all the writers write VHD
|
||||
* directories with the same compression (the source disks must also use it, checked per disk when reading)
|
||||
* @returns {string | undefined} the common compression, undefined if a writer writes plain VHD files or if they differ
|
||||
*/
|
||||
#getRawBlocksCompression() {
|
||||
const compressions = new Set([...this._writers].map(writer => writer.getVhdDirectoryCompression()))
|
||||
// a single undefined value means plain VHD files on every writer: no raw blocks either
|
||||
return compressions.size === 1 ? [...compressions][0] : undefined
|
||||
}
|
||||
|
||||
async _run() {
|
||||
const transferList = await this._computeTransferList(({ mode }) => mode === 'delta')
|
||||
// the source remote writes with the same compression as all the writers: the blocks can be copied as stored
|
||||
// this is based on the remote configurations, each source disk checks its own compression since it may predate a
|
||||
// configuration change, and falls back to a normal read
|
||||
const writersCompression = this.#getRawBlocksCompression()
|
||||
const rawBlocksCompression =
|
||||
writersCompression === this._sourceRemoteAdapter.getVhdDirectoryCompression() ? writersCompression : undefined
|
||||
const nbTransferrableVms = transferList.length
|
||||
let nbTransferredVms = 0
|
||||
for (const metadata of transferList) {
|
||||
assert.strictEqual(metadata.mode, 'delta')
|
||||
const incrementalExport = await this._sourceRemoteAdapter.readIncrementalVmBackup(metadata, undefined, {
|
||||
useChain: false,
|
||||
rawBlocksCompression,
|
||||
})
|
||||
// don't trust metadata too much
|
||||
// recompute if it's a base backup
|
||||
@@ -112,7 +131,7 @@ class IncrementalRemoteVmBackupRunner extends AbstractRemote {
|
||||
|
||||
await this._callWriters(writer => writer.cleanup(), 'writer.cleanup()')
|
||||
// for healthcheck
|
||||
this._tags = metadata.vm.tags
|
||||
this._vm = metadata.vm
|
||||
nbTransferredVms++
|
||||
Task.set('progress', Math.round((nbTransferredVms * 100) / nbTransferrableVms))
|
||||
}
|
||||
|
||||
@@ -18,10 +18,19 @@ import { ThrottledDisk, SynchronizedDisk } from '@xen-orchestra/disk-transform'
|
||||
import { AggregatedIncrementalRemoteWriter } from '../_writers/AggregatedIncrementalRemoteWriter.mjs'
|
||||
import { AggregatedIncrementalXapiWriter } from '../_writers/AggregatedIncrementalXapiWriter.mjs'
|
||||
import { TaskProgressHandler } from './_TaskProgressHandler.mjs'
|
||||
import { compileExpression, isExpression } from '../../_expressionPredicate.mjs'
|
||||
import { buildVmContext } from '../../_buildContext.mjs'
|
||||
|
||||
const { debug } = createLogger('xo:backups:IncrementalXapiVmBackup')
|
||||
|
||||
export const IncrementalXapi = class IncrementalXapiVmBackupRunner extends AbstractXapi {
|
||||
constructor(props) {
|
||||
super(props)
|
||||
|
||||
const fullInterval = this._settings.fullInterval
|
||||
this._fullIntervalPredicate = isExpression(fullInterval) ? compileExpression(fullInterval) : null
|
||||
}
|
||||
|
||||
_getWriters() {
|
||||
return [
|
||||
IncrementalRemoteWriter,
|
||||
@@ -229,15 +238,17 @@ export const IncrementalXapi = class IncrementalXapiVmBackupRunner extends Abstr
|
||||
}
|
||||
|
||||
// we do tafter checkbasevdis because we want the writer to know the target VM
|
||||
// especially on replication when we alwayr update the same VM
|
||||
// especially on replication when we always update the same VM
|
||||
const fullInterval = this._settings.fullInterval
|
||||
if (fullInterval !== 0 && fullInterval <= deltaChainLength + 1) {
|
||||
debug('not using base VM because fullInterval reached', {
|
||||
fullInterval,
|
||||
deltaChainLength,
|
||||
eq: fullInterval < deltaChainLength + 1,
|
||||
dc1: deltaChainLength + 1,
|
||||
})
|
||||
const chainLength = (deltaChainLength ?? 0) + 1
|
||||
if (isExpression(fullInterval)) {
|
||||
const context = buildVmContext(this._vm, this._runContext, chainLength)
|
||||
if (this._fullIntervalPredicate(context)) {
|
||||
debug('forcing full backup: fullInterval expression matched', { fullInterval })
|
||||
return
|
||||
}
|
||||
} else if (fullInterval !== 0 && fullInterval <= chainLength) {
|
||||
debug('not using base VM because fullInterval reached', { fullInterval, chainLength })
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { asyncMap } from '@xen-orchestra/async-map'
|
||||
import { createLogger } from '@xen-orchestra/log'
|
||||
import { Task } from '@vates/task'
|
||||
import { compileExpression, isExpression } from '../../_expressionPredicate.mjs'
|
||||
import { buildVmContext } from '../../_buildContext.mjs'
|
||||
|
||||
const { debug, warn } = createLogger('xo:backups:AbstractVmRunner')
|
||||
|
||||
@@ -73,24 +75,58 @@ export const Abstract = class AbstractVmBackupRunner {
|
||||
}
|
||||
|
||||
// check if current VM has tags
|
||||
const tags = this._tags
|
||||
const tags = this._vm.tags
|
||||
|
||||
// accept both 'xo:no-health-check' and 'xo:no-health-check=reason'
|
||||
const vmAlwaysIgnored = tags.some(t => t.startsWith('xo:no-health-check'))
|
||||
const intersect = !vmAlwaysIgnored && settings.healthCheckVmsWithTags.some(t => tags.includes(t))
|
||||
|
||||
if (vmAlwaysIgnored || (settings.healthCheckVmsWithTags.length !== 0 && !intersect)) {
|
||||
// create a task to have an info in the logs and reports
|
||||
if (tags.some(t => t.startsWith('xo:no-health-check'))) {
|
||||
return Task.run(
|
||||
{
|
||||
properties: { name: 'health check' },
|
||||
properties: {
|
||||
name: 'health check',
|
||||
},
|
||||
},
|
||||
() => {
|
||||
Task.info(`This VM doesn't match the health check's tags for this schedule`)
|
||||
Task.info(`This VM is excluded from health checks via tag`)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
if (isExpression(settings.healthCheckVmsWithTags)) {
|
||||
const context = buildVmContext(this._vm, this._runContext)
|
||||
if (!this._healthCheckPredicate(context)) {
|
||||
return Task.run(
|
||||
{
|
||||
properties: { name: 'health check' },
|
||||
},
|
||||
() => {
|
||||
Task.info(`VM did not match healthCheckVmsWithTags expression`)
|
||||
}
|
||||
)
|
||||
}
|
||||
} else {
|
||||
const intersect = settings.healthCheckVmsWithTags.some(t => tags.includes(t))
|
||||
if (settings.healthCheckVmsWithTags.length !== 0 && !intersect) {
|
||||
// create a task to have an info in the logs and reports
|
||||
return Task.run(
|
||||
{
|
||||
properties: { name: 'health check' },
|
||||
},
|
||||
() => {
|
||||
Task.info(`This VM doesn't match the health check's tags for this schedule`)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
await this._callWriters(writer => writer.healthCheck(), 'writer.healthCheck()')
|
||||
}
|
||||
|
||||
_compileHealthCheckPredicate() {
|
||||
const { healthCheckVmsWithTags } = this._settings
|
||||
if (isExpression(healthCheckVmsWithTags)) {
|
||||
this._healthCheckPredicate = compileExpression(healthCheckVmsWithTags)
|
||||
} else {
|
||||
this._healthCheckPredicate = undefined
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ export const AbstractRemote = class AbstractRemoteVmBackupRunner extends Abstrac
|
||||
job,
|
||||
healthCheckSr,
|
||||
remoteAdapters,
|
||||
runContext,
|
||||
schedule,
|
||||
settings,
|
||||
sourceRemoteAdapter,
|
||||
@@ -40,11 +41,14 @@ export const AbstractRemote = class AbstractRemoteVmBackupRunner extends Abstrac
|
||||
this._throttleGenerator = throttleGenerator
|
||||
this._throttleStream = throttleStream
|
||||
this._vmUuid = vmUuid
|
||||
this._runContext = runContext
|
||||
|
||||
const allSettings = job.settings
|
||||
const writers = new Set()
|
||||
this._writers = writers
|
||||
|
||||
this._compileHealthCheckPredicate()
|
||||
|
||||
const [BackupWriter, AggregratedBackupWriter] = this._getRemoteWriters()
|
||||
if (settings.distributeBackups) {
|
||||
writers.add(
|
||||
|
||||
@@ -36,6 +36,7 @@ export const AbstractXapi = class AbstractXapiVmBackupRunner extends Abstract {
|
||||
job,
|
||||
remoteAdapters,
|
||||
remotes,
|
||||
runContext,
|
||||
schedule,
|
||||
settings,
|
||||
srs,
|
||||
@@ -63,12 +64,13 @@ export const AbstractXapi = class AbstractXapiVmBackupRunner extends Abstract {
|
||||
this.timestamp = undefined
|
||||
|
||||
// VM currently backed up
|
||||
const tags = (this._tags = vm.tags)
|
||||
const tags = vm.tags
|
||||
|
||||
// VM (snapshot) that is really exported
|
||||
this._exportedVm = undefined
|
||||
this._synchronizedSnapshotTimestamp = synchronizedSnapshotTimestamp
|
||||
this._vm = vm
|
||||
this._runContext = runContext
|
||||
|
||||
this._getSnapshotNameLabel = getSnapshotNameLabel
|
||||
this._isIncremental = job.mode === 'delta'
|
||||
@@ -90,6 +92,9 @@ export const AbstractXapi = class AbstractXapiVmBackupRunner extends Abstract {
|
||||
settings.offlineSnapshot = true
|
||||
}
|
||||
this._settings = settings
|
||||
|
||||
this._compileHealthCheckPredicate()
|
||||
|
||||
// Create writers
|
||||
{
|
||||
const writers = new Set()
|
||||
|
||||
@@ -13,7 +13,9 @@ export class TaskProgressHandler {
|
||||
if (progress < 0 || progress > 1) {
|
||||
return
|
||||
}
|
||||
// the end is always emitted: throttled, a last step under the threshold would leave the task at 99%
|
||||
if (
|
||||
progress !== 1 &&
|
||||
this.#lastProgressDate !== undefined &&
|
||||
this.#lastProgressValue !== undefined &&
|
||||
Date.now() - this.#lastProgressDate < MAX_DURATION_BETWEEN_PROGRESS_EMIT &&
|
||||
|
||||
@@ -1,5 +1,15 @@
|
||||
import { AbstractAggregatedRemoteWriter } from './_AbstractAggregatedRemoteWriter.mjs'
|
||||
export class AggregatedIncrementalRemoteWriter extends AbstractAggregatedRemoteWriter {
|
||||
/**
|
||||
* the backup can land on any of the adapters, the compression is only known if they all use the same one
|
||||
* (based on the adapters since the child writers are only created in beforeBackup())
|
||||
* @returns {string | undefined} see RemoteAdapter#getVhdDirectoryCompression
|
||||
*/
|
||||
getVhdDirectoryCompression() {
|
||||
const compressions = new Set(Object.values(this.adapters).map(adapter => adapter.getVhdDirectoryCompression()))
|
||||
return compressions.size === 1 ? [...compressions][0] : undefined
|
||||
}
|
||||
|
||||
async deleteOldBackupsOnAdapter(adapter, backups) {
|
||||
await adapter.deleteDeltaVmBackups(backups)
|
||||
}
|
||||
|
||||
@@ -25,6 +25,14 @@ const { warn } = createLogger('xo:backups:DeltaBackupWriter')
|
||||
export class IncrementalRemoteWriter extends MixinRemoteWriter(AbstractIncrementalWriter) {
|
||||
#parentVdiPaths
|
||||
#parentUuids
|
||||
|
||||
/**
|
||||
* @returns {string | undefined} see RemoteAdapter#getVhdDirectoryCompression
|
||||
*/
|
||||
getVhdDirectoryCompression() {
|
||||
return this._adapter.getVhdDirectoryCompression()
|
||||
}
|
||||
|
||||
async checkBaseVdis(baseUuidToSrcVdi) {
|
||||
this.#parentVdiPaths = {}
|
||||
this.#parentUuids = {}
|
||||
|
||||
@@ -65,6 +65,11 @@ export class IncrementalXapiWriter extends MixinXapiWriter(AbstractIncrementalWr
|
||||
|
||||
const snapshotCandidates = new Map()
|
||||
|
||||
// skip orphan snapshots (active VDI destroyed): they can't be validated against an active disk
|
||||
const replicaSnapshots = sr.$VDIs.filter(
|
||||
vdi => vdi?.managed && vdi?.is_a_snapshot && vdi.$snapshot_of !== undefined
|
||||
)
|
||||
|
||||
for (const [baseUuid, srcVdiuid] of baseUuidToSrcVdi) {
|
||||
let target
|
||||
|
||||
@@ -72,11 +77,9 @@ export class IncrementalXapiWriter extends MixinXapiWriter(AbstractIncrementalWr
|
||||
|
||||
if (contentKey !== undefined) {
|
||||
debug('got one content key, look for a candidate')
|
||||
target = sr.$VDIs.find(
|
||||
target = replicaSnapshots.find(
|
||||
vdi =>
|
||||
vdi?.managed &&
|
||||
vdi?.is_a_snapshot &&
|
||||
vdi.other_config[CONTENT_KEY] === contentKey && // &&
|
||||
vdi.other_config[CONTENT_KEY] === contentKey &&
|
||||
// ensure we don't replicate on ourself or any of the vdi in the source chain
|
||||
vdi.$snapshot_of.uuid !== srcVdiuid
|
||||
)
|
||||
@@ -85,13 +88,11 @@ export class IncrementalXapiWriter extends MixinXapiWriter(AbstractIncrementalWr
|
||||
// fall back for older snapshots
|
||||
if (target === undefined) {
|
||||
debug('content key not here or not found , look by jobid ')
|
||||
target = sr.$VDIs.find(
|
||||
target = replicaSnapshots.find(
|
||||
vdi =>
|
||||
vdi?.managed &&
|
||||
vdi?.is_a_snapshot &&
|
||||
vdi.other_config[JOB_ID] === this._job.id &&
|
||||
vdi.other_config[VM_UUID] === this._vmUuid &&
|
||||
vdi?.other_config[COPY_OF] === baseUuid
|
||||
vdi.other_config[COPY_OF] === baseUuid
|
||||
)
|
||||
}
|
||||
if (target !== undefined) {
|
||||
@@ -391,6 +392,7 @@ export class IncrementalXapiWriter extends MixinXapiWriter(AbstractIncrementalWr
|
||||
mostRecentEntry = this._oldEntries.pop()
|
||||
}
|
||||
await this._deleteOldEntries()
|
||||
await this._deleteOrphanVdis()
|
||||
this._oldEntries = mostRecentEntry !== undefined ? [mostRecentEntry] : []
|
||||
}
|
||||
}
|
||||
@@ -398,6 +400,7 @@ export class IncrementalXapiWriter extends MixinXapiWriter(AbstractIncrementalWr
|
||||
async cleanup() {
|
||||
if (!this._settings.skipDeleteOldEntries) {
|
||||
await this._deleteOldEntries()
|
||||
await this._deleteOrphanVdis()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -405,6 +408,35 @@ export class IncrementalXapiWriter extends MixinXapiWriter(AbstractIncrementalWr
|
||||
return asyncMapSettled(this._oldEntries, vm => vm.$destroy({ bypassBlockedOperation: true }))
|
||||
}
|
||||
|
||||
// VDIs from this job/VM/SR that ended up detached from any VM, because a previous run
|
||||
// was interrupted or a storage backend refused to destroy a VDI at import time.
|
||||
// Destroying a VM only reaches the VDIs still attached to it, _deleteOldEntries never cleans them.
|
||||
async _deleteOrphanVdis() {
|
||||
const sr = this._sr
|
||||
const vmUuid = this._vmUuid
|
||||
const jobId = this._job.id
|
||||
|
||||
const orphanVdis = sr.$VDIs.filter(
|
||||
vdi =>
|
||||
vdi?.managed &&
|
||||
vdi.other_config[JOB_ID] === jobId &&
|
||||
vdi.other_config[VM_UUID] === vmUuid &&
|
||||
vdi.other_config[REPLICATED_TO_SR_UUID] === sr.uuid &&
|
||||
vdi.type !== 'suspend' &&
|
||||
// a VBD or VM missing from the cache (e.g. just destroyed) keeps the VDI, next run will handle it
|
||||
vdi.$VBDs.every(vbd => vbd?.$VM?.is_control_domain === true)
|
||||
)
|
||||
|
||||
await asyncEach(
|
||||
orphanVdis,
|
||||
vdi =>
|
||||
sr.$xapi
|
||||
.call('VDI.destroy', vdi.$ref)
|
||||
.catch(error => Task.warning('failed to delete orphan VDI', { vdi: vdi.uuid, error })),
|
||||
{ concurrency: 5 }
|
||||
)
|
||||
}
|
||||
|
||||
#decorateVmMetadata(backup, timestamp) {
|
||||
const { _warmMigration } = this._settings
|
||||
const sr = this._sr
|
||||
|
||||
@@ -12,11 +12,14 @@ const { warn } = createLogger('xo:backups:vdiRestoreTargets')
|
||||
*/
|
||||
|
||||
/**
|
||||
* Attach the backup disk itself to the restored VM, through a live mount: no copy, read only.
|
||||
* Attach the backup disk itself to the restored VM, through a live mount: no copy, read only —
|
||||
* unless it is cached, which makes it read/write.
|
||||
*
|
||||
* @typedef {object} LiveMountVdiTarget
|
||||
* @property {'live-mount'} type
|
||||
* @property {string} host - uuid of the host the disk is attached to
|
||||
* @property {string} [cacheSr] - uuid of the SR of the local VDI the disk is materialized into, which
|
||||
* also holds the writes, lost on unmount
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -133,6 +136,20 @@ export function hasLiveMountTarget(mapVdisSrs) {
|
||||
return Object.values(mapVdisSrs ?? {}).some(value => value?.type === 'live-mount')
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a raw `mapVdisSrs` setting asks for at least one cached, hence read/write, live mount.
|
||||
*
|
||||
* Such a mount needs the VM of the appliance serving it, which the caller has to look up. A `null`
|
||||
* cache SR does not ask for one: it is invalid, and left to {@link normalizeVdiRestoreTargets} to
|
||||
* reject, rather than failing on the lookup of a VM which was never needed.
|
||||
*
|
||||
* @param {object} [mapVdisSrs]
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function hasCachedLiveMountTarget(mapVdisSrs) {
|
||||
return Object.values(mapVdisSrs ?? {}).some(value => value?.type === 'live-mount' && value.cacheSr != null)
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize the `mapVdisSrs` restore setting into per-VDI targets.
|
||||
*
|
||||
@@ -186,10 +203,17 @@ function normalizeTarget(vdiUuid, value, useDifferentialRestore) {
|
||||
}
|
||||
|
||||
if (type === 'live-mount') {
|
||||
if (!isNonEmptyString(value.host)) {
|
||||
const { cacheSr, host } = value
|
||||
if (!isNonEmptyString(host)) {
|
||||
throw invalidTarget(vdiUuid, 'a live mount requires the id of the host the disk is attached to')
|
||||
}
|
||||
return { type: 'live-mount', host: value.host }
|
||||
if (cacheSr === undefined) {
|
||||
return { type: 'live-mount', host }
|
||||
}
|
||||
if (!isNonEmptyString(cacheSr)) {
|
||||
throw invalidTarget(vdiUuid, `expected the uuid of the cache SR, got ${typeof cacheSr}`)
|
||||
}
|
||||
return { type: 'live-mount', host, cacheSr }
|
||||
}
|
||||
|
||||
if (type === 'restore') {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { describe, it } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import { hasLiveMountTarget, normalizeVdiRestoreTargets } from './_vdiRestoreTargets.mjs'
|
||||
import { hasCachedLiveMountTarget, hasLiveMountTarget, normalizeVdiRestoreTargets } from './_vdiRestoreTargets.mjs'
|
||||
|
||||
const VDI_1 = 'vdi-uuid-1'
|
||||
const VDI_2 = 'vdi-uuid-2'
|
||||
@@ -64,6 +64,11 @@ describe('normalizeVdiRestoreTargets()', () => {
|
||||
assert.equal(targets.getLiveMountHost(), HOST)
|
||||
})
|
||||
|
||||
it('keeps the cache SR of a live mount', () => {
|
||||
const targets = normalizeVdiRestoreTargets({ [VDI_1]: { type: 'live-mount', host: HOST, cacheSr: SR } })
|
||||
assert.deepEqual(targets.get(VDI_1), { type: 'live-mount', host: HOST, cacheSr: SR })
|
||||
})
|
||||
|
||||
it('accepts a restore without SR', () => {
|
||||
const targets = normalizeVdiRestoreTargets({ [VDI_1]: { type: 'restore' } })
|
||||
assert.deepEqual(targets.get(VDI_1), { type: 'restore', sr: undefined, useDifferential: false })
|
||||
@@ -97,6 +102,13 @@ describe('normalizeVdiRestoreTargets()', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('rejects a non string cache SR', () => {
|
||||
assert.throws(
|
||||
() => normalizeVdiRestoreTargets({ [VDI_1]: { type: 'live-mount', host: HOST, cacheSr: null } }),
|
||||
/expected the uuid of the cache SR/
|
||||
)
|
||||
})
|
||||
|
||||
it('rejects a non string SR', () => {
|
||||
assert.throws(() => normalizeVdiRestoreTargets({ [VDI_1]: { type: 'restore', sr: 42 } }), /expected an SR uuid/)
|
||||
})
|
||||
@@ -130,3 +142,20 @@ describe('hasLiveMountTarget()', () => {
|
||||
assert.equal(hasLiveMountTarget({ [VDI_1]: SR, [VDI_2]: null, [VDI_3]: { type: 'ignore' } }), false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('hasCachedLiveMountTarget()', () => {
|
||||
it('detects a cached live mount entry', () => {
|
||||
assert.equal(hasCachedLiveMountTarget({ [VDI_1]: { type: 'live-mount', host: HOST, cacheSr: SR } }), true)
|
||||
})
|
||||
|
||||
it('is false for an uncached live mount, and every other shape', () => {
|
||||
assert.equal(hasCachedLiveMountTarget(undefined), false)
|
||||
assert.equal(hasCachedLiveMountTarget({ [VDI_1]: { type: 'live-mount', host: HOST } }), false)
|
||||
assert.equal(hasCachedLiveMountTarget({ [VDI_1]: SR, [VDI_2]: null, [VDI_3]: { type: 'ignore' } }), false)
|
||||
})
|
||||
|
||||
// the normalization rejects it, which is the error worth reporting
|
||||
it('is false for a null cache SR', () => {
|
||||
assert.equal(hasCachedLiveMountTarget({ [VDI_1]: { type: 'live-mount', host: HOST, cacheSr: null } }), false)
|
||||
})
|
||||
})
|
||||
|
||||
26
@xen-orchestra/backups/backupExpressions.mjs
Normal file
26
@xen-orchestra/backups/backupExpressions.mjs
Normal file
@@ -0,0 +1,26 @@
|
||||
import { isExpression, compileExpression } from './_expressionPredicate.mjs'
|
||||
import { invalidParameters } from 'xo-common/api-errors.js'
|
||||
|
||||
export function assertValidSettingsExpression(settings, keys) {
|
||||
for (const [scheduleId, scheduleSettings] of Object.entries(settings ?? {})) {
|
||||
for (const key of keys) {
|
||||
assertValidExpression(`settings/${scheduleId === '' ? 'general' : scheduleId}/${key}`, scheduleSettings?.[key])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function assertValidExpression(name, value) {
|
||||
if (isExpression(value)) {
|
||||
try {
|
||||
compileExpression(value)
|
||||
} catch (error) {
|
||||
throw invalidParameters(`Invalid ${name} expression: ${error.message}`, [
|
||||
{ instancePath: `/${name}`, message: error.message },
|
||||
])
|
||||
}
|
||||
} else if (typeof value === 'string' && value.trim() === '') {
|
||||
throw invalidParameters(`Invalid ${name} expression: must be a non-empty expression`, [
|
||||
{ instancePath: `/${name}`, message: 'must be a non-empty expression' },
|
||||
])
|
||||
}
|
||||
}
|
||||
77
@xen-orchestra/backups/backupExpressions.test.mjs
Normal file
77
@xen-orchestra/backups/backupExpressions.test.mjs
Normal file
@@ -0,0 +1,77 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { describe, it } from 'node:test'
|
||||
import { invalidParameters } from 'xo-common/api-errors.js'
|
||||
|
||||
import { assertValidExpression, assertValidSettingsExpression } from './backupExpressions.mjs'
|
||||
|
||||
const VALID = 'vm:tags:prod'
|
||||
const INVALID = 'vm:('
|
||||
|
||||
// checks that the error is `invalidParameters` and points to the expected field
|
||||
const isInvalidAt = instancePath => error =>
|
||||
invalidParameters.is(error) && error.data.errors[0].instancePath === instancePath
|
||||
|
||||
describe('assertValidExpression', function () {
|
||||
it('accepts a valid expression', function () {
|
||||
assert.doesNotThrow(() => assertValidExpression('srs', VALID))
|
||||
})
|
||||
|
||||
it('rejects an invalid expression with invalidParameters', function () {
|
||||
assert.throws(() => assertValidExpression('srs', INVALID), isInvalidAt('/srs'))
|
||||
})
|
||||
|
||||
it('ignores values which are not expressions', function () {
|
||||
for (const value of [undefined, '3', { id: 'sr-1' }, { id: { __or: ['sr-1', 'sr-2'] } }]) {
|
||||
assert.doesNotThrow(() => assertValidExpression('srs', value))
|
||||
}
|
||||
})
|
||||
|
||||
it('rejects blank strings', function () {
|
||||
for (const value of ['', ' ']) {
|
||||
assert.throws(() => assertValidExpression('srs', value), isInvalidAt('/srs'))
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('assertValidSettingsExpression', function () {
|
||||
const KEYS = ['healthCheckVmsWithTags', 'fullInterval']
|
||||
|
||||
it('accepts valid expressions in the general and the schedule settings', function () {
|
||||
assert.doesNotThrow(() =>
|
||||
assertValidSettingsExpression(
|
||||
{ '': { fullInterval: VALID }, 'schedule-1': { healthCheckVmsWithTags: VALID } },
|
||||
KEYS
|
||||
)
|
||||
)
|
||||
})
|
||||
|
||||
it('rejects an invalid expression in the schedule settings and points to it', function () {
|
||||
assert.throws(
|
||||
() => assertValidSettingsExpression({ '': {}, 'schedule-1': { fullInterval: INVALID } }, KEYS),
|
||||
isInvalidAt('/settings/schedule-1/fullInterval')
|
||||
)
|
||||
})
|
||||
|
||||
it('rejects an invalid expression in the general settings', function () {
|
||||
// the general settings are keyed by '', named "general" in the path.
|
||||
assert.throws(
|
||||
() => assertValidSettingsExpression({ '': { healthCheckVmsWithTags: INVALID } }, KEYS),
|
||||
isInvalidAt('/settings/general/healthCheckVmsWithTags')
|
||||
)
|
||||
})
|
||||
|
||||
it('ignores the legacy array and number values', function () {
|
||||
assert.doesNotThrow(() =>
|
||||
assertValidSettingsExpression({ 'schedule-1': { healthCheckVmsWithTags: ['tag'], fullInterval: 3 } }, KEYS)
|
||||
)
|
||||
})
|
||||
|
||||
it('ignores the keys it has not been asked to check', function () {
|
||||
assert.doesNotThrow(() => assertValidSettingsExpression({ 'schedule-1': { vmFilter: INVALID } }, KEYS))
|
||||
})
|
||||
|
||||
it('handles missing settings and undefined entries', function () {
|
||||
assert.doesNotThrow(() => assertValidSettingsExpression(undefined, KEYS))
|
||||
assert.doesNotThrow(() => assertValidSettingsExpression({ 'schedule-1': undefined }, KEYS))
|
||||
})
|
||||
})
|
||||
@@ -43,7 +43,7 @@ A VM directory holds **both** the full and the incremental backups of that VM: t
|
||||
|
||||
Transient entries, not listed in the trees below:
|
||||
|
||||
- `xo-vm-backups/<VM UUID>.lock`, held for the duration of a job run on that VM. When listing VMs, entries starting with `.` and entries ending with `.lock` are ignored.
|
||||
- `xo-vm-backups/<VM UUID>.lock`, held while a job writes to that VM's folder and while `cleanVm` cleans or merges it (after a job, after a deletion, or in the merge worker on local remotes). It is effective on S3 (an object rewritten every 30 seconds) and Azure (blob lease) too. When listing VMs, entries starting with `.` and entries ending with `.lock` are ignored.
|
||||
- `xo-vm-backups/.queue/clean-vm/<YYYYMMDD>T<HHmmss>-<random>`, the merge worker queue
|
||||
- `.<VHD file name>.merge.json`, in a VDI directory, interrupted merge states.
|
||||
|
||||
@@ -167,6 +167,7 @@ Append-only log of what happened to the VM backup metadata.
|
||||
- entries are never modified nor overwritten, so it works on an immutable remote
|
||||
- written unencrypted, so it stays readable without the encryption key
|
||||
- one directory per day: a reader lists only the days it misses, a purge drops whole directories
|
||||
- xo-server removes the days older than `backups.journalRetention` from every enabled remote when it starts, then once a day; it always keeps the `fullListingInterval` of the remote, during which it reads the journal back; a day which cannot be removed is skipped, and the next purge tries it again
|
||||
- the time part has a fixed width, so sorting the names by name sorts them by date
|
||||
- `event` is `add`, `change` or `del`, and the file content also has a `reason` field
|
||||
- the name of the file is only there to be read by a human: XO reads every field from the file content
|
||||
|
||||
28
@xen-orchestra/backups/liveMountXapiLabels.mjs
Normal file
28
@xen-orchestra/backups/liveMountXapiLabels.mjs
Normal file
@@ -0,0 +1,28 @@
|
||||
import { formatFilenameDate } from './_filenameDate.mjs'
|
||||
|
||||
/**
|
||||
* XAPI names of what a live mount of a backed up disk shows the user, so the ones created by the REST
|
||||
* API and by a restore read the same.
|
||||
*
|
||||
* The VDI attached to the host keeps the name of the backed up disk: that is the disk of the VM as
|
||||
* the user knows it. The SR says what it is, and which restore point it serves, the same way as a
|
||||
* restored VM is named.
|
||||
*
|
||||
* @param {object} params
|
||||
* @param {string} params.vmNameLabel - name of the backed up VM
|
||||
* @param {string} params.vdiNameLabel - name of the backed up disk
|
||||
* @param {number} params.timestamp - of the restore point, in ms
|
||||
* @param {boolean} [params.readWrite] - whether the mount is a live restore rather than a live mount
|
||||
* @returns {{ srNameLabel: string, vdiNameLabel: string, vdiNameDescription: string }} - the `xapiLabels` of the live mount mixin
|
||||
*/
|
||||
export function liveMountXapiLabels({ vmNameLabel, vdiNameLabel, timestamp, readWrite = false }) {
|
||||
const what = readWrite ? 'live restore' : 'live mount'
|
||||
const restorePoint = `${vmNameLabel} (${formatFilenameDate(new Date(timestamp))})`
|
||||
return {
|
||||
srNameLabel: `[XO ${what}] ${restorePoint}`,
|
||||
vdiNameLabel,
|
||||
vdiNameDescription: readWrite
|
||||
? `Live restore of ${restorePoint}: what is written to it is lost on unmount.`
|
||||
: `Read-only live mount of ${restorePoint}, removed on unmount.`,
|
||||
}
|
||||
}
|
||||
27
@xen-orchestra/backups/liveMountXapiLabels.test.mjs
Normal file
27
@xen-orchestra/backups/liveMountXapiLabels.test.mjs
Normal file
@@ -0,0 +1,27 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { describe, it } from 'node:test'
|
||||
|
||||
import { liveMountXapiLabels } from './liveMountXapiLabels.mjs'
|
||||
|
||||
const TIMESTAMP = 1754035712000 // 2025-08-01T08:08:32Z
|
||||
|
||||
describe('liveMountXapiLabels', () => {
|
||||
it('keeps the name of the disk and names the SR after the restore point', () => {
|
||||
assert.deepEqual(liveMountXapiLabels({ timestamp: TIMESTAMP, vdiNameLabel: 'system', vmNameLabel: 'web01' }), {
|
||||
srNameLabel: '[XO live mount] web01 (20250801T080832Z)',
|
||||
vdiNameLabel: 'system',
|
||||
vdiNameDescription: 'Read-only live mount of web01 (20250801T080832Z), removed on unmount.',
|
||||
})
|
||||
})
|
||||
|
||||
it('says a live restore loses what is written to it', () => {
|
||||
const xapiLabels = liveMountXapiLabels({
|
||||
readWrite: true,
|
||||
timestamp: TIMESTAMP,
|
||||
vdiNameLabel: 'system',
|
||||
vmNameLabel: 'web01',
|
||||
})
|
||||
assert.equal(xapiLabels.srNameLabel, '[XO live restore] web01 (20250801T080832Z)')
|
||||
assert.match(xapiLabels.vdiNameDescription, /lost on unmount/)
|
||||
})
|
||||
})
|
||||
@@ -8,7 +8,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/vatesfr/xen-orchestra.git"
|
||||
},
|
||||
"version": "0.76.1",
|
||||
"version": "0.78.0",
|
||||
"engines": {
|
||||
"node": ">=14.18"
|
||||
},
|
||||
@@ -27,19 +27,20 @@
|
||||
"@vates/disposable": "^0.1.6",
|
||||
"@vates/fuse-vhd": "^2.1.3",
|
||||
"@vates/generator-toolbox": "^1.1.3",
|
||||
"@vates/nbd-client": "^3.6.0",
|
||||
"@vates/nbd-client": "^3.7.0",
|
||||
"@vates/parse-duration": "^0.1.1",
|
||||
"@vates/task": "^0.7.0",
|
||||
"@vates/types": "^2.3.0",
|
||||
"@vates/types": "^2.5.0",
|
||||
"@xen-orchestra/async-map": "^0.1.5",
|
||||
"@xen-orchestra/disk-transform": "^1.3.3",
|
||||
"@xen-orchestra/fs": "^4.9.5",
|
||||
"@xen-orchestra/disk-transform": "^1.3.4",
|
||||
"@xen-orchestra/fs": "^4.10.0",
|
||||
"@xen-orchestra/log": "^0.7.2",
|
||||
"@xen-orchestra/qcow2": "^1.4.0",
|
||||
"@xen-orchestra/qcow2": "^1.4.1",
|
||||
"@xen-orchestra/template": "^0.1.1",
|
||||
"@xen-orchestra/backup-archive": "^1.1.3",
|
||||
"@xen-orchestra/backup-archive": "^1.2.0",
|
||||
"app-conf": "^3.0.0",
|
||||
"compare-versions": "^6.0.0",
|
||||
"complex-matcher": "^1.1.1",
|
||||
"d3-time-format": "^4.1.0",
|
||||
"decorator-synchronized": "^0.6.0",
|
||||
"golike-defer": "^0.5.1",
|
||||
@@ -55,8 +56,10 @@
|
||||
"tar": "^7.5.19",
|
||||
"uuid": "^9.0.0",
|
||||
"value-matcher": "^0.2.0",
|
||||
"vhd-lib": "^4.16.2",
|
||||
"xen-api": "^5.0.2",
|
||||
"vhd-lib": "^4.17.0",
|
||||
"xen-api": "^5.1.0",
|
||||
"xo-common": "^0.11.0",
|
||||
"xo-remote-parser": "^2.0.0",
|
||||
"yazl": "^2.5.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -67,7 +70,7 @@
|
||||
"typescript": "^5.9.3"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@xen-orchestra/xapi": "^9.0.1"
|
||||
"@xen-orchestra/xapi": "^9.1.0"
|
||||
},
|
||||
"license": "AGPL-3.0-or-later",
|
||||
"author": {
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
"preferGlobal": true,
|
||||
"dependencies": {
|
||||
"golike-defer": "^0.5.1",
|
||||
"xen-api": "^5.0.2"
|
||||
"xen-api": "^5.1.0"
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"private": false,
|
||||
"name": "@xen-orchestra/disk-cli",
|
||||
"version": "2.2.3",
|
||||
"version": "2.2.5",
|
||||
"description": "CLI tool to inspect and manage disks in backup repositories",
|
||||
"type": "module",
|
||||
"bin": {
|
||||
@@ -20,13 +20,13 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@vates/async-each": "^1.0.3",
|
||||
"@xen-orchestra/backups": "^0.76.1",
|
||||
"@xen-orchestra/backup-archive": "^1.1.3",
|
||||
"@xen-orchestra/disk-transform": "^1.3.3",
|
||||
"@xen-orchestra/fs": "^4.9.5",
|
||||
"@xen-orchestra/qcow2": "^1.4.0",
|
||||
"@xen-orchestra/backups": "^0.78.0",
|
||||
"@xen-orchestra/backup-archive": "^1.2.0",
|
||||
"@xen-orchestra/disk-transform": "^1.3.4",
|
||||
"@xen-orchestra/fs": "^4.10.0",
|
||||
"@xen-orchestra/qcow2": "^1.4.1",
|
||||
"promise-toolbox": "^0.21.0",
|
||||
"vhd-lib": "^4.16.2"
|
||||
"vhd-lib": "^4.17.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.3.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@xen-orchestra/disk-transform",
|
||||
"version": "1.3.3",
|
||||
"version": "1.3.4",
|
||||
"main": "./dist/index.mjs",
|
||||
"license": "MIT",
|
||||
"private": false,
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user