0

We have some Linux servers with several disks.

   [root@xxxxx~]# lsblk
NAME                                    MAJ:MIN    RM   SIZE RO TYPE  MOUNTPOINT
sda                                       8:0       0 558.4G  0 disk
├─sda1                                    8:1       0   600M  0 part  /boot/efi
├─sda2                                    8:2       0     1G  0 part  /boot
└─sda3                                    8:3       0   146G  0 part
  ├─ol-root                             252:0       0    70G  0 lvm   /
  ├─ol-swap                             252:1       0    16G  0 lvm   [SWAP]
  ├─ol-tmp                              252:2       0    20G  0 lvm   /tmp
  ├─ol-var                              252:3       0    20G  0 lvm   /var
  └─ol-home                             252:4       0    20G  0 lvm   /home
sdb                                       8:16      0   100G  0 disk
└─360060e80225273005041527300002000     252:5       0   100G  0 mpath
  └─360060e80225273005041527300002000p1 252:8       0   100G  0 part
sdc                                       8:32      0   100G  0 disk
└─360060e80225273005041527300002001     252:6       0   100G  0 mpath
  └─360060e80225273005041527300002001p1 252:9       0   100G  0 part
sdd                                       8:48      0   100G  0 disk
└─360060e80225273005041527300002002     252:28      0   100G  0 mpath
  └─360060e80225273005041527300002002p1 252:32      0   100G  0 part
sde                                       8:64      0   100G  0 disk
└─360060e80225273005041527300002003     252:33      0   100G  0 mpath
  └─360060e80225273005041527300002003p1 252:36      0   100G  0 part
sdf                                       8:80      0   100G  0 disk
└─360060e80225273005041527300002004     252:35      0   100G  0 mpath
  └─360060e80225273005041527300002004p1 252:38      0   100G  0 part
sdg                                       8:96      0   100G  0 disk
└─360060e80225273005041527300002005     252:37      0   100G  0 mpath
  └─360060e80225273005041527300002005p1 252:40      0   100G  0 part
sdh                                       8:112     0   100G  0 disk
└─360060e80225273005041527300002006     252:39      0   100G  0 mpath
  └─360060e80225273005041527300002006p1 252:42      0   100G  0 part
sdi                                       8:128     0   100G  0 disk
└─360060e80225273005041527300002007     252:41      0   100G  0 mpath
  └─360060e80225273005041527300002007p1 252:44      0   100G  0 part
sdj                                       8:144     0   100G  0 disk
└─360060e80225273005041527300002008     252:43      0   100G  0 mpath
  └─360060e80225273005041527300002008p1 252:46      0   100G  0 part
sdk                                       8:160     0   100G  0 disk
└─360060e80225273005041527300002009     252:45      0   100G  0 mpath
  └─360060e80225273005041527300002009p1 252:47      0   100G  0 part
sdl                                       8:176     0   100G  0 disk
└─360060e8022527300504152730000200a     252:7       0   100G  0 mpath
  └─360060e8022527300504152730000200a1  252:11      0   100G  0 part
sdm                                       8:192     0   200G  0 disk
└─360060e8022527300504152730000200b     252:10      0   200G  0 mpath
  └─360060e8022527300504152730000200b1  252:13      0   200G  0 part
sdn                                       8:208     0   200G  0 disk
└─360060e8022527300504152730000200c     252:12      0   200G  0 mpath
  └─360060e8022527300504152730000200c1  252:15      0   200G  0 part
sdo                                       8:224     0   200G  0 disk
└─360060e8022527300504152730000200d     252:14      0   200G  0 mpath
  └─360060e8022527300504152730000200d1  252:17      0   200G  0 part
sdp                                       8:240     0     6G  0 disk
└─360060e8022527300504152730000200e     252:16      0     6G  0 mpath
  └─360060e8022527300504152730000200e1  252:19      0     6G  0 part
sdq                                      65:0       0     6G  0 disk
└─360060e8022527300504152730000200f     252:18      0     6G  0 mpath
  └─360060e8022527300504152730000200f1  252:21      0     6G  0 part
