Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
Tool for computing partman partition sizes
Based on partman docs at https://wikitech.wikimedia.org/wiki/PartManAuto
To compile: gcc partman-check.c -o partman-check
To run: ./partman-check
*/
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 80
typedef struct PARTITION
{
int min;
int max;
int priority;
}Partition;
int read_int(char* prompt)
{
int result;
char content[BUFFER_SIZE];
printf("%s\n>",prompt);
fgets(content, BUFFER_SIZE, stdin);
sscanf(content, "%i", &result);
return result;
}
int main( int argc, char** argv)
{
int i = 0;
int N; // number of partitions
int free_space; // total size of disk
Partition* partitions;
printf("For help, please see https://wikitech.wikimedia.org/wiki/PartManAuto\n");
N = read_int("How many partitions to configure?");
free_space = read_int("How much free space is there on disk? (in MB)");
partitions = malloc (sizeof(Partition) * N);
int factor[N];
for (i = 0; i < N; i++)
{
printf("Configuring partition %i of %i\n",i+1,N);
partitions[i].min = read_int("\tMin size for partition (in MB)");
partitions[i].max = read_int("\tMax size for partition (in MB)");
partitions[i].priority = read_int("\tPartition priority (see documentation).") ;
}
for(i=0;i<=N;i++)
factor[i] = partitions[i].priority - partitions[i].min;
int ready = 0;
while (! ready) {
int minsum = 0;
int factsum = 0;
for (i = 0; i<N; i++)
minsum += partitions[i].min;
for (i = 0; i<N; i++)
factsum += factor[i];
ready = 1;
for(i=0;i<N;i++) {
float scale = (float) factor[i] / (float) factsum;
float step = ( (float)free_space - (float)minsum) * scale ;
int x = partitions[i].min + (int)step;
if (x > partitions[i].max)
x = partitions[i].max;
if (x != partitions[i].min) {
ready = 0;
partitions[i].min = x;
}
}
}
for (i = 0; i < N; i++)
printf("Partition %i: %i \n",i+1,partitions[i].min);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment