aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xkvmx51
-rwxr-xr-xkvmx-create66
-rw-r--r--kvmxfile7
3 files changed, 108 insertions, 16 deletions
diff --git a/kvmx b/kvmx
index 75c0c3a..6a5e2d2 100755
--- a/kvmx
+++ b/kvmx
@@ -41,7 +41,21 @@ function kvmx_up {
# Check if image exists, create otherwise
if [ ! -e "$image" ]; then
- kvmx-create $GLOBAL_USER_CONFIG_FOLDER/$VM
+ if [ ! -z "$basebox" ]; then
+ if [ -e "$GLOBAL_USER_CONFIG_FOLDER/$basebox" ]; then
+ baseimage="`kvmx list_image $basebox`"
+
+ if [ ! -e "$baseimage" ]; then
+ echo "$BASENAME: could not find basebox $baseimage. Please create it first."
+ exit 1
+ fi
+
+ echo "Copying base image $baseimage to $image"
+ cp $baseimage $image
+ fi
+ else
+ kvmx-create $GLOBAL_USER_CONFIG_FOLDER/$VM
+ fi
fi
# Run virtual machine
@@ -52,7 +66,7 @@ function kvmx_up {
-chardev spicevmc,id=spicechannel0,name=vdagent \
-smp 2 -soundhw ac97 -cpu host -balloon virtio \
-net nic,model=virtio \
- -net user,hostfwd=tcp:127.0.0.1:$SSH-:22 &
+ -net user,hostfwd=tcp:127.0.0.1:$SSH-:22 &> $LOGFILE &
PID="$!"
@@ -70,8 +84,8 @@ function kvmx_usage {
echo "examples:"
echo ""
echo "$BASENAME list"
- echo "$BASENAME init <machine> [folder]"
- echo "$BASENAME clone <orig> <dest>"
+ echo "$BASENAME init <machine> [folder]"
+ echo "$BASENAME clone <orig-guest> <dest-folder>"
exit 1
}
@@ -192,9 +206,10 @@ function kvmx_initialize {
PIDFILE="$STATE_DIR/pid"
PORTFILE="$STATE_DIR/port"
SSHFILE="$STATE_DIR/ssh"
+ LOGFILE="$STATE_DIR/log"
mkdir -p $STATE_DIR
- if [ ! -e "$image" ] && [ "$ACTION" != "up" ]; then
+ if [ ! -e "$image" ] && [ "$ACTION" != "up" ] && [ "$ACTION" != "purge" ] && [ "$ACTION" != "destroy" ]; then
echo "$BASENAME: file not found: $image"
exit 1
fi
@@ -205,8 +220,13 @@ function kvmx_initialize {
function kvmx_init {
FOLDER="$3"
- if [ -z "$VM" ]; then
- VM="$(basename `pwd`)"
+ if [ -z "$FOLDER" ]; then
+ if [ -z "$VM" ]; then
+ VM="$(basename `pwd`)"
+ FOLDER="$(dirname `pwd`)/$VM"
+ else
+ FOLDER="$(pwd)/$VM"
+ fi
fi
if [ -e "$GLOBAL_USER_CONFIG_FOLDER/$VM" ]; then
@@ -214,10 +234,6 @@ function kvmx_init {
exit 1
fi
- if [ -z "$FOLDER" ]; then
- FOLDER="."
- fi
-
if [ ! -d "$FOLDER" ]; then
mkdir -p $FOLDER
fi
@@ -266,7 +282,7 @@ function kvmx_clone {
# Copy image and configuration
cp -r `dirname $image` $FOLDER/
- cp $GLOBAL_USER_CONFIG_FOLDER/$VM $GLOBAL_USER_CONFIG_FOLDER/$DEST
+ ( cd $GLOBAL_USER_CONFIG_FOLDER && ln -s $FOLDER/kvmxfile $DEST )
# Update config file
new_image="$FOLDER/`basename $image`"
@@ -312,6 +328,17 @@ function kvmx_purge {
rm -f $GLOBAL_USER_CONFIG_FOLDER/$VM
}
+# Provision a machine
+function kvmx_provision {
+ echo "TODO: provision"
+ exit 1
+}
+
+# Print guest image file name
+function kvmx_list_image {
+ echo $image
+}
+
# Print guest status
function kvmx_status {
if kvmx_running; then
diff --git a/kvmx-create b/kvmx-create
index 2eb97d5..b460705 100755
--- a/kvmx-create
+++ b/kvmx-create
@@ -19,6 +19,7 @@
# Parameters
BASENAME="`basename $0`"
+DIRNAME="`dirname $0`"
# Load configuration
function kvmx_config_load {
@@ -197,6 +198,9 @@ function kvmx_create_custom {
tac $WORK/etc/hosts | $SUDO tee $WORK/etc/hosts.new > /dev/null
kvmx_sudo_run mv $WORK/etc/hosts.new $WORK/etc/hosts
+ # Fstab
+ echo "/dev/vda2 / ext4 errors=remount-ro 0 1" | $SUDO tee $WORK/etc/fstab > /dev/null
+
# Initial upgrade
echo "Applying initial upgrades..."
kvmx_sudo_run chroot $WORK/ apt-get update
@@ -208,9 +212,71 @@ function kvmx_create_custom {
kernel_arch="$arch"
fi
+ # Basic packages
$APT_INSTALL locales
$APT_INSTALL screen cron lsb-release openssl -y
+ $APT_INSTALL spice-vdagent qemu-guest-agent
+
+ # Kernel
$APT_INSTALL linux-image-$kernel_arch -y
+
+ # OpenSSH
+ $APT_INSTALL openssh-server -y
+ kvmx_sudo_run chroot $WORK/ service ssh stop
+
+ # Sudo
+ echo "Installing sudo..."
+ $APT_INSTALL sudo -y
+ echo "%sudo ALL=NOPASSWD: ALL" | $SUDO tee $WORK/etc/sudoers.d/local > /dev/null
+ kvmx_sudo_run chmod 440 $WORK/etc/sudoers.d/local
+
+ # Initscript
+ cat <<-EOF | $SUDO tee $WORK/etc/rc.local > /dev/null
+#!/bin/sh -e
+#
+# rc.local
+#
+# This script is executed at the end of each multiuser runlevel.
+# Make sure that the script will "exit 0" on success or any other
+# value on error.
+#
+# In order to enable or disable this script just change the execution
+# bits.
+#
+# By default this script does nothing.
+
+# Somehow it is starting before DBUS and then crashing, so we try to start again
+/usr/sbin/service spice-vdagent start
+
+# Ensure file sharing between host and guest
+if [ ! -z "$shared_mountpoint" ]; then
+ /bin/mkdir -p $shared_mountpoint
+ /bin/mount -t 9p -o trans=virtio shared $shared_mountpoint -oversion=9p2000.L,posixacl,cache=loose
+fi
+
+exit 0
+EOF
+
+ # Root password
+ echo 'root:root' | kvmx_sudo_run chroot $WORK/ chpasswd
+
+ # Initial user
+ kvmx_sudo_run chroot $WORK/ useradd user -G sudo -s /bin/bash
+ kvmx_sudo_run chroot $WORK/ mkdir -p /home/user/.ssh
+ kvmx_sudo_run chroot $WORK/ chmod 700 /home/user/.ssh
+ kvmx_sudo_run cp $DIRNAME/ssh/insecure_private_key.pub $WORK/home/user/.ssh/authorized_keys
+ kvmx_sudo_run chroot $WORK/ chmod 600 /home/user/.ssh/authorized_keys
+ kvmx_sudo_run touch $WORK/home/user/.hushlogin
+ kvmx_sudo_run chroot $WORK/ chown -R user.user /home/user
+ echo 'user:user' | kvmx_sudo_run chroot $WORK/ chpasswd
+
+ # Networking
+ cat <<-EOF | $SUDO tee $WORK/etc/network/interfaces.d/ens3 > /dev/null
+allow-hotplug ens3
+iface ens3 inet dhcp
+EOF
+
+ # Grub
$APT_INSTALL grub-pc -y
kvmx_sudo_run chroot $WORK/ update-grub
kvmx_sudo_run chroot $WORK/ grub-install $device
diff --git a/kvmxfile b/kvmxfile
index 7f65bc9..f3b0498 100644
--- a/kvmxfile
+++ b/kvmxfile
@@ -2,16 +2,15 @@
# Sample kvmx file
#
-# Which base box you should use.
-# If none is set, kvmx will bootstrap one for you.
+# Which base box you should use. Leave unconfigured to use kvmx-create instead.
#basebox="stretch"
# Absolute or relative path for a provision script.
#provision_script="default"
# Set this is you want to be able to share folders between host and guest.
-#shared_folder="."
-#shared_folder_mountpoint="/media/shared"
+shared_folder="."
+shared_folder_mountpoint="/media/shared"
# Set this if you want to automatically attach an spice client when the machine
# boots.