You are not logged in.

#1 2009-01-11 03:26:31

oliwer
Member
From: Paris
Registered: 2007-06-30
Posts: 153
Website

How to get a partition size ?

Do you know how to get, in bytes, the size of a partition without mounting it ?

Offline

#2 2009-01-11 03:46:43

Ranguvar
Member
Registered: 2008-08-12
Posts: 2,544

Re: How to get a partition size ?

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

#3 2009-01-11 04:02:15

dsr
Member
Registered: 2008-05-31
Posts: 187

Re: How to get a partition size ?

# parted
unit B
print

Offline

#4 2009-01-11 04:08:12

Ranguvar
Member
Registered: 2008-08-12
Posts: 2,544

Re: How to get a partition size ?

dsr wrote:
# 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

#5 2009-01-11 05:28:57

dsr
Member
Registered: 2008-05-31
Posts: 187

Re: How to get a partition size ?

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

#6 2009-01-13 13:24:56

SiC
Member
From: Liverpool, England
Registered: 2008-01-10
Posts: 430

Re: How to get a partition size ?

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

#7 2009-01-14 00:16:47

dsr
Member
Registered: 2008-05-31
Posts: 187

Re: How to get a partition size ?

SiC wrote:

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

#8 2009-01-14 00:48:50

anrxc
Member
From: Croatia
Registered: 2008-03-22
Posts: 834
Website

Re: How to get a partition size ?

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}'


You need to install an RTFM interface.

Offline

#9 2009-01-14 02:56:17

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,217
Website

Re: How to get a partition size ?

anrxc wrote:

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 wink

Offline

#10 2009-01-14 03:23:10

dsr
Member
Registered: 2008-05-31
Posts: 187

Re: How to get a partition size ?

anrxc wrote:
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 tongue 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

Board footer

Powered by FluxBB