2019-03-13 16:39:10 +01:00
|
|
|
package zfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-03-27 00:57:33 +01:00
|
|
|
"context"
|
2019-03-13 16:39:10 +01:00
|
|
|
"crypto/sha512"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
2019-03-19 17:43:28 +01:00
|
|
|
const (
|
|
|
|
// For a placeholder filesystem to be a placeholder, the property source must be local,
|
|
|
|
// i.e. not inherited.
|
|
|
|
PlaceholderPropertyName string = "zrepl:placeholder"
|
|
|
|
placeholderPropertyOn string = "on"
|
|
|
|
placeholderPropertyOff string = "off"
|
|
|
|
)
|
2019-03-13 16:39:10 +01:00
|
|
|
|
2019-03-19 17:43:28 +01:00
|
|
|
// computeLegacyPlaceholderPropertyValue is a legacy-compatibility function.
|
2019-03-13 16:39:10 +01:00
|
|
|
//
|
2019-03-19 17:43:28 +01:00
|
|
|
// In the 0.0.x series, the value stored in the PlaceholderPropertyName user property
|
|
|
|
// was a hash value of the dataset path.
|
|
|
|
// A simple `on|off` value could not be used at the time because `zfs list` was used to
|
|
|
|
// list all filesystems and their placeholder state with a single command: due to property
|
|
|
|
// inheritance, `zfs list` would print the placeholder state for all (non-placeholder) children
|
|
|
|
// of a dataset, so the hash value was used to distinguish whether the property was local or
|
2019-03-13 16:39:10 +01:00
|
|
|
// inherited.
|
|
|
|
//
|
2019-03-19 17:43:28 +01:00
|
|
|
// One of the drawbacks of the above approach is that `zfs rename` renders a placeholder filesystem
|
|
|
|
// a non-placeholder filesystem if any of the parent path components change.
|
2019-03-13 16:39:10 +01:00
|
|
|
//
|
2019-03-19 17:43:28 +01:00
|
|
|
// We `zfs get` nowadays, which returns the property source, making the hash value no longer
|
|
|
|
// necessary. However, we want to keep legacy compatibility.
|
|
|
|
func computeLegacyHashBasedPlaceholderPropertyValue(p *DatasetPath) string {
|
2019-03-13 16:39:10 +01:00
|
|
|
ps := []byte(p.ToString())
|
|
|
|
sum := sha512.Sum512_256(ps)
|
|
|
|
return hex.EncodeToString(sum[:])
|
|
|
|
}
|
|
|
|
|
2019-03-19 17:43:28 +01:00
|
|
|
// the caller asserts that placeholderPropertyValue is sourceLocal
|
|
|
|
func isLocalPlaceholderPropertyValuePlaceholder(p *DatasetPath, placeholderPropertyValue string) (isPlaceholder bool) {
|
|
|
|
legacy := computeLegacyHashBasedPlaceholderPropertyValue(p)
|
|
|
|
switch placeholderPropertyValue {
|
|
|
|
case legacy:
|
|
|
|
return true
|
|
|
|
case placeholderPropertyOn:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
2019-03-13 16:39:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 17:43:28 +01:00
|
|
|
type FilesystemPlaceholderState struct {
|
|
|
|
FS string
|
|
|
|
FSExists bool
|
|
|
|
IsPlaceholder bool
|
|
|
|
RawLocalPropertyValue string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ZFSGetFilesystemPlaceholderState is the authoritative way to determine whether a filesystem
|
|
|
|
// is a placeholder. Note that the property source must be `local` for the returned value to be valid.
|
|
|
|
//
|
|
|
|
// For nonexistent FS, err == nil and state.FSExists == false
|
2020-03-27 00:57:33 +01:00
|
|
|
func ZFSGetFilesystemPlaceholderState(ctx context.Context, p *DatasetPath) (state *FilesystemPlaceholderState, err error) {
|
2019-03-19 17:43:28 +01:00
|
|
|
state = &FilesystemPlaceholderState{FS: p.ToString()}
|
|
|
|
state.FS = p.ToString()
|
2020-03-27 00:57:33 +01:00
|
|
|
props, err := zfsGet(ctx, p.ToString(), []string{PlaceholderPropertyName}, sourceLocal)
|
2019-03-19 17:43:28 +01:00
|
|
|
var _ error = (*DatasetDoesNotExist)(nil) // weak assertion on zfsGet's interface
|
|
|
|
if _, ok := err.(*DatasetDoesNotExist); ok {
|
|
|
|
return state, nil
|
2019-03-13 16:39:10 +01:00
|
|
|
} else if err != nil {
|
2019-03-19 17:43:28 +01:00
|
|
|
return state, err
|
2019-03-13 16:39:10 +01:00
|
|
|
}
|
2019-03-19 17:43:28 +01:00
|
|
|
state.FSExists = true
|
|
|
|
state.RawLocalPropertyValue = props.Get(PlaceholderPropertyName)
|
|
|
|
state.IsPlaceholder = isLocalPlaceholderPropertyValuePlaceholder(p, state.RawLocalPropertyValue)
|
|
|
|
return state, nil
|
2019-03-13 16:39:10 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 00:57:33 +01:00
|
|
|
func ZFSCreatePlaceholderFilesystem(ctx context.Context, p *DatasetPath) (err error) {
|
2019-09-07 20:01:15 +02:00
|
|
|
if p.Length() == 1 {
|
|
|
|
return fmt.Errorf("cannot create %q: pools cannot be created with zfs create", p.ToString())
|
|
|
|
}
|
2020-03-27 00:57:33 +01:00
|
|
|
cmd := exec.CommandContext(ctx, ZFS_BINARY, "create",
|
2019-03-19 17:43:28 +01:00
|
|
|
"-o", fmt.Sprintf("%s=%s", PlaceholderPropertyName, placeholderPropertyOn),
|
2019-03-13 16:39:10 +01:00
|
|
|
"-o", "mountpoint=none",
|
|
|
|
p.ToString())
|
|
|
|
|
|
|
|
stderr := bytes.NewBuffer(make([]byte, 0, 1024))
|
|
|
|
cmd.Stderr = stderr
|
|
|
|
|
|
|
|
if err = cmd.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = cmd.Wait(); err != nil {
|
|
|
|
err = &ZFSError{
|
|
|
|
Stderr: stderr.Bytes(),
|
|
|
|
WaitErr: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2019-03-13 18:33:20 +01:00
|
|
|
|
2020-03-27 00:57:33 +01:00
|
|
|
func ZFSSetPlaceholder(ctx context.Context, p *DatasetPath, isPlaceholder bool) error {
|
2019-03-13 18:33:20 +01:00
|
|
|
props := NewZFSProperties()
|
2019-03-19 17:43:28 +01:00
|
|
|
prop := placeholderPropertyOff
|
|
|
|
if isPlaceholder {
|
|
|
|
prop = placeholderPropertyOn
|
|
|
|
}
|
|
|
|
props.Set(PlaceholderPropertyName, prop)
|
2020-03-27 00:57:33 +01:00
|
|
|
return zfsSet(ctx, p.ToString(), props)
|
2019-03-19 17:43:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type MigrateHashBasedPlaceholderReport struct {
|
|
|
|
OriginalState FilesystemPlaceholderState
|
|
|
|
NeedsModification bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// fs must exist, will panic otherwise
|
2020-03-27 00:57:33 +01:00
|
|
|
func ZFSMigrateHashBasedPlaceholderToCurrent(ctx context.Context, fs *DatasetPath, dryRun bool) (*MigrateHashBasedPlaceholderReport, error) {
|
|
|
|
st, err := ZFSGetFilesystemPlaceholderState(ctx, fs)
|
2019-03-19 17:43:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting placeholder state: %s", err)
|
|
|
|
}
|
|
|
|
if !st.FSExists {
|
|
|
|
panic("inconsistent placeholder state returned: fs must exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
report := MigrateHashBasedPlaceholderReport{
|
|
|
|
OriginalState: *st,
|
|
|
|
}
|
|
|
|
report.NeedsModification = st.IsPlaceholder && st.RawLocalPropertyValue != placeholderPropertyOn
|
|
|
|
|
|
|
|
if dryRun || !report.NeedsModification {
|
|
|
|
return &report, nil
|
|
|
|
}
|
|
|
|
|
2020-03-27 00:57:33 +01:00
|
|
|
err = ZFSSetPlaceholder(ctx, fs, st.IsPlaceholder)
|
2019-03-19 17:43:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error re-writing placeholder property: %s", err)
|
|
|
|
}
|
|
|
|
return &report, nil
|
|
|
|
}
|