* Since we're sharing, here's my kvmctl script
@ 2008-06-11 22:52 Freddie Cash
2008-06-11 23:04 ` Freddie Cash
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Freddie Cash @ 2008-06-11 22:52 UTC (permalink / raw)
To: kvm
For everyone's viewing (and critiquing, I guess) pleasure, I present
my version of a kvmctl script.
>From the usage output (looks best viewed with a monospace font):
kvmctl is a management and control script for KVM-based virtual machines.
Usage: kvmctl start host - start the named VM
kvmctl startvnc host - start the named VM, and then connect
to console via VNC
kvmctl stop host - stop the named VM (only use if the
guest is hung)
kvmctl restart host - stop and then start the named VM
(only use if the guest is hung)
kvmctl vnc host - connect via VNC to the console of the named VM
kvmctl whichvnc host - show which VNC display port is
assigned to the named VM
kvmctl killvnc host - kills any running vncviewer
processes attached to the named VM
kvmctl edit host - open config file for host using
$EDITOR, or create a new config file based on a template
kvmctl status - show the names of all running VMs
kvmctl status kvm - show full details for all running
kvm processes
kvmctl status host - show full details for the named kvm process
kvmctl help - show this usage blurb
** Using stop is the same as pulling the power cord on a physical
system. Use with caution.
A couple of assumptions are made:
- the host is configured with a bridge device
- /etc/kvm/kvm-ifup is configured to use that bridge device by name
- all VMs will use bridged networking
- you want to use VNC for the console for all VMs (and have a
vncviewer installed on the host)
The script can be run as a normal user, as it will use sudo where
needed. However, this causes all the VMs to be run as root (this is
developed on Debian where they've added that annoying "feature" of not
being able to create/use tun/tap devices as non-root users). If
anyone knows how to unbreak Debian to allow non-root users to create
tun/tap devices, I'm all ears.
There's a simple init.d/ script included in the tarball to autostart
any VMs who have their config files linked to /etc/kvm/auto/ (modelled
after Xen). The auto-stop feature just kills the kvm processes, so
use with caution, or just link to the start function.
There's a simple README in the tarball, along with a sample interfaces
file for creating a kvmbr0 bridge (for Debian-based systems).
The only thing I haven't been able to figure out, is how to send a
"shutdown" command from the host OS to the guest OS, such that the
guest OS will do a proper shutdown sequence. You have to switch to
the VM console and manually tell it to shutdown. :(
It's released under the BSD License, so do with it as you wish. :)
Patches and suggestions are always welcome, of course.
http://www.sd73.bc.ca/downloads/kvmctl-2.0.0.tbz
--
Freddie Cash
fjwcash@gmail.com
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Since we're sharing, here's my kvmctl script
2008-06-11 22:52 Since we're sharing, here's my kvmctl script Freddie Cash
@ 2008-06-11 23:04 ` Freddie Cash
2008-06-12 15:15 ` Freddie Cash
2008-06-12 0:42 ` qemu-send.c (was Re: Since we're sharing, here's my kvmctl script) Chris Webb
2008-06-12 2:07 ` Since we're sharing, here's my kvmctl script Javier Guerra Giraldez
2 siblings, 1 reply; 8+ messages in thread
From: Freddie Cash @ 2008-06-11 23:04 UTC (permalink / raw)
To: kvm
On Wed, Jun 11, 2008 at 3:52 PM, Freddie Cash <fjwcash@gmail.com> wrote:
> For everyone's viewing (and critiquing, I guess) pleasure, I present
> my version of a kvmctl script.
[snip]
> It's released under the BSD License, so do with it as you wish. :)
> Patches and suggestions are always welcome, of course.
>
> http://www.sd73.bc.ca/downloads/kvmctl-2.0.0.tbz
Of course, right after sending that, I find a bunch of issues with it.
So, grab the new tarball, if you're interested:
http://www.sd73.bc.ca/downloads/kvmctl-2.0.1.tbz
--
Freddie Cash
fjwcash@gmail.com
^ permalink raw reply [flat|nested] 8+ messages in thread
* qemu-send.c (was Re: Since we're sharing, here's my kvmctl script)
2008-06-11 22:52 Since we're sharing, here's my kvmctl script Freddie Cash
2008-06-11 23:04 ` Freddie Cash
@ 2008-06-12 0:42 ` Chris Webb
2008-06-12 2:10 ` Javier Guerra Giraldez
2008-06-12 2:07 ` Since we're sharing, here's my kvmctl script Javier Guerra Giraldez
2 siblings, 1 reply; 8+ messages in thread
From: Chris Webb @ 2008-06-12 0:42 UTC (permalink / raw)
To: Freddie Cash; +Cc: kvm
[-- Attachment #1: Type: text/plain, Size: 1510 bytes --]
Freddie Cash <fjwcash@gmail.com> writes:
> For everyone's viewing (and critiquing, I guess) pleasure, I present
> my version of a kvmctl script.
Hi. I have a small 'qemu-send' utility for talking to a running qemu/kvm
process whose monitor console listens on a filesystem socket, which I think
might be a useful building block when extending these kinds of script to do
things like migratation, pausing, and so on. The source is attached.
It's careful to extract and pass on the correct command output from kvm and
waits for the (qemu) prompt to return before exiting, so you can do stuff
like
qemu-send /var/run/vm.ctl migrate file:///var/statedumps/foo
do-something-with-the-file /var/statedumps/foo
without any race problem, and
qemu-send /tmp/vm.ctl 'info blockstats'
will list the right info without any extraneous command echo or prompt text
leaking out.
Any comments or feedback welcome, and please feel free to incorporate it
where useful.
> The only thing I haven't been able to figure out, is how to send a
> "shutdown" command from the host OS to the guest OS, such that the
> guest OS will do a proper shutdown sequence. You have to switch to
> the VM console and manually tell it to shutdown. :(
If you have the qemu monitor listening on a unix socket /var/run/vm.ctl,
i.e. if you've started the kvm with an argument like
-monitor unix:/var/run/vm.ctl,server,nowait
you could send a graceful shutdown using
qemu-send /var/run/vm.ctl system_powerdown
Best wishes,
Chris.
[-- Attachment #2: qemu-send.c --]
[-- Type: text/x-csrc, Size: 2378 bytes --]
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
const char *prompt = "(qemu) ";
void echo(char *s, size_t n, int *skip) {
char *p;
for (p = s; *skip != 0 && p < n + s; p++, (*skip)--)
if ((p = memchr(p, '\n', n + s - p)) == NULL)
return;
if (p < n + s)
write(STDOUT_FILENO, p, n + s - p);
}
void getprompt(int fd, int skip, int eof) {
char s[PIPE_BUF];
int n, sl = 0;
do {
if ((n = read(fd, s + sl, sizeof(s) - sl)) < 0) {
perror("read");
exit(EXIT_FAILURE);
} else
sl += n;
if (n == 0) {
echo(s, sl, &skip);
exit(eof);
}
if (sl > strlen(prompt)) {
echo(s, sl - strlen(prompt), &skip);
memmove(s, s + sl - strlen(prompt), strlen(prompt));
sl = strlen(prompt);
}
} while (memcmp(s, prompt, strlen(prompt)));
}
void usage(char *progname) {
fprintf(stderr, "\
Usage: %1$s [-n] [-q] SOCKET COMMAND\n\
%1$s -t SOCKET\n\
Options:\n\
-n do not wait for command to finish before returning\n\
-q do not echo output from kvm/qemu to stdout\n\
-t test if kvm/qemu is listening on SOCKET without issuing a command\n\
", progname);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv) {
int n, sock, quiet = 0, wait = 1, test = 0;
struct sockaddr_un sockaddr;
while ((n = getopt(argc, argv, "nqt")) > 0)
switch (n) {
case 'n':
wait = 0;
break;
case 'q':
quiet = 1;
break;
case 't':
test = 1;
break;
default:
usage(argv[0]);
}
if ((argc -= optind) != (test ? 1 : 2))
usage(argv[0]);
argv += optind;
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
perror("socket");
return EXIT_FAILURE;
}
sockaddr.sun_family = AF_UNIX;
strcpy(sockaddr.sun_path, argv[0]);
n = strlen(sockaddr.sun_path) + sizeof(sockaddr.sun_family);
if (connect(sock, (struct sockaddr *) &sockaddr, n) < 0) {
if (test == 0)
perror("connect");
return EXIT_FAILURE;
}
getprompt(sock, -1, EXIT_FAILURE);
if (test == 0) {
write(sock, argv[1], strlen(argv[1]));
write(sock, "\n", 1);
if (wait)
/* always discard first line because of command echo */
getprompt(sock, quiet ? -1 : 1, EXIT_SUCCESS);
}
return EXIT_SUCCESS;
}
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Since we're sharing, here's my kvmctl script
2008-06-11 22:52 Since we're sharing, here's my kvmctl script Freddie Cash
2008-06-11 23:04 ` Freddie Cash
2008-06-12 0:42 ` qemu-send.c (was Re: Since we're sharing, here's my kvmctl script) Chris Webb
@ 2008-06-12 2:07 ` Javier Guerra Giraldez
2008-06-12 8:38 ` William Boughton
2 siblings, 1 reply; 8+ messages in thread
From: Javier Guerra Giraldez @ 2008-06-12 2:07 UTC (permalink / raw)
To: Freddie Cash; +Cc: kvm
[-- Attachment #1: Type: text/plain, Size: 541 bytes --]
On Wednesday 11 June 2008, Freddie Cash wrote:
> The script can be run as a normal user, as it will use sudo where
> needed. However, this causes all the VMs to be run as root (this is
> developed on Debian where they've added that annoying "feature" of not
> being able to create/use tun/tap devices as non-root users). If
> anyone knows how to unbreak Debian to allow non-root users to create
> tun/tap devices, I'm all ears.
change the group, owner, and/or privileges of /dev/net/tun, usually maneged by
udev
--
Javier
[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: qemu-send.c (was Re: Since we're sharing, here's my kvmctl script)
2008-06-12 0:42 ` qemu-send.c (was Re: Since we're sharing, here's my kvmctl script) Chris Webb
@ 2008-06-12 2:10 ` Javier Guerra Giraldez
2008-06-12 8:35 ` Chris Webb
0 siblings, 1 reply; 8+ messages in thread
From: Javier Guerra Giraldez @ 2008-06-12 2:10 UTC (permalink / raw)
To: Chris Webb; +Cc: Freddie Cash, kvm
[-- Attachment #1: Type: text/plain, Size: 589 bytes --]
On Wednesday 11 June 2008, Chris Webb wrote:
> Hi. I have a small 'qemu-send' utility for talking to a running qemu/kvm
> process whose monitor console listens on a filesystem socket, which I think
> might be a useful building block when extending these kinds of script to do
> things like migratation, pausing, and so on. The source is attached.
there's a utility called socat that let's you send text to/from TCP sockets
and unix-domain sockets. it can even (temporarily) attach the terminal, or
use GNU's readline to regain interactive control of KVM/Qemu
--
Javier
[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: qemu-send.c (was Re: Since we're sharing, here's my kvmctl script)
2008-06-12 2:10 ` Javier Guerra Giraldez
@ 2008-06-12 8:35 ` Chris Webb
0 siblings, 0 replies; 8+ messages in thread
From: Chris Webb @ 2008-06-12 8:35 UTC (permalink / raw)
To: Javier Guerra Giraldez; +Cc: Freddie Cash, kvm
Javier Guerra Giraldez <javier@guerrag.com> writes:
> On Wednesday 11 June 2008, Chris Webb wrote:
> > Hi. I have a small 'qemu-send' utility for talking to a running qemu/kvm
> > process whose monitor console listens on a filesystem socket, which I think
> > might be a useful building block when extending these kinds of script to do
> > things like migratation, pausing, and so on. The source is attached.
>
> there's a utility called socat that let's you send text to/from TCP sockets
> and unix-domain sockets. it can even (temporarily) attach the terminal, or
> use GNU's readline to regain interactive control of KVM/Qemu
Hi. Yes, I'm aware of socat, netcat, tcpclient et al. and even have a
similar pair of little unix/tcp/udp/syslogging utilities myself called
sk/skd which I initially used for scripting our local kvm management system.
However, it's a little bit clumsy to use these tools correctly from a shell
script if you want to get back the command output intact. You need to open
your connection to the unix server socket, wait for the prompt (skipping the
welcome banner), send the command, copy the response out until you get a
line '(qemu) ', then disconnect. For the same reason you can't do
echo -e "GET / HTTP/1.1\n\n" >/dev/tcp/www.google.com/80
cat </dev/tcp/www.google.com/80
having to write
exec 3<>/dev/tcp/www.google.com/80
echo -e "GET / HTTP/1.1\n\n" >&3
cat <&3
instead, you need to avoid disconnecting from the socket in the middle of
the command/response exchange.
(In fact, with qemu, it nearly works anyway: the new connection gets all the
output and the next prompt from the old one before the new banner, so you
just have a couple of extra prompts, a command echo and a banner at the top
and bottom to filter away. However, I'd be very reluctant to rely on this
behaviour, and in particular on it not losing output between connections.
The method I implemented in qemu-send.c should be robust again changes in
the way qemu handles its monitor sockets.)
To get the convenient syntax and behaviour I wanted, it felt easier
and cleaner to write the few lines of C needed for a standalone utility
rather than introduce a parsing shell script/function plus a dependency on
one of sk/socat/netcat/tcpclient. I suspect also that I'm just more
comfortable in C than sh; YMMV!
Cheers,
Chris.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Since we're sharing, here's my kvmctl script
2008-06-12 2:07 ` Since we're sharing, here's my kvmctl script Javier Guerra Giraldez
@ 2008-06-12 8:38 ` William Boughton
0 siblings, 0 replies; 8+ messages in thread
From: William Boughton @ 2008-06-12 8:38 UTC (permalink / raw)
To: kvm
On Wed, Jun 11, 2008 at 09:07:49PM -0500, Javier Guerra Giraldez wrote:
> On Wednesday 11 June 2008, Freddie Cash wrote:
> > The script can be run as a normal user, as it will use sudo where
> > needed. However, this causes all the VMs to be run as root (this is
> > developed on Debian where they've added that annoying "feature" of not
> > being able to create/use tun/tap devices as non-root users). If
> > anyone knows how to unbreak Debian to allow non-root users to create
> > tun/tap devices, I'm all ears.
>
> change the group, owner, and/or privileges of /dev/net/tun, usually maneged by
> udev
This won't help with recent kernels as you need CAP_NET_ADMIN to create
a device. I use tunctl which is part of uml-utilities in Debian to create
the network device and then pass it to qemu with ifname.
e.g
USER=kvm
NAME=test
IFACE=tap${NAME}
IFACE=$(sudo $TUNCTL -b -u $USER -t ${IFACE})
qemu-system-x86_64 ... -net tap,script=/etc/qemu-ifup,ifname=${IFACE}
bill
--
Bill Boughton
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Since we're sharing, here's my kvmctl script
2008-06-11 23:04 ` Freddie Cash
@ 2008-06-12 15:15 ` Freddie Cash
0 siblings, 0 replies; 8+ messages in thread
From: Freddie Cash @ 2008-06-12 15:15 UTC (permalink / raw)
To: kvm
On Wed, Jun 11, 2008 at 4:04 PM, Freddie Cash <fjwcash@gmail.com> wrote:
> On Wed, Jun 11, 2008 at 3:52 PM, Freddie Cash <fjwcash@gmail.com> wrote:
>> For everyone's viewing (and critiquing, I guess) pleasure, I present
>> my version of a kvmctl script.
> [snip]
>> It's released under the BSD License, so do with it as you wish. :)
>> Patches and suggestions are always welcome, of course.
>>
>> http://www.sd73.bc.ca/downloads/kvmctl-2.0.0.tbz
>
> Of course, right after sending that, I find a bunch of issues with it.
> So, grab the new tarball, if you're interested:
> http://www.sd73.bc.ca/downloads/kvmctl-2.0.1.tbz
And one more refresh. http://www.sd73.bc.ca/downloads/kvmctl-2.0.2.tbz
--
Freddie Cash
fjwcash@gmail.com
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2008-06-12 15:15 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-11 22:52 Since we're sharing, here's my kvmctl script Freddie Cash
2008-06-11 23:04 ` Freddie Cash
2008-06-12 15:15 ` Freddie Cash
2008-06-12 0:42 ` qemu-send.c (was Re: Since we're sharing, here's my kvmctl script) Chris Webb
2008-06-12 2:10 ` Javier Guerra Giraldez
2008-06-12 8:35 ` Chris Webb
2008-06-12 2:07 ` Since we're sharing, here's my kvmctl script Javier Guerra Giraldez
2008-06-12 8:38 ` William Boughton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox