LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 09-12-2011, 01:13 PM   #16
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612

Try it like this:
sfdisk --force -C $cylindernum -S $sectors -H $heads $device0
 
Old 09-12-2011, 07:24 PM   #17
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Isn't a loop device already equivalent to a partition? And if so, isn't it a non-sequitur that a partition table can be written to it? I think you need something that is more like a block device, not a partition. I don't know of any such thing, but I won't say it doesn't exist.
And, doesn't the device driver for the block device abstract the physical geometry of the device into just 'blocks', so you don't have to worry about all of the OP's concerns? As I understand it, you should just have to write blocks to the device, and the geometry will look after itself.

--- rod.
 
Old 09-12-2011, 09:44 PM   #18
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by theNbomr View Post
Isn't a loop device already equivalent to a partition? And if so, isn't it a non-sequitur that a partition table can be written to it? I think you need something that is more like a block device, not a partition. I don't know of any such thing, but I won't say it doesn't exist.
And, doesn't the device driver for the block device abstract the physical geometry of the device into just 'blocks', so you don't have to worry about all of the OP's concerns? As I understand it, you should just have to write blocks to the device, and the geometry will look after itself.

--- rod.
I was wondering some of the same things. I think geometry is relevant with actual hardware, which is the ultimate target, so the image should probably have the same geometry as the drive. On the other hand, I'm not sure how you access the partitions of the loop device, unless you somehow deduce the minor numbers and then mknod them.
Kevin Barry
 
Old 09-13-2011, 02:45 AM   #19
krisweston
LQ Newbie
 
Registered: Sep 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by theNbomr View Post
Isn't a loop device already equivalent to a partition? And if so, isn't it a non-sequitur that a partition table can be written to it? I think you need something that is more like a block device, not a partition. I don't know of any such thing, but I won't say it doesn't exist.
And, doesn't the device driver for the block device abstract the physical geometry of the device into just 'blocks', so you don't have to worry about all of the OP's concerns? As I understand it, you should just have to write blocks to the device, and the geometry will look after itself.

--- rod.
if you look at my OP it has the how to of how to mount partitions out of a loop device, you can either do it with a mount or a losetup offset. you need to calculate the offset, which is the point of trying to specify geometry.. i know.... PITA

Last edited by krisweston; 09-13-2011 at 02:47 AM.
 
Old 09-13-2011, 02:47 AM   #20
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
The first sector of a disk image is the MBR -the partition table is in the last part of that. But the partitions start after that. The offset used in the OP's link is used when the partitions are being formated and/or written to -the filesystem there.

To create and/or edit the partition table, the image needs to associated to a loop device without any offset. To loop-mount to any particual partition on the image, then you need to use the proper offset, although on some systems you may get a single block device with pasrttions showing just like with a normal hard disk.

Since the OP wants to eventually dd the image to a raw device, the size and geometry can matter -especially if the raw device is not the same size as the image. Plus, assuming he wants to create a bootable device, the size, geometry and partition scheme makes a big difference as those are the things that different BIOS's look at to decide if a device looks bootable. A good example is the old zip disks -they could one of two different sizes, but what really makes them get recognized is that there is a 4th partition marked bootable.

ISO images using eltoro look like fat floppies to the BIOS. There is a tool which comes with syslinux which lets you write the first sector in a way that it can be seen as an ISO or as hard-disk. makebootfat has some other tools which make a boot-sector look bootable to many machines.
 
Old 09-14-2011, 08:56 AM   #21
krisweston
LQ Newbie
 
Registered: Sep 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
ive also posted this on another site, but nobody seems to know! mebbe ill go ask the people who make sfdisk if i can... thanks anyway for all the answers!
 
Old 09-14-2011, 10:59 AM   #22
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by krisweston View Post
if you look at my OP it has the how to of how to mount partitions out of a loop device, you can either do it with a mount or a losetup offset. you need to calculate the offset, which is the point of trying to specify geometry.. i know.... PITA
Okay, so I'm now trying to figure out whether the problem is how to create an image file with the accordant partition table, how to script that process, or how to mount partitions from an existing image file on loop devices.

I tried the example recipe from the link in article #1, and it seems to work fine, and made good sense to me. So if that is the hangup, I cannot contribute any solution there. If the objective is to mount partitions from an image of a block device (or 'raw device' as described by gnashley), then I think you should look at the tool kpartx (an unfortunate choice of name). Google will point you to some help on kpartx.

So that leaves the scripting issue, and that one is probably the most difficult to overcome. I would suggest considering a couple of different approaches to that problem. First, the expect tool, which is made for just the purpose you have, which is to supply input to a console-mode program, as if it were being typed by a human. I would consider expect to be the definitive solution in this case. There is also the possibility of of running a process insode of a Gnu screen session, and using its stuff command to provide a scripted form of input. This is conceptually similar to the expect method, and I would consider it a poor substitute. Without having looked deeply into the problem, I suspect it boils down to tools like fdisk not using stdin for its input, but instead using direct tty input, which is not readily subjected to redirection.

