writing a partition table to a loop device using a script (fdisk)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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.
writing a partition table to a loop device using a script (fdisk)
im trying to make a linux customisation script, part of it involves writing the customisation to a disk image for later use. unfortunately a lot of the how to's and information out there seems to be incorrect for my debian based system, or perhaps i just dont understand it. this has for literally three days solid eluded me, i would really really appreciate some help here. so far ive been following this howto
this suggests you want the heads to be 16 to sectors to be 63 etc...
im stuck on making this loop file be the geometry i want it to be, ive also had some weird errors complaining that 16 heads are impossible, dont know why.
so, anyway, im trying to follow this and make a loop file with the correct geometry first before i partition it for whatever scheme i use in the script, (the script needs to set multiple partition types etc, for various installs)
i also looked at a buch more links than this (literally every link to do with fdisk sfdisk parted EOF etc is purple on my browser, it seems there is nothing left on the net to describe what i want to do :/ )
after discovering parted wont let me set the geometry i moved to fdisk, it seems fdisk also wont let me set the geometry.. so what people seem to be saying works is this:
Code:
/sbin/fdisk $parameters $device << EOF
p
q
EOF
exit
fdisk *always* hangs whatever i do. im on ubuntu 11.04 atm...
it seems to me that fdisk might not be seeing a newline or return, but im not sure.
i tried to put a (slash n) after the commands, which also didnt work.
How do you plan to use the image (e.g. VM, copied to hd, looped)? I just created an image file and partitioned it in cfdisk without setting any geometry, but I'm in Slackware. Where is the image located and on what type of filesystem? Try echo p | strace fdisk /dev/loop0 to see where the hang happens. That doesn't hang for me, so if it hangs for you it might be a device problem. You might also try various blockdev queries, which also work for me.
Kevin Barry
hey ta0kira,
i plan to dd it direct to /dev/sdx when finished.
the image is located in my root/test folder and its standard linux filesystem.
i zero'd the loop file as above with settings it described in the howto. no joy.
i should mention again perhaps im trying to script it.
write(1, "Device contains neither a valid "..., 247Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
) = 247
write(1, "\n", 1
) = 1
write(1, "Using /dev/loop1\n", 17Using /dev/loop1
) = 17
ta0kira, thanks, i didnt know you could do that...
yeah, youre right, it just hangs there, so there is something up with that...
im revising the answer using sfdisk again, but im unable to create a partition table with it atm...
ill post back shortly...
the main thing im not understanding is how to build a complete image with partition table at the front
so i can mount partitions from the image with offsets...
and write the entire image *without* using offsets...
Last edited by krisweston; 09-11-2011 at 04:41 PM.
write(1, "Device contains neither a valid "..., 247Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
) = 247
write(1, "\n", 1
) = 1
write(1, "Using /dev/loop1\n", 17Using /dev/loop1
) = 17
ta0kira, thanks, i didnt know you could do that...
yeah, youre right, it just hangs there, so there is something up with that...
im revising the answer using sfdisk again, but im unable to create a partition table with it atm...
ill post back shortly...
the main thing im not understanding is how to build a complete image with partition table at the front
so i can mount partitions from the image with offsets...
and write the entire image *without* using offsets...
That's strange. I would have expected an incomplete line at the end of the output, which would indicate a hung system call, or a signal such as SIGTTIN. What is the status of the process when it hangs?
Kevin Barry
well taking a leaf out of the sfdisk manual, im confused again, i just dont see what the relation of the geometry is to mounting a partition, the whole reason im trying to do it this way is because of the howto above. perhaps its wrong... :/
i get this returned when i run the above:
as you can see its ignored my telling it to have 16 heads, which makes the settings sent to it with EOF string useless, because cylinder length is way larger.
Code:
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/loop0p1 0+ 406 407- 3269227 83 Linux
/dev/loop0p2 407 813 407 3269227+ 83 Linux
/dev/loop0p3 0 - 0 0 0 Empty
/dev/loop0p4 0 - 0 0 0 Empty
Warning: partition 1 extends past end of disk
Successfully wrote the new partition table
Re-reading the partition table ...
BLKRRPART: Invalid argument
why does it ignore my geometry ? i really cant understand how im supposed to write the partition table bit at the front....i want to dd this file direct into eg /dev/sdb and have it all bootable etc, just as though you would copy the entire disk into a file. i also cant understand what these /dev/loop0p1 partitions are doing there. it doesnt seem to follow the usual naming convention ie /dev/loop0 etc and i dont see where the begining of the disk is supposed to be displayed, so is this first partition really the first part of the disk ? or do i have to use something else to write that ? and should i be trying to mount these special partitions with kpartx or something ? or should i stay with using offsets with the mount command or losetup ? scuse my ignorance.
Last edited by krisweston; 09-11-2011 at 05:35 PM.
That's strange. I would have expected an incomplete line at the end of the output, which would indicate a hung system call, or a signal such as SIGTTIN. What is the status of the process when it hangs?
Kevin Barry
how do i find that out ? is it $? or something ?
it had no specific message at the end, it stopped on that line and i had to ctrl c to get out...
edit: doh by 'hanging' i mean - it stops responding and i have to ctrl c to get out
not an actual crash.
Last edited by krisweston; 09-11-2011 at 05:33 PM.
how do i find that out ? is it $? or something ?
it had no specific message at the end, it stopped on that line and i had to ctrl c to get out...
edit: doh by 'hanging' i mean - it stops responding and i have to ctrl c to get out
not an actual crash.
So when you Ctrl-C is the cursor at the beginning of a line in the terminal? Is the output you posted what it shows prior to Ctrl-C? The place your output stops (the line with "= 17") would be a very strange place for it to hang. As an example, type strace sleep 10. It will hang for 10s on the line "nanosleep({10, 0}, ", until nanosleep completes.
To get the status of fdisk, type ps $( pgrep fdisk ) in a separate terminal when fdisk is stuck. The "STAT" column should be something like "S+".
Kevin Barry
hey there, thanks a lot for these debugging tips, never knew how to do this, im learning more today!
(just learnt arrays and almost learnt spawning processes with FIFO files but that didnt quite work out
yes youre right cursor is on next line at left after the end of that line...
Code:
test# ps $( pgrep fdisk )
PID TTY STAT TIME COMMAND
7416 pts/4 R+ 0:04 fdisk /dev/loop1
hmmm, why do they have some many grep commands! pgrep, egrep, grep, then all the different kinds of regex in sed, awk, etc etc... mighty confusing... never seen pgrep before...
Last edited by krisweston; 09-11-2011 at 06:47 PM.
hey there, thanks a lot for these debugging tips, never knew how to do this, im learning more today!
(just learnt arrays and almost learnt spawning processes with FIFO files but that didnt quite work out
yes youre right cursor is on next line at left after the end of that line...
Code:
test# ps $( pgrep fdisk )
PID TTY STAT TIME COMMAND
7416 pts/4 R+ 0:04 fdisk /dev/loop1
hmmm, why do they have some many grep commands! pgrep, egrep, grep, then all the different kinds of regex in sed, awk, etc etc... mighty confusing... never seen pgrep before...
pgrep comes with ps and it's not directly related to grep. egrep is short for grep -E.
"R" means "running". That probably means fdisk is stuck in an infinite loop containing no system calls, which would explain your strace output. That sounds like an fdisk bug to me, unless fdisk has access to the loop without subsequent system calls (e.g. via a previous mmap), but I'm not sure if that's possible.
Kevin Barry
yeah, its probably best to use something else, i turned to fdisk out of desperation with the other things not working.. well its parted and sfdisk left now
many thanks for the grep tips!
Last edited by krisweston; 09-11-2011 at 09:33 PM.
Yeah, use sfdisk if you are scripting. But, I keep getting the idea that you aren't properly setting up your loop device for the image file. Are you offsetting with losetup before or after trying to write the partition table?
so what is wrong here ? i just dont see what the relation of the geometry is to mounting a partition, the whole reason im trying to do it this way is because of the howto above. perhaps its wrong... :/ i get this returned when i run the above: as you can see its ignored my telling it to have 16 heads, which makes the settings sent to it with EOF string useless, because cylinder length is way larger.
Code:
Disk /dev/loop0: 7899 cylinders, 255 heads, 63 sectors/track
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0
Device Boot Start End #cyls #blocks Id System
/dev/loop0p1 0+ 406 407- 3269227 83 Linux
/dev/loop0p2 407 813 407 3269227+ 83 Linux
/dev/loop0p3 0 - 0 0 0 Empty
/dev/loop0p4 0 - 0 0 0 Empty
Warning: partition 1 extends past end of disk
Successfully wrote the new partition table
Re-reading the partition table ...
BLKRRPART: Invalid argument`code`
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.