LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 07-26-2006, 01:08 AM   #1
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Rep: Reputation: 45
write partition table from the command line


I need to write to the partition table (delete partitions and add new ones). Is it possible to call the cfdisk commands from the CLI instead of using the cfdisk utility. If not, are there any utilities that will allow me to do so?

If no utilities exist, can anyone suggest some reading material about on creating my own partition table then writing it to the beginning of the disk ( with dd maybe? ).

Thanks for you time!
...drkstr
 
Old 07-26-2006, 01:14 AM   #2
prozac
Member
 
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753

Rep: Reputation: 32
try the 'fdisk' utility, though not recognized more than the 'cfdisk' utility, its pretty neat, simplistic and does the job well. it runs from CLI and would be perfect for your kinda job. i always use fdisk and it has never given me any problem whatsoever.
 
Old 07-26-2006, 01:25 AM   #3
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
Thanks for the quick reply. I actually glanced over the fdisk man page before posting this and I didn't see that many command line options. I see there are options to set sectorsize and whatnot, am I supposed to use these options to resize existing partitions? How can I delete/add an existing partition? More specificly I want to delete a partition and turn it into 3 partitions of a certain size. The sizes will be determined as a percentage of the original partition size.

What would a basic command look like to delete /dev/hdaX and turn it into /dev/hdaY and /dev/hdaZ?

Thanks for the help!
...drkstr

**edit**
I just saw this in the fdisk man page:
Quote:
sfdisk is for hackers only - the user interface is terrible, but it is more correct than fdisk and more powerful than both fdisk and cfdisk. Moreover, it can be used noninterac-tively.)
Looks like sfdik might be a good choice. I'll have to read up a bit on it.

Last edited by drkstr; 07-26-2006 at 01:30 AM.
 
Old 07-26-2006, 01:30 AM   #4
prozac
Member
 
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753

Rep: Reputation: 32
fdisk is a single program.
Code:
fdisk /dev/hdX
'p' to print out partition tables much like 'fdisk -l', 'd' to delete a partition (asks for partiton number, remember it from the fdisk -l o/p), 'n' to create a new partition, 't' to check fstype, 'l' to list available fstype.. lastly 'w' to write the new partition table. after all that use mkfs to create the fs you want in each of the newly created partitions.
 
Old 07-26-2006, 01:33 AM   #5
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
oh ok. It uses all the same commands on the CLI as it does in the utility. I'll try playing around with it a bit. I wonder why they don't put this info tin the man page.

Thanks for the help!
...drkstr
 
Old 07-26-2006, 01:51 AM   #6
prozac
Member
 
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753

Rep: Reputation: 32
are u confused here? or am i? all the commands (p,n,d,l,t ..) work once you are inside fdisk utility after doing 'fdisk /dev/hdX' and not straight from the command line.
 
Old 07-26-2006, 02:02 AM   #7
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Maybe you want to use "parted", it's got a non-interactive mode.
 
Old 07-26-2006, 02:06 AM   #8
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
Quote:
are u confused here? or am i? all the commands (p,n,d,l,t ..) work once you are inside fdisk utility after doing 'fdisk /dev/hdX' and not straight from the command line.
hmmm, I guess I misunderstood you then. This isn't what I need. I have to be able to write to the partition table from the command line. I've been reading the man page for sfdisk and it looks like this is my best bet.

Quote:
Maybe you want to use "parted", it's got a non-interactive mode.
Thanks for the tip, I will look into this one as well.

Thanks for the help everyone!
...drkstr
 
Old 07-26-2006, 02:07 AM   #9
Alien Bob
Slackware Contributor
 
Registered: Sep 2005
Location: Eindhoven, The Netherlands
Distribution: Slackware
Posts: 8,559

