Skip to content

Commit 96af430

Browse files
authored
Merge pull request #62 from xibz/patch_clean
Adding support for patch guest drive
2 parents d54e468 + d9baa99 commit 96af430

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

firecracker.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,27 @@ func (f *Client) GetMachineConfig(opts ...GetMachineConfigOpt) (*ops.GetMachineC
228228

229229
return f.client.Operations.GetMachineConfig(p)
230230
}
231+
232+
// PatchGuestDriveByIDOpt is a functional option to be used for the PutMmds API in setting
233+
// any additional optional fields.
234+
type PatchGuestDriveByIDOpt func(*ops.PatchGuestDriveByIDParams)
235+
236+
// PatchGuestDriveByID is a wrapper for the swagger generated client to make calling of the
237+
// API easier.
238+
func (f *Client) PatchGuestDriveByID(ctx context.Context, driveID, pathOnHost string, opts ...PatchGuestDriveByIDOpt) (*ops.PatchGuestDriveByIDNoContent, error) {
239+
params := ops.NewPatchGuestDriveByIDParams()
240+
params.SetContext(ctx)
241+
242+
partialDrive := models.PartialDrive{
243+
DriveID: &driveID,
244+
PathOnHost: &pathOnHost,
245+
}
246+
params.SetBody(&partialDrive)
247+
params.DriveID = driveID
248+
249+
for _, opt := range opts {
250+
opt(params)
251+
}
252+
253+
return f.client.Operations.PatchGuestDriveByID(params)
254+
}

machine.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,18 @@ func (m *Machine) SetMetadata(ctx context.Context, metadata interface{}) error {
576576
return nil
577577
}
578578

579+
// UpdateGuestDrive will modify the current guest drive of ID index with the new
580+
// parameters of the partialDrive.
581+
func (m *Machine) UpdateGuestDrive(ctx context.Context, driveID, pathOnHost string, opts ...PatchGuestDriveByIDOpt) error {
582+
if _, err := m.client.PatchGuestDriveByID(ctx, driveID, pathOnHost, opts...); err != nil {
583+
m.logger.Errorf("PatchGuestDrive failed: %v", err)
584+
return err
585+
}
586+
587+
m.logger.Printf("PatchGuestDrive successful")
588+
return nil
589+
}
590+
579591
// refreshMachineConfig synchronizes our cached representation of the machine configuration
580592
// with that reported by the Firecracker API
581593
func (m *Machine) refreshMachineConfig() error {

machine_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ func TestMicroVMExecution(t *testing.T) {
145145
t.Run("TestAttachSecondaryDrive", func(t *testing.T) { testAttachSecondaryDrive(ctx, t, m) })
146146
t.Run("TestAttachVsock", func(t *testing.T) { testAttachVsock(ctx, t, m) })
147147
t.Run("SetMetadata", func(t *testing.T) { testSetMetadata(ctx, t, m) })
148+
t.Run("TestUpdateGuestDrive", func(t *testing.T) { testUpdateGuestDrive(vmmCtx, t, m) })
148149
t.Run("TestStartInstance", func(t *testing.T) { testStartInstance(vmmCtx, t, m) })
149150

150151
// Let the VMM start and stabilize...
@@ -291,6 +292,13 @@ func testCreateBootSource(ctx context.Context, t *testing.T, m *Machine, vmlinux
291292
}
292293
}
293294

295+
func testUpdateGuestDrive(ctx context.Context, t *testing.T, m *Machine) {
296+
path := filepath.Join(testDataPath, "drive-3.img")
297+
if err := m.UpdateGuestDrive(ctx, "2", path); err != nil {
298+
t.Errorf("unexpected error on swapping guest drive: %v", err)
299+
}
300+
}
301+
294302
func testCreateNetworkInterfaceByID(ctx context.Context, t *testing.T, m *Machine) {
295303
if skipTuntap {
296304
t.Skip("Skipping: tuntap tests explicitly disabled")

testdata/drive-3.img

Whitespace-only changes.

0 commit comments

Comments
 (0)