Skip to content

Commit

Permalink
Allow only one concurrent download with flock
Browse files Browse the repository at this point in the history
  • Loading branch information
enescakir committed Jan 31, 2024
1 parent b11578c commit d4150b6
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions rhizome/host/lib/vm_setup.rb
Expand Up @@ -448,22 +448,23 @@ def download_boot_image(boot_image, force: false, custom_url: nil)
fail "Unsupported boot_image format: #{image_ext}"
end

# Use of File::EXCL provokes a crash rather than a race
# condition if two VMs are lazily getting their images at the
# same time.
download_path = File.join(vp.image_root, boot_image + image_ext + ".tmp")
File.open(download_path, File::RDWR | File::CREAT | File::EXCL, 0o644) do
r "curl -f -L10 -o #{download_path.shellescape} #{download.shellescape}"
end
File.open(File.join(vp.image_root, boot_image + ".lock"), File::RDWR | File::CREAT) do |lock|
fail "Another vm is downloading #{boot_image}" unless lock.flock(File::LOCK_EX | File::LOCK_NB)

download_path = File.join(vp.image_root, boot_image + image_ext + ".tmp")
File.open(download_path, File::RDWR | File::CREAT, 0o644) do
r "curl -f -L10 -o #{download_path.shellescape} #{download.shellescape}"
end

temp_path = File.join(vp.image_root, boot_image + ".raw.tmp")
if initial_format != "raw"
# 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 #{download_path.shellescape} #{temp_path.shellescape}"
rm_if_exists(download_path)
temp_path = File.join(vp.image_root, boot_image + ".raw.tmp")
if initial_format != "raw"
# 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 #{download_path.shellescape} #{temp_path.shellescape}"
rm_if_exists(download_path)
end
File.rename(temp_path, image_path)
end
File.rename(temp_path, image_path)
end

# Unnecessary if host has this set before creating the netns, but
Expand Down

0 comments on commit d4150b6

Please sign in to comment.