Skip to content

Commit ec0a040

Browse files
committed
add metadata support and generated files
Signed-off-by: Yuval Kohavi <[email protected]>
1 parent 91e88e3 commit ec0a040

File tree

11 files changed

+133
-23
lines changed

11 files changed

+133
-23
lines changed

HACKING.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ You need some external resources in order to run the tests, as described below:
1313
1. A firecracker binary (tested with 0.10.1), but any recent version should
1414
work. Must either be installed as `./firecracker` or the path must be
1515
specified through the `FC_TEST_BIN` environment variable.
16-
2. Access to hardware virtualization via `/dev/kvm` (ensure you have mode
16+
2. Access to hardware virtualization via `/dev/kvm` and `/dev/vhost-vsock` (ensure you have mode
1717
`+rw`!)
1818
3. An uncompressed Linux kernel binary that can boot in Firecracker VM (Must be
19-
installed as `./vmlinux`)
19+
installed as `./testdata/vmlinux`)
2020
4. A tap device owned by your userid (Must be either named `fc-test-tap0` or
2121
have the name specified with the `FC_TEST_TAP` environment variable; try
2222
`sudo ip tuntap add fc-test-tap0 mode tap user $UID` to create `fc-test-tap0`
2323
if you need to create one)
24-
5. A root filesystem image installed (Must be named `root-drive.img`; can be
24+
5. A root filesystem image installed (Must be named `testdata/root-drive.img`)
25+
6. A secondary device image (Must be named `testdata/drive-2.img`; can be
2526
empty, create it something like
26-
`dd if=/dev/zero of=drive-2.img bs=1k count=102400`)
27+
`dd if=/dev/zero of=testdata/drive-2.img bs=1k count=102400`)
2728

2829
With all of those set up, `make test EXTRAGOARGS=-v` should create a Firecracker
2930
process and run the Linux kernel in a MicroVM.

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ EXTRAGOARGS:=
1818

1919
all: build
2020

21-
build clean test:
21+
generate build clean test:
2222
go $@ $(EXTRAGOARGS)
2323
for d in $(SUBDIRS); do \
2424
cd $$d && go $@ $(EXTRAGOARGS); \
2525
done
2626

27-
.PHONY: all clean build test
27+
.PHONY: all generate clean build test

client/models/error.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/operations/operations_client.go

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/operations/put_mmds_responses.go

Lines changed: 29 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/swagger.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,10 @@ paths:
257257
schema:
258258
type: object
259259
responses:
260+
201:
261+
description: MMDS data store created
260262
204:
261-
description: MMDS data store created/updated.
263+
description: MMDS data store updated.
262264
400:
263265
description: MMDS data store cannot be created due to bad input.
264266
schema:
@@ -393,7 +395,7 @@ definitions:
393395

394396
Error:
395397
properties:
396-
faultMessage:
398+
fault_message:
397399
type: string
398400
description: A description of the error condition
399401

cmd/firectl/main.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package main
1515

