-
Notifications
You must be signed in to change notification settings - Fork 14
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.
+1,567
−2
Draft
Changes from all commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
filepath "path/filepath" | ||
|
||
"github.com/containers/podman-bootc/pkg/podman" | ||
"github.com/containers/podman-bootc/pkg/vm" | ||
"github.com/containers/podman-bootc/pkg/vm/domain" | ||
"github.com/containers/podman/v5/pkg/bindings" | ||
"github.com/spf13/cobra" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type installCmd struct { | ||
image string | ||
bootcCmdLine []string | ||
artifactsDir string | ||
diskPath string | ||
ctx context.Context | ||
socket string | ||
podmanSocketDir string | ||
libvirtDir string | ||
outputImage string | ||
containerStorage string | ||
configPath string | ||
outputPath string | ||
installVM *vm.InstallVM | ||
} | ||
|
||
func filterCmdlineArgs(args []string) ([]string, error) { | ||
sepIndex := -1 | ||
for i, arg := range args { | ||
if arg == "--" { | ||
sepIndex = i | ||
break | ||
} | ||
} | ||
if sepIndex == -1 { | ||
return nil, fmt.Errorf("no command line specified") | ||
} | ||
|
||
return args[sepIndex+1:], nil | ||
} | ||
|
||
func NewInstallCommand() *cobra.Command { | ||
c := installCmd{} | ||
cmd := &cobra.Command{ | ||
Use: "install", | ||
Short: "Install the OS Containers", | ||
Long: "Run bootc install to build the OS Containers. Specify the bootc cmdline after the '--'", | ||
RunE: c.doInstall, | ||
} | ||
cacheDir, err := os.UserCacheDir() | ||
if err != nil { | ||
cacheDir = "" | ||
} | ||
cacheDir = filepath.Join(cacheDir, "bootc") | ||
cmd.PersistentFlags().StringVar(&c.image, "bootc-image", "", "bootc-vm container image") | ||
cmd.PersistentFlags().StringVar(&c.artifactsDir, "dir", cacheDir, "directory where the artifacts are extracted") | ||
cmd.PersistentFlags().StringVar(&c.outputPath, "output-dir", "", "directory to store the output results") | ||
cmd.PersistentFlags().StringVar(&c.outputImage, "output-image", "", "path of the image to use for the installation") | ||
cmd.PersistentFlags().StringVar(&c.configPath, "config-dir", "", "path where to find the config.toml") | ||
cmd.PersistentFlags().StringVar(&c.containerStorage, "container-storage", podman.DefaultContainerStorage(), "Container storage to use") | ||
cmd.PersistentFlags().StringVar(&c.socket, "podman-socket", podman.DefaultPodmanSocket(), "path to the podman socket") | ||
if args, err := filterCmdlineArgs(os.Args); err == nil { | ||
c.bootcCmdLine = args | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(NewInstallCommand()) | ||
} | ||
|
||
func (c *installCmd) validateArgs() error { | ||
if c.image == "" { | ||
return fmt.Errorf("the bootc-image cannot be empty") | ||
} | ||
if c.artifactsDir == "" { | ||
return fmt.Errorf("the artifacts directory path cannot be empty") | ||
} | ||
if c.outputImage == "" { | ||
return fmt.Errorf("the output-image needs to be set") | ||
} | ||
if c.outputPath == "" { | ||
return fmt.Errorf("the output-path needs to be set") | ||
} | ||
if c.configPath == "" { | ||
return fmt.Errorf("the config-dir needs to be set") | ||
} | ||
if c.containerStorage == "" { | ||
return fmt.Errorf("the container storage cannot be empty") | ||
} | ||
if c.socket == "" { | ||
return fmt.Errorf("the socket for podman cannot be empty") | ||
} | ||
if len(c.bootcCmdLine) == 0 { | ||
return fmt.Errorf("the bootc commandline needs to be specified after the '--'") | ||
} | ||
var err error | ||
c.ctx, err = bindings.NewConnection(context.Background(), "unix://"+c.socket) | ||
if err != nil { | ||
return fmt.Errorf("failed to connect to podman at %s: %v", c.socket, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *installCmd) installBuildVM(kernel, initrd string) error { | ||
image := filepath.Join(c.outputPath, c.outputImage) | ||
outputImageFormat, err := domain.GetDiskInfo(image) | ||
if err != nil { | ||
return err | ||
} | ||
c.installVM = vm.NewInstallVM(filepath.Join(c.libvirtDir, "virtqemud-sock"), vm.InstallOptions{ | ||
OutputFormat: outputImageFormat, | ||
OutputImage: filepath.Join(vm.OutputDir, c.outputImage), // Path relative to the container filesystem | ||
Root: false, | ||
Kernel: kernel, | ||
Initrd: initrd, | ||
}) | ||
if err := c.installVM.Run(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *installCmd) doInstall(_ *cobra.Command, _ []string) error { | ||
if err := c.validateArgs(); err != nil { | ||
return err | ||
} | ||
c.libvirtDir = filepath.Join(c.artifactsDir, "libvirt") | ||
if _, err := os.Stat(c.libvirtDir); os.IsNotExist(err) { | ||
if err := os.Mkdir(c.libvirtDir, 0755); err != nil { | ||
return err | ||
} | ||
} | ||
c.podmanSocketDir = filepath.Join(c.artifactsDir, "podman") | ||
if _, err := os.Stat(c.podmanSocketDir); os.IsNotExist(err) { | ||
if err := os.Mkdir(c.podmanSocketDir, 0755); err != nil { | ||
return err | ||
} | ||
} | ||
remoteSocket := filepath.Join(c.podmanSocketDir, "podman-vm.sock") | ||
vmCont := podman.NewVMContainer(c.image, c.socket, &podman.RunVMContainerOptions{ | ||
ContainerStoragePath: c.containerStorage, | ||
ConfigDir: c.configPath, | ||
OutputDir: c.outputPath, | ||
SocketDir: c.podmanSocketDir, | ||
LibvirtSocketDir: c.libvirtDir, | ||
}) | ||
if err := vmCont.Run(); err != nil { | ||
return err | ||
} | ||
defer vmCont.Stop() | ||
|
||
kernel, initrd, err := vmCont.GetBootArtifacts() | ||
if err != nil { | ||
return err | ||
} | ||
log.Debugf("Boot artifacts kernel: %s and initrd: %s", kernel, initrd) | ||
|
||
if err := c.installBuildVM(kernel, initrd); err != nil { | ||
return err | ||
} | ||
defer c.installVM.Stop() | ||
|
||
if err := podman.RunPodmanCmd(remoteSocket, c.image, c.bootcCmdLine); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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,28 @@ | ||
FROM quay.io/fedora/fedora:42 | ||
|
||
RUN dnf install -y \ | ||
libvirt-client \ | ||
libvirt-daemon \ | ||
libvirt-daemon-driver-qemu \ | ||
libvirt-daemon-driver-storage-core \ | ||
qemu-kvm \ | ||
socat \ | ||
virt-install \ | ||
virtiofsd \ | ||
&& dnf clean all | ||
|
||
RUN mkdir -p /home/qemu && chown -R qemu:qemu /home/qemu | ||
RUN mkdir -p /etc/libvirt /vm_files | ||
|
||
COPY containerfiles/vm/entrypoint.sh /entrypoint.sh | ||
COPY ./bin/vsock-proxy /usr/local/bin/vsock-proxy | ||
COPY containerfiles/vm/files /vm_files | ||
COPY containerfiles/vm/qemu.conf /etc/libvirt/qemu.conf | ||
COPY containerfiles/vm/virtqemud.conf /etc/libvirt/virtqemud.conf | ||
COPY containerfiles/vm/virtiofsd-wrapper /usr/local/bin/virtiofsd-wrapper | ||
|
||
EXPOSE 5959 | ||
|
||
RUN dnf install -y socat | ||
|
||
ENTRYPOINT ["/entrypoint.sh"] |
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,34 @@ | ||
#!/usr/bin/bash | ||
|
||
set -xe | ||
|
||
BOOTC_ROOT=/bootc-data | ||
|
||
# Inject the binaries, systemd and configuration files in the bootc image | ||
mkdir -p ${BOOTC_ROOT}/etc/sysusers.d | ||
mkdir -p ${BOOTC_ROOT}/usr/lib/containers/storage | ||
cp /vm_files/bootc.conf ${BOOTC_ROOT}/etc/sysusers.d/bootc.conf | ||
cp /vm_files/podman-vsock-proxy.service ${BOOTC_ROOT}/etc/systemd/system/podman-vsock-proxy.service | ||
cp /vm_files/mount-vfsd-targets.service ${BOOTC_ROOT}/etc/systemd/system/mount-vfsd-targets.service | ||
cp /vm_files/mount-vfsd-targets.sh ${BOOTC_ROOT}/usr/local/bin/mount-vfsd-targets.sh | ||
cp /vm_files/container-storage.conf ${BOOTC_ROOT}/etc/containers/storage.conf | ||
cp /vm_files/selinux-config ${BOOTC_ROOT}/etc/selinux/config | ||
cp /vm_files/sudoers-bootc ${BOOTC_ROOT}/etc/sudoers.d/bootc | ||
cp /usr/local/bin/vsock-proxy ${BOOTC_ROOT}/usr/local/bin/vsock-proxy | ||
|
||
# Enable systemd services | ||
chroot ${BOOTC_ROOT} systemctl enable mount-vfsd-targets | ||
chroot ${BOOTC_ROOT} systemctl enable podman.socket | ||
chroot ${BOOTC_ROOT} systemctl enable podman-vsock-proxy.service | ||
# Create an empty password for the bootc user | ||
entry='bootc::20266::::::' | ||
echo $entry >> ${BOOTC_ROOT}/etc/shadow | ||
|
||
# Start proxy the VM port 1234 to unix socket | ||
vsock-proxy --log-level debug -s /run/podman/podman-vm.sock -p 1234 --cid 3 \ | ||
--listen-mode unixToVsock &> /var/log/vsock-proxy.log & | ||
|
||
# Finally, start libvirt | ||
/usr/sbin/virtlogd & | ||
/usr/bin/virtstoraged & | ||
/usr/sbin/virtqemud -v -t 0 |
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 @@ | ||
u bootc - "Bootc User" /home/bootc /bin/bash |
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,14 @@ | ||
[storage] | ||
|
||
driver = "overlay" | ||
runroot = "/run/containers/storage" | ||
graphroot = "/var/lib/containers/storage" | ||
|
||
[storage.options] | ||
additionalimagestores = [ | ||
"/usr/lib/containers/storage", | ||
"/usr/lib/bootc/container_storage", | ||
] | ||
pull_options = {enable_partial_images = "true", use_hard_links = "false", ostree_repos=""} | ||
[storage.options.overlay] | ||
mountopt = "nodev,metacopy=on" |
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,12 @@ | ||
[Unit] | ||
Description=Mount all virtiofs targets | ||
After=local-fs.target | ||
ConditionPathExists=/sys/fs/virtiofs | ||
|
||
[Service] | ||
Type=oneshot | ||
ExecStart=/usr/local/bin/mount-vfsd-targets.sh | ||
RemainAfterExit=true | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
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,9 @@ | ||
#!/bin/bash | ||
|
||
set -xe | ||
mkdir -p /usr/lib/bootc/config | ||
mkdir -p /usr/lib/bootc/container_storage | ||
mkdir -p /usr/lib/bootc/output | ||
mount -t virtiofs config /usr/lib/bootc/config | ||
mount -t virtiofs storage /usr/lib/bootc/container_storage | ||
mount -t virtiofs output /usr/lib/bootc/output |
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,15 @@ | ||
[Unit] | ||
Description=Proxy vsock (PORT: 1234) to Unix podman socket | ||
After=network.target | ||
Requires=network.target | ||
|
||
[Service] | ||
Type=simple | ||
ExecStart=/usr/local/bin/vsock-proxy --log-level debug --cid 3 --port 1234 \ | ||
--socket /var/run/podman/podman.sock --listen-mode vsockToUnix | ||
Restart=always | ||
RestartSec=3 | ||
|
||
[Install] | ||
WantedBy=multi-user.target | ||
|
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 @@ | ||
SELINUX=disabled |
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 @@ | ||
bootc ALL=(ALL) NOPASSWD: ALL |
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,10 @@ | ||
stdio_handler = "logd" | ||
vnc_listen = "0.0.0.0" | ||
vnc_tls = 0 | ||
vnc_sasl = 0 | ||
user = "qemu" | ||
group = "qemu" | ||
dynamic_ownership = 1 | ||
remember_owner = 0 | ||
namespaces = [ ] | ||
cgroup_controllers = [ ] |
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,6 @@ | ||
#!/bin/bash | ||
exec /usr/libexec/virtiofsd \ | ||
--sandbox=none \ | ||
--cache=auto --modcaps=-mknod \ | ||
--log-level debug \ | ||
"$@" |
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,3 @@ | ||
listen_tls = 0 | ||
listen_tcp = 0 | ||
log_outputs = "1:stderr" |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.