Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions kernelstub/drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
terms.
"""

import os, logging, subprocess
import os, logging, subprocess, re

class NoBlockDevError(Exception):
pass
Expand Down Expand Up @@ -104,12 +104,41 @@ def get_drive_dev(self, esp):

def get_uuid(self, path):
self.log.debug('Looking for UUID for path %s' % path)

# 1. Try findmnt for UUID directly
try:
args = ['findmnt', '-n', '-o', 'UUID', '--mountpoint', path]
result = subprocess.run(args, stdout=subprocess.PIPE)
uuid = result.stdout.decode('ASCII')
uuid = uuid.strip()
return uuid
except OSError as e:
raise UUIDNotFoundError from e
uuid = result.stdout.decode('ASCII').strip()
if uuid:
return uuid
except OSError:
pass

# 2. Try findmnt SOURCE -> blkid
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason this is being handled after the existing code? If we need to add the -n flag, we should just add it instead of failing without it and falling back on nearly identical code.

try:
args = ['findmnt', '-n', '-o', 'SOURCE', '--mountpoint', path]
result = subprocess.run(args, stdout=subprocess.PIPE)
src = result.stdout.decode('ASCII').strip()
if src:
args = ['blkid', '-s', 'UUID', '-o', 'value', src]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my machine, this command does not return anything regardless of what UUID I feed it. It just exits with status 2.

result = subprocess.run(args, stdout=subprocess.PIPE)
uuid = result.stdout.decode('ASCII').strip()
if uuid:
return uuid
except OSError:
pass

# 3. If path is root, try /proc/cmdline
if path == '/':
try:
with open("/proc/cmdline", "r") as f:
cmdline = f.read()
match = re.search(r"\broot=UUID=([0-9a-fA-F-]{36})\b", cmdline)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a good method to determine the UUID required. If kernelstub is running on a live system, this method will always fail.

if match:
return match.group(1)
except Exception:
pass

raise UUIDNotFoundError('Could not determine UUID for %s' % path)