Skip to content

Commit

Permalink
Use dd and blkid to get the partition label at an offset into the image
Browse files Browse the repository at this point in the history
This shaves off around 7 seconds for fetchign partition info in my
local testing

Change-type: patch
  • Loading branch information
Page- committed Jul 23, 2021
1 parent 49a25f5 commit 92fb951
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/preload.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
from re import match, search
from retry.api import retry_call
from sh import (
blkid,
btrfs,
dd,
df,
docker as _docker,
dockerd,
file,
fsck,
losetup,
mount,
Expand Down Expand Up @@ -235,17 +235,16 @@ def __init__(
self.label = self._get_label()

def _get_label(self):
with self.losetup_context_manager() as device:
out = file("-s", device, **SH_OPTS).stdout.decode("utf8").strip()
# "label:" is for fat partitions,
# "volume name" is for ext partitions
# "BTRFS Filesystem label" is for btrfs partitions
match = search(
'(BTRFS Filesystem label|label:|volume name) "(.*)"',
out,
)
if match is not None:
return match.groups()[1].strip()
# Extract the first 4kb of the current partition from the image
ddd(_if=self.image, of="temp.img", iflag="skip_bytes", skip=self.start*512, bs=4096, count=1, status="noxfer")
# Use blkid to get the partition label from the extracted 4kb
out = blkid("temp.img", "-o", "export").stdout.decode("utf8")
match = search(
'LABEL=(.*)',
out,
)
if match is not None:
return match.groups()[0].strip()

def set_parent(self, parent):
# For logical partitions on MBR disks we store the parent extended
Expand Down

0 comments on commit 92fb951

Please sign in to comment.