From 7fe916c9163d39dad6ebd2df7fc7944be2d4c30c Mon Sep 17 00:00:00 2001 From: Travis Glenn Hansen Date: Mon, 25 Apr 2022 10:45:46 -0600 Subject: [PATCH] more ntfs fixes Signed-off-by: Travis Glenn Hansen --- src/driver/index.js | 10 ++----- src/utils/filesystem.js | 62 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/driver/index.js b/src/driver/index.js index 3c4281e..8a0bd20 100644 --- a/src/driver/index.js +++ b/src/driver/index.js @@ -1001,8 +1001,8 @@ class CsiBaseDriver { if (fs_type == "ntfs") { if (partition_count < 1) { - // dos is what csi-proxy uses by default - let ntfs_partition_label = "dos"; + // gpt is what csi-proxy uses by default + let ntfs_partition_label = "gpt"; switch (ntfs_partition_label.toLowerCase()) { case "dos": // partion dos @@ -1010,11 +1010,7 @@ class CsiBaseDriver { break; case "gpt": // partion gpt - await filesystem.partitionDevice( - device, - "gpt", - "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7" - ); + await filesystem.partitionDeviceWindows(device); break; default: throw new GrpcError( diff --git a/src/utils/filesystem.js b/src/utils/filesystem.js index 284c713..d6fdbb9 100644 --- a/src/utils/filesystem.js +++ b/src/utils/filesystem.js @@ -429,6 +429,60 @@ class Filesystem { } } + /** + * mimic the behavior of partitioning a new data drive in windows directly + * + * https://en.wikipedia.org/wiki/Microsoft_Reserved_Partition + * + * @param {*} device + */ + async partitionDeviceWindows(device) { + const filesystem = this; + let args = [device]; + let result; + let block_device_info = await filesystem.getBlockDevice(device); + + //let sixteen_megabytes = 16777216; + //let thirtytwo_megabytes = 33554432; + //let onehundredtwentyeight_megabytes = 134217728; + + let msr_partition_size = "16M"; + let label = "gpt"; + let msr_guid = "E3C9E316-0B5C-4DB8-817D-F92DF00215AE"; + let ntfs_guid = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7"; + + if (block_device_info.type != "disk") { + throw new Error( + `cannot partition device of type: ${block_device_info.type}` + ); + } + + /** + * On drives less than 16GB in size, the MSR is 32MB. + * On drives greater than or equal two 16GB, the MSR is 128 MB. + * It is only 128 MB for Win 7/8 ( On drives less than 16GB in size, the MSR is 32MB ) & 16 MB for win 10! + */ + let msr_partition_size_break = 17179869184; // 16GB + + // TODO: this size may be sectors so not really disk size in terms of GB + if (block_device_info.size >= msr_partition_size_break) { + // ignoring for now, appears windows 10+ use 16MB always + //msr_partition_size = "128M"; + } + + try { + result = await filesystem.exec("sfdisk", args, { + stdin: `label: ${label}\n`, + }); + // must send ALL partitions at once (newline separated), cannot send them 1 at a time + result = await filesystem.exec("sfdisk", args, { + stdin: `size=${msr_partition_size},type=${msr_guid}\ntype=${ntfs_guid}\n`, + }); + } catch (err) { + throw err; + } + } + /** * * @param {*} device @@ -631,6 +685,9 @@ class Filesystem { case "ntfs": // must be unmounted command = "ntfsresize"; + await filesystem.exec(command, ["-c", device]); + await filesystem.exec(command, ["-n", device]); + args = args.concat("-P", "-f"); args = args.concat(options); //args = args.concat(["-s", "max"]); args.push(device); @@ -651,6 +708,10 @@ class Filesystem { try { result = await filesystem.exec(command, args); + // must clear the dirty bit after resize + if (fstype.toLowerCase() == "ntfs") { + await filesystem.exec("ntfsfix", ["-d", device]); + } return result; } catch (err) { throw err; @@ -697,6 +758,7 @@ class Filesystem { * -d, --clear-dirty Clear the volume dirty flag */ command = "ntfsfix"; + args.puuh("-d"); args.push(device); break; case "xfs":