Skip to content

Commit

Permalink
feat: Add support for virtiofs
Browse files Browse the repository at this point in the history
Resolve #48
  • Loading branch information
fengye87 committed Sep 20, 2022
1 parent e217008 commit 900779d
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build/virt-prerunner/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RUN --mount=type=cache,target=/root/.cache/go-build go build -a cmd/virt-prerunn

FROM alpine

RUN apk add --no-cache tini curl screen dnsmasq cdrkit iptables iproute2
RUN apk add --no-cache tini curl screen dnsmasq cdrkit iptables iproute2 qemu-virtiofsd

RUN set -eux; \
mkdir /var/lib/cloud-hypervisor; \
Expand Down
40 changes: 39 additions & 1 deletion cmd/virt-prerunner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ func main() {
cloudHypervisorCmd = append(cloudHypervisorCmd, "--cpus", fmt.Sprintf("boot=%d,topology=%d:%d:%d:%d,affinity=%s",
vmConfig.Cpus.BootVcpus, vmConfig.Cpus.Topology.ThreadsPerCore, vmConfig.Cpus.Topology.CoresPerDie,
vmConfig.Cpus.Topology.DiesPerPackage, vmConfig.Cpus.Topology.Packages, cpuAffinity))
cloudHypervisorCmd = append(cloudHypervisorCmd, "--memory", fmt.Sprintf("size=%d", vmConfig.Memory.Size))

memoryArg := fmt.Sprintf("size=%d", vmConfig.Memory.Size)
if vmConfig.Memory.Shared {
memoryArg = memoryArg + ",shared=on"
}
cloudHypervisorCmd = append(cloudHypervisorCmd, "--memory", memoryArg)

if len(vmConfig.Disks) > 0 {
cloudHypervisorCmd = append(cloudHypervisorCmd, "--disk")
Expand All @@ -87,6 +92,14 @@ func main() {
}
}

if len(vmConfig.Fs) > 0 {
cloudHypervisorCmd = append(cloudHypervisorCmd, "--fs")
for _, fs := range vmConfig.Fs {
arg := fmt.Sprintf("id=%s,socket=%s,tag=%s", fs.Id, fs.Socket, fs.Tag)
cloudHypervisorCmd = append(cloudHypervisorCmd, arg)
}
}

if len(vmConfig.Net) > 0 {
cloudHypervisorCmd = append(cloudHypervisorCmd, "--net")
for _, net := range vmConfig.Net {
Expand Down Expand Up @@ -190,6 +203,31 @@ func buildVMConfig(ctx context.Context, vm *virtv1alpha1.VirtualMachine) (*cloud
}
}

for _, fs := range vm.Spec.Instance.FileSystems {
vmConfig.Memory.Shared = true

if err := os.MkdirAll("/var/run/virtink/virtiofsd", 0755); err != nil {
return nil, fmt.Errorf("create virtiofsd socket dir: %s", err)
}

for _, volume := range vm.Spec.Volumes {
if volume.Name == fs.Name {
socketPath := fmt.Sprintf("/var/run/virtink/virtiofsd/%s.sock", volume.Name)
if err := exec.Command("/usr/lib/qemu/virtiofsd", "--socket-path="+socketPath, "-o", "source=/mnt/"+volume.Name).Start(); err != nil {
return nil, fmt.Errorf("start virtiofsd: %s", err)
}

fsConfig := cloudhypervisor.FsConfig{
Id: fs.Name,
Socket: socketPath,
Tag: fs.Name,
}
vmConfig.Fs = append(vmConfig.Fs, &fsConfig)
break
}
}
}

networkStatusList := []netv1.NetworkStatus{}
if os.Getenv("NETWORK_STATUS") != "" {
if err := json.Unmarshal([]byte(os.Getenv("NETWORK_STATUS")), &networkStatusList); err != nil {
Expand Down
9 changes: 9 additions & 0 deletions deploy/crd/virt.virtink.smartx.com_virtualmachines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,15 @@ spec:
- name
type: object
type: array
fileSystems:
items:
properties:
name:
type: string
required:
- name
type: object
type: array
interfaces:
items:
properties:
Expand Down
15 changes: 10 additions & 5 deletions pkg/apis/virt/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ const (
)

type Instance struct {
CPU CPU `json:"cpu,omitempty"`
Memory Memory `json:"memory"`
Kernel *Kernel `json:"kernel,omitempty"`
Disks []Disk `json:"disks,omitempty"`
Interfaces []Interface `json:"interfaces,omitempty"`
CPU CPU `json:"cpu,omitempty"`
Memory Memory `json:"memory"`
Kernel *Kernel `json:"kernel,omitempty"`
Disks []Disk `json:"disks,omitempty"`
FileSystems []FileSystem `json:"fileSystems,omitempty"`
Interfaces []Interface `json:"interfaces,omitempty"`
}

type CPU struct {
Expand All @@ -80,6 +81,10 @@ type Disk struct {
ReadOnly *bool `json:"readOnly,omitempty"`
}

type FileSystem struct {
Name string `json:"name"`
}

type Interface struct {
Name string `json:"name"`
MAC string `json:"mac,omitempty"`
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/virt/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions pkg/controller/vm_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ func ValidateInstance(ctx context.Context, instance *virtv1alpha1.Instance, fiel
errs = append(errs, ValidateDisk(ctx, &disk, fieldPath)...)
}

for i, fs := range instance.FileSystems {
fieldPath := fieldPath.Child("fileSystems").Index(i)
if _, ok := diskNames[fs.Name]; ok {
errs = append(errs, field.Duplicate(fieldPath.Child("name"), fs.Name))
}
diskNames[fs.Name] = struct{}{}
errs = append(errs, ValidateFileSystem(ctx, &fs, fieldPath)...)
}

ifaceNames := map[string]struct{}{}
for i, iface := range instance.Interfaces {
fieldPath := fieldPath.Child("interfaces").Index(i)
Expand Down Expand Up @@ -322,6 +331,19 @@ func ValidateDisk(ctx context.Context, disk *virtv1alpha1.Disk, fieldPath *field
return errs
}

func ValidateFileSystem(ctx context.Context, fs *virtv1alpha1.FileSystem, fieldPath *field.Path) field.ErrorList {
var errs field.ErrorList
if fs == nil {
errs = append(errs, field.Required(fieldPath, ""))
return errs
}

if fs.Name == "" {
errs = append(errs, field.Required(fieldPath.Child("name"), ""))
}
return errs
}

func ValidateInterface(ctx context.Context, iface *virtv1alpha1.Interface, fieldPath *field.Path) field.ErrorList {
var errs field.ErrorList
if iface == nil {
Expand Down
17 changes: 17 additions & 0 deletions pkg/controller/vm_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func TestValidateVM(t *testing.T) {
Disks: []virtv1alpha1.Disk{{
Name: "vol-1",
}},
FileSystems: []virtv1alpha1.FileSystem{{
Name: "vol-2",
}},
Interfaces: []virtv1alpha1.Interface{{
Name: "net-1",
InterfaceBindingMethod: virtv1alpha1.InterfaceBindingMethod{
Expand All @@ -40,6 +43,13 @@ func TestValidateVM(t *testing.T) {
Image: "container-disk",
},
},
}, {
Name: "vol-2",
VolumeSource: virtv1alpha1.VolumeSource{
PersistentVolumeClaim: &virtv1alpha1.PersistentVolumeClaimVolumeSource{
ClaimName: "vol-2",
},
},
}},
Networks: []virtv1alpha1.Network{{
Name: "net-1",
Expand Down Expand Up @@ -199,6 +209,13 @@ func TestValidateVM(t *testing.T) {
return vm
}(),
invalidFields: []string{"spec.instance.disks[0].name"},
}, {
vm: func() *virtv1alpha1.VirtualMachine {
vm := validVM.DeepCopy()
vm.Spec.Instance.FileSystems[0].Name = ""
return vm
}(),
invalidFields: []string{"spec.instance.fileSystems[0].name"},
}, {
vm: func() *virtv1alpha1.VirtualMachine {
vm := validVM.DeepCopy()
Expand Down

0 comments on commit 900779d

Please sign in to comment.