| 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::Disk; | ||
| 21 | our $VERSION = '0.3.1'; | ||
| 22 | |||
| 23 | use File::Basename; | ||
| 24 | use Fugu::File; | ||
| 25 | use Fugu::Log; | ||
| 26 | use Fugu::Process; | ||
| 27 | use JSON::PP (); | ||
| 28 | |||
| 29 | sub new ( $class, $state_dir ) | ||
| 30 | { | ||
| 31 | my $self = bless { state_dir => $state_dir, }, $class; | ||
| 32 | |||
| 33 | return $self; | ||
| 34 | } | ||
| 35 | |||
| 36 | # $self->create($name, $size, $backing_image): | ||
| 37 | # Create the VM disk image. $size can be undef for an overlay. | ||
| 38 | # The overlay then inherits the virtual size of $backing_image. | ||
| 39 | # Every backing image is a qcow2: a cached base image or a | ||
| 40 | # snapshot. | ||
| 41 | # | ||
| 42 | # The method returns early when the path already exists. Thus | ||
| 43 | # callers that replace a disk with an overlay must unlink the | ||
| 44 | # disk first. | ||
| 45 | sub create ( $self, $name, $size = undef, $backing_image = undef ) | ||
| 46 | { | ||
| 47 | my $path = $self->path($name); | ||
| 48 | |||
| 49 | Fugu::File->ensure_dir( dirname($path) ) or return; | ||
| 50 | |||
| 51 | return $path if -f $path; # Already exists | ||
| 52 | |||
| 53 | my @cmd = ( 'qemu-img', 'create', '-f', 'qcow2' ); | ||
| 54 | |||
| 55 | if ( defined $backing_image ) { | ||
| 56 | push @cmd, '-b', $backing_image, '-F', 'qcow2'; | ||
| 57 | } | ||
| 58 | |||
| 59 | push @cmd, $path; | ||
| 60 | push @cmd, $size if defined $size; | ||
| 61 | |||
| 62 | # The capture also swallows the verbose "Formatting..." line of | ||
| 63 | # qemu-img, which no caller wants to see | ||
| 64 | my $result = Fugu::Process->run( cmd => \@cmd ); | ||
| 65 | |||
| 66 | unless ( $result->{success} ) { | ||
| 67 | Fugu::Log->default->error( 'Failed to create disk image %s: %s', | ||
| 68 | $path, | ||
| 69 | $result->{stderr} || $result->{error} || 'unknown' ); | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | return $path; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub path ( $self, $name ) | ||
| 77 | { | ||
| 78 | return "$self->{state_dir}/$name/disk.qcow2"; | ||
| 79 | } | ||
| 80 | |||
| 81 | # $class_or_self->convert($source, $target, %opts): | ||
| 82 | # Convert $source into a fresh image at $target. This method is | ||
| 83 | # the one home of 'qemu-img convert'. The 'format' option is | ||
| 84 | # 'qcow2' or 'raw', and the default is 'qcow2'. A raw target is | ||
| 85 | # sparse. The 'backing' option names a parent image, and the | ||
| 86 | # target then stores only the difference. The method also works | ||
| 87 | # on the class, because it reads no state directory. Return the | ||
| 88 | # target path, or undef after a diagnostic. | ||
| 89 | sub convert ( $, $source, $target, %opts ) | ||
| 90 | { | ||
| 91 | my $format = $opts{format} // 'qcow2'; | ||
| 92 | |||
| 93 | if ( !-f $source ) { | ||
| 94 | Fugu::Log->default->error( 'Cannot convert missing image: %s', | ||
| 95 | $source ); | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | |||
| 99 | my @cmd = ( 'qemu-img', 'convert', '-O', $format ); | ||
| 100 | push @cmd, '-B', $opts{backing}, '-F', 'qcow2' | ||
| 101 | if defined $opts{backing}; | ||
| 102 | push @cmd, $source, $target; | ||
| 103 | |||
| 104 | my $result = Fugu::Process->run( cmd => \@cmd ); | ||
| 105 | unless ( $result->{success} ) { | ||
| 106 | Fugu::Log->default->error( 'Failed to convert %s to %s: %s', | ||
| 107 | $source, $target, | ||
| 108 | $result->{stderr} || $result->{error} || 'unknown' ); | ||
| 109 | return; | ||
| 110 | } | ||
| 111 | |||
| 112 | return $target; | ||
| 113 | } | ||
| 114 | |||
| 115 | # $self->info($name): | ||
| 116 | # Get the qemu-img report on the disk as a hashref. The method | ||
| 117 | # returns undef when there is no disk or when it cannot read the | ||
| 118 | # disk. The inspection is read-only. Thus it asks for shared | ||
| 119 | # access with -U. A running QEMU holds an exclusive lock. Without | ||
| 120 | # shared access, the query fails on exactly the VMs whose backing | ||
| 121 | # chain callers most need to resolve. If 'cache clear' skips the | ||
| 122 | # disk of a running VM, it can remove the base from under that | ||
| 123 | # VM. | ||
| 124 | sub info ( $self, $name ) | ||
| 125 | { | ||
| 126 | my $path = $self->path($name); | ||
| 127 | return if !-f $path; | ||
| 128 | |||
| 129 | # The inspection asks for shared access with -U, so it also | ||
| 130 | # works against the disk of a running VM | ||
| 131 | my $result = Fugu::Process->run( | ||
| 132 | cmd => [ 'qemu-img', 'info', '-U', '--output=json', $path ] ); | ||
| 133 | return if !$result->{success}; | ||
| 134 | |||
| 135 | return eval { JSON::PP->new->utf8->decode( $result->{stdout} ) }; | ||
| 136 | } | ||
| 137 | |||
| 138 | # $self->backing_file($name): | ||
| 139 | # Get the absolute path of the image that backs the disk. The | ||
| 140 | # method returns undef when the disk is standalone or when it | ||
| 141 | # cannot inspect the disk. qemu-img reports a backing reference | ||
| 142 | # even when the file it names is gone. This lets callers diagnose | ||
| 143 | # a broken chain. | ||
| 144 | sub backing_file ( $self, $name ) | ||
| 145 | { | ||
| 146 | my $info = $self->info($name); | ||
| 147 | return if !defined $info; | ||
| 148 | |||
| 149 | my $backing = $info->{'full-backing-filename'} | ||
| 150 | // $info->{'backing-filename'}; | ||
| 151 | return if !defined $backing || $backing eq ''; | ||
| 152 | |||
| 153 | # Relative references resolve against the disk's own directory | ||
| 154 | if ( $backing !~ m{^/} ) { | ||
| 155 | $backing = dirname( $self->path($name) ) . "/$backing"; | ||
| 156 | } | ||
| 157 | |||
| 158 | return $backing; | ||
| 159 | } | ||
| 160 | |||
| 161 | # P5: Check the disk image integrity. The method returns a hashref | ||
| 162 | # with the 'status' and 'output' keys. The 'status' key is 'ok' or | ||
| 163 | # 'corrupted'. | ||
| 164 | sub check ( $self, $name ) | ||
| 165 | { | ||
| 166 | my $path = $self->path($name); | ||
| 167 | return if !-f $path; | ||
| 168 | |||
| 169 | my $result = | ||
| 170 | Fugu::Process->run( cmd => [ 'qemu-img', 'check', $path ] ); | ||
| 171 | my $output = $result->{stdout} . $result->{stderr}; | ||
| 172 | |||
| 173 | return { | ||
| 174 | status => $result->{success} ? 'ok' : 'corrupted', | ||
| 175 | output => $output, | ||
| 176 | path => $path, | ||
| 177 | }; | ||
| 178 | } | ||
| 179 | |||
| 180 | # P5: Repair the disk image. The method returns true on success and | ||
| 181 | # false on failure. | ||
| 182 | sub repair ( $self, $name ) | ||
| 183 | { | ||
| 184 | my $path = $self->path($name); | ||
| 185 | return 0 if !-f $path; | ||
| 186 | |||
| 187 | # Run qemu-img check with the repair option | ||
| 188 | my $result = Fugu::Process->run( | ||
| 189 | cmd => [ 'qemu-img', 'check', '-r', 'all', $path ] ); | ||
| 190 | |||
| 191 | return $result->{success} ? 1 : 0; | ||
| 192 | } | ||
| 193 | |||
| 194 | 1; |