]>
git.proxmox.com Git - qemu-server.git/blob - qm
10 use PVE
::Tools
qw(extract_param);
14 use PVE
::RPCEnvironment
;
17 use PVE
::JSONSchema
qw(get_standard_option);
22 use base
qw(PVE::CLIHandler);
24 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
28 die "please run as root\n" if $> != 0;
30 PVE
::INotify
::inotify_init
();
32 my $rpcenv = PVE
::RPCEnvironment-
>init('cli');
34 $rpcenv->init_request();
35 $rpcenv->set_language($ENV{LANG
});
36 $rpcenv->set_user('root@pam');
40 my $status = PVE
::Tools
::upid_read_status
($upid);
41 exit($status eq 'OK' ?
0 : -1);
44 my $nodename = PVE
::INotify
::nodename
();
49 my $path = PVE
::QemuServer
::vnc_socket
($vmid);
52 while ( ++$c < 10 && !-e
$path ) { sleep(1); }
54 my $s = IO
::Socket
::UNIX-
>new(Peer
=> $path, Timeout
=> 120);
56 die "unable to connect to socket '$path' - $!" if !$s;
58 my $select = new IO
::Select
;
60 $select->add(\
*STDIN
);
63 my $timeout = 60*15; # 15 minutes
66 while ($select->count &&
67 scalar(@handles = $select->can_read ($timeout))) {
68 foreach my $h (@handles) {
70 my $n = $h->sysread($buf, 4096);
80 syswrite(\
*STDOUT
, $buf);
90 __PACKAGE__-
>register_method ({
94 description
=> "Show command line which is used to start the VM (debug info).",
96 additionalProperties
=> 0,
98 vmid
=> get_standard_option
('pve-vmid'),
101 returns
=> { type
=> 'null'},
105 my $storecfg = PVE
::Storage
::config
();
106 print PVE
::QemuServer
::vm_commandline
($storecfg, $param->{vmid
}) . "\n";
111 __PACKAGE__-
>register_method ({
115 description
=> "Show VM status.",
117 additionalProperties
=> 0,
119 vmid
=> get_standard_option
('pve-vmid'),
121 description
=> "Verbose output format",
127 returns
=> { type
=> 'null'},
132 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
134 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
});
135 my $stat = $vmstatus->{$param->{vmid
}};
136 if ($param->{verbose
}) {
137 foreach my $k (sort (keys %$stat)) {
138 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
140 next if !defined($v);
144 my $status = $stat->{status
} || 'unknown';
145 print "status: $status\n";
151 __PACKAGE__-
>register_method ({
155 description
=> "Proxy VM VNC traffic to stdin/stdout",
157 additionalProperties
=> 0,
159 vmid
=> get_standard_option
('pve-vmid'),
162 returns
=> { type
=> 'null'},
166 my $vmid = $param->{vmid
};
168 run_vnc_proxy
($vmid);
173 __PACKAGE__-
>register_method ({
177 description
=> "Unlock the VM.",
179 additionalProperties
=> 0,
181 vmid
=> get_standard_option
('pve-vmid'),
184 returns
=> { type
=> 'null'},
188 my $vmid = $param->{vmid
};
190 PVE
::QemuServer
::lock_config
($vmid, sub {
191 my $conf = PVE
::QemuServer
::load_config
($vmid);
192 delete $conf->{lock};
193 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
199 __PACKAGE__-
>register_method ({
203 description
=> "Used by qmigrate - do not use manually.",
205 additionalProperties
=> 0,
208 returns
=> { type
=> 'null'},
212 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
217 print "tunnel online\n";
220 while (my $line = <>) {
222 last if $line =~ m/^quit$/;
228 __PACKAGE__-
>register_method ({
232 description
=> "Wait until the VM is stopped.",
234 additionalProperties
=> 0,
236 vmid
=> get_standard_option
('pve-vmid'),
238 description
=> "Timeout in seconds. Default is to wait forever.",
245 returns
=> { type
=> 'null'},
249 my $vmid = $param->{vmid
};
250 my $timeout = $param->{timeout
};
252 my $pid = PVE
::QemuServer
::check_running
($vmid);
255 print "waiting until VM $vmid stopps (PID $pid)\n";
258 while ((!$timeout || ($count < $timeout)) && PVE
::QemuServer
::check_running
($vmid)) {
263 die "wait failed - got timeout\n" if PVE
::QemuServer
::check_running
($vmid);
268 __PACKAGE__-
>register_method ({
272 description
=> "Enter Qemu Monitor interface.",
274 additionalProperties
=> 0,
276 vmid
=> get_standard_option
('pve-vmid'),
279 returns
=> { type
=> 'null'},
283 my $vmid = $param->{vmid
};
285 my $conf = PVE
::QemuServer
::load_config
($vmid); # check if VM exists
287 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
289 my $term = new Term
::ReadLine
('qm');
292 while (defined ($input = $term->readline('qm> '))) {
295 next if $input =~ m/^\s*$/;
297 last if $input =~ m/^\s*q(uit)?\s*$/;
300 print PVE
::QemuServer
::vm_human_monitor_command
($vmid, $input);
302 print "ERROR: $@" if $@;
309 __PACKAGE__-
>register_method ({
313 description
=> "Rescan all storages and update disk sizes and unused disk images.",
315 additionalProperties
=> 0,
317 vmid
=> get_standard_option
('pve-vmid', {optional
=> 1}),
320 returns
=> { type
=> 'null'},
324 PVE
::QemuServer
::rescan
($param->{vmid
});
330 list
=> [ "PVE::API2::Qemu", 'vmlist', [],
331 { node
=> $nodename }, sub {
334 exit 0 if (!scalar(@$vmlist));
336 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
337 qw(VMID NAME STATUS MEM(MB) BOOTDISK
(GB
) PID
);
339 foreach my $rec (sort { $a->{vmid
} <=> $b->{vmid
} } @$vmlist) {
340 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid
}, $rec->{name
},
342 ($rec->{maxmem
} || 0)/(1024*1024),
343 ($rec->{maxdisk
} || 0)/(1024*1024*1024),
350 create
=> [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
352 destroy
=> [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
354 migrate
=> [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node
=> $nodename }, $upid_exit ],
356 set
=> [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node
=> $nodename } ],
358 resize
=> [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node
=> $nodename } ],
360 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid', 'idlist'], { node
=> $nodename } ],
362 config
=> [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
363 { node
=> $nodename }, sub {
365 foreach my $k (sort (keys %$config)) {
366 next if $k eq 'digest';
367 my $v = $config->{$k};
368 if ($k eq 'description') {
369 $v = PVE
::Tools
::encode_text
($v);
375 showcmd
=> [ __PACKAGE__
, 'showcmd', ['vmid']],
377 status
=> [ __PACKAGE__
, 'status', ['vmid']],
379 snapshot
=> [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
381 delsnapshot
=> [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
383 rollback
=> [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
385 template
=> [ "PVE::API2::Qemu", 'template', ['vmid'], { node
=> $nodename }],
387 start
=> [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node
=> $nodename } , $upid_exit ],
389 stop
=> [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node
=> $nodename }, $upid_exit ],
391 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node
=> $nodename }, $upid_exit ],
393 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node
=> $nodename }, $upid_exit ],
395 suspend
=> [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node
=> $nodename }, $upid_exit ],
397 resume
=> [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node
=> $nodename }, $upid_exit ],
399 sendkey
=> [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node
=> $nodename } ],
401 vncproxy
=> [ __PACKAGE__
, 'vncproxy', ['vmid']],
403 wait => [ __PACKAGE__
, 'wait', ['vmid']],
405 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
407 rescan
=> [ __PACKAGE__
, 'rescan', []],
409 monitor
=> [ __PACKAGE__
, 'monitor', ['vmid']],
411 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],
416 # Note: disable '+' prefix for Getopt::Long (for resize command)
417 use Getopt
::Long
qw(:config no_getopt_compat);
419 PVE
::CLIHandler
::handle_cmd
($cmddef, "qm", $cmd, \
@ARGV, undef, $0);
427 qm - qemu/kvm virtual machine manager
435 qm is a script to manage virtual machines with qemu/kvm. You can
436 create and destroy virtual machines, and control execution
437 (start/stop/suspend/resume). Besides that, you can use qm to set
438 parameters in the associated config file. It is also possible to
439 create and delete virtual disks.
443 All configuration files consists of lines in the form
447 See L<vm.conf|vm.conf> for a complete list of options.
449 Configuration files are stored inside the Proxmox configuration file system, and can be access at F</etc/pve/qemu-server/C<VMID>.conf>.
451 The default for option 'keyboard' is read from
452 F</etc/pve/datacenter.conf>.
456 Online migration and backups (vzdump) set a lock to prevent
457 unintentional action on such VMs. Sometimes you need remove such lock
458 manually (power failure).
464 # create a new VM with 4 GB ide disk
465 qm create 300 -ide0 4 -net0 e1000 -cdrom proxmox-mailgateway_2.1.iso
470 # send shutdown, then wait until VM is stopped
471 qm shutdown 300 && qm wait 300
473 # same as above, but only wait for 40 seconds
474 qm shutdown 300 && qm wait 300 -timeout 40
477 =include pve_copyright