This repository has been archived by the owner on May 7, 2021. It is now read-only.
/
cluster.go
198 lines (164 loc) · 4.92 KB
/
cluster.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2019 Red Hat
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package unprivqemu
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/pborman/uuid"
"github.com/coreos/mantle/platform"
"github.com/coreos/mantle/platform/conf"
"github.com/coreos/mantle/system/exec"
"github.com/coreos/mantle/util"
)
// Cluster is a local cluster of QEMU-based virtual machines.
//
// XXX: must be exported so that certain QEMU tests can access struct members
// through type assertions.
type Cluster struct {
*platform.BaseCluster
flight *flight
mu sync.Mutex
}
func (qc *Cluster) NewMachine(userdata *conf.UserData) (platform.Machine, error) {
return qc.NewMachineWithOptions(userdata, platform.MachineOptions{})
}
func (qc *Cluster) NewMachineWithOptions(userdata *conf.UserData, options platform.MachineOptions) (platform.Machine, error) {
id := uuid.New()
dir := filepath.Join(qc.RuntimeConf().OutputDir, id)
if err := os.Mkdir(dir, 0777); err != nil {
return nil, err
}
// hacky solution for cloud config ip substitution
// NOTE: escaping is not supported
qc.mu.Lock()
conf, err := qc.RenderUserData(userdata, map[string]string{})
if err != nil {
qc.mu.Unlock()
return nil, err
}
qc.mu.Unlock()
var confPath string
if conf.IsIgnition() {
confPath = filepath.Join(dir, "ignition.json")
if err := conf.WriteFile(confPath); err != nil {
return nil, err
}
} else if conf.IsEmpty() {
} else {
return nil, fmt.Errorf("unprivileged qemu only supports Ignition or empty configs")
}
journal, err := platform.NewJournal(dir)
if err != nil {
return nil, err
}
qm := &machine{
qc: qc,
id: id,
journal: journal,
consolePath: filepath.Join(dir, "console.txt"),
}
qmCmd, extraFiles, err := platform.CreateQEMUCommand(qc.flight.opts.Board, qm.id, qc.flight.opts.BIOSImage, qm.consolePath, confPath, qc.flight.diskImagePath, conf.IsIgnition(), options)
if err != nil {
return nil, err
}
for _, file := range extraFiles {
defer file.Close()
}
qc.mu.Lock()
qmCmd = append(qmCmd, "-netdev", "user,id=eth0,restrict=yes,hostfwd=tcp:127.0.0.1:0-:22", "-device", platform.Virtio(qc.flight.opts.Board, "net", "netdev=eth0"))
plog.Debugf("NewMachine: %q", qmCmd)
qm.qemu = exec.Command(qmCmd[0], qmCmd[1:]...)
qc.mu.Unlock()
cmd := qm.qemu.(*exec.ExecCmd)
cmd.Stderr = os.Stderr
cmd.ExtraFiles = append(cmd.ExtraFiles, extraFiles...)
if err = qm.qemu.Start(); err != nil {
return nil, err
}
pid := strconv.Itoa(qm.qemu.Pid())
err = util.Retry(6, 5*time.Second, func() error {
var err error
qm.ip, err = getAddress(pid)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
if err := platform.StartMachine(qm, qm.journal); err != nil {
qm.Destroy()
return nil, err
}
qc.AddMach(qm)
return qm, nil
}
func (qc *Cluster) Destroy() {
qc.BaseCluster.Destroy()
qc.flight.DelCluster(qc)
}
// parse /proc/net/tcp to determine the port selected by QEMU
func getAddress(pid string) (string, error) {
data, err := ioutil.ReadFile("/proc/net/tcp")
if err != nil {
return "", fmt.Errorf("reading /proc/net/tcp: %v", err)
}
for _, line := range strings.Split(string(data), "\n")[1:] {
fields := strings.Fields(line)
if len(fields) < 10 {
// at least 10 fields are neeeded for the local & remote address and the inode
continue
}
localAddress := fields[1]
remoteAddress := fields[2]
inode := fields[9]
isLocalPat := regexp.MustCompile("0100007F:[[:xdigit:]]{4}")
if !isLocalPat.MatchString(localAddress) || remoteAddress != "00000000:0000" {
continue
}
dir := fmt.Sprintf("/proc/%s/fd/", pid)
fds, err := ioutil.ReadDir(dir)
if err != nil {
return "", fmt.Errorf("listing %s: %v", dir, err)
}
for _, f := range fds {
link, err := os.Readlink(filepath.Join(dir, f.Name()))
if err != nil {
continue
}
socketPattern := regexp.MustCompile("socket:\\[([0-9]+)\\]")
match := socketPattern.FindStringSubmatch(link)
if len(match) > 1 {
if inode == match[1] {
// this entry belongs to the QEMU pid, parse the port and return the address
portHex := strings.Split(localAddress, ":")[1]
port, err := strconv.ParseInt(portHex, 16, 32)
if err != nil {
return "", fmt.Errorf("decoding port %q: %v", portHex, err)
}
return fmt.Sprintf("127.0.0.1:%d", port), nil
}
}
}
}
return "", fmt.Errorf("didn't find an address")
}