lsblk
If you want partition information from lsblk
:
lsblk -n -o NAME,TYPE,FSTYPE,PTTYPE
If you want to know whether a top level block device is partitioned or not, you can check the partition type, and restrict the results to skip dependent devices. (e.g. show only /dev/sda
, ignoring /dev/sda1
, /dev/sda2
, etc)
For example:
# this device has a partition table
$ lsblk -nd -o PTTYPE /dev/sda
gpt
# this device is unformatted, and does not have a partition table
$ lsblk -nd -o PTTYPE /dev/sdd
# ^ outputs blank line
# this device does not exist
$ lsblk -nd -o PTTYPE /dev/sdf
lsblk: /dev/sdf: not a block device
$ echo $? # non-zero exit code
32
To determine if a disk has a partition table using lsblk
:
$ cat test.sh
#!/bin/bash
partitioned() {
local DISK="$1"
# Define PARTITION_CHECK as local here so we can capture the exit code
# from lsblk. If we try to do it all in one go we end up capturing the
# exit code of setting the variable as local rather than the exit code
# of lsblk:
# https://unix.stackexchange.com/a/637060/
# https://tldp.org/LDP/abs/html/localvar.html
local PARTITION_CHECK
if PARTITION_CHECK=$(lsblk -nd -o PTTYPE "$DISK" 2> /dev/null)
then
if [ -z "$PARTITION_CHECK" ]
then
echo "${DISK}: Not partitioned"
elif [ -n "$PARTITION_CHECK" ]
then
echo "${DISK}: Partitioned"
fi
else
echo "${DISK}: Could not determine partition status"
fi
}
for DISK in /dev/sda /dev/sdc /dev/sdf test
do
partitioned "$DISK"
echo
done
$ ./test.sh
/dev/sda: Partitioned
/dev/sdc: Not partitioned
/dev/sdf: Could not determine partition status
test: Could not determine partition status
partx
You could also try partx
, which will return zero if a partition table exists, or non-zero if it does not. Note that it also returns non-zero for a whole bunch of other reasons (insufficient permission, non-existent device, etc), which complicates things if you want to know conclusively that a device is not partitioned:
# disk with partition table
$ sudo partx /dev/sda
NR START END SECTORS SIZE NAME UUID
1 227328 62916574 62689247 29.9G a123bcf4-5678-912d-d345-6b78f90g1234
$ echo $?
0
# unformatted disk
$ sudo partx /dev/sdd
partx: /dev/sdd: failed to read partition table
$ echo $?
1
# non-existent disk
$ sudo partx /dev/sdc
partx: stat of /dev/sdc failed: No such file or directory
$ echo $?
1
NOTE: lsblk
doesn't appear to provide filesystem or partition type information for WSL virtual filesystems. It outputs a blank line above, even when df -T
shows a filesystem type.
If you need to see some information about WSL virtual disks, parted
might help (note: sudo
required):
# WSL virtual disk
$ DEV=/dev/sda; sudo parted -ms "$DEV" print 2>/dev/null | grep "$DEV" | cut -d: -f 6
loop
# real gpt partitioned disk
$ DEV=/dev/sda; sudo parted -ms "$DEV" print 2>/dev/null | grep "$DEV" | cut -d: -f 6
gpt
# real unformatted disk
$ DEV=/dev/sdd; sudo parted -ms "$DEV" print 2>/dev/null | grep "$DEV" | cut -d: -f 6
unknown
# non-existent disk
$ DEV=/dev/sdc; sudo parted -ms "$DEV" print 2>/dev/null | grep "$DEV" | cut -d: -f 6
# no output
sfdisk -d
produces a format that is somewhat easier to parse, although still not ideal.kpartx -l
is even better in that regard, if you have it installed. There may be other utilities as well. – Vinificatorsfdisk
on RHEL 5.5 complains about GPT label. Asks me to useparted
instead. – Sprinkler