| 1 | # ex:ts=8 sw=4: | ||
| 2 | # $OpenBSD$ | ||
| 3 | # | ||
| 4 | # Copyright (c) 2024 Dick Olsson <hi@senzilla.io> | ||
| 5 | # | ||
| 6 | # Permission to use, copy, modify, and distribute this software for any | ||
| 7 | # purpose with or without fee is hereby granted, provided that the above | ||
| 8 | # copyright notice and this permission notice appear in all copies. | ||
| 9 | # | ||
| 10 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 11 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 12 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 13 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 14 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 15 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 16 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 17 | |||
| 18 | use v5.36; | ||
| 19 | |||
| 20 | package App::FuguVM::Miniroot; | ||
| 21 | our $VERSION = '0.3.1'; | ||
| 22 | |||
| 23 | use Fugu::File; | ||
| 24 | |||
| 25 | # App::FuguVM::Miniroot - the OpenBSD miniroot install media. | ||
| 26 | # | ||
| 27 | # The miniroot is what the installer boots from. Its concern here is | ||
| 28 | # the install media: the file name of a version, and the cached copy. | ||
| 29 | # The mirror object holds the mirror facts: the host, the URL, the | ||
| 30 | # download and the verification. The miniroot is the boot medium, so | ||
| 31 | # no code in the guest inspects it before it runs. The host must | ||
| 32 | # therefore verify it, and the mirror does. | ||
| 33 | # | ||
| 34 | # The module caches the install media. App::FuguVM::DiskCache caches | ||
| 35 | # the disk that the installer produced. Neither is a cache of the | ||
| 36 | # other. | ||
| 37 | |||
| 38 | # $class->new($cache_dir, $proxy, $mirror): | ||
| 39 | # The proxy can be undef. The mirror is required: it carries the | ||
| 40 | # version, the architecture and the verification switch. | ||
| 41 | sub new ( $class, $cache_dir, $proxy, $mirror ) | ||
| 42 | { | ||
| 43 | die "App::FuguVM::Miniroot needs a mirror\n" | ||
| 44 | if !defined $mirror; | ||
| 45 | |||
| 46 | my $self = bless { | ||
| 47 | cache_dir => Fugu::File->expand_tilde($cache_dir), | ||
| 48 | proxy => $proxy, | ||
| 49 | mirror => $mirror, | ||
| 50 | }, $class; | ||
| 51 | |||
| 52 | return $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | # $self->path: | ||
| 56 | # Return the path to the cached miniroot image of the mirror | ||
| 57 | # version. Return undef if the image is not cached. | ||
| 58 | sub path ($self) | ||
| 59 | { | ||
| 60 | my $path = $self->_image_path; | ||
| 61 | return -f $path ? $path : undef; | ||
| 62 | } | ||
| 63 | |||
| 64 | # $self->ensure: | ||
| 65 | # Make sure that the image is available. The mirror downloads | ||
| 66 | # and verifies it when the cache misses. Return the path on | ||
| 67 | # success, or undef on failure with the reason in the mirror | ||
| 68 | # error. | ||
| 69 | sub ensure ($self) | ||
| 70 | { | ||
| 71 | my $path = $self->path; | ||
| 72 | return $path if defined $path; | ||
| 73 | |||
| 74 | return $self->{mirror}->ensure( 'release', $self->_image_filename ); | ||
| 75 | } | ||
| 76 | |||
| 77 | # $self->url: | ||
| 78 | # Return the mirror URL of the miniroot image. The mirror builds | ||
| 79 | # it, so the host has one home. | ||
| 80 | sub url ($self) | ||
| 81 | { | ||
| 82 | return $self->{mirror}->url( $self->_image_filename ); | ||
| 83 | } | ||
| 84 | |||
| 85 | # $self->_image_filename: | ||
| 86 | # Make the miniroot filename of the mirror version, for example | ||
| 87 | # "miniroot78.img". The mirror is the one home of the version, | ||
| 88 | # so this module holds no copy. | ||
| 89 | sub _image_filename ($self) | ||
| 90 | { | ||
| 91 | ( my $ver = $self->{mirror}->version ) =~ s/\.//g; | ||
| 92 | return "miniroot$ver.img"; | ||
| 93 | } | ||
| 94 | |||
| 95 | # $self->_image_path: | ||
| 96 | # Return the file that the miniroot lands in. | ||
| 97 | # | ||
| 98 | # The answer comes from the cache, not from a copy of its layout | ||
| 99 | # here. A cache that changed where it puts a URL would otherwise | ||
| 100 | # leave this module looking in the old place, and every run would | ||
| 101 | # download the image again. | ||
| 102 | sub _image_path ($self) | ||
| 103 | { | ||
| 104 | return $self->_cache->cache_path( $self->url ); | ||
| 105 | } | ||
| 106 | |||
| 107 | # $self->_cache: | ||
| 108 | # Return the cache to ask. A proxy brings its own; without one, | ||
| 109 | # the module builds a cache over the same directory so that a | ||
| 110 | # lookup still resolves. | ||
| 111 | sub _cache ($self) | ||
| 112 | { | ||
| 113 | return $self->{proxy}->cache if defined $self->{proxy}; | ||
| 114 | |||
| 115 | require App::FuguVM::Proxy; | ||
| 116 | return App::FuguVM::Proxy::Cache->new( $self->{cache_dir} ); | ||
| 117 | } | ||
| 118 | |||
| 119 | 1; |