Skip to content

Commit

Permalink
Fixes containers#14698 Use prepared image for WSL machine init
Browse files Browse the repository at this point in the history
Signed-off-by: Gerard Braad <me@gbraad.nl>
  • Loading branch information
gbraad authored Jul 13, 2022
1 parent 6999e9a commit f649b41
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 23 deletions.
65 changes: 65 additions & 0 deletions pkg/machine/fedorawsl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//go:build amd64 || arm64
// +build amd64 arm64

package machine

import (
"fmt"
"net/http"
"net/url"
"path/filepath"

"github.com/pkg/errors"
)

const (
githubLatestReleaseURL = "https://github.com/gbraad/podman-wsl-fedora/releases/latest/download/rootfs.tar.xz"
)

func NewFedoraWSLDownloader(vmType, vmName, releaseStream string) (DistributionDownload, error) {
// TODO:
imageName, downloadURL, size, err := getFedoraWSLDownload(githubLatestReleaseURL)
if err != nil {
return nil, err
}

dataDir, err := GetDataDir(vmType)
if err != nil {
return nil, err
}

f := FedoraDownload{
Download: Download{
Arch: getFcosArch(),
Artifact: artifact,
Format: Format,
ImageName: imageName,
LocalPath: filepath.Join(dataDir, imageName),
URL: downloadURL,
VMName: vmName,
Size: size,
},
}
f.Download.LocalUncompressedFile = f.getLocalUncompressedName()
return f, nil
}

func getFedoraWSLDownload(releaseURL string) (string, *url.URL, int64, error) {
resp, err := http.Head(releaseURL)

downloadURL, err := url.Parse(releaseURL)
if err != nil {
return "", nil, -1, errors.Wrapf(err, "invalid URL generated from discovered Fedora file: %s", releaseURL)
}

if err != nil {
return "", nil, -1, errors.Wrapf(err, "head request failed: %s", releaseURL)
}
_ = resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return "", nil, -1, fmt.Errorf("head request failed [%d] on download: %s", resp.StatusCode, releaseURL)
}

return "podman-wsl-f35.tar.gz", downloadURL, resp.ContentLength, nil
}
26 changes: 3 additions & 23 deletions pkg/machine/wsl/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func downloadDistro(v *MachineVM, opts machine.InitOptions) error {

if _, e := strconv.Atoi(opts.ImagePath); e == nil {
v.ImageStream = opts.ImagePath
dd, err = machine.NewFedoraDownloader(vmtype, v.Name, v.ImageStream)
dd, err = machine.NewFedoraWSLDownloader(vmtype, v.Name, v.ImageStream)
} else {
v.ImageStream = "custom"
dd, err = machine.NewGenericDownloader(vmtype, v.Name, opts.ImagePath)
Expand Down Expand Up @@ -415,29 +415,9 @@ func provisionWSLDist(v *MachineVM) (string, error) {
return "", errors.Wrap(err, "WSL import of guest OS failed")
}

fmt.Println("Installing packages (this will take awhile)...")
if err = runCmdPassThrough("wsl", "-d", dist, "dnf", "upgrade", "-y"); err != nil {
return "", errors.Wrap(err, "package upgrade on guest OS failed")
}

fmt.Println("Enabling Copr")
if err = runCmdPassThrough("wsl", "-d", dist, "dnf", "install", "-y", "'dnf-command(copr)'"); err != nil {
return "", errors.Wrap(err, "enabling copr failed")
}

fmt.Println("Enabling podman4 repo")
if err = runCmdPassThrough("wsl", "-d", dist, "dnf", "-y", "copr", "enable", "rhcontainerbot/podman4"); err != nil {
return "", errors.Wrap(err, "enabling copr failed")
}

if err = runCmdPassThrough("wsl", "-d", dist, "dnf", "install",
"podman", "podman-docker", "openssh-server", "procps-ng", "-y"); err != nil {
return "", errors.Wrap(err, "package installation on guest OS failed")
}

// Fixes newuidmap
if err = runCmdPassThrough("wsl", "-d", dist, "dnf", "reinstall", "shadow-utils", "-y"); err != nil {
return "", errors.Wrap(err, "package reinstallation of shadow-utils on guest OS failed")
if err = runCmdPassThrough("wsl", "-d", dist, "rpm", "-q", "--restore", "shadow-utils", "2>/dev/null"); err != nil {
return "", errors.Wrap(err, "package permissions restore of shadow-utils on guest OS failed")
}

// Windows 11 (NT Version = 10, Build 22000) generates harmless but scary messages on every
Expand Down

0 comments on commit f649b41

Please sign in to comment.