sdr                                      65:16      0     6G  0 disk
└─360060e80225273005041527300002010     252:20      0     6G  0 mpath
  └─360060e80225273005041527300002010p1 252:23      0     6G  0 part
sds                                      65:32      0   120G  0 disk
└─360060e80225273005041527300002013     252:22      0   120G  0 mpath
  └─360060e80225273005041527300002013p1 252:25      0   120G  0 part
    └─oracle-u01                        252:29      0   120G  0 lvm   /u01
sdt                                      65:48      0    10G  0 disk
└─360060e80225273005041527300002014     252:24      0    10G  0 mpath
  └─360060e80225273005041527300002014p1 252:27      0    10G  0 part
sdu                                      65:64      0   100G  0 disk
└─360060e80225273005041527300002015     252:26      0   100G  0 mpath
  └─360060e80225273005041527300002015p1 252:31      0   100G  0 part
sdv                                      65:80      0   100G  0 disk
└─360060e80225273005041527300002016     252:30      0   100G  0 mpath
  └─360060e80225273005041527300002016p1 252:34      0   100G  0 part
sdw                                      65:96      0   100G  0 disk
└─360060e80225273005041527300002000     252:5       0   100G  0 mpath
  └─360060e80225273005041527300002000p1 252:8       0   100G  0 part
sdx                                      65:112     0   100G  0 disk
└─360060e80225273005041527300002001     252:6       0   100G  0 mpath
  └─360060e80225273005041527300002001p1 252:9       0   100G  0 part
sdy                                      65:128     0   100G  0 disk
└─360060e80225273005041527300002002     252:28      0   100G  0 mpath
  └─360060e80225273005041527300002002p1 252:32      0   100G  0 part
sdz                                      65:144     0   100G  0 disk
└─360060e80225273005041527300002003     252:33      0   100G  0 mpath
  └─360060e80225273005041527300002003p1 252:36      0   100G  0 part
sdaa                                     65:160     0   100G  0 disk
└─360060e80225273005041527300002004     252:35      0   100G  0 mpath
  └─360060e80225273005041527300002004p1 252:38      0   100G  0 part
sdab                                     65:176     0   100G  0 disk
└─360060e80225273005041527300002005     252:37      0   100G  0 mpath
  └─360060e80225273005041527300002005p1 252:40      0   100G  0 part
sdac                                     65:192     0   100G  0 disk
└─360060e80225273005041527300002006     252:39      0   100G  0 mpath
  └─360060e80225273005041527300002006p1 252:42      0   100G  0 part
sdad                                     65:208     0   100G  0 disk
└─360060e80225273005041527300002007     252:41      0   100G  0 mpath
  └─360060e80225273005041527300002007p1 252:44      0   100G  0 part
sdae                                     65:224     0   100G  0 disk
└─360060e80225273005041527300002008     252:43      0   100G  0 mpath
  └─360060e80225273005041527300002008p1 252:46      0   100G  0 part
sdaf                                     65:240     0   100G  0 disk
└─360060e80225273005041527300002009     252:45      0   100G  0 mpath
  └─360060e80225273005041527300002009p1 252:47      0   100G  0 part
sdag                                     66:0       0   100G  0 disk
└─360060e8022527300504152730000200a     252:7       0   100G  0 mpath
  └─360060e8022527300504152730000200a1  252:11      0   100G  0 part
sdah                                     66:16      0   200G  0 disk
└─360060e8022527300504152730000200b     252:10      0   200G  0 mpath
  └─360060e8022527300504152730000200b1  252:13      0   200G  0 part
sdai                                     66:32      0   200G  0 disk
└─360060e8022527300504152730000200c     252:12      0   200G  0 mpath
  └─360060e8022527300504152730000200c1  252:15      0   200G  0 part
sdaj                                     66:48      0   200G  0 disk
└─360060e8022527300504152730000200d     252:14      0   200G  0 mpath
  └─360060e8022527300504152730000200d1  252:17      0   200G  0 part
sdak                                     66:64      0     6G  0 disk
└─360060e8022527300504152730000200e     252:16      0     6G  0 mpath
  └─360060e8022527300504152730000200e1  252:19      0     6G  0 part
