4

I am looking through the various *fdisk and *parted utilities along with a few others. I am searching for a utility (or collection of utilities) that will list the size, in bytes, of all of the disks and partitions on a machine. Bonus points for any additional information on the drive or partition such as make/model/label/filesystem. It should also be able to report the size of drives with no partition table installed

  • plain fdisk
    • ✗ No GUID Partition Table support
    • ✗ Not in bytes, no flags to allow for it
  • sfdisk
    • ✗ No GUID Partition Table support
    • ✓ Flag to support Megabytes (close)
  • parted
    • ✓ Does support GPT
    • ! Option to control unit only works on one disk at a time
    • ✗ Rounds to largest possible unit
  • lshw
    • ✗ Doesn't show partition info
    • ✗ Rounds to highest unit
    • ✗ No option to control units
  • pvdisplay / pvs
    • ✗ Only works on disks that are part of LVM2 array
    • ✗ Doesn't show disk info if partition is volume used in array
    • ✓ Does have flags to set unit to bytes
Huckle
  • 7,188

1 Answers1

2

parted has a command to control units used, and it is called ... unit:

$ sudo parted /dev/sda unit B p    
Model: ATA ST500DM002-1BD14 (scsi)
Disk /dev/sda: 500107862016B
Sector size (logical/physical): 512B/4096B
Partition Table: msdos

Number  Start          End            Size           Type      File system
lags
 1      1048576B       105906175B     104857600B     primary   ntfs
oot
 2      105906176B     62914559999B   62808653824B   primary   ntfs
 3      62914560000B   95126814719B   32212254720B   primary   ext4
 4      95127862272B   500107837439B  404979975168B  extended
 5      95127863296B   127340118015B  32212254720B   logical   btrfs

From man parted:

unit unit
     Set unit as the unit to use when displaying locations and
     sizes,  and for interpreting those given by the user when
     not suffixed with an explicit unit.  unit can be  one  of
     "s"  (sectors),  "B" (bytes), "kB", "MB", "GB", "TB", "%"
     (percentage of device  size),  "cyl"  (cylinders),  "chs"
     (cylinders,  heads, sectors), or "compact" (megabytes for
     input, and a human-friendly form for output).

parted, while an excellent tool for modifying things, is somewhat deficient when it comes to presentation. lsblk is much better in this regard, it shows the relationship between the disks, partitions and constructs over the disks (such as RAID or LVM volumes).

By default, it does not show labels, and outputs most data without needing sudo; however labels need sudo. To show the disk information in bytes, one can use the -b option:

sudo lsblk -ba -o NAME,TYPE,LABEL,SIZE,MOUNTPOINT
muru
  • 197,895
  • 55
  • 485
  • 740
  • But for some reason known only to the people who wrote it, parted will do this only for one disk at a time. The -l option ignores any attempt to use the unit command. – Huckle Dec 27 '14 at 05:13
  • @Huckle the -l ignores all commands. – muru Dec 27 '14 at 05:13
  • @Huckle Anyway, parted doesn't seem to print labels. So you'll probably need to use something like lsblk, and so while you're using it, you could loop over the entries of type disk in its output. – muru Dec 27 '14 at 05:17
  • Actually looks like lsblk -ba does what I want, even tells me which volumes are used in which lvm logical volumes. Submit that as an answer and I'll accept it. – Huckle Dec 27 '14 at 06:00
  • @Huckle edited to add lsblk as well. – muru Dec 27 '14 at 08:47
  • The output format for lsblk is infuriating! The output table from either the -l or the -i invocations are not delimited, they're fixed width. That wouldn't be so bad, except that headers for purely numeric columns (size, MIN-IO) have right justified headers while all the others are left justified. This makes it nearly impossible to determine the column widths. – Huckle Dec 27 '14 at 21:02
  • @Huckle What is it you're trying to do, exactly? – muru Dec 28 '14 at 09:13
  • http://chat.stackexchange.com/rooms/19810/list-size-of-all-disks-and-partitions-in-bytes – Huckle Dec 28 '14 at 21:59
  • @Huckle Try this command if you get a chance: sudo parted <<<'unit B p all q' – muru Dec 31 '14 at 11:04
  • Works until it hits the first unpartitioned disk (or in my case the first whole-disk LVM volume) at which point it throws an error Error: /dev/sda: unrecognised disk label and stops printing. – Huckle Jan 01 '15 at 21:25