Rep: Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106Reputation: 8106
Prozac, I think drkstr want to know about a program that can non-interactively manipulate the partition table. With tools like fdisk and cfdisk, you will have to type in the commands yourself one by one.
IMO these are the ways to non-interactively work with your partition table (for instance from a shell script):
  1. Use fdisk with a "here document" where you state the commands you would have executed. For instance, to get the print listing that fdisk's "p" command shows, you can type:
    Code:
    fdisk /dev/hda <<EOT
    p
    EOT
    The part between the <<EOT and EOT (which has to be written on a line of it's own without preceding whitespace) is the "here-document"
  2. Use sfdisk and feed it an input file with the desired partition formatting commands. To get an idea about what this input file format looks like, use the dump command to dump your partition to stdout like this:
    Code:
    sfdisk -d /dev/hda
    Once you've written the desired partition format file, stored in sat "mynewpart.txt" you replace your current partition table with the new one:
    Code:
    sfdisk /dev/hda < mynewpart.txt

Read the man pages.

Eric
 
Old 07-26-2006, 02:14 AM   #10
prozac
Member
 
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753

Rep: Reputation: 32
@drkstr
sfdisk i think is what will give you the power and the flexibility to work non-interactively.

@Alien Bob
Thankyou for clearing out.
 
Old 07-26-2006, 02:34 AM   #11
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
Quote:
Prozac, I think drkstr want to know about a program that can non-interactively manipulate the partition table.
You would be correct. Sorry for the confusion, after re-reading my post, I realized I was not very clear on that fact.

Quote:
Use sfdisk and feed it an input file with the desired partition formatting commands. To get an idea about what this input file format looks like, use the dump command to dump your partition to stdout like this:

Code:
sfdisk -d /dev/hda
Once you've written the desired partition format file, stored in sat "mynewpart.txt" you replace your current partition table with the new one:

Code:
sfdisk /dev/hda < mynewpart.txt
Awesome, this will work very well for me since I am actually needing to do this from a perl script. I have the partition table read in as a 3-dimensenial array (an array of memory addresses (disks) which point to an array of memory addresses (partitions) wich point to an array of partition information) I will use the data structure to generate the input text which I will then feed into sfdisk via a system call. Yuck, life got so much more complicated when I stopped using windows.

thanks for the advice everyone!
...drkstr
 
Old 07-26-2006, 03:34 AM   #12
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
If you know the offsets and hex values you could always use cat or dd!
 
Old 07-26-2006, 11:52 AM   #13
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
Quote:
If you know the offsets and hex values you could always use cat or dd!
Thanks for the suggestion. I’ve actually been considering this after reading the sfdisk manpage. I must give props to the makers of sfdisk for their wonderful documentation. Not only do they explain all the sfdisk options in detail, but they go into the concepts of how partitioning works including the cylinders to write to, and the data structure used to denote each partition:
Code:
struct partition {
   unsigned char bootable;  # 0 or 0x80 
   hsc begin_hsc;
   unsigned char id;
   hsc end_hsc;
   unsigned int starting_sector;
   unsigned int nr_of_sectors;
 }
#The two hsc fields indicate head, sector and cylinder
# of the begin and the end of the partition.
I could then use ‘sfdisk -d /dev/hdX > partition.out’ to dump the partition table to a file in a readable format (or in my case I would pipe it to a file handle in perl). I could then modify the it and use dd to write it back to the disk on cylinder 0. Although it might be safer to just use ‘sfdisk /dev/hdX < partition.out’, as Alien Bob sugegsted. This way I will still get the sanity checks from sfdisk. Once I ebcome more confident in what I am doing, I can skip that step and use dd to write directly to the MBR.

I will probably completely forbar my system the first dew times, but it’s no big deal since I am using an old laptop for testing purposes.


Thank you every one for the input!
…drkstr
 
Old 07-27-2006, 12:53 AM   #14
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,291

Rep: Reputation: 62
for sfdisk you can use something like this:

Code:
sfdisk /dev/hda <<EOF
,50,S                # partition 1
,1000,L              # partition 2
;                    # partition 3
;                    # partition 4
EOF
check out man sfdisk for more info.

WARNING do not run this on you current machine it will wipe all partitions and data will be lost

Last edited by fotoguy; 07-27-2006 at 12:54 AM.
 
Old 07-27-2006, 03:47 AM   #15
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
HI. Thank you everyone for your responses. For what I am going to be doing, dumping the partition table, updating it, then writing back over the original will work the best for me. This will allow me to modify the partition table without having to reboot the system (pretty neat little trick).

Moved rest of post to it's own thread since it was sort of off-topic
http://www.linuxquestions.org/questi...newthread&f=14

**EDIT**
I just wanted to through a little warning on here. While this method might work for my purpouses, I would not recommend it for regular partitioning. There is a reason why you have to use the -f option when writing the partition table for a mounted file system.

Last edited by drkstr; 07-27-2006 at 04:29 PM.
 
  


Reply



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
You will not be able to write the partition table ulmo_55 Slackware - Installation 6 02-20-2009 04:10 AM
How write new MBR using command line only lonecrow Linux - Newbie 2 11-02-2005 10:17 AM
Write to MBR while in the command line? EThitop Linux - Newbie 10 05-11-2004 11:04 PM
cant write partition table brandonloserkid Slackware - Installation 1 01-19-2004 01:04 AM
you will not be able to write the partition table 5oh Linux - Newbie 9 01-05-2004 12:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 08:20 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