sdal                                     66:80      0     6G  0 disk
└─360060e8022527300504152730000200f     252:18      0     6G  0 mpath
  └─360060e8022527300504152730000200f1  252:21      0     6G  0 part
sdam                                     66:96      0     6G  0 disk
└─360060e80225273005041527300002010     252:20      0     6G  0 mpath
  └─360060e80225273005041527300002010p1 252:23      0     6G  0 part
sdan                                     66:112     0   120G  0 disk
└─360060e80225273005041527300002013     252:22      0   120G  0 mpath
  └─360060e80225273005041527300002013p1 252:25      0   120G  0 part
    └─oracle-u01                        252:29      0   120G  0 lvm   /u01
sdao                                     66:128     0    10G  0 disk
└─360060e80225273005041527300002014     252:24      0    10G  0 mpath
  └─360060e80225273005041527300002014p1 252:27      0    10G  0 part
sdap                                     66:144     0   100G  0 disk
└─360060e80225273005041527300002015     252:26      0   100G  0 mpath
  └─360060e80225273005041527300002015p1 252:31      0   100G  0 part
sdaq                                     66:160     0   100G  0 disk
└─360060e80225273005041527300002016     252:30      0   100G  0 mpath
  └─360060e80225273005041527300002016p1 252:34      0   100G  0 part
sdar                                     66:176     0   100G  0 disk
└─360060e80225273005041527300002000     252:5       0   100G  0 mpath
  └─360060e80225273005041527300002000p1 252:8       0   100G  0 part
sdas                                     66:192     0   100G  0 disk
└─360060e80225273005041527300002001     252:6       0   100G  0 mpath
  └─360060e80225273005041527300002001p1 252:9       0   100G  0 part
sdat                                     66:208     0   100G  0 disk
└─360060e80225273005041527300002002     252:28      0   100G  0 mpath
  └─360060e80225273005041527300002002p1 252:32      0   100G  0 part
sdau                                     66:224     0   100G  0 disk
└─360060e80225273005041527300002003     252:33      0   100G  0 mpath
  └─360060e80225273005041527300002003p1 252:36      0   100G  0 part
sdav                                     66:240     0   100G  0 disk
└─360060e80225273005041527300002004     252:35      0   100G  0 mpath
  └─360060e80225273005041527300002004p1 252:38      0   100G  0 part
sdaw                                     67:0       0   100G  0 disk
└─360060e80225273005041527300002005     252:37      0   100G  0 mpath
  └─360060e80225273005041527300002005p1 252:40      0   100G  0 part
sdax                                     67:16      0   100G  0 disk
└─360060e80225273005041527300002006     252:39      0   100G  0 mpath
  └─360060e80225273005041527300002006p1 252:42      0   100G  0 part
sday                                     67:32      0   100G  0 disk
└─360060e80225273005041527300002007     252:41      0   100G  0 mpath
  └─360060e80225273005041527300002007p1 252:44      0   100G  0 part
sdaz                                     67:48      0   100G  0 disk
└─360060e80225273005041527300002008     252:43      0   100G  0 mpath
  └─360060e80225273005041527300002008p1 252:46      0   100G  0 part
sdba                                     67:64      0   100G  0 disk
└─360060e80225273005041527300002009     252:45      0   100G  0 mpath
  └─360060e80225273005041527300002009p1 252:47      0   100G  0 part
sdbb                                     67:80      0   100G  0 disk
└─360060e8022527300504152730000200a     252:7       0   100G  0 mpath
  └─360060e8022527300504152730000200a1  252:11      0   100G  0 part
sdbc                                     67:96      0   200G  0 disk
└─360060e8022527300504152730000200b     252:10      0   200G  0 mpath
  └─360060e8022527300504152730000200b1  252:13      0   200G  0 part
sdbd                                     67:112     0   200G  0 disk
└─360060e8022527300504152730000200c     252:12      0   200G  0 mpath
  └─360060e8022527300504152730000200c1  252:15      0   200G  0 part
sdbe                                     67:128     0   200G  0 disk
└─360060e8022527300504152730000200d     252:14      0   200G  0 mpath
  └─360060e8022527300504152730000200d1  252:17      0   200G  0 part
