Formatting a Linux partition

I have the need to be able to programatically format a thumb drive having a single Linux partition on it using C/C++. What function call(s) do I need to make in order to get this done ?

I want to format the partition using a default partition type of my choosing if the partition has not yet been formatted. Otherwise I want to reformat the partition with the same partition type it already has. I know how to programatically mount or unmount the thumb drive to a path. What I do not know are the details of the functionality for formatting or reformatting the partition of the thumb drive.

Any help in this area would be welcome. I am an experienced C++ programmer, but have not dealt with this functionality on Linux before.
There tends to be utility mkfs.foo for creating filesystem type foo. E.g. mkfs.ext4, mkfs.xfs, etc. In fact, each filesystem has their own toolkit.

Similarly, there are multiple utilities for creating and editing partition tables. E.g. fdisk, gdisk, sfdisk, parted, gparted.

Practically all those tools are open source. You can look at them on how they do their job.
Amazing that their is no common programming API for formatting a partition in Linux, but that you have to learn each partition type's own way of doing this and even then there is no documentation but you are expected to find and look at source code to figure this out. What an incredible mess for Linux.
How often do you initialize filesystems?
How often you don't do that with one of the existing tools?

https://opensource.com/article/17/5/introduction-ext4-filesystem
Topic archived. No new replies allowed.