Add resizefs utility

Change-Id: I5f503cf07216df64dcc33081f24cd083d003e581
Reviewed-on: https://review.haiku-os.org/c/haiku/+/943
Reviewed-by: Jérôme Duval <[email protected]>
Reviewed-by: Axel Dörfler <[email protected]>
Reviewed-by: Adrien Destugues <[email protected]>
This commit is contained in:
ahenriksson
2019-08-20 09:48:51 +00:00
committed by Adrien Destugues
parent a7536efa8b
commit 03867a467e
3 changed files with 131 additions and 1 deletions
+1
View File
@@ -128,6 +128,7 @@ StdBinCommands
# standard commands that need libbe.so, libsupc++.so, and libshared.a
StdBinCommands
ramdisk.cpp
resizefs.cpp
: shared be [ TargetLibsupc++ ] : $(haiku-utils_rsrc) ;
# standard commands that need libbe.so, libbnetapi.so, libsupc++.so
+128
View File
@@ -0,0 +1,128 @@
/*
* Copyright 2012, Andreas Henriksson, [email protected].
* Copyright 2008, Axel Dörfler, [email protected].
* Distributed under the terms of the MIT License.
*/
#include <stdio.h>
#include <DiskDevice.h>
#include <DiskDeviceRoster.h>
#include <DiskSystem.h>
#include <StringForSize.h>
extern "C" const char* __progname;
static const char* kProgramName = __progname;
int
main(int argc, char** argv)
{
if (argc != 3) {
fprintf(stderr, "Usage: %s <device> <size>\n",
"Resize volume on <device> to new size <size> (in bytes, suffixes "
"'k', 'm', 'g', 't' are interpreted as KiB, Mib, GiB, TiB).\n"
kProgramName);
return 1;
}
int64 size = parse_size(argv[2]);
if (size <= 0) {
fprintf(stderr, "%s: The new size \"%s\" is not a number\n",
kProgramName, argv[2]);
return 1;
}
BDiskDeviceRoster roster;
BPartition* partition;
BDiskDevice device;
status_t status = roster.GetPartitionForPath(argv[1], &device,
&partition);
if (status != B_OK) {
if (strncmp(argv[1], "/dev", 4)) {
// try mounted volume
status = roster.FindPartitionByMountPoint(argv[1], &device,
&partition) ? B_OK : B_BAD_VALUE;
}
// TODO: try to register file device
if (status != B_OK) {
fprintf(stderr, "%s: Failed to get disk device for path \"%s\": "
"%s\n", kProgramName, argv[1], strerror(status));
return 1;
}
}
// Prepare the device for modifications
status = device.PrepareModifications();
if (status != B_OK) {
fprintf(stderr, "%s: Could not prepare the device for modifications: "
"%s\n", kProgramName, strerror(status));
return false;
}
// Check if the partition supports resizing
bool canResizeWhileMounted;
bool canResize = partition->CanResize(NULL, &canResizeWhileMounted);
if (!canResize && !canResizeWhileMounted) {
fprintf(stderr, "%s: The disk system does not support resizing.\n",
kProgramName);
return 1;
}
if (partition->IsMounted() && !canResizeWhileMounted) {
fprintf(stderr, "%s: The disk system does not support resizing a "
"mounted volume.\n", kProgramName);
return 1;
}
if (!partition->IsMounted() && !canResize) {
fprintf(stderr, "%s: The disk system does not support resizing a "
"volume that is not mounted.\n", kProgramName);
return 1;
}
// Validate the requested size
off_t validatedSize = size;
status = partition->ValidateResize(&validatedSize);
if (status != B_OK) {
fprintf(stderr, "%s: Failed to validate the size: %s\n", kProgramName,
strerror(status));
return 1;
}
if (validatedSize != size) {
printf("The requested size (%" B_PRId64 " bytes) was not accepted.\n"
"Resize to %" B_PRId64 " bytes instead? (y/N): ", size,
(int64)validatedSize);
int c = getchar();
if (c != 'y')
return 0;
}
// Resize the volume
status = partition->Resize(validatedSize);
if (status != B_OK) {
fprintf(stderr, "%s: Resizing failed: %s\n", kProgramName,
strerror(status));
return 1;
}
status = device.CommitModifications();
if (status != B_OK) {
fprintf(stderr, "%s: Failed to commit modifications: %s\n",
kProgramName, strerror(status));
return 1;
}
return 0;
}