4

Suppose I have a disk located at /dev/DISK_NAME. I'm curious if there is a command to find the nth partition of this disk.

It seems like this sort of thing should be possible with lsblk. The best I can come up with is:

lsblk -nlpo NAME,TYPE /dev/DISK_NAME | awk '/part$/ {print $1}' | grep "[^0-9]n$"

Here, the n in the last grep represents the partition number in question.

The command above works for the arch linux machine I'm currently on, where DISK_NAME=sda and the output of lsblk -nlpo NAME,TYPE is

/dev/sda  disk
/dev/sda1 part
/dev/sda2 part
/dev/sda3 part
/dev/sda5 part

My command also works on my arch linux machine where DISK_NAME=nvme0n1 and the output of lsblk -nlpo NAME,TYPE is

/dev/nvme0n1   disk
/dev/nvme0n1p1 part
/dev/nvme0n1p2 part

Still, this feels rather hacky, and I'm not sure if it would work on an arbitrary disk.

Are there better ways to do this?

Julian
  • 87
  • 8
Brian Fitzpatrick
  • 2,755
  • 3
  • 23
  • 43
  • On what OS? Also, that... doesn't actually work. You seem to be expecting to find non-numeric characters and then an `n` before the end of the line. What output are you expecting? Also, what is the Nth partition? Should extended partitions count? If so, the Nth partition of sdX will always be sdXN so I don't quite see what you would need to script. – terdon Jun 11 '21 at 17:33
  • I'm on arch linux. I ran into this problem because I had a script that assumed the Nth partition of `/dev/DISK_NAME` was `/dev/DISK_NAMEN` that failed when `DISK_NAME=nvme0`. In this case, the Nth partition was `/dev/nvme0pN`. – Brian Fitzpatrick Jun 11 '21 at 17:40
  • So it sounds like my problem is that I'm ignorant of the ways disks are generally partitioned. I assumed that each partition of a disk is tagged with a number that I'm clumsily calling the "Nth partition" – Brian Fitzpatrick Jun 11 '21 at 17:43
  • Not sure if you're ignorant or if I am, but I'm pretty sure one or both of us is, yes :) You're quite right about the weird `/dev/nvme0pN` names, I've seen those but don't remember under what circumstances. I believe those are the special flash type drives some laptops have for the OS. It would probably help if you could edit and show us the output of `lsblk -nlpo NAME,TYPE /dev/nvme`. However, even in that case, why would you have the letter `n` after the number? Wouldn't the 2nd partition be `nvme0p2`? – terdon Jun 11 '21 at 17:47
  • @terdon the letter n comes after `[^0-9]`, which should match any *non-integer* character (because of the `^` symbol). – Brian Fitzpatrick Jun 11 '21 at 17:49
  • Yes, but but you are looking for a lower case `n` for some reason. Isn't the partition naming scheme `nvme`, then the disk number, then `p` and then the partition number? Like `nvme0p1`? (also see [persistent device naming for NVMe storage devices](https://unix.stackexchange.com/a/452135)) – terdon Jun 11 '21 at 17:56
  • @terdon I meant for `n` to algebraically represent the partition number in question. So replace `n` with `1` for `nvme0p1` – Brian Fitzpatrick Jun 11 '21 at 17:58
  • 1
    NVMe disks typically have a namespace ID after the NVME controller number, i.e. `/dev/nvme0` = first NVMe controller, `/dev/nvme0n1` = its first (and often only) namespace (whole "disk"), and `/dev/nvme0n1p1` = first partition of first namespace of first NVMe controller. Consumer NVMe stuff typically supports just one namespace, while enterprise-grade NVMe might support more than one. If Arch has decided to hide the namespace ID, that's a new thing and probably Arch's own idea. – telcoM Jun 11 '21 at 18:01
  • 1
    One question to make this more complicated: With `n = 1` do you want partition number 1 or first partition on the disk? Because strictly speaking `sda1` is not guaranteed to be first partition on `sda`. – Vojtech Trefny Jun 11 '21 at 18:04
  • @telcoM My mistake. On my arch system its `DISK_NAME=nvme0n1` (I forgot the `n1` when typing from my memory). – Brian Fitzpatrick Jun 11 '21 at 18:06
  • @VojtechTrefny Yeah that's what I thought. So what's the best way to determine what the "nth partition on the disk" is? – Brian Fitzpatrick Jun 11 '21 at 18:08
  • This sounds like the xy problem – symcbean Jun 11 '21 at 19:28
  • @symcbean No it is not. I want to know the name of the nth partition on my disk. – Brian Fitzpatrick Jun 11 '21 at 19:37

1 Answers1

1

There is no guarantee that sda1 exists at all - I have a SSD with only sda4 and sda5. So would the first partition be the one with the lowest start LBA?

Or perhaps the first entry in the partition table? According to parted -l,

...
Number  Start  End    Size    File system  Name                   Flags
 2      316MB  333GB  333GB   ext4         /mnt/backup
 3      333GB  791GB  457GB   ext4         /mnt/filestore
 4      791GB  832GB  41.9GB  ext4         /run/timeshift/backup
 1      832GB  841GB  8590MB  ext4         /mnt/music

Which is the first partition - is it 2 (which has the lowest LBA) or is it 1 (which has the lowest partition number).

If you prefer the ancient fdisk -l command, then this will sort the partition table for you [so that lowest numbered partitions come first].

Jeremy Boden
  • 1,290
  • 11
  • 21
  • 1
    A minor note for the sake of completeness: after you get the type of partition list you want, filter out any headings and other non-partition lines, and then you can use the old `... | head -$n | tail -1` trick to pick the nth line (`$n` is assumed to contain the number of the desired line). – telcoM Jun 11 '21 at 18:52
  • Yes, I realize now that I was making assumptions that the names of the partitions were indicative of the location on the disk which is obviously incorrect. – Brian Fitzpatrick Jun 11 '21 at 18:53
  • 1
    Now that I've read this answer and the comments, I've realized that my question should have been "how can I identify the name of the partition of `DISK_NAME` with the lowest LBA" – Brian Fitzpatrick Jun 11 '21 at 20:10