Slackware This Forum is for the discussion of Slackware Linux.
|
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
07-26-2006, 02:08 AM
|
#1
|
Senior Member
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191
Rep:
|
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
|
|
|
07-26-2006, 02:14 AM
|
#2
|
Member
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753
Rep:
|
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.
|
|
|
07-26-2006, 02:25 AM
|
#3
|
Senior Member
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191
Original Poster
Rep:
|
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 02:30 AM.
|
|
|
07-26-2006, 02:30 AM
|
#4
|
Member
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753
Rep:
|
fdisk is a single program.
'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.
|
|
|
07-26-2006, 02:33 AM
|
#5
|
Senior Member
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191
Original Poster
Rep:
|
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
|
|
|
07-26-2006, 02:51 AM
|
#6
|
Member
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753
Rep:
|
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.
|
|
|
07-26-2006, 03:02 AM
|
#7
|
Member
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424
Rep:
|
Maybe you want to use "parted", it's got a non-interactive mode.
|
|
|
07-26-2006, 03:06 AM
|
#8
|
Senior Member
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191
Original Poster
Rep:
|
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
|
|
|
07-26-2006, 03:07 AM
|
#9
|
Slackware Contributor
Registered: Sep 2005
Location: Eindhoven, The Netherlands
Distribution: Slackware
Posts: 8,559
|
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):
- 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"
- 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:
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
|
|
|
07-26-2006, 03:14 AM
|
#10
|
Member
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753
Rep:
|
@drkstr
sfdisk i think is what will give you the power and the flexibility to work non-interactively.
@Alien Bob
Thankyou for clearing out.
|
|
|
07-26-2006, 03:34 AM
|
#11
|
Senior Member
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191
Original Poster
Rep:
|
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
|
|
|
07-26-2006, 04:34 AM
|
#12
|
Amigo developer
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928
|
If you know the offsets and hex values you could always use cat or dd!
|
|
|
07-26-2006, 12:52 PM
|
#13
|
Senior Member
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191
Original Poster
Rep:
|
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
|
|
|
07-27-2006, 01:53 AM
|
#14
|
Senior Member
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,291
Rep:
|
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 01:54 AM.
|
|
|
07-27-2006, 04:47 AM
|
#15
|
Senior Member
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191
Original Poster
Rep:
|
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 05:29 PM.
|
|
|
All times are GMT -5. The time now is 03:32 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|