]> git.proxmox.com Git - pve-container.git/commitdiff
move resize to have it available in the pveshell and at the rest api
authorWolfgang Link <w.link@proxmox.com>
Wed, 28 Oct 2015 08:40:40 +0000 (09:40 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 28 Oct 2015 10:18:21 +0000 (11:18 +0100)
src/PVE/API2/LXC.pm
src/PVE/API2/LXC/Config.pm
src/PVE/CLI/pct.pm

index 584241f699c864d2a7ca44c3b3936f0250163cf2..972f6282ca7ac743ffc008b924622c00ea8a820a 100644 (file)
@@ -341,6 +341,7 @@ __PACKAGE__->register_method({
            { subdir => 'rrddata' },
            { subdir => 'firewall' },
            { subdir => 'snapshot' },
+           { subdir => 'resize' },
            ];
 
        return $res;
@@ -906,4 +907,150 @@ __PACKAGE__->register_method({
        return undef;
     }});
 
+__PACKAGE__->register_method({
+    name => 'resize_vm',
+    path => '{vmid}/resize',
+    method => 'PUT',
+    protected => 1,
+    proxyto => 'node',
+    description => "Resize a container mountpoint.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', ['VM.Config.Disk'], any => 1],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => PVE::LXC::json_config_properties(
+           {
+               node => get_standard_option('pve-node'),
+               vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
+               disk => {
+                   type => 'string',
+                   description => "The disk you want to resize.",
+                   enum => [PVE::LXC::mountpoint_names()],
+               },
+               size => {
+                   type => 'string',
+                   pattern => '\+?\d+(\.\d+)?[KMGT]?',
+                   description => "The new size. With the '+' sign the value is added to the actual size of the volume and without it, the value is taken as an absolute one. Shrinking disk size is not supported.",
+               },
+               digest => {
+                   type => 'string',
+                   description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
+                   maxLength => 40,
+                   optional => 1,
+               }
+           }),
+    },
+    returns => { type => 'null'},
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+
+       my $authuser = $rpcenv->get_user();
+
+       my $node = extract_param($param, 'node');
+
+       my $vmid = extract_param($param, 'vmid');
+
+       my $digest = extract_param($param, 'digest');
+
+       my $sizestr = extract_param($param, 'size');
+       my $ext = ($sizestr =~ s/^\+//);
+       my $newsize = PVE::JSONSchema::parse_size($sizestr);
+       die "invalid size string" if !defined($newsize);
+
+       die "no options specified\n" if !scalar(keys %$param);
+
+       PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
+
+       my $storage_cfg = cfs_read_file("storage.cfg");
+
+       my $query_loopdev = sub {
+           my ($path) = @_;
+           my $found;
+           my $parser = sub {
+               my $line = shift;
+               if ($line =~ m@^(/dev/loop\d+):@) {
+                   $found = $1;
+               }
+           };
+           my $cmd = ['losetup', '--associated', $path];
+           PVE::Tools::run_command($cmd, outfunc => $parser);
+           return $found;
+       };
+
+       my $code = sub {
+
+           my $conf = PVE::LXC::load_config($vmid);
+           PVE::LXC::check_lock($conf);
+
+           PVE::Tools::assert_if_modified($digest, $conf->{digest});
+
+           my $running = PVE::LXC::check_running($vmid);
+
+           my $disk = $param->{disk};
+           my $mp = PVE::LXC::parse_ct_mountpoint($conf->{$disk});
+           my $volid = $mp->{volume};
+
+           my (undef, undef, $owner, undef, undef, undef, $format) =
+               PVE::Storage::parse_volname($storage_cfg, $volid);
+
+           die "can't resize mountpoint owned by another container ($owner)"
+               if $vmid != $owner;
+
+           die "can't resize volume: $disk if snapshot exists\n"
+               if %{$conf->{snapshots}} && $format eq 'qcow2';
+
+           my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
+
+           $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
+
+           my $size = PVE::Storage::volume_size_info($storage_cfg, $volid, 5);
+           $newsize += $size if $ext;
+           $newsize = int($newsize);
+
+           die "unable to shrink disk size\n" if $newsize < $size;
+
+           return if $size == $newsize;
+
+           PVE::Cluster::log_msg('info', $authuser, "update CT $vmid: resize --disk $disk --size $sizestr");
+
+           # FIXME: volume_resize doesn't do anything if $running=1?
+           PVE::Storage::volume_resize($storage_cfg, $volid, $newsize, 0);
+
+           $mp->{size} = $newsize;
+           $conf->{$disk} = PVE::LXC::print_ct_mountpoint($mp, $disk eq 'rootfs');
+
+           PVE::LXC::write_config($vmid, $conf);
+
+           if ($format eq 'raw') {
+               my $path = PVE::Storage::path($storage_cfg, $volid, undef);
+               if ($running) {
+                   $path = &$query_loopdev($path);
+                   die "internal error: CT running but mountpoint not attached to a loop device"
+                       if !$path; # fixme: zvols and other storages?
+                   PVE::Tools::run_command(['losetup', '--set-capacity', $path]);
+
+                   # In order for resize2fs to know that we need online-resizing a mountpoint needs
+                   # to be visible to it in its namespace.
+                   # To not interfere with the rest of the system we unshare the current mount namespace,
+                   # mount over /tmp and then run resize2fs.
+
+                   # interestingly we don't need to e2fsck on mounted systems...
+                   my $quoted = PVE::Tools::shellquote($path);
+                   my $cmd = "mount --make-rprivate / && mount $quoted /tmp && resize2fs $quoted";
+                   PVE::Tools::run_command(['unshare', '-m', '--', 'sh', '-c', $cmd]);
+               } else {
+                   PVE::Tools::run_command(['e2fsck', '-f', '-y', $path]);
+                   PVE::Tools::run_command(['resize2fs', $path]);
+               }
+           }
+       };
+
+       PVE::LXC::lock_container($vmid, undef, $code);
+
+       return undef;
+    }});
+
 1;
