cubuntulinux-kernelarchlinuxbtrfs

Is there any BTRFS library interface in c for creating , deleting or listing out btrfs subvolume?


I want a convenient API in c to get the list of sub-volumes in the given btrfs partition as listed out when we run the command below.

btrfs subvolume list btrfs/subvol/path


Solution

  • If you can't find a convenient API, popen is what you want:

    #include <stdio.h>
    
    FILE *popen(const char *command, const char *mode);
    int pclose(FILE *stream);
    
    int main(void)
    {
        FILE *cmd = popen("btrfs subvolume list btrfs/subvol/path", "r");
        char result[128];
    
        while (fgets(result, sizeof(result), cmd) != NULL)
               printf("%s", result);
        pclose(cmd);
        return 0;
    }