1616
import (
1717
"context"
18+
"encoding/json"
1819
"errors"
1920
"fmt"
2021
"os"
@@ -107,6 +108,7 @@ func main() {
107108
FcCPUCount int64 `long:"ncpus" short:"c" description:"Number of CPUs" default:"1"`
108109
FcCPUTemplate string `long:"cpu-template" description:"Firecracker CPU Template (C3 or T2)"`
109110
FcMemSz int64 `long:"memory" short:"m" description:"VM memory, in MiB" default:"512"`
111+
FcMetadata string `long:"metadata" description:"Firecracker Meatadata for MMDS (json)"`
110112
Debug bool `long:"debug" short:"d" description:"Enable debug output"`
111113
Help bool `long:"help" short:"h" description:"Show usage"`
112114
}
@@ -131,6 +133,13 @@ func main() {
131133
logger.SetLevel(log.DebugLevel)
132134
}
133135

136+
var metadata interface{}
137+
if opts.FcMetadata != "" {
138+
if err := json.Unmarshal([]byte(opts.FcMetadata), &metadata); err != nil {
139+
log.Fatalf("Unable to parse metadata as json: %s", err)
140+
}
141+
}
142+
134143
var NICs []firecracker.NetworkInterface
135144

136145
if len(opts.FcNicConfig) > 0 {
@@ -139,10 +148,12 @@ func main() {
139148
log.Fatalf("Unable to parse NIC config: %s", err)
140149
} else {
141150
log.Printf("Adding tap device %s", tapDev)
151+
allowMDDS := metadata != nil
142152
NICs = []firecracker.NetworkInterface{
143153
firecracker.NetworkInterface{
144154
MacAddress: tapMacAddr,
145155
HostDevName: tapDev,
156+
AllowMDDS: allowMDDS,
146157
},
147158
}
148159
}
@@ -192,8 +203,17 @@ func main() {
192203
errchan, err := m.Init(ctx)
193204
if err != nil {
194205
log.Fatalf("Firecracker Init returned error %s", err)
195-
} else {
196-
m.StartInstance(vmmCtx)
206+
}
207+
208+
if metadata != nil {
209+
err := m.SetMetadata(vmmCtx, metadata)
210+
if err != nil {
211+
log.Fatalf("Firecracker SetMetadata returned error %s", err)
212+
}
213+
}
214+
err = m.StartInstance(vmmCtx)
215+
if err != nil {
216+
log.Fatalf("Firecracker StartInstance returned error %s", err)
197217
}
198218

199219
// wait for the VMM to exit

firecracker.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ func (f *FirecrackerClient) CreateSyncAction(ctx context.Context, info *models.I
132132
return f.client.Operations.CreateSyncAction(params)
133133
}
134134

135+
func (f *FirecrackerClient) PutMmds(ctx context.Context, metadata interface{}) (*ops.PutMmdsCreated, *ops.PutMmdsNoContent, error) {
136+
params := ops.NewPutMmdsParams()
137+
params.SetContext(ctx)
138+
params.SetBody(metadata)
139+
140+
return f.client.Operations.PutMmds(params)
141+
}
142+
135143
func (f *FirecrackerClient) GetMachineConfig() (*ops.GetMachineConfigOK, error) {
136144
p := ops.NewGetMachineConfigParams()
137145
p.SetTimeout(firecrackerRequestTimeout)

machine.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type Firecracker interface {
5858
PutGuestDriveByID(ctx context.Context, driveID string, drive *models.Drive) (*ops.PutGuestDriveByIDNoContent, error)
5959
PutGuestVsockByID(ctx context.Context, vsockID string, vsock *models.Vsock) (*ops.PutGuestVsockByIDCreated, *ops.PutGuestVsockByIDNoContent, error)
6060
CreateSyncAction(ctx context.Context, info *models.InstanceActionInfo) (*ops.CreateSyncActionNoContent, error)
61+
PutMmds(ctx context.Context, metadata interface{}) (*ops.PutMmdsCreated, *ops.PutMmdsNoContent, error)
6162
GetMachineConfig() (*ops.GetMachineConfigOK, error)
6263
}
6364

@@ -129,6 +130,7 @@ func (m *Machine) Logger() *log.Entry {
129130
type NetworkInterface struct {
130131
MacAddress string
131132
HostDevName string
133+
AllowMDDS bool
132134
}
133135

134136
// BlockDevice represents a host block device mapped to the firecracker VM.
@@ -433,10 +435,11 @@ func (m *Machine) createNetworkInterface(ctx context.Context, iface NetworkInter
433435
m.logger.Printf("Attaching NIC %s (hwaddr %s) at index %s", iface.HostDevName, iface.MacAddress, ifaceID)
434436

435437
ifaceCfg := models.NetworkInterface{
436-
IfaceID: &ifaceID,
437-
GuestMac: iface.MacAddress,
438-
HostDevName: iface.HostDevName,
439-
State: models.DeviceStateAttached,
438+
IfaceID: &ifaceID,
439+
GuestMac: iface.MacAddress,
440+
HostDevName: iface.HostDevName,
441+
State: models.DeviceStateAttached,
442+
AllowMmdsRequests: iface.AllowMDDS,
440443
}
441444

442445
resp, err := m.client.PutGuestNetworkInterfaceByID(ctx, ifaceID, &ifaceCfg)
@@ -518,14 +521,30 @@ func (m *Machine) startInstance(ctx context.Context) error {
518521
ActionType: models.InstanceActionInfoActionTypeInstanceStart,
519522
}
520523

521-
ctx, cancel := context.WithCancel(ctx)
522-
defer cancel()
523-
524524
resp, err := m.client.CreateSyncAction(ctx, &info)
525525
if err == nil {
526526
m.logger.Printf("startInstance successful: %s", resp.Error())
527527
} else {
528-
m.logger.Printf("Error starting instance: %s", err)
528+
m.logger.Errorf("Starting instance: %s", err)
529+
}
530+
return err
531+
}
532+
533+
// SetMetadata sets the machine's metadata for MDDS
534+
func (m *Machine) SetMetadata(ctx context.Context, metadata interface{}) error {
535+
respcreated, respnocontent, err := m.client.PutMmds(ctx, metadata)
536+
537+
if err == nil {
538+
var message string
539+
if respcreated != nil {
540+
message = respcreated.Error()
541+
}
542+
if respnocontent != nil {
543+
message = respnocontent.Error()
544+
}
545+
m.logger.Printf("SetMetadata successful: %s", message)
546+
} else {
547+
m.logger.Errorf("Setting metadata: %s", err)
529548
}
530549
return err
531550
}

machine_mock_test.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)