You are not logged in.
Pages: 1
Should be viewable in the partition table. Assuming you use the MBR, not GPT:
dd if=/dev/sda of=mbr bs=512 count=1
That will save the partition table to a file. Obviously, change if and of for your drive name, etc.
You would need an app to read it, though. 'file' can do it. You will get your results in sectors, which must be converted (Google probably has info how, or I'm guessing you can get the ratio of total disk sectors to size of hard drive and use that to calculate the partition sizes). Note that this won't reveal logical partitions, just primary and extended.
You didn't provide info, so I don't know if you're writing a shell script, in which case you could use the above, or if you're using a full-fledged language, in which case you'd be better off using libraries...libparted probably. *shrug* I doubt libparted has LVM support... not sure about RAID or anything else like LUKS...
Last edited by Ranguvar (2009-01-11 03:53:05)
Offline
# parted
unit B
print
Offline
# parted unit B print
Interactive apps aren't really nice to get working in shell scripts, and not really at all for full-fledged programs: Libraries are the proper way there.
Offline
If he needs to obtain the size (in bytes) of a partition in a shell script, he could do something like this:
parted /dev/sdaN unit B print | tail -n2 | head -n1 | awk '{print $4}'
I'm sure that could be cleaned up quite a bit, but it would do the job. No clue how to do it without root privileges though.
Offline
An easier way would be to read the disksize from /sys/block/disk/partition/size You can then multiply this value by 512 to get the size of the partition in bytes. Has the added advantage of not requiring parted being installed.
So for /dev/sda1 you would read /sys/block/sda/sda1/size.
Simple.
Offline
An easier way would be to read the disksize from /sys/block/disk/partition/size You can then multiply this value by 512 to get the size of the partition in bytes. Has the added advantage of not requiring parted being installed.
So for /dev/sda1 you would read /sys/block/sda/sda1/size.
+1
That's a much better solution than mine. `expr 512 '*' $(cat /sys/block/sda/sdaN/size)'
Offline
parted /dev/sdaN unit B print | tail -n2 | head -n1 | awk '{print $4}'
I'm sure that could be cleaned up quite a bit, but it would do the job.
I often see people using awk as a secondary quick cleanup tool, but it's awesome and could handle it all on it's own:
$ parted /dev/sdaN unit B print | awk '{y=x; x=$4};END{print y}'
You need to install an RTFM interface.
Offline
I often see people using awk as a secondary quick cleanup tool, but it's awesome and could handle it all on it's own:
$ parted /dev/sdaN unit B print | awk '{y=x; x=$4};END{print y}'
Except for people who don't understand awk's archaic "language", the former is much easier to read while still getting the job done
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
dsr wrote:parted /dev/sdaN unit B print | tail -n2 | head -n1 | awk '{print $4}'
I'm sure that could be cleaned up quite a bit, but it would do the job.I often see people using awk as a secondary quick cleanup tool, but it's awesome and could handle it all on it's own:
$ parted /dev/sdaN unit B print | awk '{y=x; x=$4};END{print y}'
s/$/#/ since parted can only be run as root but you're right that many programmers (myself included) don't treat awk as the full-featured text processing language that it is. Like fukawi2, I've never invested the time to learn awk when shell utilities get the job done. If a more powerful programming language is called for, Python or Perl can be used in place of awk. They're not as ubiquitous on UNIX systems as awk is, but they're certainly more ubiquitously understood. I'll eventually get around to teaching myself the ins and outs of shell scripting, and when I do, I'll probably learn the basics of awk while I'm at it.
Offline
Pages: 1