From b8fb55d5c2e0433df46ac7bc3eeaea08e12d572d Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Thu, 31 Aug 2023 22:21:41 +0400 Subject: [PATCH] fix: use a mount prefix when installing a bootloader This is not a problem in general, but when running multiple image generation procedures using the same mount point is a problem. This is a no-op if `MountPrefix` is not set (when installing/upgrading vs. creating an image). Signed-off-by: Andrey Smirnov --- cmd/installer/pkg/install/install.go | 18 ++++++++++-------- .../v1alpha1/bootloader/grub/install.go | 19 ++++++++++++++----- .../v1alpha1/bootloader/options/options.go | 3 +++ .../v1alpha1/bootloader/sdboot/sdboot.go | 12 +++++++++--- pkg/imager/out.go | 5 ++++- 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/cmd/installer/pkg/install/install.go b/cmd/installer/pkg/install/install.go index 3a1ad41e3d..1d87a2452f 100644 --- a/cmd/installer/pkg/install/install.go +++ b/cmd/installer/pkg/install/install.go @@ -43,6 +43,7 @@ type Options struct { Version string BootAssets bootloaderoptions.BootAssets Printf func(string, ...any) + MountPrefix string } // Mode is the install mode. @@ -234,7 +235,7 @@ func (i *Installer) Install(ctx context.Context, mode Mode) (err error) { var mountpoint *mount.Point - mountpoint, err = mount.SystemMountPointForLabel(ctx, bd, label) + mountpoint, err = mount.SystemMountPointForLabel(ctx, bd, label, mount.WithPrefix(i.options.MountPrefix)) if err != nil { return err } @@ -262,13 +263,14 @@ func (i *Installer) Install(ctx context.Context, mode Mode) (err error) { // Install the bootloader. if err = i.bootloader.Install(bootloaderoptions.InstallOptions{ - BootDisk: i.options.Disk, - Arch: i.options.Arch, - Cmdline: i.cmdline.String(), - Version: i.options.Version, - ImageMode: mode.IsImage(), - BootAssets: i.options.BootAssets, - Printf: i.options.Printf, + BootDisk: i.options.Disk, + Arch: i.options.Arch, + Cmdline: i.cmdline.String(), + Version: i.options.Version, + ImageMode: mode.IsImage(), + MountPrefix: i.options.MountPrefix, + BootAssets: i.options.BootAssets, + Printf: i.options.Printf, }); err != nil { return err } diff --git a/internal/app/machined/pkg/runtime/v1alpha1/bootloader/grub/install.go b/internal/app/machined/pkg/runtime/v1alpha1/bootloader/grub/install.go index 7b04eca629..f252fa5c8b 100644 --- a/internal/app/machined/pkg/runtime/v1alpha1/bootloader/grub/install.go +++ b/internal/app/machined/pkg/runtime/v1alpha1/bootloader/grub/install.go @@ -35,8 +35,14 @@ func (c *Config) Install(options options.InstallOptions) error { if err := utils.CopyFiles( options.Printf, - utils.SourceDestination(options.BootAssets.KernelPath, filepath.Join(constants.BootMountPoint, string(c.Default), constants.KernelAsset)), - utils.SourceDestination(options.BootAssets.InitramfsPath, filepath.Join(constants.BootMountPoint, string(c.Default), constants.InitramfsAsset)), + utils.SourceDestination( + options.BootAssets.KernelPath, + filepath.Join(options.MountPrefix, constants.BootMountPoint, string(c.Default), constants.KernelAsset), + ), + utils.SourceDestination( + options.BootAssets.InitramfsPath, + filepath.Join(options.MountPrefix, constants.BootMountPoint, string(c.Default), constants.InitramfsAsset), + ), ); err != nil { return err } @@ -45,7 +51,7 @@ func (c *Config) Install(options options.InstallOptions) error { return err } - if err := c.Write(ConfigPath, options.Printf); err != nil { + if err := c.Write(filepath.Join(options.MountPrefix, ConfigPath), options.Printf); err != nil { return err } @@ -69,8 +75,11 @@ func (c *Config) Install(options options.InstallOptions) error { } for _, platform := range platforms { - args := []string{"--boot-directory=" + constants.BootMountPoint, "--efi-directory=" + - constants.EFIMountPoint, "--removable"} + args := []string{ + "--boot-directory=" + filepath.Join(options.MountPrefix, constants.BootMountPoint), + "--efi-directory=" + filepath.Join(options.MountPrefix, constants.EFIMountPoint), + "--removable", + } if options.ImageMode { args = append(args, "--no-nvram") diff --git a/internal/app/machined/pkg/runtime/v1alpha1/bootloader/options/options.go b/internal/app/machined/pkg/runtime/v1alpha1/bootloader/options/options.go index d327e7d036..8719161c7b 100644 --- a/internal/app/machined/pkg/runtime/v1alpha1/bootloader/options/options.go +++ b/internal/app/machined/pkg/runtime/v1alpha1/bootloader/options/options.go @@ -25,6 +25,9 @@ type InstallOptions struct { // Are we running in image mode? ImageMode bool + // Mount prefix for /boot-like partitions. + MountPrefix string + // Boot assets to install. BootAssets BootAssets diff --git a/internal/app/machined/pkg/runtime/v1alpha1/bootloader/sdboot/sdboot.go b/internal/app/machined/pkg/runtime/v1alpha1/bootloader/sdboot/sdboot.go index 6a199d5fa6..5c7df33ec1 100644 --- a/internal/app/machined/pkg/runtime/v1alpha1/bootloader/sdboot/sdboot.go +++ b/internal/app/machined/pkg/runtime/v1alpha1/bootloader/sdboot/sdboot.go @@ -140,7 +140,7 @@ func (c *Config) Install(options options.InstallOptions) error { } // list existing UKIs, and clean up all but the current one (used to boot) - files, err := filepath.Glob(filepath.Join(constants.EFIMountPoint, "EFI", "Linux", "Talos-*.efi")) + files, err := filepath.Glob(filepath.Join(options.MountPrefix, constants.EFIMountPoint, "EFI", "Linux", "Talos-*.efi")) if err != nil { return err } @@ -169,8 +169,14 @@ func (c *Config) Install(options options.InstallOptions) error { if err := utils.CopyFiles( options.Printf, - utils.SourceDestination(options.BootAssets.UKIPath, filepath.Join(constants.EFIMountPoint, "EFI", "Linux", ukiPath)), - utils.SourceDestination(options.BootAssets.SDBootPath, filepath.Join(constants.EFIMountPoint, "EFI", "boot", sdbootFilename)), + utils.SourceDestination( + options.BootAssets.UKIPath, + filepath.Join(options.MountPrefix, constants.EFIMountPoint, "EFI", "Linux", ukiPath), + ), + utils.SourceDestination( + options.BootAssets.SDBootPath, + filepath.Join(options.MountPrefix, constants.EFIMountPoint, "EFI", "boot", sdbootFilename), + ), ); err != nil { return err } diff --git a/pkg/imager/out.go b/pkg/imager/out.go index e34c8fb56b..dba3c18cec 100644 --- a/pkg/imager/out.go +++ b/pkg/imager/out.go @@ -175,6 +175,8 @@ func (i *Imager) buildImage(ctx context.Context, path string, printf func(string cmdline := procfs.NewCmdline(i.cmdline) + scratchSpace := filepath.Join(i.tempDir, "image") + opts := &install.Options{ Disk: loDevice, Platform: i.prof.Platform, @@ -190,7 +192,8 @@ func (i *Imager) buildImage(ctx context.Context, path string, printf func(string UKIPath: i.ukiPath, SDBootPath: i.sdBootPath, }, - Printf: printf, + MountPrefix: scratchSpace, + Printf: printf, } if opts.Board == "" {