-
Notifications
You must be signed in to change notification settings - Fork 15
Create installation VM and run bootc install inside a VM using rootless podman #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
alicefr
wants to merge
7
commits into
containers:main
Choose a base branch
from
alicefr:add-appliance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
abd9df0
Build vm image for bootc installation VM
alicefr 76f7c4c
Add podman package
alicefr bc1f15d
Add proxy for VSOCK
alicefr e6d3d39
utils: add generic function for pointers
alicefr 98016b8
Add domain package
alicefr 2893086
vm: create installation VM
alicefr 0c3202e
cmd: add install command
alicefr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package vsock | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net" | ||
"os" | ||
|
||
"github.com/mdlayher/vsock" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type Proxy struct { | ||
cid uint32 | ||
port uint32 | ||
socket string | ||
done chan struct{} | ||
start func(socket string, port, cid uint32, done chan struct{}) error | ||
} | ||
|
||
func NewProxyUnixSocketToVsock(port, cid uint32, socket string) *Proxy { | ||
p := &Proxy{ | ||
cid: cid, | ||
port: port, | ||
socket: socket, | ||
done: make(chan struct{}), | ||
start: startUnixToVsock, | ||
} | ||
return p | ||
} | ||
|
||
func NewProxyVSockToUnixSocket(port uint32, socket string) *Proxy { | ||
p := &Proxy{ | ||
port: port, | ||
socket: socket, | ||
done: make(chan struct{}), | ||
start: startVsockToUnix, | ||
} | ||
return p | ||
} | ||
|
||
func (proxy *Proxy) GetSocket() string { | ||
return proxy.socket | ||
} | ||
|
||
func (proxy *Proxy) Stop() { | ||
select { | ||
case <-proxy.done: | ||
// already closed | ||
default: | ||
close(proxy.done) | ||
} | ||
os.Remove(proxy.socket) | ||
log.Debugf("Stopped proxy") | ||
} | ||
|
||
func (p *Proxy) Start() error { | ||
return p.start(p.socket, p.port, p.cid, p.done) | ||
} | ||
|
||
func startUnixToVsock(socket string, port, cid uint32, done chan struct{}) error { | ||
_ = os.Remove(socket) | ||
|
||
unixListener, err := net.Listen("unix", socket) | ||
if err != nil { | ||
return fmt.Errorf("Failed to listen on unix socket: %v", err) | ||
} | ||
go func() { | ||
defer unixListener.Close() | ||
|
||
for { | ||
select { | ||
case <-done: | ||
return | ||
default: | ||
unixConn, err := unixListener.Accept() | ||
if err != nil { | ||
log.Warnf("Accept error: %v", err) | ||
continue | ||
} | ||
log.Debugf("Accepted connection from %s to port %d and cid", socket, port, cid) | ||
|
||
go handleConnectionToVsock(unixConn, port, cid, done) | ||
} | ||
} | ||
}() | ||
|
||
log.Debugf("Started proxy at: %s", socket) | ||
|
||
return nil | ||
} | ||
|
||
func handleConnectionToVsock(unixConn net.Conn, port, cid uint32, done chan struct{}) { | ||
defer unixConn.Close() | ||
vsockConn, err := vsock.Dial(cid, port, nil) | ||
if err != nil { | ||
log.Printf("vsock connect error (cid: %d, port: %d): %v", cid, port, err) | ||
return | ||
} | ||
defer vsockConn.Close() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
errCh := make(chan error, 2) | ||
go proxy(ctx, vsockConn, unixConn, errCh, done) | ||
go proxy(ctx, unixConn, vsockConn, errCh, done) | ||
|
||
// Wait for the first error or cancellation | ||
select { | ||
case <-done: | ||
case err := <-errCh: | ||
if err != nil && err != io.EOF { | ||
log.Errorf("proxy error: %v", err) | ||
} | ||
} | ||
} | ||
|
||
func proxy(ctx context.Context, src, dst net.Conn, errCh chan error, done chan struct{}) { | ||
go func() { | ||
_, err := io.Copy(dst, src) | ||
errCh <- err | ||
}() | ||
select { | ||
case <-ctx.Done(): | ||
case <-done: | ||
case <-errCh: | ||
} | ||
} | ||
|
||
func startVsockToUnix(socket string, port, cid uint32, done chan struct{}) error { | ||
vsockListener, err := vsock.Listen(port, &vsock.Config{}) | ||
if err != nil { | ||
return fmt.Errorf("failed to listen on vsock port %d: %v", port, err) | ||
} | ||
go func() { | ||
defer vsockListener.Close() | ||
|
||
for { | ||
select { | ||
case <-done: | ||
return | ||
default: | ||
vsockConn, err := vsockListener.Accept() | ||
if err != nil { | ||
log.Warnf("Accept error: %v", err) | ||
continue | ||
} | ||
log.Debugf("Accepted connection from port %d to socket %d", port, socket) | ||
|
||
go handleConnectionToUnix(vsockConn, socket, port, done) | ||
} | ||
} | ||
}() | ||
|
||
log.Debugf("Started proxy at port: %d", port) | ||
|
||
return nil | ||
} | ||
|
||
func handleConnectionToUnix(vsockConn net.Conn, socket string, port uint32, done chan struct{}) { | ||
defer vsockConn.Close() | ||
|
||
conn, err := net.Dial("unix", socket) | ||
if err != nil { | ||
log.Errorf("failed to connect: %v", err) | ||
} | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
errCh := make(chan error, 2) | ||
go proxy(ctx, conn, vsockConn, errCh, done) | ||
go proxy(ctx, vsockConn, conn, errCh, done) | ||
|
||
// Wait for the first error or cancellation | ||
select { | ||
case <-done: | ||
case err := <-errCh: | ||
if err != nil && err != io.EOF { | ||
log.Errorf("proxy error: %v", err) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/containers/podman-bootc/pkg/vsock" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type mode string | ||
|
||
const ( | ||
unixToVsock mode = "unixToVsock" | ||
vsockToUnix mode = "vsockToUnix" | ||
) | ||
|
||
func (m *mode) String() string { | ||
return string(*m) | ||
} | ||
|
||
func (m *mode) Set(val string) error { | ||
switch val { | ||
case string(vsockToUnix), string(unixToVsock): | ||
*m = mode(val) | ||
return nil | ||
default: | ||
return fmt.Errorf("invalid mode: %s (must be '%s' or '%s')", val, unixToVsock, vsockToUnix) | ||
} | ||
} | ||
|
||
func (m *mode) Type() string { | ||
return "mode" | ||
} | ||
|
||
type rootCmd struct { | ||
proxy *vsock.Proxy | ||
logLevel string | ||
listenMode mode | ||
cid uint32 | ||
port uint32 | ||
socket string | ||
} | ||
|
||
func NewRootCmd() *cobra.Command { | ||
c := rootCmd{} | ||
cmd := &cobra.Command{ | ||
Use: "proxy", | ||
Short: "Proxy connections between VSOCK and UNIX socket", | ||
Long: "Proxy the connection between VSOCK and UNIX socket based on the direction", | ||
PersistentPreRunE: c.preExec, | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
return c.run() | ||
}, | ||
} | ||
|
||
cmd.PersistentFlags().Uint32VarP(&c.cid, "cid", "c", 0, "CID allocated by the VM") | ||
cmd.PersistentFlags().Uint32VarP(&c.port, "port", "p", 0, "Port for the VSOCK on the VM") | ||
cmd.PersistentFlags().StringVarP(&c.socket, "socket", "s", "", "Socket for the proxy") | ||
cmd.PersistentFlags().StringVarP(&c.logLevel, "log-level", "", "", "Set log level") | ||
cmd.PersistentFlags().VarP(&c.listenMode, "listen-mode", "l", | ||
fmt.Sprintf("Direction for the listentin proxy, values: %s or %s", unixToVsock, vsockToUnix)) | ||
cmd.MarkPersistentFlagRequired("port") | ||
cmd.MarkPersistentFlagRequired("socket") | ||
cmd.MarkPersistentFlagRequired("listen-mode") | ||
|
||
return cmd | ||
} | ||
|
||
func (c *rootCmd) preExec(cmd *cobra.Command, args []string) error { | ||
if c.logLevel != "" { | ||
level, err := log.ParseLevel(c.logLevel) | ||
if err != nil { | ||
return err | ||
} | ||
log.SetLevel(level) | ||
} else { | ||
log.SetLevel(log.InfoLevel) | ||
} | ||
socket, _ := cmd.Flags().GetString("socket") | ||
if socket == "" { | ||
return fmt.Errorf("the socket needs to be set") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *rootCmd) validateArgs() error { | ||
if c.port == 0 { | ||
return fmt.Errorf("the port cannot be 0") | ||
} | ||
if c.listenMode == unixToVsock && c.cid == 0 { | ||
return fmt.Errorf("the cid cannot be 0 when the listen mode is unixToVsock") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *rootCmd) run() error { | ||
if err := c.validateArgs(); err != nil { | ||
return err | ||
} | ||
switch c.listenMode { | ||
case vsockToUnix: | ||
c.proxy = vsock.NewProxyVSockToUnixSocket(c.port, c.socket) | ||
case unixToVsock: | ||
c.proxy = vsock.NewProxyUnixSocketToVsock(c.port, c.cid, c.socket) | ||
} | ||
|
||
if err := c.proxy.Start(); err != nil { | ||
return err | ||
} | ||
defer c.proxy.Stop() | ||
|
||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) | ||
defer stop() | ||
<-ctx.Done() | ||
|
||
return nil | ||
} | ||
|
||
func Execute() { | ||
if err := NewRootCmd().Execute(); err != nil { | ||
os.Exit(1) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "github.com/containers/podman-bootc/proxy/cmd" | ||
|
||
func main() { | ||
cmd.Execute() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, you can use https://github.com/inetaf/tcpproxy to implement this kind of proxying, see https://github.com/crc-org/vfkit/blob/main/pkg/vf/vsock.go for an example of this.
Not sure it’s going to be significantly less code though, so the additional dependency is not necessarily worth it.