forked from siderolabs/talos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support lvm auto-activation as per https://man7.org/linux/man-pages/man7/lvmautoactivation.7.html. This changes from how Talos previously used to unconditionally tried to activate all volume groups to based on udev events. Fixes: siderolabs#9300 Signed-off-by: Noel Georgi <[email protected]>
- Loading branch information
Showing
7 changed files
with
247 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package block | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/cosi-project/runtime/pkg/controller" | ||
"github.com/cosi-project/runtime/pkg/safe" | ||
"github.com/cosi-project/runtime/pkg/state" | ||
"github.com/siderolabs/gen/optional" | ||
"github.com/siderolabs/go-cmd/pkg/cmd" | ||
"go.uber.org/zap" | ||
|
||
"github.com/siderolabs/talos/pkg/machinery/constants" | ||
"github.com/siderolabs/talos/pkg/machinery/resources/block" | ||
runtimeres "github.com/siderolabs/talos/pkg/machinery/resources/runtime" | ||
"github.com/siderolabs/talos/pkg/machinery/resources/v1alpha1" | ||
) | ||
|
||
// LVMActivationController activates LVM volumes when they are discovered by the block.DiscoveryController. | ||
type LVMActivationController struct{} | ||
|
||
// Name implements controller.Controller interface. | ||
func (ctrl *LVMActivationController) Name() string { | ||
return "block.LVMActivationController" | ||
} | ||
|
||
// Inputs implements controller.Controller interface. | ||
func (ctrl *LVMActivationController) Inputs() []controller.Input { | ||
return []controller.Input{ | ||
{ | ||
Namespace: v1alpha1.NamespaceName, | ||
Type: runtimeres.MountStatusType, | ||
ID: optional.Some(constants.EphemeralPartitionLabel), | ||
Kind: controller.InputWeak, | ||
}, | ||
{ | ||
Namespace: block.NamespaceName, | ||
Type: block.DiscoveredVolumeType, | ||
Kind: controller.InputWeak, | ||
}, | ||
} | ||
} | ||
|
||
// Outputs implements controller.Controller interface. | ||
func (ctrl *LVMActivationController) Outputs() []controller.Output { | ||
return nil | ||
} | ||
|
||
// Run implements controller.Controller interface. | ||
// | ||
//nolint:gocyclo | ||
func (ctrl *LVMActivationController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error { | ||
touchedIDs := make(map[string]struct{}) | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-r.EventCh(): | ||
if _, err := safe.ReaderGetByID[*runtimeres.MountStatus](ctx, r, constants.EphemeralPartitionLabel); err != nil { | ||
if state.IsNotFoundError(err) { | ||
// wait for the mount status to be available | ||
continue | ||
} | ||
|
||
return fmt.Errorf("failed to get mount status: %w", err) | ||
} | ||
} | ||
|
||
discoveredVolumes, err := safe.ReaderListAll[*block.DiscoveredVolume](ctx, r) | ||
if err != nil && !state.IsNotFoundError(err) { | ||
return fmt.Errorf("failed to list discovered volumes: %w", err) | ||
} | ||
|
||
for iterator := discoveredVolumes.Iterator(); iterator.Next(); { | ||
if iterator.Value().TypedSpec().Name != "lvm2-pv" { | ||
continue | ||
} | ||
|
||
// first we check if all associated volumes are available | ||
// https://man7.org/linux/man-pages/man7/lvmautoactivation.7.html | ||
stdOut, err := cmd.RunContext(ctx, | ||
"/sbin/lvm", | ||
"pvscan", | ||
"--cache", | ||
"--verbose", | ||
"--listvg", | ||
"--checkcomplete", | ||
"--vgonline", | ||
"--autoactivation", | ||
"event", | ||
"--udevoutput", | ||
iterator.Value().TypedSpec().DevPath, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("failed to check LVM volume availability: %w", err) | ||
} | ||
|
||
if stdOut == "" || !strings.HasPrefix(stdOut, "LVM_VG_NAME_COMPLETE") { | ||
continue | ||
} | ||
|
||
vgName := strings.TrimSuffix(strings.TrimPrefix(strings.TrimSuffix(stdOut, "\n"), "LVM_VG_NAME_COMPLETE='"), "'") | ||
|
||
if _, ok := touchedIDs[vgName]; ok { | ||
continue | ||
} | ||
|
||
logger.Info("activating LVM volume", zap.String("name", vgName)) | ||
|
||
// activate the volume group | ||
if _, err = cmd.RunContext(ctx, | ||
"/sbin/lvm", | ||
"vgchange", | ||
"-aay", | ||
"--autoactivation", | ||
"event", | ||
vgName, | ||
); err != nil { | ||
return fmt.Errorf("failed to activate LVM volume %s: %w", vgName, err) | ||
} | ||
|
||
touchedIDs[vgName] = struct{}{} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters