Skip to content

Commit 56620d0

Browse files
committed
feat: copy rootfs when creating new VM
1 parent 9a493ee commit 56620d0

File tree

8 files changed

+35
-10
lines changed

8 files changed

+35
-10
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,7 @@ Our C program has been successfully compiled and executed!
161161
"stdout": "Hello, World!"
162162
}
163163
```
164+
165+
## Known issues
166+
167+
Cleanup when shutting down VMs does not work properly.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
github.com/hashicorp/go-multierror v1.1.1 // indirect
2020
github.com/imroc/req v0.3.0
2121
github.com/mailru/easyjson v0.7.7 // indirect
22+
github.com/rs/xid v1.3.0 // indirect
2223
github.com/sirupsen/logrus v1.8.1
2324
github.com/streadway/amqp v1.0.0
2425
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
276276
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
277277
github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
278278
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
279+
github.com/rs/xid v1.3.0 h1:6NjYksEUlhurdVehpc7S7dk6DAmcKv8V9gG0FsVN2U4=
280+
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
279281
github.com/safchain/ethtool v0.0.0-20190326074333-42ed695e3de8/go.mod h1:Z0q5wiBQGYcxhMZ6gUqHn6pYNLypFAvaL3UvgZLR0U4=
280282
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
281283
github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=

job.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func (job benchJob) run(ctx context.Context, WarmVMs <-chan runningFirecracker)
5353
var httpRes *http.Response
5454
var agentRes agentExecRes
5555

56-
// FIXME
5756
httpRes, err = http.Post("http://"+vm.ip.String()+":8080/run", "application/json", bytes.NewBuffer(reqJSON))
5857
if err != nil {
5958
log.WithError(err).Error("Failed to request execution to agent")
@@ -76,5 +75,4 @@ func (job benchJob) run(ctx context.Context, WarmVMs <-chan runningFirecracker)
7675
if err != nil {
7776
q.setjobFailed(ctx, job, agentExecRes{Error: err.Error()})
7877
}
79-
8078
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type agentExecRes struct {
4747
type runningFirecracker struct {
4848
vmmCtx context.Context
4949
vmmCancel context.CancelFunc
50+
vmmID string
5051
machine *firecracker.Machine
5152
ip net.IP
5253
}

options.go

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

33
import (
44
"fmt"
5-
"math/rand"
65
"os"
76
"path/filepath"
87
"strconv"
@@ -12,17 +11,16 @@ import (
1211
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
1312
)
1413

15-
// Converts options to a usable firecracker config
16-
func getFirecrackerConfig() (firecracker.Config, error) {
17-
socket := getSocketPath()
14+
func getFirecrackerConfig(vmmID string) (firecracker.Config, error) {
15+
socket := getSocketPath(vmmID)
1816
return firecracker.Config{
1917
SocketPath: socket,
2018
KernelImagePath: "../../linux/vmlinux",
2119
LogPath: fmt.Sprintf("%s.log", socket),
2220
Drives: []models.Drive{{
2321
DriveID: firecracker.String("1"),
2422
// TODO: copy base rootfs and use a temp roots per VM
25-
PathOnHost: firecracker.String("../agent/rootfs.ext4"),
23+
PathOnHost: firecracker.String("/tmp/rootfs-" + vmmID + ".ext4"),
2624
IsRootDevice: firecracker.Bool(true),
2725
IsReadOnly: firecracker.Bool(false),
2826
RateLimiter: firecracker.NewRateLimiter(
@@ -54,11 +52,12 @@ func getFirecrackerConfig() (firecracker.Config, error) {
5452
}, nil
5553
}
5654

57-
func getSocketPath() string {
55+
func getSocketPath(vmmID string) string {
5856
filename := strings.Join([]string{
5957
".firecracker.sock",
6058
strconv.Itoa(os.Getpid()),
61-
strconv.Itoa(rand.Intn(10000))},
59+
vmmID,
60+
},
6261
"-",
6362
)
6463
dir := os.TempDir()

vm.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@ func (vm runningFirecracker) shutDown() {
4646
if err != nil {
4747
log.WithError(err).Error("Failed to delete firecracker socket")
4848
}
49+
err = os.Remove("/tmp/rootfs-" + vm.vmmID + ".ext4")
50+
if err != nil {
51+
log.WithError(err).Error("Failed to delete firecracker rootfs")
52+
}
4953
}

vmm.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,31 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"io/ioutil"
67
"os"
78
"os/exec"
89

910
firecracker "github.com/firecracker-microvm/firecracker-go-sdk"
11+
"github.com/rs/xid"
1012
log "github.com/sirupsen/logrus"
1113
)
1214

15+
func copy(src string, dst string) error {
16+
data, err := ioutil.ReadFile(src)
17+
if err != nil {
18+
return err
19+
}
20+
err = ioutil.WriteFile(dst, data, 0644)
21+
return err
22+
}
23+
1324
// Create a VMM with a given set of options and start the VM
1425
func createAndStartVM(ctx context.Context) (*runningFirecracker, error) {
15-
fcCfg, err := getFirecrackerConfig()
26+
vmmID := xid.New().String()
27+
28+
copy("../agent/rootfs.ext4", "/tmp/rootfs-"+vmmID+".ext4")
29+
30+
fcCfg, err := getFirecrackerConfig(vmmID)
1631
if err != nil {
1732
log.Errorf("Error: %s", err)
1833
return nil, err
@@ -78,6 +93,7 @@ func createAndStartVM(ctx context.Context) (*runningFirecracker, error) {
7893
return &runningFirecracker{
7994
vmmCtx: vmmCtx,
8095
vmmCancel: vmmCancel,
96+
vmmID: vmmID,
8197
machine: m,
8298
ip: m.Cfg.NetworkInterfaces[0].StaticConfiguration.IPConfiguration.IPAddr.IP,
8399
}, nil

0 commit comments

Comments
 (0)