Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gadget: improve device lookup, add helper for mount point lookup #6873

Merged

Conversation

bboozzoo
Copy link
Collaborator

@bboozzoo bboozzoo commented May 16, 2019

Add a helper for a filesystem structure mount point lookup. Take a live core18 system, for a structure EFI System defined like this:

volumes:
  pc:
    bootloader: grub
   ...
      - name: EFI System
        type: EF,C12A7328-F81F-11D2-BA4B-00A0C93EC93B
        filesystem: vfat
        filesystem-label: system-boot
        size: 50M
        content:
          - source: grubx64.efi
            target: EFI/boot/grubx64.efi
          - source: shim.efi.signed
            target: EFI/boot/bootx64.efi
          - source: grub.cfg
            target: EFI/ubuntu/grub.cfg

we should be able to idenfity /boot/efi as its mount point.

Improve the lookup of matching devices. Although os.FileExists() already does os.Stat() and that will catch symlinks pointing nowhere, try the belt and suspenders approach to be on the safe side should somebody tweak the implementation.

cc @cmatsuoka

Look out for symlinks that point nowhere. The osutil.FileExists() helper already
uses os.Stat() internally and that will catch symlinks pointing nowhere.
However, add a safety check in case anyone tweaks the osutil implementation.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
Add a helper for locating mount points of given filesystem structure.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
It is in Go 1.12+ only.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
Copy link
Collaborator

@zyga zyga left a comment

Choose a reason for hiding this comment

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

Looks good, I've added one comment about a case where mount entries shadow older entries. I also wonder if the kind of lookup should not belong in osutil (with a helper function to match so that PositionedVolume can stay here), simply because of the fact that looking at mount table is tricky.

return "", fmt.Errorf("cannot read mount info: %v", err)
}
for _, entry := range mountInfo {
if entry.MountSource != devpath || entry.FsType != ps.Filesystem {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would add a small snippet like this here:

if mountPoint != "" && (mountPoint == entry.MountDir || strings.HasPrefix(mountPoint, entry.MountDir + "/")) {
  // mount entry clobbers visibility to previously discovered match
  mountPoint = "" // clobbered
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There's a break couple of lines down, so the loop will finish once a match is found. This makes me think, I should restructure the loop bit to make the lookup easier to read.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Pushed an update, should be clearer now.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
Copy link
Contributor

@stolowski stolowski left a comment

Choose a reason for hiding this comment

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

+1

@bboozzoo bboozzoo merged commit 95919a3 into snapcore:master May 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants