mirror of
https://github.com/containers/podman.git
synced 2025-07-29 19:33:13 +08:00
Merge pull request #14919 from gbraad/fedorawsl
Use prepared image for WSL machine init
This commit is contained in:
@ -6,29 +6,23 @@ package machine
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"os"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
githubURL = "http://github.com/fedora-cloud/docker-brew-fedora/"
|
githubLatestReleaseURL = "https://github.com/containers/podman-wsl-fedora/releases/latest/download/rootfs.tar.xz"
|
||||||
)
|
)
|
||||||
|
|
||||||
var fedoraxzRegex = regexp.MustCompile(`fedora[^\"]+xz`)
|
|
||||||
|
|
||||||
type FedoraDownload struct {
|
type FedoraDownload struct {
|
||||||
Download
|
Download
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFedoraDownloader(vmType, vmName, releaseStream string) (DistributionDownload, error) {
|
func NewFedoraDownloader(vmType, vmName, releaseStream string) (DistributionDownload, error) {
|
||||||
imageName, downloadURL, size, err := getFedoraDownload(releaseStream)
|
downloadURL, size, err := getFedoraDownload(githubLatestReleaseURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -38,6 +32,8 @@ func NewFedoraDownloader(vmType, vmName, releaseStream string) (DistributionDown
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
imageName := "rootfs.tar.xz"
|
||||||
|
|
||||||
f := FedoraDownload{
|
f := FedoraDownload{
|
||||||
Download: Download{
|
Download: Download{
|
||||||
Arch: getFcosArch(),
|
Arch: getFcosArch(),
|
||||||
@ -69,56 +65,21 @@ func (f FedoraDownload) HasUsableCache() (bool, error) {
|
|||||||
return info.Size() == f.Size, nil
|
return info.Size() == f.Size, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func truncRead(url string) ([]byte, error) {
|
func getFedoraDownload(releaseURL string) (*url.URL, int64, error) {
|
||||||
resp, err := http.Get(url)
|
downloadURL, err := url.Parse(releaseURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, -1, fmt.Errorf("invalid URL generated from discovered Fedora file: %s: %w", releaseURL, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
resp, err := http.Head(releaseURL)
|
||||||
if err := resp.Body.Close(); err != nil {
|
|
||||||
logrus.Error(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 10*1024*1024))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, -1, fmt.Errorf("head request failed: %s: %w", releaseURL, err)
|
||||||
}
|
|
||||||
|
|
||||||
_, _ = io.Copy(io.Discard, resp.Body)
|
|
||||||
|
|
||||||
return body, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getFedoraDownload(releaseStream string) (string, *url.URL, int64, error) {
|
|
||||||
dirURL := githubURL + "tree/" + releaseStream + "/" + getFcosArch() + "/"
|
|
||||||
body, err := truncRead(dirURL)
|
|
||||||
if err != nil {
|
|
||||||
return "", nil, -1, err
|
|
||||||
}
|
|
||||||
|
|
||||||
file := fedoraxzRegex.FindString(string(body))
|
|
||||||
if len(file) == 0 {
|
|
||||||
return "", nil, -1, fmt.Errorf("could not locate Fedora download at %s", dirURL)
|
|
||||||
}
|
|
||||||
|
|
||||||
rawURL := githubURL + "raw/" + releaseStream + "/" + getFcosArch() + "/"
|
|
||||||
newLocation := rawURL + file
|
|
||||||
downloadURL, err := url.Parse(newLocation)
|
|
||||||
if err != nil {
|
|
||||||
return "", nil, -1, fmt.Errorf("invalid URL generated from discovered Fedora file: %s: %w", newLocation, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := http.Head(newLocation)
|
|
||||||
if err != nil {
|
|
||||||
return "", nil, -1, fmt.Errorf("head request failed: %s: %w", newLocation, err)
|
|
||||||
}
|
}
|
||||||
_ = resp.Body.Close()
|
_ = resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return "", nil, -1, fmt.Errorf("head request failed [%d] on download: %s", resp.StatusCode, newLocation)
|
return nil, -1, fmt.Errorf("head request failed: %s: %w", releaseURL, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return file, downloadURL, resp.ContentLength, nil
|
return downloadURL, resp.ContentLength, nil
|
||||||
}
|
}
|
||||||
|
@ -386,7 +386,7 @@ func downloadDistro(v *MachineVM, opts machine.InitOptions) error {
|
|||||||
|
|
||||||
if _, e := strconv.Atoi(opts.ImagePath); e == nil {
|
if _, e := strconv.Atoi(opts.ImagePath); e == nil {
|
||||||
v.ImageStream = opts.ImagePath
|
v.ImageStream = opts.ImagePath
|
||||||
dd, err = machine.NewFedoraDownloader(vmtype, v.Name, v.ImageStream)
|
dd, err = machine.NewFedoraDownloader(vmtype, v.Name, opts.ImagePath)
|
||||||
} else {
|
} else {
|
||||||
v.ImageStream = "custom"
|
v.ImageStream = "custom"
|
||||||
dd, err = machine.NewGenericDownloader(vmtype, v.Name, opts.ImagePath)
|
dd, err = machine.NewGenericDownloader(vmtype, v.Name, opts.ImagePath)
|
||||||
@ -449,34 +449,14 @@ func provisionWSLDist(v *MachineVM) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dist := toDist(v.Name)
|
dist := toDist(v.Name)
|
||||||
fmt.Println("Importing operating system into WSL (this may take 5+ minutes on a new WSL install)...")
|
fmt.Println("Importing operating system into WSL (this may take a few minutes on a new WSL install)...")
|
||||||
if err = runCmdPassThrough("wsl", "--import", dist, distTarget, v.ImagePath); err != nil {
|
if err = runCmdPassThrough("wsl", "--import", dist, distTarget, v.ImagePath); err != nil {
|
||||||
return "", fmt.Errorf("the WSL import of guest OS failed: %w", err)
|
return "", fmt.Errorf("the WSL import of guest OS failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Installing packages (this will take awhile)...")
|
|
||||||
if err = runCmdPassThrough("wsl", "-d", dist, "dnf", "upgrade", "-y"); err != nil {
|
|
||||||
return "", fmt.Errorf("package upgrade on guest OS failed: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("Enabling Copr")
|
|
||||||
if err = runCmdPassThrough("wsl", "-d", dist, "dnf", "install", "-y", "'dnf-command(copr)'"); err != nil {
|
|
||||||
return "", fmt.Errorf("enabling copr failed: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("Enabling podman4 repo")
|
|
||||||
if err = runCmdPassThrough("wsl", "-d", dist, "dnf", "-y", "copr", "enable", "rhcontainerbot/podman4"); err != nil {
|
|
||||||
return "", fmt.Errorf("enabling copr failed: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = runCmdPassThrough("wsl", "-d", dist, "dnf", "install",
|
|
||||||
"podman", "podman-docker", "openssh-server", "procps-ng", "-y"); err != nil {
|
|
||||||
return "", fmt.Errorf("package installation on guest OS failed: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fixes newuidmap
|
// Fixes newuidmap
|
||||||
if err = runCmdPassThrough("wsl", "-d", dist, "dnf", "reinstall", "shadow-utils", "-y"); err != nil {
|
if err = runCmdPassThrough("wsl", "-d", dist, "rpm", "-q", "--restore", "shadow-utils", "2>/dev/null"); err != nil {
|
||||||
return "", fmt.Errorf("package reinstallation of shadow-utils on guest OS failed: %w", err)
|
return "", fmt.Errorf("package permissions restore of shadow-utils on guest OS failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Windows 11 (NT Version = 10, Build 22000) generates harmless but scary messages on every
|
// Windows 11 (NT Version = 10, Build 22000) generates harmless but scary messages on every
|
||||||
|
Reference in New Issue
Block a user