]>
git.proxmox.com Git - qemu-server.git/blob - qm
456406c6d4cf0e61ff50bd2929212ef48c9e6d0b
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
();
40 PVE
::JSONSchema
::register_standard_option
('skiplock', {
41 description
=> "Ignore locks - only root is allowed to use this option.",
49 my $path = PVE
::QemuServer
::vnc_socket
($vmid);
51 my $s = IO
::Socket
::UNIX-
>new(Peer
=> $path, Timeout
=> 120);
53 die "unable to connect to socket '$path' - $!" if !$s;
55 my $select = new IO
::Select
;
57 $select->add(\
*STDIN
);
60 my $timeout = 60*15; # 15 minutes
63 while ($select->count &&
64 scalar(@handles = $select->can_read ($timeout))) {
65 foreach my $h (@handles) {
67 my $n = $h->sysread($buf, 4096);
77 syswrite(\
*STDOUT
, $buf);
87 __PACKAGE__-
>register_method ({
91 description
=> "Show command line which is used to start the VM (debug info).",
93 additionalProperties
=> 0,
95 vmid
=> get_standard_option
('pve-vmid'),
98 returns
=> { type
=> 'null'},
102 my $storecfg = PVE
::Storage
::config
();
103 print PVE
::QemuServer
::vm_commandline
($storecfg, $param->{vmid
}) . "\n";
108 __PACKAGE__-
>register_method ({
112 description
=> "Show VM status.",
114 additionalProperties
=> 0,
116 vmid
=> get_standard_option
('pve-vmid'),
118 description
=> "Verbose output format",
124 returns
=> { type
=> 'null'},
129 my $conf = PVE
::QemuServer
::load_config
($param->{vmid
});
131 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
});
132 my $stat = $vmstatus->{$param->{vmid
}};
133 if ($param->{verbose
}) {
134 foreach my $k (sort (keys %$stat)) {
135 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
137 next if !defined($v);
141 my $status = $stat->{status
} || 'unknown';
142 print "status: $status\n";
148 __PACKAGE__-
>register_method ({
152 description
=> "Proxy VM VNC traffic to stdin/stdout",
154 additionalProperties
=> 0,
156 vmid
=> get_standard_option
('pve-vmid'),
159 returns
=> { type
=> 'null'},
163 my $vmid = $param->{vmid
};
165 run_vnc_proxy
($vmid);
170 __PACKAGE__-
>register_method ({
174 description
=> "Unlock the VM.",
176 additionalProperties
=> 0,
178 vmid
=> get_standard_option
('pve-vmid'),
181 returns
=> { type
=> 'null'},
185 my $vmid = $param->{vmid
};
187 PVE
::QemuServer
::lock_config
($vmid, sub {
188 PVE
::QemuServer
::change_config_nolock
($vmid, {}, { lock => 1 }, 1);
194 __PACKAGE__-
>register_method ({
198 description
=> "Used by qmigrate - do not use manually.",
200 additionalProperties
=> 0,
203 returns
=> { type
=> 'null'},
207 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
212 print "tunnel online\n";
215 while (my $line = <>) {
217 last if $line =~ m/^quit$/;
223 __PACKAGE__-
>register_method ({
227 description
=> "Start all virtual machines (when onboot=1).",
229 additionalProperties
=> 0,
232 returns
=> { type
=> 'null'},
236 my $vzlist = PVE
::QemuServer
::vzlist
();
237 my $storecfg = PVE
::Storage
::config
();
239 PVE
::Cluster
::check_cfs_quorum
();
242 foreach my $vmid (keys %$vzlist) {
243 next if $vzlist->{$vmid}->{pid
}; # already running
245 sleep(2) if $count != 0; # reduce load
248 PVE
::Cluster
::check_cfs_quorum
(); # abort when we loose quorum
251 my $conf = PVE
::QemuServer
::load_config
($vmid);
252 if ($conf->{onboot
}) {
253 print STDERR
"Starting Qemu VM $vmid\n";
254 PVE
::QemuServer
::vm_start
($storecfg, $vmid);
257 print STDERR
$@ if $@;
263 __PACKAGE__-
>register_method ({
267 description
=> "Stop all virtual machines.",
269 additionalProperties
=> 0,
272 description
=> "Timeout in seconds. Default is to wait 3 minutes.",
279 returns
=> { type
=> 'null'},
283 my $timeout = $param->{timeout
};
285 PVE
::QemuServer
::vm_stopall
($timeout);
290 __PACKAGE__-
>register_method ({
294 description
=> "Wait until the VM is stopped.",
296 additionalProperties
=> 0,
298 vmid
=> get_standard_option
('pve-vmid'),
300 description
=> "Timeout in seconds. Default is to wait forever.",
307 returns
=> { type
=> 'null'},
311 my $vmid = $param->{vmid
};
312 my $timeout = $param->{timeout
};
314 my $pid = PVE
::QemuServer
::check_running
($vmid);
317 print "waiting until VM $vmid stopps (PID $pid)\n";
320 while ((!$timeout || ($count < $timeout)) && PVE
::QemuServer
::check_running
($vmid)) {
325 die "wait failed - got timeout\n" if PVE
::QemuServer
::check_running
($vmid);
330 __PACKAGE__-
>register_method ({
334 description
=> "Enter Qemu Monitor interface.",
336 additionalProperties
=> 0,
338 vmid
=> get_standard_option
('pve-vmid'),
341 returns
=> { type
=> 'null'},
345 my $vmid = $param->{vmid
};
347 my $conf = PVE
::QemuServer
::load_config
($vmid); # check if VM exists
349 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
351 my $term = new Term
::ReadLine
('qm');
354 while (defined ($input = $term->readline('qm> '))) {
357 next if $input =~ m/^\s*$/;
359 last if $input =~ m/^\s*q(uit)?\s*$/;
362 print PVE
::QemuServer
::vm_monitor_command
($vmid, $input);
364 print "ERROR: $@" if $@;
372 list
=> [ "PVE::API2::Qemu", 'vmlist', [],
373 { node
=> $nodename }, sub {
376 exit 0 if (!scalar(@$vmlist));
378 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
379 qw(VMID NAME STATUS MEM(MB) BOOTDISK
(GB
) PID
);
381 foreach my $rec (sort { $a->{vmid
} <=> $b->{vmid
} } @$vmlist) {
382 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid
}, $rec->{name
},
384 ($rec->{maxmem
} || 0)/(1024*1024),
385 ($rec->{maxdisk
} || 0)/(1024*1024*1024),
392 create
=> [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node
=> $nodename } ],
394 destroy
=> [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node
=> $nodename } ],
396 set
=> [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node
=> $nodename } ],
398 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid', 'idlist'], { node
=> $nodename } ],
400 config
=> [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
401 { node
=> $nodename }, sub {
403 foreach my $k (sort (keys %$config)) {
404 next if $k eq 'digest';
405 my $v = $config->{$k};
406 if ($k eq 'description') {
407 $v = PVE
::Tools
::encode_text
($v);
413 showcmd
=> [ __PACKAGE__
, 'showcmd', ['vmid']],
415 status
=> [ __PACKAGE__
, 'status', ['vmid']],
417 vncproxy
=> [ __PACKAGE__
, 'vncproxy', ['vmid']],
419 wait => [ __PACKAGE__
, 'wait', ['vmid']],
421 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
423 monitor
=> [ __PACKAGE__
, 'monitor', ['vmid']],
425 startall
=> [ __PACKAGE__
, 'startall', []],
427 stopall
=> [ __PACKAGE__
, 'stopall', []],
429 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],
432 sub register_vm_command
{
433 my ($cmd, $descr) = @_;
435 # we create a wrapper, because we want one 'description' per command
436 __PACKAGE__-
>register_method ({
440 description
=> $descr,
442 additionalProperties
=> 0,
444 vmid
=> get_standard_option
('pve-vmid'),
445 skiplock
=> get_standard_option
('skiplock'),
448 returns
=> { type
=> 'null'},
452 $param->{command
} = $cmd;
453 $param->{node
} = $nodename;
455 return PVE
::API2
::Qemu-
>vm_command($param);
458 $cmddef->{$cmd} = [ __PACKAGE__
, $cmd, ['vmid']];
461 register_vm_command
('start', "Start virtual machine.");
462 register_vm_command
('stop', "Stop virtual machine.");
463 register_vm_command
('reset', "Reset virtual machine.");
464 register_vm_command
('shutdown', "Shutdown virtual machine (send ACPI showdown request)");
465 register_vm_command
('suspend', "Suspend virtual machine.");
466 register_vm_command
('resume', "Resume virtual machine.");
467 register_vm_command
('cad', "Send CTRL-ALT-DELETE keys.");
471 PVE
::CLIHandler
::handle_cmd
($cmddef, "qm", $cmd, \
@ARGV, undef, $0);
479 qm - qemu/kvm virtual machine manager
487 qm is a script to manage virtual machines with qemu/kvm. You can
488 create and destroy virtual machines, and control execution
489 (start/stop/suspend/resume). Besides that, you can use qm to set
490 parameters in the associated config file. It is also possible to
491 create and delete virtual disks.
495 All configuration files consists of lines in the form
499 See L<vm.conf|vm.conf> for a complete list of options.
501 Configuration files are stored inside the Proxmox configuration file system, and can be access at F</etc/pve/qemu-server/C<VMID>.conf>.
503 The default for option 'keyboard' is read from
504 F</etc/pve/datacenter.conf>.
508 Online migration and backups (vzdump) set a lock to prevent
509 unintentional action on such VMs. Sometimes you need remove such lock
510 manually (power failure).
516 # create a new VM with 4 GB ide disk
517 qm create 300 -ide0 4 -net0 e1000 -cdrom proxmox-mailgateway_2.1.iso
522 # send shutdown, then wait until VM is stopped
523 qm shutdown 300 && qm wait 300
525 # same as above, but only wait for 40 seconds
526 qm shutdown 300 && qm wait 300 -timeout 40
529 =include pve_copyright