sdbf                                     67:144     0     6G  0 disk
└─360060e8022527300504152730000200e     252:16      0     6G  0 mpath
  └─360060e8022527300504152730000200e1  252:19      0     6G  0 part
sdbg                                     67:160     0     6G  0 disk
└─360060e8022527300504152730000200f     252:18      0     6G  0 mpath
  └─360060e8022527300504152730000200f1  252:21      0     6G  0 part
sdbh                                     67:176     0     6G  0 disk
└─360060e80225273005041527300002010     252:20      0     6G  0 mpath
  └─360060e80225273005041527300002010p1 252:23      0     6G  0 part
sdbi                                     67:192     0   120G  0 disk
└─360060e80225273005041527300002013     252:22      0   120G  0 mpath
  └─360060e80225273005041527300002013p1 252:25      0   120G  0 part
    └─oracle-u01                        252:29      0   120G  0 lvm   /u01
sdbj                                     67:208     0    10G  0 disk
└─360060e80225273005041527300002014     252:24      0    10G  0 mpath
  └─360060e80225273005041527300002014p1 252:27      0    10G  0 part
sdbk                                     67:224     0   100G  0 disk
└─360060e80225273005041527300002015     252:26      0   100G  0 mpath
  └─360060e80225273005041527300002015p1 252:31      0   100G  0 part
sdbl                                     67:240     0   100G  0 disk
└─360060e80225273005041527300002016     252:30      0   100G  0 mpath
  └─360060e80225273005041527300002016p1 252:34      0   100G  0 part
sdbm                                     68:0       0   100G  0 disk
└─360060e80225273005041527300002000     252:5       0   100G  0 mpath
  └─360060e80225273005041527300002000p1 252:8       0   100G  0 part
sdbn                                     68:16      0   100G  0 disk
└─360060e80225273005041527300002001     252:6       0   100G  0 mpath
  └─360060e80225273005041527300002001p1 252:9       0   100G  0 part
sdbo                                     68:32      0   100G  0 disk
└─360060e80225273005041527300002002     252:28      0   100G  0 mpath
  └─360060e80225273005041527300002002p1 252:32      0   100G  0 part
sdbp                                     68:48      0   100G  0 disk
└─360060e80225273005041527300002003     252:33      0   100G  0 mpath
  └─360060e80225273005041527300002003p1 252:36      0   100G  0 part
sdbq                                     68:64      0   100G  0 disk
└─360060e80225273005041527300002004     252:35      0   100G  0 mpath
  └─360060e80225273005041527300002004p1 252:38      0   100G  0 part
sdbr                                     68:80      0   100G  0 disk
└─360060e80225273005041527300002005     252:37      0   100G  0 mpath
  └─360060e80225273005041527300002005p1 252:40      0   100G  0 part
sdbs                                     68:96      0   100G  0 disk
└─360060e80225273005041527300002006     252:39      0   100G  0 mpath
  └─360060e80225273005041527300002006p1 252:42      0   100G  0 part
sdbt                                     68:112     0   100G  0 disk
└─360060e80225273005041527300002007     252:41      0   100G  0 mpath
  └─360060e80225273005041527300002007p1 252:44      0   100G  0 part
sdbu                                     68:128     0   100G  0 disk
└─360060e80225273005041527300002008     252:43      0   100G  0 mpath
  └─360060e80225273005041527300002008p1 252:46      0   100G  0 part
sdbv                                     68:144     0   100G  0 disk
└─360060e80225273005041527300002009     252:45      0   100G  0 mpath
  └─360060e80225273005041527300002009p1 252:47      0   100G  0 part
sdbw                                     68:160     0   100G  0 disk
└─360060e8022527300504152730000200a     252:7       0   100G  0 mpath
  └─360060e8022527300504152730000200a1  252:11      0   100G  0 part
sdbx                                     68:176     0   200G  0 disk
└─360060e8022527300504152730000200b     252:10      0   200G  0 mpath
  └─360060e8022527300504152730000200b1  252:13      0   200G  0 part
sdby                                     68:192     0   200G  0 disk
└─360060e8022527300504152730000200c     252:12      0   200G  0 mpath
  └─360060e8022527300504152730000200c1  252:15      0   200G  0 part
