1

I would like to use a C program to create and edit the disk label, and the partitions of block devices. Is this possible in User-space, is there a syscall to do this, or do I need a kernel module in order to do this?

I know it's possible by doing it like this:

#include <stdlib.h>

static const char *cmds[] =
{
    "parted /dev/sda.....",
    "fdisk /dev/sdb....",
    0,
};

int main(void)
{
    int i;

    for (i = 0; cmds[i] != 0; i++)
        if (system(cmds[i]) != 0)
            exit(EXIT_FAILURE);
    return 0;
}

but that's not the way I want to do it, because I can never be sure that the commands are executable on the system the program is used.

So, is there any way to do it with a C library?

xcynthos
  • 49
  • 2
  • 5
  • You just open `/dev/sda` and `lseek(), read(), write()`. I think there is an `ioctl()` at the end to tell the kernel to refresh its in-memory version of the partition table. But AFAIK you get no help from the OS in deciding what to write; your program has to compose the raw partition table for itself. – Nate Eldredge Jul 26 '20 at 23:12
  • 1
    parted calls libparted. You can do the same. – stark Jul 27 '20 at 02:21
  • I'm sure you need more rights than the standard user. It would open the systems to all kinds of attacks if a "simple" program could write to the filesystems. – the busybee Jul 27 '20 at 06:28

0 Answers0