1

I have ran this command to see what disks there are:

sudo lsblk -o NAME,HCTL,SIZE,MOUNTPOINT | grep -i "sd"

And the result is:

sda     1:0:1:0      680G 
└─sda1               680G /mnt
sdb     3:0:0:0        1T 
sdc     0:0:0:0      512G 
├─sdc1             511.9G /
├─sdc14                4M 
└─sdc15              106M /boot/efi

From this, I can see that sdb is the one I want. I will then create a partition on that one, but first I need to get it's name into a variable so I can use in the next step.

As I've deployed this VM, it seems like not always sdb is the one I need, some times it's sda, so I want to have a way to automate this.

Is there any script that helps me?

Thank you

3
  • 1
    Run a sudo blkid. You'll encounter some UUIDs. You can use them to specify a disk: udev creates symlinks of the form /dev/disk/by-uuid/<UUID>, those link to actual devices. Also if you want to access them by the path (controller, device id on that controller and so on), you can use symlinks in /dev/disk/by-path/. Now I feel you've got where to explore. Jan 15, 2021 at 16:22
  • Thanks for the response, I've come closer to what I want by running this command: sudo parted -l | grep "error" . This will output the not-parted disk, this is what I want, however, I still need to keep only the disk name and not the full output (Error: /dev/sdb: unrecognised disk label) as /dev/sdb. Any suggestions? TY
    – fabian278
    Jan 15, 2021 at 17:15
  • While this might work, it is very unreliable way. What if your virtual disk homehow has a label? What if some other disk display an "error" by the parted's consideration? I suggest to find other way to do that. Jan 15, 2021 at 18:10

4 Answers 4

0

Let's first drop the ASCII art by getting just the sd_ part of lines:

$ sudo lsblk -o NAME,HCTL,SIZE,MOUNTPOINT | grep -o sd.
sda
sda
sdb
sdc
sdc
sdc
sdc

Then remove lines that appear more than once using uniq -u:

$ sudo lsblk -o NAME,HCTL,SIZE,MOUNTPOINT | grep -o sd. | uniq -q
sdb

It's probably not the most reliable way to do this, but it gets the job done :)

Please note that more than one result can be returned. You can add head -1 to get just the first one.

0

Even though the answer from gronostaj did the trick, there might be another way to get this done, but we need to install jq (apt-get install jq -y), after that the script would look something like:

echo $(lsblk --fs --json | jq -r '.blockdevices[] | select(.children == null and .fstype == null) | .name'| grep -i "sd")

And the result in this case is: sdb as expected.

Happy coding.

0

I would suggest to write a script that will check the last column of lsblk output - for all mounted partitions you have a mount point listed there. So, if there is at least one mounted partition on a disk, this disk does not interest you. Only if there are no mount points at all, this is probably the disk you want. I guess it can be quite easily coded using eg. perl.

0

For those that have an lsblk version that doesn't support --json flag (mine is lsblk from util-linux 2.23.2), you can use the following snippet:

DEVICES=$(lsblk --noheadings -o NAME | grep "^[a-zA-Z]" | grep -v $ROOT_DEVICE)
for d in $DEVICES; do
    if [ $(lsblk --noheadings -o NAME| grep $d | wc -l) -eq 1 ]; then
        DEVICE=$d;
        break;
    fi;
done
echo "Device: $DEVICE"

You must log in to answer this question.

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