Skip to content

Commit 206bd0c

Browse files
cfergeaupraveenkumar
authored andcommitted
vfkit: Implement disk resizing
Since vfkit uses a raw disk image, resizing the disk is done with an os.Truncate() call.
1 parent 02b09c7 commit 206bd0c

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

pkg/drivers/vfkit/driver_darwin.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package vfkit
1818

1919
import (
20+
"encoding/json"
2021
"fmt"
2122
"io/ioutil"
2223
"os"
@@ -93,6 +94,23 @@ func (d *Driver) getDiskPath() string {
9394
return d.ResolveStorePath(fmt.Sprintf("%s.img", d.MachineName))
9495
}
9596

97+
func (d *Driver) resize(newSize int64) error {
98+
diskPath := d.getDiskPath()
99+
fi, err := os.Stat(diskPath)
100+
if err != nil {
101+
return err
102+
}
103+
if newSize == fi.Size() {
104+
log.Debugf("%s is already %d bytes", diskPath, newSize)
105+
return nil
106+
}
107+
if newSize < fi.Size() {
108+
return fmt.Errorf("current disk image capacity is bigger than the requested size (%d > %d)", fi.Size(), newSize)
109+
}
110+
return os.Truncate(diskPath, newSize)
111+
112+
}
113+
96114
// Create a host using the driver's config
97115
func (d *Driver) Create() error {
98116
if err := d.PreCreateCheck(); err != nil {
@@ -111,8 +129,7 @@ func (d *Driver) Create() error {
111129
return fmt.Errorf("%s is an unsupported disk image format", d.ImageFormat)
112130
}
113131

114-
// TODO: resize disk
115-
return nil
132+
return d.resize(int64(d.DiskCapacity))
116133
}
117134

118135
func startVfkit(vfkitPath string, args []string) (*os.Process, error) {
@@ -279,8 +296,21 @@ func (d *Driver) Remove() error {
279296
}
280297

281298
// UpdateConfigRaw allows to change the state (memory, ...) of an already created machine
282-
func (d *Driver) UpdateConfigRaw(rawDriver []byte) error {
283-
return errors.New("UpdateConfigRaw() is not implemented")
299+
func (d *Driver) UpdateConfigRaw(rawConfig []byte) error {
300+
var newDriver Driver
301+
err := json.Unmarshal(rawConfig, &newDriver)
302+
if err != nil {
303+
return err
304+
}
305+
306+
err = d.resize(int64(newDriver.DiskCapacity))
307+
if err != nil {
308+
log.Debugf("failed to resize disk image: %v", err)
309+
return err
310+
}
311+
*d = newDriver
312+
313+
return nil
284314
}
285315

286316
// Stop a host gracefully

0 commit comments

Comments
 (0)