-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add device wipe and partition devname
Device wipe mostly copied from v1 library with some fixes. Partition devname was in Talos PR, but it should be better here. Signed-off-by: Andrey Smirnov <[email protected]>
- Loading branch information
Showing
8 changed files
with
302 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// 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 ( | ||
"io" | ||
"os" | ||
"runtime" | ||
"unsafe" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
const ( | ||
// FastWipeRange fast wipe block. | ||
FastWipeRange = 1024 * 1024 | ||
) | ||
|
||
// Wipe the device contents. | ||
// | ||
// In order of availability this tries to perform the following: | ||
// - secure discard (secure erase) | ||
// - discard with zeros | ||
// - zero out via ioctl | ||
// - zero out from userland | ||
func (d *Device) Wipe() (string, error) { | ||
size, err := d.GetSize() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return d.WipeRange(0, size) | ||
} | ||
|
||
// FastWipe the device contents. | ||
// | ||
// This method is much faster than Wipe(), but it doesn't guarantee | ||
// that device will be zeroed out completely. | ||
func (d *Device) FastWipe() error { | ||
size, err := d.GetSize() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// BLKDISCARD is implemented via TRIM on SSDs, it might or might not zero out device contents. | ||
r := [2]uint64{0, size} | ||
|
||
// ignoring the error here as DISCARD might be not supported by the device | ||
unix.Syscall(unix.SYS_IOCTL, d.f.Fd(), unix.BLKDISCARD, uintptr(unsafe.Pointer(&r[0]))) //nolint: errcheck | ||
|
||
// zero out the first N bytes of the device to clear any partition table | ||
wipeLength := min(size, uint64(FastWipeRange)) | ||
|
||
_, err = d.WipeRange(0, wipeLength) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// wipe the last FastWipeRange bytes of the device as well | ||
if size >= FastWipeRange*2 { | ||
_, err = d.WipeRange(size-FastWipeRange, FastWipeRange) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// WipeRange the device [start, start+length). | ||
func (d *Device) WipeRange(start, length uint64) (string, error) { | ||
r := [2]uint64{start, length} | ||
|
||
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, d.f.Fd(), unix.BLKSECDISCARD, uintptr(unsafe.Pointer(&r[0]))); errno == 0 { | ||
runtime.KeepAlive(d) | ||
|
||
return "blksecdiscard", nil | ||
} | ||
|
||
var zeroes int | ||
|
||
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, d.f.Fd(), unix.BLKDISCARDZEROES, uintptr(unsafe.Pointer(&zeroes))); errno == 0 && zeroes != 0 { | ||
if _, _, errno = unix.Syscall(unix.SYS_IOCTL, d.f.Fd(), unix.BLKDISCARD, uintptr(unsafe.Pointer(&r[0]))); errno == 0 { | ||
runtime.KeepAlive(d) | ||
|
||
return "blkdiscardzeros", nil | ||
} | ||
} | ||
|
||
if _, _, errno := unix.Syscall(unix.SYS_IOCTL, d.f.Fd(), unix.BLKZEROOUT, uintptr(unsafe.Pointer(&r[0]))); errno == 0 { | ||
runtime.KeepAlive(d) | ||
|
||
return "blkzeroout", nil | ||
} | ||
|
||
zero, err := os.Open("/dev/zero") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
defer zero.Close() //nolint: errcheck | ||
|
||
_, err = io.CopyN(d.f, zero, int64(r[1])) | ||
|
||
return "writezeroes", 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// 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_test | ||
|
||
import ( | ||
"crypto/rand" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/freddierice/go-losetup/v2" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/siderolabs/go-blockdevice/v2/block" | ||
) | ||
|
||
func TestDeviceWipe(t *testing.T) { | ||
if os.Geteuid() != 0 { | ||
t.Skip("skipping test; must be root") | ||
} | ||
|
||
tmpDir := t.TempDir() | ||
|
||
rawImage := filepath.Join(tmpDir, "image.raw") | ||
|
||
f, err := os.Create(rawImage) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, f.Truncate(int64(2*GiB))) | ||
|
||
t.Cleanup(func() { | ||
require.NoError(t, f.Close()) | ||
}) | ||
|
||
var loDev losetup.Device | ||
|
||
loDev, err = losetup.Attach(rawImage, 0, false) | ||
require.NoError(t, err) | ||
|
||
t.Cleanup(func() { | ||
assert.NoError(t, loDev.Detach()) | ||
}) | ||
|
||
devPath := loDev.Path() | ||
|
||
devWhole, err := block.NewFromPath(devPath, block.OpenForWrite()) | ||
require.NoError(t, err) | ||
|
||
t.Cleanup(func() { | ||
assert.NoError(t, devWhole.Close()) | ||
}) | ||
|
||
magic := make([]byte, 1024) | ||
|
||
_, err = io.ReadFull(rand.Reader, magic) | ||
require.NoError(t, err) | ||
|
||
_, err = f.WriteAt(magic, 0) | ||
require.NoError(t, err) | ||
|
||
_, err = f.WriteAt(magic, 10*MiB) | ||
require.NoError(t, err) | ||
|
||
method, err := devWhole.Wipe() | ||
require.NoError(t, err) | ||
|
||
t.Logf("wipe method: %s", method) | ||
|
||
assertZeroed(t, f, 0, 1024) | ||
assertZeroed(t, f, 10*MiB, 1024) | ||
|
||
_, err = f.WriteAt(magic, 0) | ||
require.NoError(t, err) | ||
|
||
_, err = f.WriteAt(magic, 2*GiB-1024) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, devWhole.FastWipe()) | ||
|
||
assertZeroed(t, f, 0, 1024) | ||
assertZeroed(t, f, 2*GiB-1024, 1024) | ||
} | ||
|
||
func assertZeroed(t *testing.T, f *os.File, offset, length int64) { //nolint:unparam | ||
t.Helper() | ||
|
||
buf := make([]byte, length) | ||
|
||
_, err := f.ReadAt(buf, offset) | ||
require.NoError(t, err) | ||
|
||
for i, b := range buf { | ||
if b != 0 { | ||
t.Fatalf("expected zero at offset %d, got %d", offset+int64(i), b) | ||
} | ||
} | ||
} |
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,43 @@ | ||
// 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 partitioning_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/siderolabs/go-blockdevice/v2/partitioning" | ||
) | ||
|
||
func TestDevName(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, test := range []struct { //nolint:govet | ||
devname string | ||
partition uint | ||
|
||
expected string | ||
}{ | ||
{ | ||
devname: "/dev/sda", | ||
partition: 1, | ||
|
||
expected: "/dev/sda1", | ||
}, | ||
{ | ||
devname: "/dev/nvme0n1", | ||
partition: 2, | ||
|
||
expected: "/dev/nvme0n1p2", | ||
}, | ||
} { | ||
t.Run(test.devname, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert.Equal(t, test.expected, partitioning.DevName(test.devname, test.partition)) | ||
}) | ||
} | ||
} |
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,19 @@ | ||
// 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 partitioning implements common partitioning functions. | ||
package partitioning | ||
|
||
import "strconv" | ||
|
||
// DevName returns the devname for the partition on a disk. | ||
func DevName(device string, part uint) string { | ||
result := device | ||
|
||
if len(result) > 0 && result[len(result)-1] >= '0' && result[len(result)-1] <= '9' { | ||
result += "p" | ||
} | ||
|
||
return result + strconv.FormatUint(uint64(part), 10) | ||
} |