Skip to content

Commit 0d1a8c0

Browse files
Merge pull request #21768 from baude/zstd
zstd now default compression for podman machine
2 parents f756e5d + a31e8d2 commit 0d1a8c0

8 files changed

Lines changed: 84 additions & 70 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ require (
4141
github.com/hashicorp/go-multierror v1.1.1
4242
github.com/hugelgupf/p9 v0.3.1-0.20230822151754-54f5c5530921
4343
github.com/json-iterator/go v1.1.12
44+
github.com/klauspost/compress v1.17.5
4445
github.com/linuxkit/virtsock v0.0.0-20220523201153-1a23e78aa7a2
4546
github.com/mattn/go-shellwords v1.0.12
4647
github.com/mattn/go-sqlite3 v1.14.21
@@ -149,7 +150,6 @@ require (
149150
github.com/inconshreveable/mousetrap v1.1.0 // indirect
150151
github.com/jinzhu/copier v0.4.0 // indirect
151152
github.com/josharian/intern v1.0.0 // indirect
152-
github.com/klauspost/compress v1.17.5 // indirect
153153
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
154154
github.com/klauspost/pgzip v1.2.6 // indirect
155155
github.com/kr/fs v0.1.0 // indirect

pkg/machine/compression/compression_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ func Test_compressionFromFile(t *testing.T) {
3333
want: Bz2,
3434
},
3535
{
36-
name: "default is xz",
36+
name: "default is zstd",
3737
args: args{
3838
path: "/tmp/foo",
3939
},
40-
want: Xz,
40+
want: Zstd,
4141
},
4242
}
4343
for _, tt := range tests {
@@ -76,9 +76,9 @@ func TestImageCompression_String(t *testing.T) {
7676
want: "zip",
7777
},
7878
{
79-
name: "xz is default",
79+
name: "zstd is default",
8080
c: 99,
81-
want: "xz",
81+
want: "zst",
8282
},
8383
}
8484
for _, tt := range tests {

pkg/machine/compression/config.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const (
99
Zip
1010
Gz
1111
Bz2
12+
Zstd
1213
)
1314

1415
func KindFromFile(path string) ImageCompression {
@@ -19,8 +20,10 @@ func KindFromFile(path string) ImageCompression {
1920
return Gz
2021
case strings.HasSuffix(path, Zip.String()):
2122
return Zip
23+
case strings.HasSuffix(path, Xz.String()):
24+
return Xz
2225
}
23-
return Xz
26+
return Zstd
2427
}
2528

2629
func (c ImageCompression) String() string {
@@ -31,6 +34,8 @@ func (c ImageCompression) String() string {
3134
return "zip"
3235
case Bz2:
3336
return "bz2"
37+
case Xz:
38+
return "xz"
3439
}
35-
return "xz"
40+
return "zst"
3641
}

pkg/machine/compression/decompress.go

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/containers/podman/v5/utils"
1919
"github.com/containers/storage/pkg/archive"
2020
crcOs "github.com/crc-org/crc/v2/pkg/os"
21+
"github.com/klauspost/compress/zstd"
2122
"github.com/sirupsen/logrus"
2223
"github.com/ulikunitz/xz"
2324
)
@@ -59,12 +60,22 @@ func Decompress(localPath *define.VMFile, uncompressedPath string) error {
5960
if err != nil {
6061
return err
6162
}
63+
// darwin really struggles with sparse files. being diligent here
6264
fmt.Printf("Copying uncompressed file %q to %q/n", localPath.GetPath(), dstFile.Name())
65+
66+
// Keeping CRC implementation for now, but ideally this could be pruned and
67+
// sparsewriter could be used. in that case, this area needs rework or
68+
// sparsewriter be made to honor the *file interface
6369
_, err = crcOs.CopySparse(uncompressedFileWriter, dstFile)
6470
return err
6571
case archive.Gzip:
6672
if runtime.GOOS == "darwin" {
67-
return decompressGzWithSparse(prefix, localPath, uncompressedPath)
73+
return decompressGzWithSparse(prefix, localPath, uncompressedFileWriter)
74+
}
75+
fallthrough
76+
case archive.Zstd:
77+
if runtime.GOOS == "darwin" {
78+
return decompressZstdWithSparse(prefix, localPath, uncompressedFileWriter)
6879
}
6980
fallthrough
7081
default:
@@ -225,22 +236,31 @@ func decompressZip(prefix string, src string, output io.WriteCloser) error {
225236
return err
226237
}
227238

228-
func decompressGzWithSparse(prefix string, compressedPath *define.VMFile, uncompressedPath string) error {
229-
stat, err := os.Stat(compressedPath.GetPath())
230-
if err != nil {
231-
return err
232-
}
233-
234-
dstFile, err := os.OpenFile(uncompressedPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, stat.Mode())
235-
if err != nil {
236-
return err
237-
}
239+
func decompressWithSparse(prefix string, compressedReader io.Reader, uncompressedFile *os.File) error {
240+
dstFile := NewSparseWriter(uncompressedFile)
238241
defer func() {
239242
if err := dstFile.Close(); err != nil {
240-
logrus.Errorf("unable to close uncompressed file %s: %q", uncompressedPath, err)
243+
logrus.Errorf("unable to close uncompressed file %s: %q", uncompressedFile.Name(), err)
241244
}
242245
}()
243246

247+
// TODO remove the following line when progress bars work
248+
_ = prefix
249+
// p, bar := utils.ProgressBar(prefix, stat.Size(), prefix+": done")
250+
// proxyReader := bar.ProxyReader(f)
251+
// defer func() {
252+
// if err := proxyReader.Close(); err != nil {
253+
// logrus.Error(err)
254+
// }
255+
// }()
256+
257+
// p.Wait()
258+
_, err := io.Copy(dstFile, compressedReader)
259+
return err
260+
}
261+
262+
func decompressGzWithSparse(prefix string, compressedPath *define.VMFile, uncompressedFileWriter *os.File) error {
263+
logrus.Debugf("decompressing %s", compressedPath.GetPath())
244264
f, err := os.Open(compressedPath.GetPath())
245265
if err != nil {
246266
return err
@@ -260,20 +280,34 @@ func decompressGzWithSparse(prefix string, compressedPath *define.VMFile, uncomp
260280
logrus.Errorf("unable to close gzreader: %q", err)
261281
}
262282
}()
283+
// This way we get something to look at in debug mode
284+
defer func() {
285+
logrus.Debug("decompression complete")
286+
}()
287+
return decompressWithSparse(prefix, gzReader, uncompressedFileWriter)
288+
}
263289

264-
// TODO remove the following line when progress bars work
265-
_ = prefix
266-
// p, bar := utils.ProgressBar(prefix, stat.Size(), prefix+": done")
267-
// proxyReader := bar.ProxyReader(f)
268-
// defer func() {
269-
// if err := proxyReader.Close(); err != nil {
270-
// logrus.Error(err)
271-
// }
272-
// }()
273-
290+
func decompressZstdWithSparse(prefix string, compressedPath *define.VMFile, uncompressedFileWriter *os.File) error {
274291
logrus.Debugf("decompressing %s", compressedPath.GetPath())
275-
_, err = crcOs.CopySparse(dstFile, gzReader)
276-
logrus.Debug("decompression complete")
277-
// p.Wait()
278-
return err
292+
f, err := os.Open(compressedPath.GetPath())
293+
if err != nil {
294+
return err
295+
}
296+
defer func() {
297+
if err := f.Close(); err != nil {
298+
logrus.Errorf("unable to close on compressed file %s: %q", compressedPath.GetPath(), err)
299+
}
300+
}()
301+
302+
zstdReader, err := zstd.NewReader(f)
303+
if err != nil {
304+
return err
305+
}
306+
defer zstdReader.Close()
307+
308+
// This way we get something to look at in debug mode
309+
defer func() {
310+
logrus.Debug("decompression complete")
311+
}()
312+
return decompressWithSparse(prefix, zstdReader, uncompressedFileWriter)
279313
}

pkg/machine/define/image_format.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package define
22

3+
import "fmt"
4+
35
type ImageFormat int64
46

57
const (
@@ -22,13 +24,9 @@ func (imf ImageFormat) Kind() string {
2224
}
2325

2426
func (imf ImageFormat) KindWithCompression() string {
25-
switch imf {
26-
case Vhdx:
27-
return "vhdx.zip"
28-
case Tar:
27+
// Tar uses xz; all others use zstd
28+
if imf == Tar {
2929
return "tar.xz"
30-
case Raw:
31-
return "raw.gz"
3230
}
33-
return "qcow2.xz"
31+
return fmt.Sprintf("%s.zst", imf.Kind())
3432
}

pkg/machine/define/image_format_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ func TestImageFormat_KindWithCompression(t *testing.T) {
4545
want string
4646
}{
4747
{
48-
name: "vhdx.zip",
48+
name: "vhdx",
4949
imf: Vhdx,
50-
want: "vhdx.zip",
50+
want: "vhdx.zst",
5151
},
5252
{
5353
name: "qcow2",
5454
imf: Qcow,
55-
want: "qcow2.xz",
55+
want: "qcow2.zst",
5656
},
5757
{
58-
name: "raw.gz",
58+
name: "raw",
5959
imf: Raw,
60-
want: "raw.gz",
60+
want: "raw.zst",
6161
}, {
6262
name: "tar.xz",
6363
imf: Tar,

pkg/machine/ignition/ignition.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,6 @@ ExecStart=
178178
ExecStart=-/usr/sbin/agetty --autologin root --noclear %I $TERM
179179
`
180180

181-
deMoby := parser.NewUnitFile()
182-
deMoby.Add("Unit", "Description", "Remove moby-engine")
183-
deMoby.Add("Unit", "After", "systemd-machine-id-commit.service")
184-
deMoby.Add("Unit", "Before", "zincati.service")
185-
deMoby.Add("Unit", "ConditionPathExists", "!/var/lib/%N.stamp")
186-
187-
deMoby.Add("Service", "Type", "oneshot")
188-
deMoby.Add("Service", "RemainAfterExit", "yes")
189-
deMoby.Add("Service", "ExecStart", "/usr/bin/rpm-ostree override remove moby-engine")
190-
deMoby.Add("Service", "ExecStart", "/usr/bin/rpm-ostree ex apply-live --allow-replacement")
191-
deMoby.Add("Service", "ExecStartPost", "/bin/touch /var/lib/%N.stamp")
192-
193-
deMoby.Add("Install", "WantedBy", "default.target")
194-
deMobyFile, err := deMoby.ToString()
195-
if err != nil {
196-
return err
197-
}
198-
199181
// This service gets environment variables that are provided
200182
// through qemu fw_cfg and then sets them into systemd/system.conf.d,
201183
// profile.d and environment.d files
@@ -252,11 +234,6 @@ ExecStart=-/usr/sbin/agetty --autologin root --noclear %I $TERM
252234
Name: "docker.socket",
253235
Mask: BoolToPtr(true),
254236
},
255-
{
256-
Enabled: BoolToPtr(true),
257-
Name: "remove-moby.service",
258-
Contents: &deMobyFile,
259-
},
260237
{
261238
// Disable auto-updating of fcos images
262239
// https://github.com/containers/podman/issues/20122
@@ -871,7 +848,7 @@ func GetNetRecoveryUnitFile() *parser.UnitFile {
871848

872849
func DefaultReadyUnitFile() parser.UnitFile {
873850
u := parser.NewUnitFile()
874-
u.Add("Unit", "After", "remove-moby.service sshd.socket sshd.service")
851+
u.Add("Unit", "After", "sshd.socket sshd.service")
875852
u.Add("Unit", "OnFailure", "emergency.target")
876853
u.Add("Unit", "OnFailureJobMode", "isolate")
877854
u.Add("Service", "Type", "oneshot")

pkg/machine/ocipull/ociartifact.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const (
2727
// TODO This is temporary until we decide on a proper image name
2828
artifactRegistry = "quay.io"
2929
artifactRepo = "baude"
30-
artifactImageName = "podman-machine-images-art"
30+
artifactImageName = "stage-podman-machine"
3131
artifactOriginalName = "org.opencontainers.image.title"
3232
machineOS = "linux"
3333
)

0 commit comments

Comments
 (0)