Skip to content

Commit

Permalink
Do not convert the image if it's already in .raw format
Browse files Browse the repository at this point in the history
We convert 'qcow2', 'img', and 'vhd' images to 'raw' format to enable
their use with Cloud Hypervisor. If the source image is already in
'raw' format, there's no need for conversion; we simply move it.

At present, we don't have any source images in 'raw' format. However, I
plan to distribute GitHub runner images via our internal MinIO cluster.
Instead of uploading 'vhd' images, I will upload the converted 'raw'
images to MinIO. This way, each VM host won't need to convert images,
which has two main benefits:

  - Converting 86GB images typically takes 6-7 minutes. Distributing
    'raw' images will speed up the image download operation on VM hosts.
  - When each VM host converts the image locally, the resulting raw
    images are not identical, as their checksums differ. By distributing
    a 'raw' image, we can ensure that all VM hosts have the exact same
    image file.
  • Loading branch information
enescakir committed Feb 2, 2024
1 parent b4aba9c commit 906ad3d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 9 additions & 3 deletions rhizome/host/lib/vm_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ def download_boot_image(boot_image, custom_url: nil)
"qcow2"
when ".vhd"
"vpc"
when ".raw"
"raw"
else
fail "Unsupported boot_image format: #{image_ext}"
end
Expand All @@ -458,9 +460,13 @@ def download_boot_image(boot_image, custom_url: nil)
end
end

# Images are presumed to be atomically renamed into the path,
# i.e. no partial images will be passed to qemu-image.
r "qemu-img convert -p -f #{initial_format.shellescape} -O raw #{temp_path.shellescape} #{image_path.shellescape}"
if initial_format == "raw"
File.rename(temp_path, image_path)
else
# Images are presumed to be atomically renamed into the path,
# i.e. no partial images will be passed to qemu-image.
r "qemu-img convert -p -f #{initial_format.shellescape} -O raw #{temp_path.shellescape} #{image_path.shellescape}"
end

rm_if_exists(temp_path)
end
Expand Down
14 changes: 14 additions & 0 deletions rhizome/host/spec/vm_setup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ def key_wrapping_secrets
vs.download_boot_image("github-ubuntu-2204", custom_url: "https://images.blob.core.windows.net/images/ubuntu2204.vhd?sp=r&st=2023-09-05T22:44:05Z&se=2023-10-07T06:44:05")
end

it "does not convert image if it's in raw format already" do
expect(File).to receive(:exist?).with("/var/storage/images/github-ubuntu-2204.raw").and_return(false)
expect(File).to receive(:open) do |path, *_args|
expect(path).to eq("/var/storage/images/github-ubuntu-2204.raw.tmp")
end.and_yield
expect(FileUtils).to receive(:mkdir_p).with("/var/storage/images/")
expect(vs).to receive(:r).with("which azcopy")
expect(vs).to receive(:r).with("AZCOPY_CONCURRENCY_VALUE=5 azcopy copy https://images.blob.core.windows.net/images/ubuntu2204.raw\\?sp\\=r\\&st\\=2023-09-05T22:44:05Z\\&se\\=2023-10-07T06:44:05 /var/storage/images/github-ubuntu-2204.raw.tmp")
expect(File).to receive(:rename).with("/var/storage/images/github-ubuntu-2204.raw.tmp", "/var/storage/images/github-ubuntu-2204.raw")
expect(FileUtils).to receive(:rm_r).with("/var/storage/images/github-ubuntu-2204.raw.tmp")

vs.download_boot_image("github-ubuntu-2204", custom_url: "https://images.blob.core.windows.net/images/ubuntu2204.raw?sp=r&st=2023-09-05T22:44:05Z&se=2023-10-07T06:44:05")
end

it "can use an image that's already downloaded" do
expect(File).to receive(:exist?).with("/var/storage/images/almalinux-9.1.raw").and_return(true)
vs.download_boot_image("almalinux-9.1")
Expand Down

0 comments on commit 906ad3d

Please sign in to comment.