system: return the mounted and unmounted block devices (as long as xfs or ext4)

This commit is contained in:
Girish Ramakrishnan
2025-09-23 17:30:09 +02:00
parent 8668ff8939
commit f92cdf36f5
7 changed files with 43 additions and 38 deletions
+14 -6
View File
@@ -13,9 +13,9 @@ exports = module.exports = {
MOUNT_TYPE_CIFS: 'cifs',
MOUNT_TYPE_NFS: 'nfs',
MOUNT_TYPE_SSHFS: 'sshfs',
MOUNT_TYPE_EXT4: 'ext4',
MOUNT_TYPE_XFS: 'xfs',
MOUNT_TYPE_DISK: 'disk',
MOUNT_TYPE_EXT4: 'ext4', // raw disk path
MOUNT_TYPE_XFS: 'xfs', // raw disk path
MOUNT_TYPE_DISK: 'disk', // this provides a selector of block devices
MOUNT_TYPE_LOOPBACK: 'loopback'
};
@@ -36,7 +36,7 @@ const REMOUNT_MOUNT_CMD = path.join(__dirname, 'scripts/remountmount.sh');
const SYSTEMD_MOUNT_EJS = fs.readFileSync(path.join(__dirname, 'systemd-mount.ejs'), { encoding: 'utf8' });
// https://man7.org/linux/man-pages/man8/mount.8.html
function validateMountOptions(type, options) {
async function validateMountOptions(type, options) {
assert.strictEqual(typeof type, 'string');
assert.strictEqual(typeof options, 'object');
@@ -66,10 +66,18 @@ function validateMountOptions(type, options) {
case exports.MOUNT_TYPE_EXT4:
case exports.MOUNT_TYPE_XFS:
case exports.MOUNT_TYPE_DISK:
case exports.MOUNT_TYPE_LOOPBACK:
case exports.MOUNT_TYPE_LOOPBACK: {
if (typeof options.diskPath !== 'string') return new BoxError(BoxError.BAD_FIELD, 'diskPath is not a string');
// TODO: check if this diskPath is not mounted on '/' or somewhere dangerous
const [error, output] = await safe(shell.spawn('lsblk', ['--paths', '--json', '--list', '--fs', options.diskPath ], { encoding: 'utf8' }));
if (error) return new BoxError(BoxError.BAD_FIELD, `Bad disk path: ${error.message}`);
const info = safe.JSON.parse(output);
if (!info) return new BoxError(BoxError.BAD_FIELD, `Bad disk path: ${safe.error.message}`);
for (const mountpoint of info.blockdevices[0].mountpoints) {
if (mountpoint === null) break; // [ null ] means not mounted anywhere
if (mountpoint === '/' || mountpoint.startsWith('/home') || mountpoint.startsWith('/boot')) return new BoxError(BoxError.BAD_FIELD, 'Disk is mounted in a protected location');
}
return null;
}
default:
return new BoxError(BoxError.BAD_FIELD, 'Bad mount type');
}