-1

Edit:
I have several machine and I need to get the total storage size of the main disk. The problem I have is that on some machine my disk have the name /dev/sda or /dev/md126p3. How could I reliably get the size of my disk on all those system.

Machine 1:

root@maximus:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/md126p3     20G  3,6G   15G  20% /
/dev/md126p2   1008M   89M  868M  10% /boot
/dev/md126p1     99M  238K   99M   1% /boot/efi

Machine 2:

root@firefly:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        30G  2,8G   26G  10% /
/dev/sda2      1008M   61M  896M   7% /boot
/dev/sda1        20M  132K   20M   1% /boot/efi

Machine 3:

root@backup:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/md0        7,3T  297G  6,6T   5% /
/dev/md1        733M   57M  623M   9% /boot

Machine 4:

root@vmfarm1:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/md126p3     18G  8,0G  9,3G  47% /
/dev/md126p2    190M   87M   90M  50% /boot
/dev/md126p1    119M  121K  119M   1% /boot/efi

Machine 5:

root@p12:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/dm-1       6.3T  4.8T  1.3T  80% /
/dev/sda2       237M   60M  166M  27% /boot

Machine 6:

root@accountant:~# df -h
Filesystem            Size  Used Avail Use% Mounted on
rootfs                 20G  415M   19G   3% /
/dev/mapper/acc-root   20G  415M   19G   3% /
/dev/md0             1012M   70M  891M   8% /boot
/dev/mapper/acc-home   20G  313M   19G   2% /home
/dev/mapper/acc-opt    20G  6.2G   13G  33% /opt
/dev/mapper/acc-usr    20G  1.2G   18G   7% /usr
/dev/mapper/acc-var    20G  802M   18G   5% /var
/dev/mapper/acc-tmp    20G  172M   19G   1% /tmp

Machine 7:

root@pve:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/dm-0        95G   74G   16G  83% /
/dev/fuse        30M   16K   30M   1% /etc/pve

Machine 8:

root@barclay:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/md1p1      2.6T   77G  2.4T   4% /
/dev/md0        186M   36M  141M  21% /boot

Old question:

Using fdisk -l i am trying to get the full Sectors value. Using Debian OS. fdisk -l will show you alot of info. I am in need of figuring out a way to cut a specific word/number out of it. Basically, i need the biggest "Sectors" count there is. For my current machine.

 Device      Start         End     Sectors   Size Type
/dev/sda1    3072        6143        3072   1.5M BIOS boot
/dev/sda2    6144      506879      500736 244.5M Linux filesystem
/dev/sda3  506880 14064672767 14064165888   6.6T Linux filesystem

(This is also different on my other machines)

Then

 fdisk -l | grep Linux | tail -1 

Gives me /dev/sda3 506880 14064672767 14064165888 6.6T Linux filesystem Yet this is not the total that i need. I managed to cut this out on one of my machines, but i was not able to find a way to cut/grep out the "4th from the end"

On my test machine, i managed to this.

fdisk -l | grep Linux | cut -d " " -f3 | tail -1 | awk '{$1=$1/(1024^2); print $1;}' | awk '{print int($1+0.5)}' | awk '{ print $1"GB" }' 
Kiwy
  • 9,534
TheSebM8
  • 459
  • 1
    This is a completly different question from original. I am not sure what you meant by main disk. the disk that hold /? what if / is part of a 4 disk volume group ? I'll Vote To Close as "too broad". – Archemar Mar 27 '18 at 13:16
  • The whole disk or just the root partition/filesystem? – Jeff Schaller Mar 27 '18 at 15:11
  • The Whole, but someone linked me a post from 2010. There was a cat /proc/partitions/ and it be doing what i need. Sadly, some machines have other drives like md also, so yea – TheSebM8 Mar 28 '18 at 06:33

4 Answers4

2

If you want to get the size of the disk you can take the value:

cat /sys/block/sda/size

To identify the main partition I would say you can do something like this :

 df | grep -E "\/$" | cut -d' ' -f1 | sed 's/[0-9]*//g'

This will give you the name of the disk mounted / in my case /dev/sda.
After up to you how you want to script it.

Old answer

In this case, I would simply use /sys:

cat /sys/block/sda/subsystem/sda3/size 

which gives numbers of sectors and I would use :

cat /sys/block/sda/subsystem/sda3/start

to get the start sector number.

It would be way easier than parsing fdisk which is not always exactly the same. It would be easy to get those value with a simple pattern:

cat /sys/block/sda/subsystem/sda*/size
Kiwy
  • 9,534
  • Actually this is Good. Any cue how i could probably add all the numbers up into one? – TheSebM8 Mar 27 '18 at 11:54
  • Edit: found this way cat /sys/block/sda/subsystem/sda*/size | paste -sd+ -|bc – TheSebM8 Mar 27 '18 at 11:58
  • I think using sys instead of parsing is way better that's why you cleary express your goal in a question and not only the technical problem your working on. Sometime better solution exists and you're heading in the wrong direction – Kiwy Mar 27 '18 at 12:04
  • What if my machine uses other /dev/sda's also? like vda or even something else – TheSebM8 Mar 27 '18 at 12:12
  • other sata disk will appear as /dev/sdb you could use cat /sys/block/sd*/subsystem/sd*/size – Kiwy Mar 27 '18 at 12:14
  • For an example, i have the following in use: md126p* sdc* sdb* Probably afew more example could be given. – TheSebM8 Mar 27 '18 at 12:16
  • add df -h to your question and edit it to reflect your real question so we can answer in one time. – Kiwy Mar 27 '18 at 12:18
  • Added all 8 df -hs here. – TheSebM8 Mar 27 '18 at 12:30
1

try

fdisk -l | awk '/Linux/ { s=$(NF-4) ;} END { print s}'

where

  • /Linux/ will select Linux line
  • { s=$(NF-4) ;} set 4th field from last (you might also use $4 in this case.
  • END at the end (no more input)
  • { print s} print it.

Edit:

  • your grep ..| cut .. | awk^n code can be shortern to

    awk '/Linux/ { s=$4/(1024^2)  ;} END { printf "%d Gb\n",s}'
    
  • sector are usualy 512 bytes, you may need to add another /2 (divide by 2) to get Gb.

Archemar
  • 31,554
1

Using :

To get the highest sector value :

fdisk -l | awk '$6=="Linux" && v < $4{v=$4}END{print v}' 
0

instead of passing the block device to df you can also pass the mount point. actually that's the default behaviour

# df /
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/root        8191416 6002396   2088840  75% /

source: df manpage:

df displays the amount of disk space available on the file system containing each file name argument.