sdbz                                     68:208     0   200G  0 disk
└─360060e8022527300504152730000200d     252:14      0   200G  0 mpath
  └─360060e8022527300504152730000200d1  252:17      0   200G  0 part
sdca                                     68:224     0     6G  0 disk
└─360060e8022527300504152730000200e     252:16      0     6G  0 mpath
  └─360060e8022527300504152730000200e1  252:19      0     6G  0 part
sdcb                                     68:240     0     6G  0 disk
└─360060e8022527300504152730000200f     252:18      0     6G  0 mpath
  └─360060e8022527300504152730000200f1  252:21      0     6G  0 part
sdcc                                     69:0       0     6G  0 disk
└─360060e80225273005041527300002010     252:20      0     6G  0 mpath
  └─360060e80225273005041527300002010p1 252:23      0     6G  0 part
sdcd                                     69:16      0   120G  0 disk
└─360060e80225273005041527300002013     252:22      0   120G  0 mpath
  └─360060e80225273005041527300002013p1 252:25      0   120G  0 part
    └─oracle-u01                        252:29      0   120G  0 lvm   /u01
sdce                                     69:32      0    10G  0 disk
└─360060e80225273005041527300002014     252:24      0    10G  0 mpath
  └─360060e80225273005041527300002014p1 252:27      0    10G  0 part
sdcf                                     69:48      0   100G  0 disk
└─360060e80225273005041527300002015     252:26      0   100G  0 mpath
  └─360060e80225273005041527300002015p1 252:31      0   100G  0 part
sdcg                                     69:64      0   100G  0 disk
└─360060e80225273005041527300002016     252:30      0   100G  0 mpath
  └─360060e80225273005041527300002016p1 252:34      0   100G  0 part
sdch                                     69:80      0   400G  0 disk
└─360060e80225273005041527300000007     252:48      0   400G  0 mpath
  └─360060e80225273005041527300000007p1 252:51      0   400G  0 part
sdci                                     69:96      0   400G  0 disk
└─360060e80225273005041527300000008     252:49      0   400G  0 mpath
  └─360060e80225273005041527300000008p1 252:50      0   400G  0 part
sdcj                                     69:112     0   400G  0 disk
└─360060e80225273005041527300000007     252:48      0   400G  0 mpath
  └─360060e80225273005041527300000007p1 252:51      0   400G  0 part
sdck                                     69:128     0   400G  0 disk
└─360060e80225273005041527300000008     252:49      0   400G  0 mpath
  └─360060e80225273005041527300000008p1 252:50      0   400G  0 part
sdcl                                     69:144     0   400G  0 disk
└─360060e80225273005041527300000007     252:48      0   400G  0 mpath
  └─360060e80225273005041527300000007p1 252:51      0   400G  0 part
sdcm                                     69:160     0   400G  0 disk
└─360060e80225273005041527300000008     252:49      0   400G  0 mpath
  └─360060e80225273005041527300000008p1 252:50      0   400G  0 part
sdcn                                     69:176     0   400G  0 disk
└─360060e80225273005041527300000007     252:48      0   400G  0 mpath
  └─360060e80225273005041527300000007p1 252:51      0   400G  0 part
sdco                                     69:192     0   400G  0 disk
└─360060e80225273005041527300000008     252:49      0   400G  0 mpath
  └─360060e80225273005041527300000008p1 252:50      0   400G  0 part
asm/acfsvol001-308                      250:157697  0   454G  0 disk  /ORACLE_BKP

Is there a command to check the total size (sum) of all disks? We only want the disk sizes, excluding the multipath paths and partitions size, to avoid redundancy and wrong size (in this case, only the size of sda, sdb etc..., without the partitions sda1,sda2 etc... and without the paths like 360060e80225273005041527300002000 and 360060e80225273005041527300002000p1 etc...).

For example, in my sever only have 3 disks of 6GBs

[root@xxxxxx~]# multipath -ll|grep '6.0G'
size=6.0G features='0' hwhandler='0' wp=rw
size=6.0G features='0' hwhandler='0' wp=rw
size=6.0G features='0' hwhandler='0' wp=rw

