Ever since the first Vyatta versions that supported image upgrade (that's 6.0/Larkspur for the curious), when a user runs add system image https://example.com/vyos-1.3.0-amd64.iso, the update script:
- Downloads the image without caching it.
- Checks if there's enough space for the upgrade.
- Performs the upgrade.
This is alright if there's enough drive space for the upgrade. If not, it's:
- Annoying because you need to repeat the procedure.
- Time-consuming on slow links.
- Expensive on metered connections.
Checking free space _before_ downloading would be a big UX improvement in that case.
We can do it the following way:
- Try an HTTP HEAD request for the image URL.
- If that request succeeds and returns a Content-Length header, then calculate the required space precisely based on that data.
- If there's no Content-Length in the response (or the protocol doesn't have an equivalent of HEAD and Content-Length), then use some heuristic, e.g. 500M, and do a precise check after the image is downloaded.