From 644f2f8f7a238523e612e5ae769d2074c5de5338 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 7 Aug 2015 10:31:41 +0200 Subject: [PATCH] allow to use drbd storage for containers --- src/PVE/API2/LXC.pm | 6 +++++- src/PVE/LXC.pm | 5 +++++ src/PVE/LXCCreate.pm | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm index 87fadd3..d25d4a6 100644 --- a/src/PVE/API2/LXC.pm +++ b/src/PVE/API2/LXC.pm @@ -77,6 +77,10 @@ my $alloc_rootfs = sub { $volid = PVE::Storage::vdisk_alloc($storage_conf, $storage, $vmid, 'subvol', "subvol-$vmid-rootfs", $size); + } elsif ($scfg->{type} eq 'drbd') { + + $volid = PVE::Storage::vdisk_alloc($storage_conf, $storage, $vmid, 'raw', undef, $size); + } else { die "unable to create containers on storage type '$scfg->{type}'\n"; } @@ -243,7 +247,7 @@ __PACKAGE__->register_method({ my $scfg = PVE::Storage::storage_check_node($storage_cfg, $storage, $node); raise_param_exc({ storage => "storage '$storage' does not support container root directories"}) - if !$scfg->{content}->{rootdir}; + if !($scfg->{content}->{images} || $scfg->{content}->{rootdir}); my $pool = extract_param($param, 'pool'); diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm index fc151cb..a09f5fa 100644 --- a/src/PVE/LXC.pm +++ b/src/PVE/LXC.pm @@ -942,6 +942,11 @@ sub update_lxc_config { } elsif ($scfg->{type} eq 'zfspool') { my $rootfs = PVE::Storage::path($storage_cfg, $volid); $raw .= "lxc.rootfs = $rootfs\n"; + } elsif ($scfg->{type} eq 'drbd') { + my $rootdev = PVE::Storage::path($storage_cfg, $volid); + $raw .= "lxc.rootfs = $rootdev\n"; + } else { + die "unsupported storage type '$scfg->{type}'\n"; } my $netcount = 0; diff --git a/src/PVE/LXCCreate.pm b/src/PVE/LXCCreate.pm index 1c92b20..f94c83d 100644 --- a/src/PVE/LXCCreate.pm +++ b/src/PVE/LXCCreate.pm @@ -183,6 +183,36 @@ sub create_rootfs_subvol { restore_and_configure($vmid, $archive, $private, $conf, $password, $restore); } +# direct mount +sub create_rootfs_dev { + my ($storage_conf, $storage, $volid, $vmid, $conf, $archive, $password, $restore) = @_; + + my $image_path = PVE::Storage::path($storage_conf, $volid); + + my $cmd = ['mkfs.ext4', $image_path]; + PVE::Tools::run_command($cmd); + + my $mountpoint; + + eval { + my $tmp = "/var/lib/lxc/$vmid/rootfs"; + File::Path::mkpath($tmp); + PVE::Tools::run_command(['mount', '-t', 'ext4', $image_path, $tmp]); + $mountpoint = $tmp; + + restore_and_configure($vmid, $archive, $mountpoint, $conf, $password, $restore); + }; + if (my $err = $@) { + if ($mountpoint) { + eval { PVE::Tools::run_command(['umount', $mountpoint]) }; + warn $@ if $@; + } + die $err; + } + + PVE::Tools::run_command(['umount', '-l', $mountpoint]); +} + # create a raw file, then loop mount sub create_rootfs_dir_loop { my ($storage_conf, $storage, $volid, $vmid, $conf, $archive, $password, $restore) = @_; @@ -254,6 +284,8 @@ sub create_rootfs { create_rootfs_dir_loop($storage_cfg, $storage, $volid, $vmid, $conf, $archive, $password, $restore); } elsif ($scfg->{type} eq 'zfspool') { create_rootfs_subvol($storage_cfg, $storage, $volid, $vmid, $conf, $archive, $password, $restore); + } elsif ($scfg->{type} eq 'drbd') { + create_rootfs_dev($storage_cfg, $storage, $volid, $vmid, $conf, $archive, $password, $restore); } else { die "unable to create containers on storage type '$scfg->{type}'\n"; } -- 2.39.5