-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
195 lines (154 loc) · 3.24 KB
/
main.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/signal"
"strconv"
"strings"
"github.com/c35s/hype/os/linux"
"github.com/c35s/hype/virtio"
"github.com/c35s/hype/vmm"
"golang.org/x/sys/unix"
"golang.org/x/term"
)
func main() {
var (
memSize = flag.Int("mem", 1024, "set the VM's memory size in MiB")
kernelPath = flag.String("kernel", "bzImage", "load bzImage from file or URL")
initrdPath = flag.String("initrd", "", "load initial ramdisk from file or URL")
cmdline = flag.String("cmdline", "console=hvc0 reboot=t", "set the kernel command line")
blkdev flagStrings
)
flag.Var(&blkdev, "block", "add a block device (multiple OK)")
flag.Parse()
bzImage, err := readURL(*kernelPath)
if err != nil {
panic(err)
}
ll := &linux.Loader{
Kernel: bzImage,
Cmdline: *cmdline,
}
if *initrdPath != "" {
initrd, err := readURL(*initrdPath)
if err != nil {
panic(err)
}
ll.Initrd = initrd
}
cfg := vmm.Config{
MemSize: *memSize << 20,
Devices: []virtio.DeviceConfig{
&virtio.ConsoleDevice{
In: os.Stdin,
Out: os.Stdout,
},
},
Loader: ll,
}
// block devices
for _, s := range blkdev {
s, ro := strings.CutSuffix(s, ":ro")
u, err := url.Parse(s)
if err != nil {
panic(err)
}
var stg virtio.BlockStorage
switch u.Scheme {
case "file", "":
var flg int
if !ro {
flg = os.O_RDWR
}
f, err := os.OpenFile(u.Path, flg, 0)
if err != nil {
panic(err)
}
stg = &virtio.FileStorage{
File: f,
}
case "http", "https":
ro = true
stg = &virtio.HTTPStorage{
URL: u.String(),
}
case "mem":
sz, err := strconv.ParseInt(u.Opaque, 10, 64)
if err != nil {
panic(err)
}
stg = &virtio.MemStorage{
Bytes: make([]byte, sz),
}
default:
panic("unsupported block storage scheme: " + u.Scheme)
}
cfg.Devices = append(cfg.Devices, &virtio.BlockDevice{
ReadOnly: ro,
Storage: stg,
})
}
m, err := vmm.New(cfg)
if err != nil {
panic(err)
}
if term.IsTerminal(int(os.Stdin.Fd())) {
old, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
defer term.Restore(int(os.Stdin.Fd()), old)
}
ctx, _ := signal.NotifyContext(context.Background(), unix.SIGINT, unix.SIGTERM)
err = m.Run(ctx)
if errors.Is(err, context.Canceled) {
return
}
if err != nil {
panic(err)
}
}
// readURL reads body from a file path or URL.
// It supports file, http, and https schemes.
func readURL(s string) (body []byte, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("hype: read URL %s: %w", s, err)
}
}()
u, err := url.Parse(s)
if err != nil {
return nil, err
}
switch u.Scheme {
case "", "file":
return os.ReadFile(u.Path)
case "http", "https":
res, err := http.Get(u.String())
if err != nil {
panic(err)
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("response status %d != %d", res.StatusCode, 200)
}
defer res.Body.Close()
return io.ReadAll(res.Body)
default:
panic(u.Scheme)
}
}
// flagStrings is a flag.Value that collects strings.
type flagStrings []string
func (*flagStrings) String() string {
return ""
}
func (fs *flagStrings) Set(s string) error {
*fs = append(*fs, s)
return nil
}