|
| 1 | +/* |
| 2 | +Copyright 2018 Mirantis |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package libvirttools |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "io/ioutil" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "strconv" |
| 25 | + "strings" |
| 26 | + |
| 27 | + libvirtxml "github.com/libvirt/libvirt-go-xml" |
| 28 | + |
| 29 | + vconfig "github.com/Mirantis/virtlet/pkg/config" |
| 30 | + "github.com/Mirantis/virtlet/pkg/utils/cgroups" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + procfsLocation = "/proc" |
| 35 | + emulatorProcessName = "qemu-system-x86_64" |
| 36 | +) |
| 37 | + |
| 38 | +// UpdateCpusetsInContainerDefinition updates libvirt domain definition for the VM |
| 39 | +// setting the environment variable which is used by vmwrapper to pin to the specified cpuset |
| 40 | +func (v *VirtualizationTool) UpdateCpusetsInContainerDefinition(containerID, cpusets string) error { |
| 41 | + domain, err := v.domainConn.LookupDomainByUUIDString(containerID) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + domainxml, err := domain.XML() |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + found := false |
| 52 | + envvars := domainxml.QEMUCommandline.Envs |
| 53 | + for _, envvar := range envvars { |
| 54 | + if envvar.Name == vconfig.CpusetsEnvVarName { |
| 55 | + envvar.Value = cpusets |
| 56 | + found = true |
| 57 | + } |
| 58 | + } |
| 59 | + if !found && cpusets != "" { |
| 60 | + domainxml.QEMUCommandline.Envs = append(envvars, libvirtxml.DomainQEMUCommandlineEnv{ |
| 61 | + Name: vconfig.CpusetsEnvVarName, |
| 62 | + Value: cpusets, |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + if err := domain.Undefine(); err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + _, err = v.domainConn.DefineDomain(domainxml) |
| 71 | + return err |
| 72 | +} |
| 73 | + |
| 74 | +// UpdateCpusetsForEmulatorProcess looks through /proc for emulator process |
| 75 | +// to find its cgroup manager for cpusets then uses it to adjust the setting |
| 76 | +func (v *VirtualizationTool) UpdateCpusetsForEmulatorProcess(containerID, cpusets string) (bool, error) { |
| 77 | + // TODO: replace iterating over the procfs with reading pid from |
| 78 | + // /run/libvirt/qemu/virtlet-CONTAINER_ID[:12]-DOMAIN_NAME.pid |
| 79 | + d, err := os.Open(procfsLocation) |
| 80 | + if err != nil { |
| 81 | + return false, err |
| 82 | + } |
| 83 | + defer d.Close() |
| 84 | + |
| 85 | + entries, err := d.Readdirnames(-1) |
| 86 | + if err != nil { |
| 87 | + return false, err |
| 88 | + } |
| 89 | + |
| 90 | + for _, name := range entries { |
| 91 | + _, err := strconv.ParseInt(name, 10, 32) |
| 92 | + if err != nil { |
| 93 | + // skip non numeric names |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + isContainerPid, err := isEmulatorPid(name, containerID) |
| 98 | + if err != nil { |
| 99 | + return false, err |
| 100 | + } |
| 101 | + |
| 102 | + if isContainerPid { |
| 103 | + controller, err := cgroups.GetProcessController(name, "cpuset") |
| 104 | + if err != nil { |
| 105 | + return false, err |
| 106 | + } |
| 107 | + |
| 108 | + if err := controller.Set("cpus", cpusets); err != nil { |
| 109 | + return false, err |
| 110 | + } |
| 111 | + return true, nil |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return false, nil |
| 116 | +} |
| 117 | + |
| 118 | +func isEmulatorPid(pid, containerID string) (bool, error) { |
| 119 | + data, err := ioutil.ReadFile(filepath.Join(procfsLocation, pid, "cmdline")) |
| 120 | + if err != nil { |
| 121 | + return false, err |
| 122 | + } |
| 123 | + |
| 124 | + cmdline := bytes.Split(data, []byte{0}) |
| 125 | + |
| 126 | + if string(cmdline[0]) != emulatorProcessName { |
| 127 | + return false, nil |
| 128 | + } |
| 129 | + |
| 130 | + searchTerm := "virtlet-" + containerID[:12] |
| 131 | + for _, param := range cmdline { |
| 132 | + if strings.Contains(string(param), searchTerm) { |
| 133 | + return true, nil |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return false, nil |
| 138 | +} |
0 commit comments