---- rod.

Last edited by theNbomr; 09-14-2011 at 12:31 PM.
 
Old 09-14-2011, 01:17 PM   #23
krisweston
LQ Newbie
 
Registered: Sep 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
loving your quote ive been fixing this for days now, and i have successfully broken everything

im sure its trivial to partition a loop image manually via fdisk or some such, but my problem is in scripting it. i want to know how to script putting a bunch of partitions onto a loop device in a way where i can mount the partition easily (and using this howto in the OP, which says i need to set geometry in order to accurately mount the partition) and then to be able to dd that image direct to eg /dev/sdx...

im aware of kpartx and expect but i havent tried either although ive done a lot of expect stuff with other things. with fdisk, i feel like its a little bit of a large tool for the small job.. and also ive seen a bunch of people on the web who seem to have successfully scripted fdisk and sfdisk with the here document thinger (EOF)... im sure im just not doing something right here in terms of setting the geometry. i shall look at kpartx as suggested and report back, thanks again for the help guys, i shall release this script to the community once im done, which allows you to extract, chroot and customise linux and *will* be able to write loop images for usb and cd and then write them to the preferred media, including encrypted partitions, which ive completed but this bit is basically the last step
 
Old 09-14-2011, 01:28 PM   #24
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You might look into dmsetup, although that seems like it could be a world of hurt. I've really only used it for encrypted block devices.
Kevin Barry
 
Old 09-14-2011, 01:57 PM   #25
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Okay, I think I know something about at least part of the problem. Your script didn't include the final newline, following the 'q' command. I used the example recipe from the posted link as a test to create a disk image (junktest.img), with no partition table. Then, I crafted the following recipe file (recipe.fdisk):
Code:
n
p
1
63
10000
w
q
Note the blank line at the end of the file. This is what results from a newline following the 'q'. When I feed the recipe file to fdisk stdin, it successfully creates a partition on the image file.
Code:
$ /sbin/fdisk -u -C20 -S63 -H16 ./junktest1.img < recipe.fdisk
I test the result:
Code:
$ /sbin/fdisk -u -C20 -S63 -H16 ./junktest1.img

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c').

Command (m for help): p

Disk ./junktest1.img: 0 MB, 0 bytes
16 heads, 63 sectors/track, 20 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xeb578a1f

          Device Boot      Start         End      Blocks   Id  System
./junktest1.img1              63       10000        4969   83  Linux
Partition 1 does not end on cylinder boundary.

Command (m for help): q
My image file is much smaller than the example, but the principle still seems to work.

--- rod.
 
Old 09-14-2011, 02:48 PM   #26
krisweston
LQ Newbie
 
Registered: Sep 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
hey rod, i just tried that, no joy here, same stuff, edited with vi so im sure the newline is there, same old thing though fdisk hangs there, waiting for something no idea what
Code:
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/loop0\n", 17Using /dev/loop0
)      = 17
then hangage again... looking through the strace there are lots of cuda and nvidia calls in there, dunno why.... i installed cuda a while back but nothing is running...
ill investigate this further...
 
Old 09-14-2011, 02:51 PM   #27
krisweston
LQ Newbie
 
Registered: Sep 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ta0kira View Post
You might look into dmsetup, although that seems like it could be a world of hurt. I've really only used it for encrypted block devices.
Kevin Barry
im assuming that dmsetup is for encryption stuff ? i already have the encrypted stuff working fine with lvm, its just this geometry thing...
 
Old 09-14-2011, 05:07 PM   #28
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by krisweston View Post
im assuming that dmsetup is for encryption stuff ? i already have the encrypted stuff working fine with lvm, its just this geometry thing...
It's more general than that. As far as I know, you can arbitrarily map sections of block devices so that literally any part of a disk can be treated as an independent block device. You can also combine different disks into one device. Anyway, it might make mounting the individual partitions easier.
Kevin Barry
 
Old 09-14-2011, 05:25 PM   #29
krisweston
LQ Newbie
 
Registered: Sep 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
thankyou kevin, ill try that as well and report back...
 
Old 09-15-2011, 10:21 AM   #30
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
write(1, "Using /dev/loop0\n", 17Using /dev/loop0
) = 17
You aren't trying to modify the partition table while there are mounted partitions, are you?

--- rod.
 
  


Reply

Tags
bash, eof, fdisk, script



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
fdisk ruined my partition table hefi Linux - Software 2 12-17-2009 06:26 AM
fdisk partition table representation vofka Linux - General 3 06-27-2009 03:20 PM
fdisk - invalid DOS partition table... hjogoo Linux - Hardware 12 01-21-2009 11:08 PM
fdisk will not write partition table winter_wizzerd Slackware 5 01-06-2007 11:08 AM
fdisk said my HD didn't have valid partition table??? bombman Linux - Newbie 6 08-28-2006 11:53 AM

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

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