Add basic logic needed to correctly mount the VHD when initializing a server.

This commit is contained in:
Dane Everitt
2021-07-04 12:12:32 -07:00
committed by DaneEveritt
parent 7fed6a68cb
commit 265f8a6b39
8 changed files with 63 additions and 32 deletions

View File

@@ -43,6 +43,7 @@ type CfgOption func(d *Disk) *Disk
// Disk represents the underlying virtual disk for the instance.
type Disk struct {
// The total size of the disk allowed in bytes.
size int64
diskPath string
mountAt string
@@ -51,8 +52,8 @@ type Disk struct {
}
// New returns a new Disk instance. The "size" parameter should be provided in
// megabytes of space allowed for the disk. An additional slice of option
// callbacks can be provided to programatically swap out the underlying filesystem
// bytes of space allowed for the disk. An additional slice of option callbacks
// can be provided to programatically swap out the underlying filesystem
// implementation or the underlying command exection engine.
func New(size int64, diskPath string, mountAt string, opts ...func(*Disk)) *Disk {
if diskPath == "" || mountAt == "" {
@@ -181,7 +182,10 @@ func (d *Disk) Allocate(ctx context.Context) error {
} else if err != nil {
return errors.Wrap(err, "vhd: failed to check for existence of root disk")
}
cmd := d.commander(ctx, "fallocate", "-l", fmt.Sprintf("%dM", d.size), d.diskPath)
// We use 1024 as the multiplier for all of the disk space logic within the
// application. Passing "K" (/1024) is the same as "KiB" for fallocate, but
// is different than "KB" (/1000).
cmd := d.commander(ctx, "fallocate", "-l", fmt.Sprintf("%dK", d.size / 1024), d.diskPath)
if _, err := cmd.Output(); err != nil {
msg := "vhd: failed to execute fallocate command"
if v, ok := err.(*exec.ExitError); ok {

View File

@@ -62,14 +62,14 @@ func newMockDisk(c CommanderProvider) *Disk {
if c != nil {
w = c
}
return New(100, "/foo", "/bar", WithFs(afero.NewMemMapFs()), WithCommander(w))
return New(100 * 1024 * 1024, "/foo", "/bar", WithFs(afero.NewMemMapFs()), WithCommander(w))
}
func Test_New(t *testing.T) {
t.Run("creates expected struct", func(t *testing.T) {
d := New(100, "/foo", "/bar")
d := New(100 * 1024 * 1024, "/foo", "/bar")
assert.NotNil(t, d)
assert.Equal(t, int64(100), d.size)
assert.Equal(t, int64(100 * 1024 * 1024), d.size)
assert.Equal(t, "/foo", d.diskPath)
assert.Equal(t, "/bar", d.mountAt)
@@ -360,7 +360,7 @@ func TestDisk_Allocate(t *testing.T) {
output: func() ([]byte, error) {
called = true
assert.Equal(t, "fallocate", name)
assert.Equal(t, []string{"-l", "100M", "/foo"}, args)
assert.Equal(t, []string{"-l", "102400K", "/foo"}, args)
return nil, nil
},
}