Skip to content

Commit 53296e2

Browse files
committed
Stop compressing applehv and hyperv by default
Apple Hypervisor doesn't inherently require images to be compressed with gzip. It's just that when we _do_ compress it, it's the most convenient format to use because gzip is guaranteed to be available on macOS. Similarly for Windows Hyper-V and ZIP. Notably, this is different from e.g. GCP, where the platform itself dictates a `tar.gz` file. And so for consistency we should have the output from the build step for `applehv` and `hyperv` just return the disk image in the format it's intended to be used in, and then `cosa compress` just compresses them using e.g. `gzip` or `zip`. This requires adding a new `platform-compressor` key in the `image.yaml` file to allow overridding the default `compressor` setting for certain platforms. This allows folks using the same code to build the disk images for those platforms and compress them with the compressor of their choice.
1 parent b3b8bd7 commit 53296e2

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

src/cmd-compress

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ else:
5353
print(f"Targeting build: {build}")
5454

5555
# common extensions for known compressors
56-
ext_dict = {'xz': '.xz', 'gzip': '.gz', 'zstd': '.zst'}
56+
ext_dict = {'xz': '.xz', 'gzip': '.gz', 'zstd': '.zst', 'zip': '.zip'}
5757

5858

5959
def get_cpu_param(param):
@@ -65,7 +65,7 @@ def strip_ext(path):
6565
return path.rsplit(".", 1)[0]
6666

6767

68-
def compress_one_builddir(builddir):
68+
def compress_one_builddir(builddir, platform_compressors):
6969
print(f"Compressing: {builddir}")
7070
buildmeta_path = os.path.join(builddir, 'meta.json')
7171
# In the case where we are doing builds for different architectures
@@ -77,9 +77,6 @@ def compress_one_builddir(builddir):
7777
with open(buildmeta_path) as f:
7878
buildmeta = json.load(f)
7979

80-
# Find what extension to use based on the selected compressor
81-
ext = ext_dict[args.compressor]
82-
8380
tmpdir = 'tmp/compress'
8481
if os.path.isdir(tmpdir):
8582
shutil.rmtree(tmpdir)
@@ -106,6 +103,10 @@ def compress_one_builddir(builddir):
106103
if only_artifacts is not None and img_format not in only_artifacts:
107104
continue
108105

106+
compressor = platform_compressors.get(img_format) or args.compressor
107+
# Find what extension to use based on the selected compressor
108+
ext = ext_dict[compressor]
109+
109110
file = img['path']
110111
filepath = os.path.join(builddir, file)
111112
if img.get('uncompressed-sha256') is None:
@@ -116,12 +117,16 @@ def compress_one_builddir(builddir):
116117
img['uncompressed-size'] = img['size']
117118
with open(tmpfile, 'wb') as f:
118119
t = ncpu()
119-
if args.compressor == 'xz':
120+
if compressor == 'xz':
120121
runcmd(['xz', '-c9', f'-T{t}', filepath], stdout=f)
121-
elif args.compressor == 'zstd':
122+
elif compressor == 'zstd':
122123
runcmd(['zstd', '-10', '-c', f'-T{t}', filepath], stdout=f)
123-
else:
124+
elif compressor == 'gzip':
124125
runcmd(['gzip', f'-{gzip_level}', '-c', filepath], stdout=f) # pylint: disable=E0606
126+
elif compressor == 'zip':
127+
runcmd(['zip', '-9j', '-', filepath], stdout=f) # pylint: disable=E0606
128+
else:
129+
raise Exception(f"Unknown compressor: {compressor}")
125130
file_with_ext = file + ext
126131
filepath_with_ext = filepath + ext
127132
compressed_size = os.path.getsize(tmpfile)
@@ -202,6 +207,8 @@ def uncompress_one_builddir(builddir):
202207
runcmd(['zstd', '-dc', filepath], stdout=f)
203208
elif file.endswith('gz'):
204209
runcmd(['gzip', '-dc', filepath], stdout=f)
210+
elif file.endswith('zip'):
211+
runcmd(['unzip', '-p', filepath], stdout=f)
205212
else:
206213
print(f"Unknown sufix of file {file}")
207214
file_without_ext = strip_ext(file)
@@ -266,16 +273,17 @@ changed = []
266273
if args.mode == "compress":
267274
# Find what compressor we should use, either picking it up from
268275
# CLI args or from image.json
276+
image_json = get_image_json()
269277
gzip_level = 6
270278
if args.fast:
271279
args.compressor = 'gzip'
272280
gzip_level = 1
273281
elif not args.compressor:
274-
image_json = get_image_json()
275282
args.compressor = image_json.get('compressor', DEFAULT_COMPRESSOR)
276283
for arch in builds.get_build_arches(build):
277284
builddir = builds.get_build_dir(build, arch)
278-
changed.append(compress_one_builddir(builddir))
285+
changed.append(compress_one_builddir(builddir,
286+
image_json.get('platform-compressor', {})))
279287
if not any(changed):
280288
print("All builds already compressed")
281289
elif args.mode == "uncompress":

src/cosalib/qemuvariants.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@
5454
},
5555
"applehv": {
5656
"image_format": "raw",
57-
"image_suffix": "raw.gz",
58-
"platform": "applehv",
59-
"compression": "gzip"
57+
"platform": "applehv"
6058
},
6159
"azure": {
6260
"image_format": "vpc",
@@ -99,9 +97,7 @@
9997
},
10098
"hyperv": {
10199
"image_format": "vhdx",
102-
"image_suffix": "vhdx.zip",
103-
"platform": "hyperv",
104-
"compression": "zip"
100+
"platform": "hyperv"
105101
},
106102
"kubevirt": {
107103
"image_format": "qcow2",

0 commit comments

Comments
 (0)