-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: David Cassany <dcassany@suse.com>
- Loading branch information
1 parent
af937bb
commit 225b21e
Showing
8 changed files
with
242 additions
and
3 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
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,77 @@ | ||
/* | ||
Copyright © 2022 - 2023 SUSE LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package snapshotter | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/rancher/elemental-toolkit/pkg/constants" | ||
|
||
v1 "github.com/rancher/elemental-toolkit/pkg/types/v1" | ||
"github.com/rancher/elemental-toolkit/pkg/utils" | ||
) | ||
|
||
const ( | ||
loopDeviceSnapsPath = ".snapshots" | ||
loopDeviceImgName = "snapshot.img" | ||
loopDeviceWorkDir = "snapshot.workDir" | ||
loopDeviceLabelPattern = "EL_SNAP%d" | ||
loopDevicePassiveSnaps = loopDeviceSnapsPath + "/passives" | ||
) | ||
|
||
var _ v1.Snapshotter = (*LoopDevice)(nil) | ||
|
||
type LoopDevice struct { | ||
cfg v1.Config | ||
snapshotterCfg v1.SnapshotterConfig | ||
loopDevCfg v1.LoopDeviceConfig | ||
rootDir string | ||
/*currentSnapshotID int | ||
activeSnapshotID int*/ | ||
bootloader v1.Bootloader | ||
} | ||
|
||
func NewLoopDeviceSnapshotter(cfg v1.Config, snapCfg v1.SnapshotterConfig, bootloader v1.Bootloader) *LoopDevice { | ||
loopDevCfg := snapCfg.Config.(v1.LoopDeviceConfig) | ||
return &LoopDevice{cfg: cfg, snapshotterCfg: snapCfg, loopDevCfg: loopDevCfg, bootloader: bootloader} | ||
} | ||
|
||
func (l *LoopDevice) InitSnapshotter(rootDir string) error { | ||
l.cfg.Logger.Infof("Initiating a LoopDevice snapshotter at %s", rootDir) | ||
l.rootDir = rootDir | ||
return utils.MkdirAll(l.cfg.Fs, filepath.Join(rootDir, loopDevicePassiveSnaps), constants.DirPerm) | ||
} | ||
|
||
func (l *LoopDevice) StartTransaction() (*v1.Snapshot, error) { | ||
var snap *v1.Snapshot | ||
|
||
return snap, nil | ||
} | ||
|
||
func (l *LoopDevice) CloseTransactionOnError(_ *v1.Snapshot) error { | ||
var err error | ||
return err | ||
} | ||
|
||
func (l *LoopDevice) CloseTransaction(_ *v1.Snapshot) (err error) { | ||
return err | ||
} | ||
|
||
func (l *LoopDevice) DeleteSnapshot(_ int) error { | ||
var err error | ||
return err | ||
} |
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,125 @@ | ||
/* | ||
Copyright © 2022 - 2023 SUSE LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1 | ||
|
||
import ( | ||
"fmt" | ||
|
||
mapstructure "github.com/mitchellh/mapstructure" | ||
"github.com/rancher/elemental-toolkit/pkg/constants" | ||
) | ||
|
||
type Snapshotter interface { | ||
InitSnapshotter(rootDir string) error | ||
StartTransaction() (*Snapshot, error) | ||
CloseTransaction(snap *Snapshot) error | ||
CloseTransactionOnError(snap *Snapshot) error | ||
DeleteSnapshot(id int) error | ||
} | ||
|
||
type SnapshotterConfig struct { | ||
Type string `yaml:"type,omitempty" mapstructure:"type"` | ||
MaxSnaps int `yaml:"max-snaps,omitempty" mapstructure:"max-snaps"` | ||
Config interface{} `yaml:"config,omitempty" mapstructure:"config"` | ||
} | ||
|
||
type Snapshot struct { | ||
ID int | ||
MountPoint string | ||
Path string | ||
WorkDir string | ||
Label string | ||
InProgress bool | ||
} | ||
|
||
type LoopDeviceConfig struct { | ||
Size uint `yaml:"size,omitempty" mapstructure:"size"` | ||
FS string `yaml:"fs,omitempty" mapstructure:"fs"` | ||
} | ||
|
||
func NewLoopDeviceConfig() LoopDeviceConfig { | ||
return LoopDeviceConfig{ | ||
FS: constants.LinuxFs, | ||
Size: constants.ImgSize, | ||
} | ||
} | ||
|
||
type snapshotterConfFactory func(defConfig interface{}, data interface{}) (interface{}, error) | ||
|
||
func newLoopDeviceConfig(defConfig interface{}, data interface{}) (interface{}, error) { | ||
cfg, ok := defConfig.(LoopDeviceConfig) | ||
if !ok { | ||
cfg = NewLoopDeviceConfig() | ||
} | ||
return innerConfigDecoder[LoopDeviceConfig](cfg, data) | ||
} | ||
|
||
var snapshotterConfFactories = map[string]snapshotterConfFactory{} | ||
|
||
func innerConfigDecoder[T any](defaultConf T, data interface{}) (T, error) { | ||
confMap, ok := data.(map[string]interface{}) | ||
if !ok { | ||
return defaultConf, fmt.Errorf("invalid 'config' format for loopdevice type") | ||
} | ||
|
||
cfg := &mapstructure.DecoderConfig{ | ||
Result: &defaultConf, | ||
} | ||
dec, err := mapstructure.NewDecoder(cfg) | ||
if err != nil { | ||
return defaultConf, fmt.Errorf("failed creating a decoder to unmarshal a loop device snapshotter: %v", err) | ||
} | ||
err = dec.Decode(confMap) | ||
if err != nil { | ||
return defaultConf, fmt.Errorf("failed to decode loopdevice configuration, invalid format: %v", err) | ||
} | ||
return defaultConf, nil | ||
} | ||
|
||
func (c *SnapshotterConfig) CustomUnmarshal(data interface{}) (bool, error) { | ||
mData, ok := data.(map[string]interface{}) | ||
if len(mData) > 0 && ok { | ||
snaphotterType, ok := mData["type"].(string) | ||
if ok && snaphotterType != "" { | ||
c.Type = snaphotterType | ||
} else { | ||
return false, fmt.Errorf("'type' is a required field for snapshotter setup") | ||
} | ||
|
||
if mData["max-snaps"] != nil { | ||
maxSnaps, ok := mData["max-snaps"].(int) | ||
if !ok { | ||
return false, fmt.Errorf("'max-snap' must be of integer type") | ||
} | ||
c.MaxSnaps = maxSnaps | ||
} | ||
|
||
if mData["config"] != nil { | ||
factory := snapshotterConfFactories[c.Type] | ||
conf, err := factory(c.Config, mData["config"]) | ||
if err != nil { | ||
return false, fmt.Errorf("failed decoding snapshotter configuration: %v", err) | ||
} | ||
c.Config = conf | ||
} | ||
} | ||
return false, nil | ||
} | ||
|
||
func init() { | ||
snapshotterConfFactories[constants.LoopDeviceSnapshotterType] = newLoopDeviceConfig | ||
} |