Skip to content

Commit

Permalink
hostdir volume
Browse files Browse the repository at this point in the history
  • Loading branch information
aajkl committed Aug 15, 2024
1 parent 7ddf6bb commit b967dc4
Show file tree
Hide file tree
Showing 20 changed files with 727 additions and 285 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ require (
github.com/vishvananda/netlink v1.2.1-beta.2.0.20230206183746-70ca0345eede
github.com/yuyang0/resource-bandwidth v0.0.0-20231102113253-8e47795c92e5
github.com/yuyang0/resource-gpu v0.0.0-20231026065700-1577d804efa8
github.com/yuyang0/resource-hostdir v0.0.0-20240815062057-56acd9dde1f8
github.com/yuyang0/resource-rbd v0.0.2-0.20230701090628-cb86da0f60b9
go.etcd.io/etcd v3.3.27+incompatible
go.etcd.io/etcd/client/v3 v3.5.12
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ github.com/yuyang0/resource-bandwidth v0.0.0-20231102113253-8e47795c92e5 h1:qLiO
github.com/yuyang0/resource-bandwidth v0.0.0-20231102113253-8e47795c92e5/go.mod h1:qq6SbQf88tieRqLkMzUgAoANLszQWdcH8H4Ji7xo2sE=
github.com/yuyang0/resource-gpu v0.0.0-20231026065700-1577d804efa8 h1:U1GBBWRCG0kmo3XG3sI5pz0i4nMwjDsM92uxfOOc/1A=
github.com/yuyang0/resource-gpu v0.0.0-20231026065700-1577d804efa8/go.mod h1:oggnae33QHkm9k2Xd0J4BFjdIV1VhPdpm4VUujYUvo0=
github.com/yuyang0/resource-hostdir v0.0.0-20240815062057-56acd9dde1f8 h1:VOtdr1qCKiCfhpe1Shv3GZjW4bOgiFjauC92iv6M6is=
github.com/yuyang0/resource-hostdir v0.0.0-20240815062057-56acd9dde1f8/go.mod h1:7lboIilJ/Ck1qALkQm5hj0kzUgF6Qm063bVH21riMSE=
github.com/yuyang0/resource-rbd v0.0.2-0.20230701090628-cb86da0f60b9 h1:2La8T7mqVy98jyAkwxIN9gB+Akx3qbLGmVEtleaxND4=
github.com/yuyang0/resource-rbd v0.0.2-0.20230701090628-cb86da0f60b9/go.mod h1:ANjyr7r+YfKtpWiIsZPzF7+krI55Uf84R9AvbNr5WAg=
go.etcd.io/etcd v3.3.27+incompatible h1:5hMrpf6REqTHV2LW2OclNpRtxI0k9ZplMemJsMSWju0=
Expand Down
25 changes: 20 additions & 5 deletions internal/service/boar/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (
"github.com/projecteru2/yavirt/internal/models"
intertypes "github.com/projecteru2/yavirt/internal/types"
"github.com/projecteru2/yavirt/internal/volume"
"github.com/projecteru2/yavirt/internal/volume/hostdir"
"github.com/projecteru2/yavirt/internal/volume/local"
"github.com/projecteru2/yavirt/internal/volume/rbd"

cpumemtypes "github.com/projecteru2/core/resource/plugins/cpumem/types"
stotypes "github.com/projecteru2/resource-storage/storage/types"
gputypes "github.com/yuyang0/resource-gpu/gpu/types"
hostdirtypes "github.com/yuyang0/resource-hostdir/hostdir/types"
rbdtypes "github.com/yuyang0/resource-rbd/rbd/types"
)

Expand All @@ -42,7 +44,7 @@ func extractGPU(resources map[string][]byte) (eParams *gputypes.EngineParams, er
return &ans, err
}

func extractVols(resources map[string][]byte) ([]volume.Volume, error) {
func extractVols(resources map[string][]byte) ([]volume.Volume, error) { //nolint
var sysVol volume.Volume
vols := make([]volume.Volume, 1) // first place is for sys volume
appendVol := func(vol volume.Volume) error {
Expand All @@ -57,8 +59,7 @@ func extractVols(resources map[string][]byte) ([]volume.Volume, error) {
return nil
}

stoResRaw, ok := resources[intertypes.PluginNameStorage]
if ok {
if stoResRaw, ok := resources[intertypes.PluginNameStorage]; ok {
eParams := &stotypes.EngineParams{}
if err := json.Unmarshal(stoResRaw, eParams); err != nil {
return nil, errors.Wrap(err, "")
Expand All @@ -73,8 +74,7 @@ func extractVols(resources map[string][]byte) ([]volume.Volume, error) {
}
}
}
rbdResRaw, ok := resources[intertypes.PluginNameRBD]
if ok {
if rbdResRaw, ok := resources[intertypes.PluginNameRBD]; ok {
eParams := &rbdtypes.EngineParams{}
if err := json.Unmarshal(rbdResRaw, eParams); err != nil {
return nil, errors.Wrap(err, "")
Expand All @@ -89,6 +89,21 @@ func extractVols(resources map[string][]byte) ([]volume.Volume, error) {
}
}
}
if hostdirResRaw, ok := resources[intertypes.PluginNameHostdir]; ok {
eParams := &hostdirtypes.EngineParams{}
if err := json.Unmarshal(hostdirResRaw, eParams); err != nil {
return nil, errors.Wrap(err, "")
}
for _, part := range eParams.Volumes {
vol, err := hostdir.NewFromStr(part)
if err != nil {
return nil, err
}
if err := appendVol(vol); err != nil {
return nil, err
}
}
}
if sysVol != nil {
vols[0] = sysVol
} else {
Expand Down
1 change: 1 addition & 0 deletions internal/types/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const (
PluginNameCPUMem = "cpumem"
PluginNameGPU = "gpu"
PluginNameRBD = "rbd"
PluginNameHostdir = "hostdir"
PluginNameStorage = "storage"
PluginNameBandwidth = "bandwidth"
)
35 changes: 3 additions & 32 deletions internal/virt/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,17 +402,12 @@ func (d *VirtDomain) render() ([]byte, error) {
return nil, err
}
}
hostDirs, err := d.hostDirs()
if err != nil {
return nil, err
}
var args = map[string]any{
"name": d.guest.ID,
"uuid": uuid,
"memory": d.guest.MemoryInMiB(),
"cpu": d.guest.CPU,
"gpus": gpus,
"host_dirs": hostDirs,
"sysvol": string(sysVolXML),
"datavols": dataVols,
"interface": d.getInterfaceType(),
Expand Down Expand Up @@ -560,29 +555,6 @@ func (d *VirtDomain) gpus() ([]map[string]string, error) {
return allocGPUs(d.guest.GPUEngineParams)
}

func (d *VirtDomain) hostDirs() ([]map[string]string, error) {
ss, ok := d.guest.JSONLabels["instance/host-dirs"]
if !ok {
return nil, nil
}
parts := strings.FieldsFunc(ss, func(r rune) bool {
return r == ',' || r == ' ' || r == ';'
})
ans := make([]map[string]string, 0, len(parts))
for _, p := range parts {
switch parts2 := strings.Split(p, ":"); len(parts2) {
case 2:
ans = append(ans, map[string]string{
"src": parts2[0],
"dst": parts2[1],
})
default:
return nil, fmt.Errorf("invalid host dir: %s", p)
}
}
return ans, nil
}

type vncConfig struct {
Port int `json:"port"`
Password string `json:"password"`
Expand Down Expand Up @@ -755,24 +727,23 @@ func (d *VirtDomain) AttachVolume(buf []byte) (st libvirt.DomainState, err error
return dom.AttachDevice(string(buf))
}

func (d *VirtDomain) DetachVolume(devPath string) (st libvirt.DomainState, err error) {
func (d *VirtDomain) DetachVolume(xmlQStr string) (st libvirt.DomainState, err error) {
x, err := d.GetXMLString()
if err != nil {
return
}
dev := filepath.Base(devPath)
doc, err := xmlquery.Parse(strings.NewReader(x))
if err != nil {
return
}
node := xmlquery.FindOne(doc, fmt.Sprintf("//devices/disk[target[@dev='%s']]", dev))
node := xmlquery.FindOne(doc, xmlQStr)
if node == nil {
err = errors.New("can't find device")
return
}

xml := node.OutputXML(true)
log.Infof(context.TODO(), "Detach volume, device(%s) xml: %s", devPath, xml)
log.Infof(context.TODO(), "Detach volume, device(%s) xml: %s", xmlQStr, xml)
var dom libvirt.Domain
if dom, err = d.Lookup(); err != nil {
return
Expand Down
11 changes: 1 addition & 10 deletions internal/virt/domain/templates/guest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@
<suspend-to-disk enabled='no'/>
</pm>

{{if .host_dirs }}
<!-- for filesystem(hostdir) -->
<memoryBacking>
<source type='memfd'/>
<access mode='shared'/>
</memoryBacking>
{{end}}

<devices>
{{ .sysvol }}
Expand All @@ -65,14 +64,6 @@
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>

{{range .host_dirs}}
<filesystem type='mount' accessmode='passthrough'>
<driver type='virtiofs' queue='1024'/>
<source dir='{{.src}}'/>
<target dir='{{.dst}}'/>
</filesystem>
{{end}}

<controller type='usb' index='0' model='ich9-ehci1'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
</controller>
Expand Down
8 changes: 3 additions & 5 deletions internal/virt/guest/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,20 +279,19 @@ func (v *bot) AttachVolume(vol volume.Volume) (rollback func(), err error) {
// DetachVolume .
func (v *bot) DetachVolume(vol volume.Volume) (err error) {
logger := log.WithFunc("DetachVolume")
devPath := vol.GetDevice()
if configs.Conf.Storage.InitGuestVolume {
switch st, err := v.GetState(); {
case err != nil:
return errors.Wrap(err, "")
case st == libvirt.DomainRunning:
if err := volFact.Unmount(vol, v.ga, devPath); err != nil {
if err := volFact.Umount(vol, v.ga); err != nil {
return errors.Wrap(err, "")
}
default:
logger.Warnf(context.TODO(), "the guest is not running, so ignore to umount")
}
}
_, err = v.dom.DetachVolume(devPath)
_, err = v.dom.DetachVolume(vol.GetXMLQStr())
return
}

Expand All @@ -306,12 +305,11 @@ func (v *bot) ReplaceSysVolume(vol volume.Volume) error {

// AmplifyVolume .
func (v *bot) AmplifyVolume(vol volume.Volume, delta int64) (err error) {
devPath := base.GetDevicePathByName(vol.GetDevice())
dom, err := v.dom.Lookup()
if err != nil {
return errors.Wrap(err, "")
}
_, err = volFact.Amplify(vol, delta, dom, v.ga, devPath)
_, err = volFact.Amplify(vol, delta, dom, v.ga)

return err
}
Expand Down
Loading

0 comments on commit b967dc4

Please sign in to comment.