|
| 1 | +// This file is part of MinIO DirectPV |
| 2 | +// Copyright (c) 2024 MinIO, Inc. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + |
| 23 | + directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types" |
| 24 | + "github.com/minio/directpv/pkg/client" |
| 25 | + drivepkg "github.com/minio/directpv/pkg/drive" |
| 26 | + "github.com/minio/directpv/pkg/types" |
| 27 | + "github.com/spf13/cobra" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + forceFlag = false |
| 33 | + disablePrefetchFlag = false |
| 34 | + dryRunFlag = false |
| 35 | +) |
| 36 | + |
| 37 | +var repairCmd = &cobra.Command{ |
| 38 | + Use: "repair <DRIVE-ID>", |
| 39 | + Short: "Start drive repair.", |
| 40 | + SilenceUsage: true, |
| 41 | + SilenceErrors: true, |
| 42 | + RunE: func(c *cobra.Command, args []string) error { |
| 43 | + switch len(args) { |
| 44 | + case 0: |
| 45 | + return errors.New("DRIVE-ID must be provided") |
| 46 | + case 1: |
| 47 | + default: |
| 48 | + return errors.New("only one DRIVE-ID must be provided") |
| 49 | + } |
| 50 | + return startRepair(c.Context(), args[0]) |
| 51 | + }, |
| 52 | +} |
| 53 | + |
| 54 | +func init() { |
| 55 | + repairCmd.PersistentFlags().BoolVar(&forceFlag, "force", forceFlag, "Force log zeroing") |
| 56 | + repairCmd.PersistentFlags().BoolVar(&disablePrefetchFlag, "disable-prefetch", disablePrefetchFlag, "Disable prefetching of inode and directory blocks") |
| 57 | + repairCmd.PersistentFlags().BoolVar(&dryRunFlag, "dry-run", dryRunFlag, "No modify mode") |
| 58 | +} |
| 59 | + |
| 60 | +func startRepair(ctx context.Context, driveID string) error { |
| 61 | + var cancel context.CancelFunc |
| 62 | + ctx, cancel = context.WithCancel(ctx) |
| 63 | + defer cancel() |
| 64 | + |
| 65 | + drive, err := client.DriveClient().Get(ctx, driveID, metav1.GetOptions{}) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + if drive.Status.Status != directpvtypes.DriveStatusRepairing { |
| 71 | + drive.Status.Status = directpvtypes.DriveStatusRepairing |
| 72 | + } |
| 73 | + |
| 74 | + updatedDrive, err := client.DriveClient().Update(ctx, drive, metav1.UpdateOptions{TypeMeta: types.NewDriveTypeMeta()}) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + return drivepkg.Repair(ctx, updatedDrive, forceFlag, disablePrefetchFlag, dryRunFlag) |
| 80 | +} |
0 commit comments