index c3f42f1ee4f271867c62372128a47b71f70c4068..95eafaae7a922ce8b55ffa1608289d6c58aadf57 100644 (file)
@@ -149,150 +149,4 @@ __PACKAGE__->register_method({
        return undef;
     }});
 
-my $query_loopdev = sub {
-    my ($path) = @_;
-    my $found;
-    my $parser = sub {
-       my $line = shift;
-       if ($line =~ m@^(/dev/loop\d+):@) {
-           $found = $1;
-       }
-    };
-    my $cmd = ['losetup', '--associated', $path];
-    PVE::Tools::run_command($cmd, outfunc => $parser);
-    return $found;
-};
-
-__PACKAGE__->register_method({
-    name => 'resize_vm',
-    path => '{vmid}/resize',
-    method => 'PUT',
-    protected => 1,
-    proxyto => 'node',
-    description => "Resize a container mountpoint.",
-    permissions => {
-       check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
-    },
-    parameters => {
-       additionalProperties => 0,
-       properties => PVE::LXC::json_config_properties(
-           {
-               node => get_standard_option('pve-node'),
-               vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
-               disk => {
-                   type => 'string',
-                   description => "The disk you want to resize.",
-                   enum => [PVE::LXC::mountpoint_names()],
-               },
-               size => {
-                   type => 'string',
-                   pattern => '\+?\d+(\.\d+)?[KMGT]?',
-                   description => "The new size. With the '+' sign the value is added to the actual size of the volume and without it, the value is taken as an absolute one. Shrinking disk size is not supported.",
-               },
-               digest => {
-                   type => 'string',
-                   description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
-                   maxLength => 40,
-                   optional => 1,
-               }
-           }),
-    },
-    returns => { type => 'null'},
-    code => sub {
-       my ($param) = @_;
-
-       my $rpcenv = PVE::RPCEnvironment::get();
-
-       my $authuser = $rpcenv->get_user();
-
-       my $node = extract_param($param, 'node');
-
-       my $vmid = extract_param($param, 'vmid');
-
-       my $digest = extract_param($param, 'digest');
-
-       my $sizestr = extract_param($param, 'size');
-       my $ext = ($sizestr =~ s/^\+//);
-       my $newsize = PVE::JSONSchema::parse_size($sizestr);
-       die "invalid size string" if !defined($newsize);
-
-       die "no options specified\n" if !scalar(keys %$param);
-
-       PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
-
-       my $storage_cfg = cfs_read_file("storage.cfg");
-
-       my $code = sub {
-
-           my $conf = PVE::LXC::load_config($vmid);
-           PVE::LXC::check_lock($conf);
-
-           PVE::Tools::assert_if_modified($digest, $conf->{digest});
-
-           my $running = PVE::LXC::check_running($vmid);
-
-           my $disk = $param->{disk};
-           my $mp = PVE::LXC::parse_ct_mountpoint($conf->{$disk});
-           my $volid = $mp->{volume};
-
-           my (undef, undef, $owner, undef, undef, undef, $format) =
-               PVE::Storage::parse_volname($storage_cfg, $volid);
-
-           die "can't resize mountpoint owned by another container ($owner)"
-               if $vmid != $owner;
-
-           die "can't resize volume: $disk if snapshot exists\n" 
-               if %{$conf->{snapshots}} && $format eq 'qcow2';
-
-           my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
-
-           $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
-
-           my $size = PVE::Storage::volume_size_info($storage_cfg, $volid, 5);
-           $newsize += $size if $ext;
-           $newsize = int($newsize);
-
-           die "unable to shrink disk size\n" if $newsize < $size;
-
-           return if $size == $newsize;
-
-           PVE::Cluster::log_msg('info', $authuser, "update CT $vmid: resize --disk $disk --size $sizestr");
-
-           # FIXME: volume_resize doesn't do anything if $running=1?
-           PVE::Storage::volume_resize($storage_cfg, $volid, $newsize, 0);
-
-           $mp->{size} = $newsize;
-           $conf->{$disk} = PVE::LXC::print_ct_mountpoint($mp, $disk eq 'rootfs');
-
-           PVE::LXC::write_config($vmid, $conf);
-
-           if ($format eq 'raw') {
-               my $path = PVE::Storage::path($storage_cfg, $volid, undef);
-               if ($running) {
-                   $path = &$query_loopdev($path);
-                   die "internal error: CT running but mountpoint not attached to a loop device"
-                       if !$path; # fixme: zvols and other storages?
-                   PVE::Tools::run_command(['losetup', '--set-capacity', $path]);
-
-                   # In order for resize2fs to know that we need online-resizing a mountpoint needs
-                   # to be visible to it in its namespace.
-                   # To not interfere with the rest of the system we unshare the current mount namespace,
-                   # mount over /tmp and then run resize2fs.
-
-                   # interestingly we don't need to e2fsck on mounted systems...
-                   my $quoted = PVE::Tools::shellquote($path);
-                   my $cmd = "mount --make-rprivate / && mount $quoted /tmp && resize2fs $quoted";
-                   PVE::Tools::run_command(['unshare', '-m', '--', 'sh', '-c', $cmd]);
-               } else {
-                   PVE::Tools::run_command(['e2fsck', '-f', '-y', $path]);
-                   PVE::Tools::run_command(['resize2fs', $path]);
-               }
-           }
-       };
-
-       PVE::LXC::lock_container($vmid, undef, $code);
-
-       return undef;
-    }});
-
 1;
index 7e69d21e22dba5749f6ebfe2e05b020fec07140e..80205bb9debf3609ade56de3b07e9f4f0e7743a9 100755 (executable)
@@ -225,7 +225,7 @@ our $cmddef = {
                }],
     set => [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node => $nodename }],
 
-    resize => [ "PVE::API2::LXC::Config", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ],
+    resize => [ "PVE::API2::LXC", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ],
     
     create => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename }, $upid_exit ],
     restore => [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node => $nodename, restore => 1 }, $upid_exit ],