but lsblks report 6GB 36 times

Update:

I've tried the following script, but the size is wrong

# Lista todos os discos com TYPE "disk" e calcula o armazenamento total

total_storage=$(lsblk -b -o NAME,SIZE,TYPE,MOUNTPOINT | awk '$3 == "disk" {total += $2} END {print total / (1024^3)}')
 
# Converte o total para T se for maior que 1000G e limita a 3 casas decimais
if (( $(echo "$total_storage > 1000" | bc -l) )); then
    total_storage_tb=$(echo "scale=3; $total_storage / 1024" | bc -l)
    echo "Armazenamento total de discos: ${total_storage_tb}T"
else
    total_storage_gb=$(printf "%.3f" "$total_storage")
    echo "Armazenamento total de discos: ${total_storage_gb}G"
fi
8
  • what have you tried so far? what's the expected result for the sample you've provided?
    – markp-fuso
    Commented Dec 11 at 17:06
  • I've added an update
    – Astora
    Commented Dec 11 at 17:08
  • 1
    in the latest update you state the size is wrong but you don't tell us a) the wrong size generated by your code b) the expected 'correct' size; please update the question with these additional details
    – markp-fuso
    Commented Dec 11 at 17:15
  • 1
    you've provided output from one lsblk (sans args) call while the code is running a different lsblk call; please update the question to either a) show the output from your actual lsblk -b -o NAME,SIZE,TYPE,MOUNTPOINT call or b) provide the actual code you're using to parse the sample data set (ie, lsblk sans all args)
    – markp-fuso
    Commented Dec 11 at 17:18
  • 1
    If looks like you might have multipath enabled so devices may show up twice in lsblk. Is your sample output complete or do you have duplicates?
    – doneal24
    Commented Dec 11 at 18:06

2 Answers 2

1

It seems like you can detect the multipathed disks by comparing the NAMEs in the lines of TYPE mpath (for eg. 360060e80225273005041527300000007 appears four times).

So, the only fields you need from lsblk should be the NAME of the device, its SIZE (in bytes) and its TYPE; but lsblk -nlb -o name,size,type obfuscates the disk => mpath associations, so you have to parse the "almost default" output of lsblk, for eg. with awk:

lsblk -nb |
awk '
    $6 == "disk"  { disk = $1; size[disk] = $4+0 }
    $6 == "mpath" && seen[$1]++ { delete size[disk] }
    END {
        for (disk in size)
            bytes += size[disk]
        gibibytes = bytes / 1024^3
        printf("%.03f%s\n", \
            (gibibytes >= 1024 ? gibibytes / 1024 : gibibytes), \
            (gibibytes >= 1024 ? "T" : "G") \
        )
    }
'

Given your sample input, the output should be 3.770TiB


Aside:

Your lsblk might be able to output JSON with the -J option. In that case it'll be more robust to parse and implement the logic with jq.

8
  • The result seems wrong, A test server has around 2.7TB and it report 2.2TB
    – Astora
    Commented Dec 11 at 18:44
  • @Astora I updated my answer with something that should work now
    – Fravadona
    Commented Dec 11 at 22:45
  • The result was 11.723TiB
    – Astora
    Commented Dec 12 at 11:56
  • @Astora I tested a similar code on your sample output of lsblk; 3.770TiB was the result. I can provide that code, or you can provide the output of lsblk -l in your question so I can fix my answer.
    – Fravadona
    Commented Dec 12 at 12:05
  • My output of lsblk -nlb -o name,size,type is dontpad.com/787967
    – Astora
    Commented Dec 12 at 13:48
1

man lsblk

lsblk -o size --nodeps --bytes --noheadings

will give you a single column of numbers, in bytes, with --nodeps giving you those numbers only for sda sdb without the partitions or other stuff.

then just sum that one single column of numbers, and divide to your desire into kb,mb,gb,tb

validate via lsblk -o size,fstype,model,name,serial,uuid

2
  • Not working for multipath devices, it show all paths
    – Astora
    Commented Dec 11 at 18:43
  • try this lsblk -o size --nodeps --bytes --noheadings -E 3600 Commented Dec 11 at 19:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .