+sub umount_all {
+ my ($vmid, $storage_cfg, $conf, $noerr) = @_;
+
+ my $loopdevs = loopdevices_list();
+
+ my $rootdir = "/var/lib/lxc/$vmid/rootfs";
+ my $volid_list = get_vm_volumes($conf);
+
+ foreach_mountpoint_reverse($conf, sub {
+ my ($ms, $mountpoint) = @_;
+
+ my $volid = $mountpoint->{volume};
+ my $mount = $mountpoint->{mp};
+
+ return if !$volid || !$mount;
+
+ $mount_path = "$rootdir/$mount";
+
+ # fixme: test if mounted?
+ eval {
+ PVE::Tools::run_command(['umount', '-d', $mountpoint_path]);
+ };
+ if (my $err = $@) {
+ if ($noerr) {
+ warn $err;
+ } else {
+ die $err;
+ }
+ }
+ });
+
+ PVE::LXC::dettach_loops($storage_cfg, $volid_list);
+}
+
+sub mount_all {
+ my ($vmid, $storage_cfg, $conf, $format_raw_images) = @_;
+
+ my $rootdir = "/var/lib/lxc/$vmid/rootfs";
+
+ my $volid_list = get_vm_volumes($conf);
+ PVE::Storage::activate_volumes($storage_cfg, $volid_list);
+
+ eval {
+ my $loopdevs = attach_loops($storage_cfg, $volid_list);
+
+ foreach_mountpoint($conf, sub {
+ my ($ms, $mountpoint) = @_;
+
+ my $volid = $mountpoint->{volume};
+ my $mount = $mountpoint->{mp};
+
+ return if !$volid || !$mount;
+
+ my $image_path = PVE::Storage::path($storage_cfg, $volid);
+ my ($vtype, undef, undef, undef, undef, $isBase, $format) =
+ PVE::Storage::parse_volname($storage_cfg, $volid);
+
+ die "unable to mount base volume - internal error" if $isBase;
+
+ if ($format_raw_images && $format eq 'raw') {
+ my $cmd = ['mkfs.ext4', $image_path];
+ PVE::Tools::run_command($cmd);
+ }
+
+ mountpoint_mount($mountpoint, $rootdir, $storage_cfg, $loopdevs);
+ });
+ };
+ if (my $err = $@) {
+ warn "mounting container failed - $err";
+ umount_all($vmid, $storage_cfg, $conf, 1);
+ } else {
+ umount_all($vmid, $storage_cfg, $conf, 0);
+ }
+
+ return $rootdir;
+}
+
+