]>
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);
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 my $conf = PVE
::QemuServer
::load_config
($vmid);
189 delete $conf->{lock};
190 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
196 __PACKAGE__-
>register_method ({
200 description
=> "Used by qmigrate - do not use manually.",
202 additionalProperties
=> 0,
205 returns
=> { type
=> 'null'},
209 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
214 print "tunnel online\n";
217 while (my $line = <>) {
219 last if $line =~ m/^quit$/;
225 __PACKAGE__-
>register_method ({
229 description
=> "Start all virtual machines (when onboot=1).",
231 additionalProperties
=> 0,
234 returns
=> { type
=> 'null'},
238 # wait up to 10 seconds for quorum
239 for (my $i = 10; $i >= 0; $i--) {
240 last if PVE
::Cluster
::check_cfs_quorum
($i != 0 ?
1 : 0);
244 my $vzlist = PVE
::QemuServer
::vzlist
();
245 my $storecfg = PVE
::Storage
::config
();
248 foreach my $vmid (keys %$vzlist) {
249 next if $vzlist->{$vmid}->{pid
}; # already running
252 eval { $conf = PVE
::QemuServer
::load_config
($vmid); };
258 next if !($conf && $conf->{onboot
});
260 sleep(2) if $count != 0; # reduce load
263 PVE
::Cluster
::check_cfs_quorum
(); # abort when we loose quorum
266 print STDERR
"Starting Qemu VM $vmid\n";
267 PVE
::QemuServer
::vm_start
($storecfg, $vmid);
275 __PACKAGE__-
>register_method ({
279 description
=> "Stop all virtual machines.",
281 additionalProperties
=> 0,
284 description
=> "Timeout in seconds. Default is to wait 3 minutes.",
291 returns
=> { type
=> 'null'},
295 my $timeout = $param->{timeout
};
297 my $storecfg = PVE
::Storage
::config
();
299 PVE
::QemuServer
::vm_stopall
($storecfg, $timeout);
304 __PACKAGE__-
>register_method ({
308 description
=> "Wait until the VM is stopped.",
310 additionalProperties
=> 0,
312 vmid
=> get_standard_option
('pve-vmid'),
314 description
=> "Timeout in seconds. Default is to wait forever.",
321 returns
=> { type
=> 'null'},
325 my $vmid = $param->{vmid
};
326 my $timeout = $param->{timeout
};
328 my $pid = PVE
::QemuServer
::check_running
($vmid);
331 print "waiting until VM $vmid stopps (PID $pid)\n";
334 while ((!$timeout || ($count < $timeout)) && PVE
::QemuServer
::check_running
($vmid)) {
339 die "wait failed - got timeout\n" if PVE
::QemuServer
::check_running
($vmid);
344 __PACKAGE__-
>register_method ({
348 description
=> "Enter Qemu Monitor interface.",
350 additionalProperties
=> 0,
352 vmid
=> get_standard_option
('pve-vmid'),
355 returns
=> { type
=> 'null'},
359 my $vmid = $param->{vmid
};
361 my $conf = PVE
::QemuServer
::load_config
($vmid); # check if VM exists
363 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
365 my $term = new Term
::ReadLine
('qm');
368 while (defined ($input = $term->readline('qm> '))) {
371 next if $input =~ m/^\s*$/;
373 last if $input =~ m/^\s*q(uit)?\s*$/;
376 print PVE
::QemuServer
::vm_monitor_command
($vmid, $input);
378 print "ERROR: $@" if $@;
386 list
=> [ "PVE::API2::Qemu", 'vmlist', [],
387 { node
=> $nodename }, sub {
390 exit 0 if (!scalar(@$vmlist));
392 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
393 qw(VMID NAME STATUS MEM(MB) BOOTDISK
(GB
) PID
);
395 foreach my $rec (sort { $a->{vmid
} <=> $b->{vmid
} } @$vmlist) {
396 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid
}, $rec->{name
},
398 ($rec->{maxmem
} || 0)/(1024*1024),
399 ($rec->{maxdisk
} || 0)/(1024*1024*1024),
406 create
=> [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
408 destroy
=> [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
410 migrate
=> [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node
=> $nodename }, $upid_exit ],
412 set
=> [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node
=> $nodename } ],
414 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid', 'idlist'], { node
=> $nodename } ],
416 config
=> [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
417 { node
=> $nodename }, sub {
419 foreach my $k (sort (keys %$config)) {
420 next if $k eq 'digest';
421 my $v = $config->{$k};
422 if ($k eq 'description') {
423 $v = PVE
::Tools
::encode_text
($v);
429 showcmd
=> [ __PACKAGE__
, 'showcmd', ['vmid']],
431 status
=> [ __PACKAGE__
, 'status', ['vmid']],
433 start
=> [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node
=> $nodename } , $upid_exit ],
435 stop
=> [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node
=> $nodename }, $upid_exit ],
437 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node
=> $nodename }, $upid_exit ],
439 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node
=> $nodename }, $upid_exit ],
441 suspend
=> [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node
=> $nodename }, $upid_exit ],
443 resume
=> [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node
=> $nodename }, $upid_exit ],
445 sendkey
=> [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node
=> $nodename } ],
447 vncproxy
=> [ __PACKAGE__
, 'vncproxy', ['vmid']],
449 wait => [ __PACKAGE__
, 'wait', ['vmid']],
451 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
453 monitor
=> [ __PACKAGE__
, 'monitor', ['vmid']],
455 startall
=> [ __PACKAGE__
, 'startall', []],
457 stopall
=> [ __PACKAGE__
, 'stopall', []],
459 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],
464 PVE
::CLIHandler
::handle_cmd
($cmddef, "qm", $cmd, \
@ARGV, undef, $0);
472 qm - qemu/kvm virtual machine manager
480 qm is a script to manage virtual machines with qemu/kvm. You can
481 create and destroy virtual machines, and control execution
482 (start/stop/suspend/resume). Besides that, you can use qm to set
483 parameters in the associated config file. It is also possible to
484 create and delete virtual disks.
488 All configuration files consists of lines in the form
492 See L<vm.conf|vm.conf> for a complete list of options.
494 Configuration files are stored inside the Proxmox configuration file system, and can be access at F</etc/pve/qemu-server/C<VMID>.conf>.
496 The default for option 'keyboard' is read from
497 F</etc/pve/datacenter.conf>.
501 Online migration and backups (vzdump) set a lock to prevent
502 unintentional action on such VMs. Sometimes you need remove such lock
503 manually (power failure).
509 # create a new VM with 4 GB ide disk
510 qm create 300 -ide0 4 -net0 e1000 -cdrom proxmox-mailgateway_2.1.iso
515 # send shutdown, then wait until VM is stopped
516 qm shutdown 300 && qm wait 300
518 # same as above, but only wait for 40 seconds
519 qm shutdown 300 && qm wait 300 -timeout 40
522 =include pve_copyright