LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-17-2018, 07:23 AM   #1
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Rep: Reputation: 17
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.
 
Old 06-17-2018, 08:01 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
As a start, you could use popen(3) on fdisk(8):
Code:
fdisk -o Device,Id,Type -l /dev/sda 2>/dev/null | grep '^/'
/dev/sda1   7 HPFS/NTFS/exFAT
/dev/sda2   7 HPFS/NTFS/exFAT
/dev/sda3   5 Extended
/dev/sda4  12 Compaq diagnostics
/dev/sda5  82 Linux swap / Solaris
/dev/sda6  83 Linux
/dev/sda7  83 Linux
 
Old 06-17-2018, 07:06 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,879
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Best way to learn how this is done via programming is to read the source code.

From the manual page for mkfs as well as fdisk:
Quote:
The mkfs/fdisk command is part of the util-linux package and is available from https://www.kernel.org/pub/linux/utils/util-linux/.
 
Old 06-18-2018, 11:15 AM   #4
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by NevemTeve View Post
As a start, you could use popen(3) on fdisk(8):
Code:
fdisk -o Device,Id,Type -l /dev/sda 2>/dev/null | grep '^/'
/dev/sda1   7 HPFS/NTFS/exFAT
/dev/sda2   7 HPFS/NTFS/exFAT
/dev/sda3   5 Extended
/dev/sda4  12 Compaq diagnostics
/dev/sda5  82 Linux swap / Solaris
/dev/sda6  83 Linux
/dev/sda7  83 Linux
What does piping the results of an fdisk call get me ? I already know the device designation I need to format. Do you mean to say that reading the output from fdisk is the standard way of determing what filesystem is on a partition rather than calling some Linux API function which gives me that information ?
 
Old 06-18-2018, 11:18 AM   #5
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by rtmistler View Post
Best way to learn how this is done via programming is to read the source code.

From the manual page for mkfs as well as fdisk:
Thanks for the link ! I was hoping to not have to read source code to understand what to do but to be pointed to some API(s) that do what I want.
 
  


Reply

Tags
format, partition


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How can I change the label of one partition on linux without formatting it. 915086731 Linux - Newbie 5 04-13-2010 01:45 AM
How to recover XP after formatting Linux partition from windows me4linux Linux - General 10 02-27-2007 07:26 AM
formatting very big fat32 partition with Linux Xavius Linux - General 6 10-08-2005 07:53 PM
Formatting linux partition in RAID configuration triley Linux - General 2 09-08-2005 10:41 AM
Formatting A Linux Partition thebear Linux - General 2 09-16-2003 04:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration