+sub clone_image {
+ my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
+
+ die "storage definintion has no path\n" if !$scfg->{path};
+
+ my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
+ $class->parse_volname($volname);
+
+ die "clone_image on wrong vtype '$vtype'\n" if $vtype ne 'images';
+
+ die "this storage type does not support clone_image on snapshot\n" if $snap;
+
+ die "this storage type does not support clone_image on subvolumes\n" if $format eq 'subvol';
+
+ die "clone_image only works on base images\n" if !$isBase;
+
+ my $imagedir = $class->get_subdir($scfg, 'images');
+ $imagedir .= "/$vmid";
+
+ mkpath $imagedir;
+
+ my $name = $class->find_free_diskname($imagedir, $scfg, $vmid, "qcow2", 1);
+
+ warn "clone $volname: $vtype, $name, $vmid to $name (base=../$basevmid/$basename)\n";
+
+ my $path = "$imagedir/$name";
+
+ die "disk image '$path' already exists\n" if -e $path;
+
+ my $server = &$get_active_server($scfg, 1);
+ my $glustervolume = $scfg->{volume};
+ my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
+
+ my $cmd = ['/usr/bin/qemu-img', 'create', '-b', "../$basevmid/$basename",
+ '-F', $format, '-f', 'qcow2', $volumepath];
+
+ run_command($cmd, errmsg => "unable to create image");
+
+ return "$basevmid/$basename/$vmid/$name";
+}
+
+sub alloc_image {
+ my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
+
+ my $imagedir = $class->get_subdir($scfg, 'images');
+ $imagedir .= "/$vmid";
+
+ mkpath $imagedir;
+
+ $name = $class->find_free_diskname($imagedir, $scfg, $vmid, $fmt, 1) if !$name;
+
+ my (undef, $tmpfmt) = parse_name_dir($name);
+
+ die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
+ if $tmpfmt ne $fmt;
+
+ my $path = "$imagedir/$name";
+
+ die "disk image '$path' already exists\n" if -e $path;
+
+ my $server = &$get_active_server($scfg, 1);
+ my $glustervolume = $scfg->{volume};
+ my $volumepath = "gluster://$server/$glustervolume/images/$vmid/$name";
+
+ my $cmd = ['/usr/bin/qemu-img', 'create'];
+
+ push @$cmd, '-o', 'preallocation=metadata' if $fmt eq 'qcow2';
+
+ push @$cmd, '-f', $fmt, $volumepath, "${size}K";
+
+ eval { run_command($cmd, errmsg => "unable to create image"); };
+ if ($@) {
+ unlink $path;
+ rmdir $imagedir;
+ die "$@";
+ }
+
+ return "$vmid/$name";
+}
+