2

With VMware's PowerCLI you can invoke a script inside a guest, even before the guest has network conenctivity, using the Invoke-VMScript Cmdlet (requires vmtools to be installed).

Is there a similar mechanism available for invoking scripts inside KVM guests, before networking is available?

I need to for post-deployment customizations.

As a workaround, I thought about mounting the disk image and chrooting, but i'm looking for a more 'scriptable' and straight forward way.

Thanks.

BlackBeret
  • 123
  • 2
  • 10

3 Answers3

3

We provide each guest with a secondary read-only block device attached to a plain file on the host. The VM host can use this channel to transmit guest-specific information down to the guest in a secure unidirectional manner: in our installation, it's just a tar file with filesystem customizations & init scripts. The guest->host attack surface is minimal, and we don't have to deal with the significant overhead imposed by libguestfs.

Here's the libvirt guest XML snippet we use:

<disk type='file' device='disk'>
  <driver name='qemu' type='raw' cache='none' io='native'/>
  <source file='/vm/hostconf/testhost.hostconf.tar'/>
  <target dev='vdb' bus='virtio'/>
  <readonly/>
  <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>

On the guest, a small init script unpacks the tar file and makes the necessary changes.

agt
  • 51
  • 4
2

So there are three options here:

  1. Use the libvirtd api to aquire console access and execute commands that way. I would not advise this as its a bit of a dirty hack. Unless you disable the network from starting right away by default you are going to have to
  2. Use libguestfs to mount the disk and adjust the settings before booting. This is what the system I designed does, its actually pretty easy to do and not complicated to manage. We use perl and Sys::GuestFS allows us to script commands without too many issues.
  3. Setup the machine to bring up networking via dhcp and run a dhcpd server that gives it a temporary IP address, then connect via ssh and adjust the network config and reload it. This is the most robust solution, but it requires you to script a dhcpd server. Hope that helps.
n8whnp
  • 1,316
  • 7
  • 9
1

There's a very standard way to run scripts on any Linux distro before networking comes up, which is to put them in the boot sequence before networking starts. For example, on my CentOS 6 box, networking is started by /etc/rc3.d/S10network. So if I wanted to run something before networking came up, I could put it in as /etc/rc3.d/S05myscript.

For distributions that have drunk the upstart or systemd coolaid, there will be other, more complex, ways to do this; but the point still applies.

Edit: that's exactly what you meant by "before networking", it's just not what you meant by "invoking a script". You want to invoke the script from somewhere other than the machine itself (in this case, from the KVM host), which is fine, and I freely concede that my solution's no help - but you should really overhaul your question to make your requirements clearer!

Incidentally, I know of no way to do that with KVM.

MadHatter
  • 78,442
  • 20
  • 178
  • 229
  • Thanks, but that not what I meant by 'before networking'. What I need is a way to run a script that modifies the network interfaces configuration on the guest and gets the info of how to do that, from the script parameters. – BlackBeret Nov 20 '12 at 13:05
  • So it's not just about running it, but also passing variable information to it. – BlackBeret Nov 20 '12 at 13:12