]>
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');
38 my $nodename = PVE
::INotify
::nodename
();
43 my $path = PVE
::QemuServer
::vnc_socket
($vmid);
45 my $s = IO
::Socket
::UNIX-
>new(Peer
=> $path, Timeout
=> 120);
47 die "unable to connect to socket '$path' - $!" if !$s;
49 my $select = new IO
::Select
;
51 $select->add(\
*STDIN
);
54 my $timeout = 60*15; # 15 minutes
57 while ($select->count &&
58 scalar(@handles = $select->can_read ($timeout))) {
59 foreach my $h (@handles) {
61 my $n = $h->sysread($buf, 4096);
71 syswrite(\
*STDOUT
, $buf);
81 __PACKAGE__-
>register_method ({
85 description
=> "Show command line which is used to start the VM (debug info).",
87 additionalProperties
=> 0,
89 vmid
=> get_standard_option
('pve-vmid'),
92 returns
=> { type
=> 'null'},
96 my $storecfg = PVE
::Storage
::config
();
97 print PVE
::QemuServer
::vm_commandline
($storecfg, $param->{vmid
}) . "\n";
102 __PACKAGE__-
>register_method ({
106 description
=> "Show VM status.",
108 additionalProperties
=> 0,
110 vmid
=> get_standard_option
('pve-vmid'),
112 description
=> "Verbose output format",
118 returns
=> { type
=> 'null'},
123 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
125 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
});
126 my $stat = $vmstatus->{$param->{vmid
}};
127 if ($param->{verbose
}) {
128 foreach my $k (sort (keys %$stat)) {
129 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
131 next if !defined($v);
135 my $status = $stat->{status
} || 'unknown';
136 print "status: $status\n";
142 __PACKAGE__-
>register_method ({
146 description
=> "Proxy VM VNC traffic to stdin/stdout",
148 additionalProperties
=> 0,
150 vmid
=> get_standard_option
('pve-vmid'),
153 returns
=> { type
=> 'null'},
157 my $vmid = $param->{vmid
};
159 run_vnc_proxy
($vmid);
164 __PACKAGE__-
>register_method ({
168 description
=> "Unlock the VM.",
170 additionalProperties
=> 0,
172 vmid
=> get_standard_option
('pve-vmid'),
175 returns
=> { type
=> 'null'},
179 my $vmid = $param->{vmid
};
181 PVE
::QemuServer
::lock_config
($vmid, sub {
182 PVE
::QemuServer
::change_config_nolock
($vmid, {}, { lock => 1 }, 1);
188 __PACKAGE__-
>register_method ({
192 description
=> "Used by qmigrate - do not use manually.",
194 additionalProperties
=> 0,
197 returns
=> { type
=> 'null'},
201 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
206 print "tunnel online\n";
209 while (my $line = <>) {
211 last if $line =~ m/^quit$/;
217 __PACKAGE__-
>register_method ({
221 description
=> "Start all virtual machines (when onboot=1).",
223 additionalProperties
=> 0,
226 returns
=> { type
=> 'null'},
230 my $vzlist = PVE
::QemuServer
::vzlist
();
231 my $storecfg = PVE
::Storage
::config
();
233 PVE
::Cluster
::check_cfs_quorum
();
236 foreach my $vmid (keys %$vzlist) {
237 next if $vzlist->{$vmid}->{pid
}; # already running
239 sleep(2) if $count != 0; # reduce load
242 PVE
::Cluster
::check_cfs_quorum
(); # abort when we loose quorum
245 my $conf = PVE
::QemuServer
::load_config
($vmid);
246 if ($conf->{onboot
}) {
247 print STDERR
"Starting Qemu VM $vmid\n";
248 PVE
::QemuServer
::vm_start
($storecfg, $vmid);
251 print STDERR
$@ if $@;
257 __PACKAGE__-
>register_method ({
261 description
=> "Stop all virtual machines.",
263 additionalProperties
=> 0,
266 description
=> "Timeout in seconds. Default is to wait 3 minutes.",
273 returns
=> { type
=> 'null'},
277 my $timeout = $param->{timeout
};
279 my $storecfg = PVE
::Storage
::config
();
281 PVE
::QemuServer
::vm_stopall
($storecfg, $timeout);
286 __PACKAGE__-
>register_method ({
290 description
=> "Wait until the VM is stopped.",
292 additionalProperties
=> 0,
294 vmid
=> get_standard_option
('pve-vmid'),
296 description
=> "Timeout in seconds. Default is to wait forever.",
303 returns
=> { type
=> 'null'},
307 my $vmid = $param->{vmid
};
308 my $timeout = $param->{timeout
};
310 my $pid = PVE
::QemuServer
::check_running
($vmid);
313 print "waiting until VM $vmid stopps (PID $pid)\n";
316 while ((!$timeout || ($count < $timeout)) && PVE
::QemuServer
::check_running
($vmid)) {
321 die "wait failed - got timeout\n" if PVE
::QemuServer
::check_running
($vmid);
326 __PACKAGE__-
>register_method ({
330 description
=> "Enter Qemu Monitor interface.",
332 additionalProperties
=> 0,
334 vmid
=> get_standard_option
('pve-vmid'),
337 returns
=> { type
=> 'null'},
341 my $vmid = $param->{vmid
};
343 my $conf = PVE
::QemuServer
::load_config
($vmid); # check if VM exists
345 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
347 my $term = new Term
::ReadLine
('qm');
350 while (defined ($input = $term->readline('qm> '))) {
353 next if $input =~ m/^\s*$/;
355 last if $input =~ m/^\s*q(uit)?\s*$/;
358 print PVE
::QemuServer
::vm_monitor_command
($vmid, $input);
360 print "ERROR: $@" if $@;
368 list
=> [ "PVE::API2::Qemu", 'vmlist', [],
369 { node
=> $nodename }, sub {
372 exit 0 if (!scalar(@$vmlist));
374 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
375 qw(VMID NAME STATUS MEM(MB) BOOTDISK
(GB
) PID
);
377 foreach my $rec (sort { $a->{vmid
} <=> $b->{vmid
} } @$vmlist) {
378 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid
}, $rec->{name
},
380 ($rec->{maxmem
} || 0)/(1024*1024),
381 ($rec->{maxdisk
} || 0)/(1024*1024*1024),
388 create
=> [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node
=> $nodename } ],
390 destroy
=> [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node
=> $nodename } ],
392 migrate
=> [ "PVE::API2::Qemu", 'migrate_vm', ['target', 'vmid'], { node
=> $nodename },
395 my $status = PVE
::Tools
::upid_read_status
($upid);
396 exit($status eq 'OK' ?
0 : -1);
399 set
=> [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node
=> $nodename } ],
401 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid', 'idlist'], { node
=> $nodename } ],
403 config
=> [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
404 { node
=> $nodename }, sub {
406 foreach my $k (sort (keys %$config)) {
407 next if $k eq 'digest';
408 my $v = $config->{$k};
409 if ($k eq 'description') {
410 $v = PVE
::Tools
::encode_text
($v);
416 showcmd
=> [ __PACKAGE__
, 'showcmd', ['vmid']],
418 status
=> [ __PACKAGE__
, 'status', ['vmid']],
420 start
=> [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node
=> $nodename } ],
422 stop
=> [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node
=> $nodename } ],
424 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node
=> $nodename } ],
426 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node
=> $nodename } ],
428 suspend
=> [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node
=> $nodename } ],
430 resume
=> [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node
=> $nodename } ],
432 sendkey
=> [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node
=> $nodename } ],
434 vncproxy
=> [ __PACKAGE__
, 'vncproxy', ['vmid']],
436 wait => [ __PACKAGE__
, 'wait', ['vmid']],
438 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
440 monitor
=> [ __PACKAGE__
, 'monitor', ['vmid']],
442 startall
=> [ __PACKAGE__
, 'startall', []],
444 stopall
=> [ __PACKAGE__
, 'stopall', []],
446 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],
451 PVE
::CLIHandler
::handle_cmd
($cmddef, "qm", $cmd, \
@ARGV, undef, $0);
459 qm - qemu/kvm virtual machine manager
467 qm is a script to manage virtual machines with qemu/kvm. You can
468 create and destroy virtual machines, and control execution
469 (start/stop/suspend/resume). Besides that, you can use qm to set
470 parameters in the associated config file. It is also possible to
471 create and delete virtual disks.
475 All configuration files consists of lines in the form
479 See L<vm.conf|vm.conf> for a complete list of options.
481 Configuration files are stored inside the Proxmox configuration file system, and can be access at F</etc/pve/qemu-server/C<VMID>.conf>.
483 The default for option 'keyboard' is read from
484 F</etc/pve/datacenter.conf>.
488 Online migration and backups (vzdump) set a lock to prevent
489 unintentional action on such VMs. Sometimes you need remove such lock
490 manually (power failure).
496 # create a new VM with 4 GB ide disk
497 qm create 300 -ide0 4 -net0 e1000 -cdrom proxmox-mailgateway_2.1.iso
502 # send shutdown, then wait until VM is stopped
503 qm shutdown 300 && qm wait 300
505 # same as above, but only wait for 40 seconds
506 qm shutdown 300 && qm wait 300 -timeout 40
509 =include pve_copyright