Skip to content

Commit

Permalink
fix: avoid adding pre-allocated space to lvm disks
Browse files Browse the repository at this point in the history
  • Loading branch information
MauriceNino committed Jan 9, 2024
1 parent 31c8747 commit f1949b4
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 12 deletions.
8 changes: 8 additions & 0 deletions apps/server/__TESTS__/dynamic-info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
TEST_CASE_21,
TEST_CASE_22,
TEST_CASE_23,
TEST_CASE_24,
TestCase,
} from './test-cases';

Expand Down Expand Up @@ -98,5 +99,12 @@ describe('Dynamic Info', () => {
).getMappedLayout();
expect(output).to.deep.equal(TEST_CASE_23.output);
});
it('Test Case 24', () => {
const output = new DynamicStorageMapper(
false,
...toStorageInp(TEST_CASE_24)
).getMappedLayout();
expect(output).to.deep.equal(TEST_CASE_24.output);
});
});
});
5 changes: 5 additions & 0 deletions apps/server/__TESTS__/static-info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
TEST_CASE_21,
TEST_CASE_22,
TEST_CASE_23,
TEST_CASE_24,
TestCase,
} from './test-cases';

Expand Down Expand Up @@ -69,5 +70,9 @@ describe('Static Info', () => {
const output = mapToStorageLayout(false, ...toStorageInp(TEST_CASE_23));
expect(output).to.deep.equal(TEST_CASE_23.layout);
});
it('Test Case 24', () => {
const output = mapToStorageLayout(false, ...toStorageInp(TEST_CASE_24));
expect(output).to.deep.equal(TEST_CASE_24.layout);
});
});
});
221 changes: 218 additions & 3 deletions apps/server/__TESTS__/test-cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ export const TEST_CASE_17 = {
size: 896998047744,
},
],
output: [220796919808, 107488473088],
output: [35927498752, 107488473088],
} as any as TestCase;

export const TEST_CASE_18 = {
Expand Down Expand Up @@ -1652,7 +1652,7 @@ export const TEST_CASE_18 = {
size: 240057409536,
},
],
output: [1120183857152, 100690599936, 48969498624],
output: [1120183857152, 91657261056, 48969498624],
} as any as TestCase;

export const TEST_CASE_19 = {
Expand Down Expand Up @@ -3223,7 +3223,7 @@ export const TEST_CASE_21 = {
size: 2000398934016,
},
],
output: [28243886080, 845393879040],
output: [19534192640, 845393879040],
} as any as TestCase;

// Currently somewhat incorrect due to BTRFS Raid not correctly supported
Expand Down Expand Up @@ -3913,3 +3913,218 @@ export const TEST_CASE_23 = {
],
output: [280609816576],
} as any as TestCase;

