Skip to content

Instantly share code, notes, and snippets.

@iarchiveml
Last active May 10, 2023 17:54
Show Gist options
  • Save iarchiveml/47946a79c0412a38f3e5f1872f040183 to your computer and use it in GitHub Desktop.
Save iarchiveml/47946a79c0412a38f3e5f1872f040183 to your computer and use it in GitHub Desktop.
Resize an APFS container on an iOS device
// clang -o resize_apfs -Wall -Wextra -Wpedantic -Werror -Wl,-U,_mh_execute_header,-e,_main -Xlinker APFS.tbd resize_apfs.c
// Tested on iOS 10 and 15, won't work on iOS 12
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
typedef int (*apfs_resize_container_t)(const char *, unsigned long long);
typedef const char *(*apfs_get_error_string_t)(int);
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s diskToResize newSize\n", argv[0]);
return 1;
}
const char *diskToResize = argv[1];
unsigned long long newSize = strtoull(argv[2], NULL, 10);
void *apfsHandle = dlopen("/System/Library/PrivateFrameworks/APFS.framework/APFS", RTLD_LAZY);
if (!apfsHandle) {
printf("Error: Failed to load APFS framework.\n");
return 1;
}
apfs_resize_container_t apfs_resize_container = (apfs_resize_container_t)dlsym(apfsHandle, "APFSContainerResize");
if (!apfs_resize_container) {
printf("Error: Failed to get symbol APFSContainerResize.\n");
dlclose(apfsHandle);
return 1;
}
apfs_get_error_string_t apfs_get_error_string = (apfs_get_error_string_t)dlsym(apfsHandle, "APFSGetErrorString");
if (!apfs_get_error_string) {
printf("Error: Failed to get symbol APFSGetErrorString.\n");
dlclose(apfsHandle);
return 1;
}
int ret = apfs_resize_container(diskToResize, newSize);
if (ret != 0) {
printf("Error: Failed to resize APFS container with error code %d: %s\n", ret, apfs_get_error_string(ret));
dlclose(apfsHandle);
return 1;
}
printf("APFS container %s has been resized to %llu bytes.\n", diskToResize, newSize);
dlclose(apfsHandle);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment