warn $@ if $@;
}
};
+
+sub mkfs {
+ my ($dev) = @_;
+ my $cmd = ['mkfs.ext4', '-O', 'mmp', $dev];
+ PVE::Tools::run_command($cmd);
+}
+
+sub format_disk {
+ my ($storage_cfg, $volid) = @_;
+
+ if ($volid =~ m@^/dev/.+@) {
+ return mkfs($volid);
+ }
+
+ my ($storage, $volname) = PVE::Storage::parse_volume_id($volid, 1);
+
+ die "cannot format volume $volid with no storage" if !$storage;
+
+ my $path = PVE::Storage::path($storage_cfg, $volid, undef);
+
+ my ($vtype, undef, undef, undef, undef, $isBase, $format) =
+ PVE::Storage::parse_volname($storage_cfg, $volid);
+
+ if ($format eq 'raw' || $format eq 'subvol') {
+ return mkfs($path);
+ }
+}
my $create_disks = sub {
my ($storecfg, $vmid, $settings, $conf) = @_;
if ($size > 0) {
$volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw',
undef, $size);
+ format_disk($storecfg, $volid);
} else {
$volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'subvol',
undef, 0);
} elsif ($scfg->{type} eq 'drbd') {
$volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw', undef, $size);
+ format_disk($storecfg, $volid);
} elsif ($scfg->{type} eq 'rbd') {
die "krbd option must be enabled on storage type '$scfg->{type}'\n" if !$scfg->{krbd};
$volid = PVE::Storage::vdisk_alloc($storecfg, $storage, $vmid, 'raw', undef, $size);
+ format_disk($storecfg, $volid);
} else {
die "unable to create containers on storage type '$scfg->{type}'\n";
}
}
sub umount_all {
- my ($vmid, $storage_cfg, $conf, $noerr) = @_;
+ my ($vmid, $storage_cfg, $conf, $noerr, $loopdevs) = @_;
- my $loopdevs = loopdevices_list();
+ $loopdevs ||= loopdevices_list();
my $rootdir = "/var/lib/lxc/$vmid/rootfs";
my $volid_list = get_vm_volumes($conf);
}
sub mount_all {
- my ($vmid, $storage_cfg, $conf, $format_raw_images) = @_;
+ my ($vmid, $storage_cfg, $conf) = @_;
my $rootdir = "/var/lib/lxc/$vmid/rootfs";
my $volid_list = get_vm_volumes($conf);
PVE::Storage::activate_volumes($storage_cfg, $volid_list);
+ my $loopdevs;
eval {
- my $loopdevs = attach_loops($storage_cfg, $volid_list);
+ $loopdevs = attach_loops($storage_cfg, $volid_list);
foreach_mountpoint($conf, sub {
my ($ms, $mountpoint) = @_;
die "unable to mount base volume - internal error" if $isBase;
- if ($format_raw_images && $format eq 'raw') {
- my $cmd = ['mkfs.ext4', '-O', 'mmp', $image_path];
- PVE::Tools::run_command($cmd);
- }
-
mountpoint_mount($mountpoint, $rootdir, $storage_cfg, $loopdevs);
});
};
umount_all($vmid, $storage_cfg, $conf, 1);
}
+ return ($rootdir, $loopdevs) if wantarray;
return $rootdir;
}
PVE::LXC::create_config($vmid, $conf);
}
+ my $loopdevs;
eval {
- my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf, 1);
+ (my $rootdir, $loopdevs) = PVE::LXC::mount_all($vmid, $storage_cfg, $conf);
restore_and_configure($vmid, $archive, $rootdir, $conf, $password, $restore);
};
if (my $err = $@) {
warn $err;
- PVE::LXC::umount_all($vmid, $storage_cfg, $conf, 1);
+ if ($loopdevs) {
+ # mount_all unmounts on error so we only need to unmount
+ # if it actually returns valid $loopdevs
+ PVE::LXC::umount_all($vmid, $storage_cfg, $conf, 1, $loopdevs);
+ }
} else {
- PVE::LXC::umount_all($vmid, $storage_cfg, $conf, 0);
+ PVE::LXC::umount_all($vmid, $storage_cfg, $conf, 0, $loopdevs);
}
PVE::Storage::deactivate_volumes($storage_cfg, PVE::LXC::get_vm_volumes($conf));