// Test case from home server
export const TEST_CASE_24 = {
disks: [
{
device: '/dev/nvme0n1',
type: 'NVMe',
name: 'PCIe SSD ',
vendor: '',
size: 512110190592,
bytesPerSector: null,
totalCylinders: null,
totalHeads: null,
totalSectors: null,
totalTracks: null,
tracksPerCylinder: null,
sectorsPerTrack: null,
firmwareRevision: '',
serialNum: '7FBC07390DD900365059',
interfaceType: 'PCIe',
smartStatus: 'unknown',
temperature: null,
},
],
sizes: [
{
fs: 'overlay',
type: 'overlay',
size: 105089261568,
used: 11768532992,
available: 87935242240,
use: 11.8,
mount: '/',
rw: false,
},
{
fs: '/dev/mapper/ubuntu--vg-ubuntu--lv',
type: 'ext4',
size: 105089261568,
used: 11768532992,
available: 87935242240,
use: 11.8,
mount: '/mnt/host',
rw: true,
},
{
fs: '/dev/loop0',
type: 'squashfs',
size: 66584576,
used: 66584576,
available: 0,
use: 100,
mount: '/mnt/host/snap/core20/1974',
rw: false,
},
{
fs: '/dev/loop1',
type: 'squashfs',
size: 117440512,
used: 117440512,
available: 0,
use: 100,
mount: '/mnt/host/snap/lxd/24322',
rw: false,
},
{
fs: '/dev/loop2',
type: 'squashfs',
size: 55967744,
used: 55967744,
available: 0,
use: 100,
mount: '/mnt/host/snap/snapd/19457',
rw: false,
},
{
fs: '/dev/nvme0n1p2',
type: 'ext4',
size: 2040373248,
used: 136232960,
available: 1779990528,
use: 7.11,
mount: '/mnt/host/boot',
rw: true,
},
{
fs: '/dev/nvme0n1p1',
type: 'vfat',
size: 1124999168,
used: 6369280,
available: 1118629888,
use: 0.57,
mount: '/mnt/host/boot/efi',
rw: true,
},
],
blocks: [
{
name: 'nvme0n1',
type: 'disk',
fsType: '',
mount: '',
size: 512110190592,
physical: 'SSD',
uuid: '',
label: '',
model: 'PCIe SSD',
serial: '7FBC07390DD900365059',
removable: false,
protocol: 'nvme',
group: '',
device: '/dev/nvme0n1',
},
{
name: 'loop0',
type: 'loop',
fsType: 'squashfs',
mount: '/mnt/host/snap/core20/1974',
size: 66531328,
physical: '',
uuid: '',
label: '',
model: '',
serial: '',
removable: false,
protocol: '',
group: '',
},
{
name: 'loop1',
type: 'loop',
fsType: 'squashfs',
mount: '/mnt/host/snap/lxd/24322',
size: 117387264,
physical: '',
uuid: '',
label: '',
model: '',
serial: '',
removable: false,
protocol: '',
group: '',
},
{
name: 'loop2',
type: 'loop',
fsType: 'squashfs',
mount: '/mnt/host/snap/snapd/19457',
size: 55844864,
physical: '',
uuid: '',
label: '',
model: '',
serial: '',
removable: false,
protocol: '',
group: '',
},
{
name: 'nvme0n1p1',
type: 'part',
fsType: 'vfat',
mount: '/mnt/host/boot/efi',
size: 1127219200,
physical: '',
uuid: '060E-D169',
label: '',
model: '',
serial: '',
removable: false,
protocol: 'nvme',
group: '',
device: '/dev/nvme0n1',
},
{
name: 'nvme0n1p2',
type: 'part',
fsType: 'ext4',
mount: '/mnt/host/boot',
size: 2147483648,
physical: '',
uuid: 'dc87bf05-b396-43cb-a967-063d9a4c0466',
label: '',
model: '',
serial: '',
removable: false,
protocol: 'nvme',
group: '',
device: '/dev/nvme0n1',
},
{
name: 'nvme0n1p3',
type: 'part',
fsType: 'LVM2_member',
mount: '',
size: 508833038336,
physical: '',
uuid: 'YVRqdg-O2wM-Xt1P-u9mk-wtWW-7G56-S5ZFKR',
label: '',
model: '',
serial: '',
removable: false,
protocol: 'nvme',
group: '',
device: '/dev/nvme0n1',
},
],
layout: [
{
disks: [{ brand: '', device: 'nvme0n1', type: 'NVMe' }],
size: 512110190592,
},
],
output: [11911135232],
} as any as TestCase;
17 changes: 10 additions & 7 deletions apps/server/src/data/storage/dynamic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StorageInfo, StorageLoad } from '@dash/common';
import { StorageInfo, StorageLoad, sumUp } from '@dash/common';
import * as si from 'systeminformation';
import { CONFIG } from '../../config';
import { getStaticServerInfo } from '../../static-info';
Expand All @@ -7,8 +7,6 @@ import { PLATFORM_IS_WINDOWS, fromHost } from '../../utils';
type Block = si.Systeminformation.BlockDevicesData;
type Size = si.Systeminformation.FsSizeData;

const unwrapUsed = (size?: Size) => size?.used ?? 0;

export class DynamicStorageMapper {
private validSizes: Size[];
private validBlocks: Block[];
Expand Down Expand Up @@ -96,12 +94,17 @@ export class DynamicStorageMapper {
})
);

const totalAvailable = sizes.reduce((acc, size) => acc + size.size, 0);
const calculatedSize = sumUp(sizes, 'used');
const isLvm = deviceBlocks.some(({ fsType }) => fsType === 'LVM2_member');

if (isLvm) {
return calculatedSize;
}

const totalAvailable = sumUp(sizes, 'size');
const preAllocated = Math.max(0, diskSize - totalAvailable);

return (
sizes.reduce((acc, size) => acc + unwrapUsed(size), 0) + preAllocated
);
return calculatedSize + preAllocated;
}

public getMappedLayout() {
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/data/storage/static.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RaidType, StorageInfo } from '@dash/common';
import { RaidType, StorageInfo, sumUp } from '@dash/common';
import * as si from 'systeminformation';
import { CONFIG } from '../../config';
import { PLATFORM_IS_WINDOWS } from '../../utils';
Expand Down Expand Up @@ -94,7 +94,7 @@ const getRaidInfo = (blocks: Block[], diskBlock: Block, hostWin32: boolean) => {
label,
type: raidBlocks.current[0].type === 'raid0' ? RaidType.ZERO : RaidType.ONE,
name: raidBlocks.current[0].name,
size: raidBlocks.current.reduce((acc, curr) => acc + curr.size, 0),
size: sumUp(raidBlocks.current, 'size'),
};
};

Expand Down
4 changes: 4 additions & 0 deletions libs/common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ export const clamp = (num: number, min: number, max: number) => {
return num > max ? max : num < min ? min : num;
};

export const sumUp = <T extends any[]>(arr: T, key: keyof T[number]) => {
return arr.reduce((acc, obj) => acc + obj[key], 0);
};

export const findLastIndex = <T>(
array: Array<T>,
predicate: (value: T, index: number, obj: T[]) => boolean
Expand Down

0 comments on commit f1949b4

Please sign in to comment.