|
22 | 22 | terms.
|
23 | 23 | """
|
24 | 24 |
|
25 |
| -import os, logging |
| 25 | +import os, logging, subprocess |
26 | 26 |
|
27 | 27 | class NoBlockDevError(Exception):
|
28 | 28 | pass
|
@@ -56,7 +56,7 @@ def __init__(self, root_path="/", esp_path="/boot/efi"):
|
56 | 56 | self.esp_fs = self.get_part_dev(self.esp_path)
|
57 | 57 | self.drive_name = self.get_drive_dev(self.esp_fs)
|
58 | 58 | self.esp_num = self.esp_fs[-1]
|
59 |
| - self.root_uuid = self.get_uuid(self.root_fs[5:]) |
| 59 | + self.root_uuid = self.get_uuid(self.root_path) |
60 | 60 | except NoBlockDevError as e:
|
61 | 61 | self.log.exception('Could not find a block device for the a ' +
|
62 | 62 | 'partition. This is a critical error and we ' +
|
@@ -102,15 +102,14 @@ def get_drive_dev(self, esp):
|
102 | 102 | self.log.debug('ESP is a partition on /dev/%s' % disk_name)
|
103 | 103 | return disk_name
|
104 | 104 |
|
105 |
| - def get_uuid(self, fs): |
106 |
| - all_uuids = os.listdir('/dev/disk/by-uuid') |
107 |
| - self.log.debug('Looking for UUID for %s' % fs) |
108 |
| - self.log.debug('List of UUIDs:\n%s' % all_uuids) |
109 |
| - |
110 |
| - for uuid in all_uuids: |
111 |
| - uuid_path = os.path.join('/dev/disk/by-uuid', uuid) |
112 |
| - if fs in os.path.realpath(uuid_path): |
113 |
| - return uuid |
| 105 | + def get_uuid(self, path): |
| 106 | + self.log.debug('Looking for UUID for path %s' % path) |
| 107 | + try: |
| 108 | + args = ['findmnt', '-n', '-o', 'uuid', '--mountpoint', path] |
| 109 | + result = subprocess.run(args, stdout=subprocess.PIPE) |
| 110 | + uuid = result.stdout.decode('ASCII') |
| 111 | + uuid = uuid.strip() |
| 112 | + return uuid |
| 113 | + except OSError as e: |
| 114 | + raise UUIDNotFoundError from e |
114 | 115 |
|
115 |
| - raise UUIDNotFoundError |
116 |
| - |
0 commit comments