Configuration from the outside
Changing the configuration of a virtual machine between boots can be
inconvenient because of the need to change config files in the root
filesystem that boots the machine. For example, if you want to change
the filesystems that are mounted, you need to change the kernel
command line to pass in the new filesystems, but you also need to
change /etc/fstab to mount them.
You can mount loopback-mount the root filesystem and go in and edit
the relevant files, but this is more manual than necessary, and it's
impossible without root privileges on the host.
The solution that I'm using is to:
Generate the config files and whatever else needs to be changed or
added to the filesystem that will boot the machine
Lay them out in a skeletal filesystem structure reflecting where
they will go in the virtual machine
Tar up that directory
Add that tar file as one of the block devices on the kernel
command line
Also add
CONFIG_DEV=device-name
to the
command line
The first thing the machine does after it mounts the root filesystem
read-write is check the CONFIG_DEV environment variable. If it has a
value, then it tars the new files from the device after saving the
originals. This is early enough to ensure that almost any new
configuration options will take effect.
To enable this, you need to put this script in
/etc/rc.d/rc.configfiles
#!/bin/sh
for ARG in $@
do
case $ARG in
start)
if [ "$CONFIG_DEV" != "" ]; then
restore=""
remove=""
for file in `tar tf $CONFIG_DEV`; do
if [ -e $file ]
then restore="$restore $file"
else remove="$remove $file"
fi
done
[ "$restore" != "" ] && tar cf /tmp/restore.tar $restore
echo $remove > /tmp/remove
tar xf $CONFIG_DEV
fi
;;
stop)
if [ "$CONFIG_DEV" != "" ]; then
[ -e /tmp/restore.tar ] && tar xf /tmp/restore.tar
[ -f /tmp/remove ] && rm -rf `cat /tmp/remove`
rm -rf /tmp/restore.tar /tmp/remove
fi
;;
*)
echo "usage: $0 (start|stop)"
exit 1
;;
esac
done
exit 0
and add a call to /etc/rc.S:
/etc/rc.d/rc.configfiles start
and a similar call to /etc/rc.0:
/etc/rc.d/rc.configfiles stop
The addition to /etc/rc.S needs to be immediately after the read-write
remount of /:
mount -o remount /
and the /etc/rc.0 needs to be just before the read-only remount of /:
mount -n -o remount,ro /
With this in place, it is possible to arbitrarily reconfigure a
virtual machine without booting it up or loopback mounting
its root filesystem beforehand.
|