]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/CLI/pct.pm
7 use PVE
::Tools
qw(extract_param);
10 use PVE
::RPCEnvironment
;
11 use PVE
::JSONSchema
qw(get_standard_option);
14 use PVE
::API2
::LXC
::Config
;
15 use PVE
::API2
::LXC
::Status
;
16 use PVE
::API2
::LXC
::Snapshot
;
20 use base
qw(PVE::CLIHandler);
22 my $nodename = PVE
::INotify
::nodename
();
26 my $status = PVE
::Tools
::upid_read_status
($upid);
27 exit($status eq 'OK' ?
0 : -1);
30 __PACKAGE__-
>register_method ({
34 description
=> "Unlock the VM.",
36 additionalProperties
=> 0,
38 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid
}),
41 returns
=> { type
=> 'null'},
45 my $vmid = $param->{vmid
};
47 PVE
::LXC
::lock_config
($vmid, sub {
48 my $conf = PVE
::LXC
::load_config
($vmid);
50 PVE
::LXC
::write_config
($vmid, $conf);
56 __PACKAGE__-
>register_method ({
60 description
=> "Launch a console for the specified container.",
62 additionalProperties
=> 0,
64 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
67 returns
=> { type
=> 'null' },
72 # test if container exists on this node
73 my $conf = PVE
::LXC
::load_config
($param->{vmid
});
75 my $cmd = PVE
::LXC
::get_console_command
($param->{vmid
}, $conf);
79 __PACKAGE__-
>register_method ({
83 description
=> "Launch a shell for the specified container.",
85 additionalProperties
=> 0,
87 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
90 returns
=> { type
=> 'null' },
95 my $vmid = $param->{vmid
};
97 # test if container exists on this node
98 PVE
::LXC
::load_config
($vmid);
100 die "Error: container '$vmid' not running!\n" if !PVE
::LXC
::check_running
($vmid);
102 exec('lxc-attach', '-n', $vmid);
105 __PACKAGE__-
>register_method ({
109 description
=> "Launch a command inside the specified container.",
111 additionalProperties
=> 0,
113 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
114 'extra-args' => get_standard_option
('extra-args'),
117 returns
=> { type
=> 'null' },
122 # test if container exists on this node
123 PVE
::LXC
::load_config
($param->{vmid
});
125 if (!@{$param->{'extra-args'}}) {
126 die "missing command";
128 exec('lxc-attach', '-n', $param->{vmid
}, '--', @{$param->{'extra-args'}});
131 __PACKAGE__-
>register_method ({
135 description
=> "Run a filesystem check (fsck) on a container volume.",
137 additionalProperties
=> 0,
139 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
143 description
=> "Force checking, even if the filesystem seems clean",
149 description
=> "A volume on which to run the filesystem check",
150 enum
=> [PVE
::LXC
::mountpoint_names
()],
154 returns
=> { type
=> 'null' },
158 my $vmid = $param->{'vmid'};
159 my $device = defined($param->{'device'}) ?
$param->{'device'} : 'rootfs';
161 my $command = ['fsck', '-a', '-l'];
162 push(@$command, '-f') if $param->{force
};
164 # critical path: all of this will be done while the container is locked
167 my $conf = PVE
::LXC
::load_config
($vmid);
168 my $storage_cfg = PVE
::Storage
::config
();
170 defined($conf->{$device}) || die "cannot run command on unexisting mountpoint $device\n";
172 my $mount_point = $device eq 'rootfs' ? PVE
::LXC
::parse_ct_rootfs
($conf->{$device}) :
173 PVE
::LXC
::parse_ct_mountpoint
($conf->{$device});
175 my $volid = $mount_point->{volume
};
178 my $storage_id = PVE
::Storage
::parse_volume_id
($volid, 1);
181 my (undef, undef, undef, undef, undef, undef, $format) =
182 PVE
::Storage
::parse_volname
($storage_cfg, $volid);
184 die "unable to run fsck for '$volid' (format == $format)\n"
187 $path = PVE
::Storage
::path
($storage_cfg, $volid);
190 if (($volid =~ m
|^/.+|) && (-b
$volid)) {
191 # pass block devices directly
194 die "path '$volid' does not point to a block device\n";
198 push(@$command, $path);
200 PVE
::LXC
::check_running
($vmid) &&
201 die "cannot run fsck on active container\n";
203 PVE
::Tools
::run_command
($command);
206 PVE
::LXC
::lock_config
($vmid, $do_fsck);
211 list
=> [ 'PVE::API2::LXC', 'vmlist', [], { node
=> $nodename }, sub {
213 return if !scalar(@$res);
214 my $format = "%-10s %-10s %-20s\n";
215 printf($format, 'VMID', 'Status', 'Name');
216 foreach my $d (sort {$a->{vmid
} <=> $b->{vmid
} } @$res) {
217 printf($format, $d->{vmid
}, $d->{status
}, $d->{name
});
220 config
=> [ "PVE::API2::LXC::Config", 'vm_config', ['vmid'],
221 { node
=> $nodename }, sub {
223 foreach my $k (sort (keys %$config)) {
224 next if $k eq 'digest';
225 my $v = $config->{$k};
226 if ($k eq 'description') {
227 $v = PVE
::Tools
::encode_text
($v);
232 set
=> [ 'PVE::API2::LXC::Config', 'update_vm', ['vmid'], { node
=> $nodename }],
234 resize
=> [ "PVE::API2::LXC", 'resize_vm', ['vmid', 'disk', 'size'], { node
=> $nodename } ],
236 create
=> [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node
=> $nodename }, $upid_exit ],
237 restore
=> [ 'PVE::API2::LXC', 'create_vm', ['vmid', 'ostemplate'], { node
=> $nodename, restore
=> 1 }, $upid_exit ],
239 start
=> [ 'PVE::API2::LXC::Status', 'vm_start', ['vmid'], { node
=> $nodename }, $upid_exit],
240 suspend
=> [ 'PVE::API2::LXC::Status', 'vm_suspend', ['vmid'], { node
=> $nodename }, $upid_exit],
241 resume
=> [ 'PVE::API2::LXC::Status', 'vm_resume', ['vmid'], { node
=> $nodename }, $upid_exit],
242 shutdown => [ 'PVE::API2::LXC::Status', 'vm_shutdown', ['vmid'], { node
=> $nodename }, $upid_exit],
243 stop
=> [ 'PVE::API2::LXC::Status', 'vm_stop', ['vmid'], { node
=> $nodename }, $upid_exit],
245 clone
=> [ "PVE::API2::LXC", 'clone_vm', ['vmid', 'newid'], { node
=> $nodename }, $upid_exit ],
246 migrate
=> [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node
=> $nodename }, $upid_exit],
248 console
=> [ __PACKAGE__
, 'console', ['vmid']],
249 enter
=> [ __PACKAGE__
, 'enter', ['vmid']],
250 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
251 exec => [ __PACKAGE__
, 'exec', ['vmid', 'extra-args']],
252 fsck
=> [ __PACKAGE__
, 'fsck', ['vmid']],
254 destroy
=> [ 'PVE::API2::LXC', 'destroy_vm', ['vmid'],
255 { node
=> $nodename }, $upid_exit ],
257 snapshot
=> [ "PVE::API2::LXC::Snapshot", 'snapshot', ['vmid', 'snapname'],
258 { node
=> $nodename } , $upid_exit ],
260 delsnapshot
=> [ "PVE::API2::LXC::Snapshot", 'delsnapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
262 listsnapshot
=> [ "PVE::API2::LXC::Snapshot", 'list', ['vmid'], { node
=> $nodename },
265 foreach my $e (@$res) {
266 my $headline = $e->{description
} || 'no-description';
267 $headline =~ s/\n.*//sg;
268 my $parent = $e->{parent
} // 'no-parent';
269 printf("%-20s %-20s %s\n", $e->{name
}, $parent, $headline);
273 rollback
=> [ "PVE::API2::LXC::Snapshot", 'rollback', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
275 template
=> [ "PVE::API2::LXC", 'template', ['vmid'], { node
=> $nodename }],
285 pct - Tool to manage Linux Containers (LXC) on Proxmox VE
293 pct is a tool to manages Linux Containers (LXC). You can create
294 and destroy containers, and control execution
295 (start/stop/suspend/resume). Besides that, you can use pct to set
296 parameters in the associated config file, like network configuration or
301 Create a container based on a Debian template
302 (provided you downloaded the template via the webgui before)
304 pct create 100 /var/lib/vz/template/cache/debian-8.0-standard_8.0-1_amd64.tar.gz
310 Start a login session via getty
314 Enter the lxc namespace and run a shell as root user
318 Display the configuration
322 Add a network interface called eth0, bridged to the host bridge vmbr0,
323 set the address and gateway, while it's running
325 pct set 100 -net0 name=eth0,bridge=vmbr0,ip=192.168.15.147/24,gw=192.168.15.1
327 Reduce the memory of the container to 512MB
329 pct set -memory 512 100
333 /etc/pve/lxc/<vmid>.conf
335 Configuration file for the container <vmid>
339 L<B<qm(1)>>, L<B<pvesh(1)>>
341 =include pve_copyright