7

I have two NVMe drives in my Proxmox computers. My problem is that the drive names are not persistent and change in connection with reinstallation or reboot. The problem is not specific to NVMe drives. The same would happen if it were SATA drives. What I need is a oneline command that returns the device name of my boot drive in the format /dev/nvme* or /dev/sd*. I need to use the command in connection with setting up my Proxmox computers via Ansible.

In Ansible, I have a list of available drives in the computer. In my case with NVMe drives, the list looks like this: [/dev/nvme0n1,/dev/nvme1n1]. If I had SATA drives the list could look like this: [/dev/sda,/dev/sdb,/dev/sdc]. In connection with setting up a Ceph cluster, I need a drive list excluding my boot drive, so I need a command that for example returns /dev/sda or /dev/nvme0n1. I then use that as a mask to remove the boot drive from the list of available drives.

0

2 Answers 2

9

If I understand the question correctly, you only want the device name that is on the boot.

Try this out for /boot/efi or /boot:

lsblk -no PKNAME "$(findmnt -no SOURCE /boot/efi)"

or

lsblk -no PKNAME "$(findmnt -no SOURCE /boot/efi)"  | awk '{print "/dev/"$1}'

or

lsblk -no PKNAME "$(readlink -f "$(findmnt -no SOURCE /boot/efi)")"

The error message:

lsblk: : not a block device is also displayed when the value being searched for does not exist like /boot if /boot not exists or what ever xxxx.


I needed the exact opposite in this post, maybe that helps you to, if you fit the code to your needs.


/boot/efi partition vs /boot partition

The /boot/efi partition is used in UEFI systems and serves as the EFI System Partition (ESP). It stores EFI bootloaders and other EFI-related files...

The /boot partition is used in legacy BIOS systems. It contains the bootloader and kernel images required for the system to boot...

The /boot partition is not necessary in UEFI systems, but it can be used for storing kernel images and related files...

5
  • Let us continue this discussion in chat. Commented Aug 10 at 19:39
  • 1
    @SørenSjøstrøm What about lsblk -no PKNAME $(findmnt -no SOURCE /boot/efi) does it return anything ... It might be helpful to add the output of lsblk -e '7' to your question as well. Commented Aug 10 at 19:40
  • Yes @ReflectYourCharacter It could be an EFI system ... However in all cases the OP should be warned that the rest of the OS components could very well be on a totally different drive than the one holding the boot/ESP partition ... So he knows and takes enough measures not to break the OS thinking it's a free drive if that is the case. Commented Aug 10 at 20:00
  • @Raffa Exactly, since I have a similar case to what I described in my post. You can adjust the search term based on what you're looking for. I've only tested this on the system I currently have in front of me. In any case, as you already mentioned, it would be good if the OP could post the output of lsblk so we can see what their setup actually looks like in detail Commented Aug 10 at 20:08
  • I think for redundancy in both cases something like lsblk -no PKNAME $([ -d /boot/efi ] && findmnt -no SOURCE /boot/efi || findmnt -no SOURCE /boot) may be used as well. Commented Aug 10 at 20:55
4

The findmnt command is probably what you want.

$ declare BootDrive=$(findmnt -rno SOURCE /boot)
$ echo $BootDrive
/dev/sda2
2
  • 2
    No, unfortunately, I don't need to know the name of the partition. I only need to know the device name itself. Commented Aug 10 at 17:41
  • 2
    The device is where a directory is mounted. At the Linux level, you know the directory name; that + findmnt is how you find the device name. Unless there's something you're leaving out / not telling us. Commented Aug 10 at 17:44

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.