Get the numeric partition index of the ESP (e.g. "1", "2"). We read `/sys/class/block/ /partition` rather than parsing device names because naming conventions vary across disk types (sd, nvme, dm, etc.). On multipath devices the sysfs `partition` attribute doesn't exist, so we fall back to the `partn` field reported by lsblk, then to parsing the partition suffix from the ESP device path rela
(&self)
| 158 | /// partition suffix from the ESP device path relative to the parent device |
| 159 | /// path (e.g. parent `/dev/mapper/mpatha`, ESP `/dev/mapper/mpatha2` → `"2"`). |
| 160 | pub fn get_esp_partition_number(&self) -> Result<String> { |
| 161 | let esp_device = self.find_partition_of_esp()?; |
| 162 | let devname = &esp_device.name; |
| 163 | |
| 164 | let partition_path = Utf8PathBuf::from(format!("/sys/class/block/{devname}/partition")); |
| 165 | if partition_path.exists() { |
| 166 | return std::fs::read_to_string(&partition_path) |
| 167 | .with_context(|| format!("Failed to read {partition_path}")); |
| 168 | } |
| 169 | |
| 170 | // On multipath the partition attribute is not existing |
| 171 | if self.is_mpath()? { |
| 172 | if let Some(partn) = esp_device.partn { |
| 173 | return Ok(partn.to_string()); |
| 174 | } |
| 175 | // Last resort: strip the parent device path from the ESP device path, |
| 176 | // then skip any non-digit separator (e.g. "p") to get the partition number. |
| 177 | // For example: parent "/dev/mapper/mpatha", ESP "/dev/mapper/mpatha2" → "2" |
| 178 | // parent "/dev/mapper/mpatha", ESP "/dev/mapper/mpathap2" → "2" |
| 179 | let parent_path = self.path(); |
| 180 | let esp_path = esp_device.path(); |
| 181 | if let Some(n) = parse_partition_number_from_suffix(&parent_path, &esp_path) { |
| 182 | return Ok(n); |
| 183 | } |
| 184 | } |
| 185 | anyhow::bail!("Not supported for {devname}") |
| 186 | } |
| 187 | |
| 188 | /// Find BIOS boot partition among children. |
| 189 | pub fn find_partition_of_bios_boot(&self) -> Option<&Device> { |
nothing calls this directly
no test coverage detected