aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xkvmx-create160
1 files changed, 84 insertions, 76 deletions
diff --git a/kvmx-create b/kvmx-create
index 7f20585..33a7351 100755
--- a/kvmx-create
+++ b/kvmx-create
@@ -146,21 +146,17 @@ kvmx_sudo_run mkdir -p `dirname $image`
#
function kvmx_create_vmdebootstrap {
# Check for requirements
- for req in vmdebootstrap mbr; do
+ for req in vmdebootstrap; do
kvmx_install_package $req
done
# Image format
if [ "$format" == "qcow2" ]; then
format="--convert-qcow2"
- else
- formt=""
fi
if [ "$booloader" == "grub" ]; then
boot_option="--grub"
- else
- boot_option=""
fi
# Run
@@ -181,8 +177,14 @@ function kvmx_create_vmdebootstrap {
function kvmx_create_custom {
WORK="`mktemp -d`"
+ if [ "$arch" == "i386" ]; then
+ kernel_arch="686"
+ else
+ kernel_arch="$arch"
+ fi
+
# Check for requirements.
- for req in debootstrap grub-pc parted; do
+ for req in debootstrap parted; do
kvmx_install_package $req
done
@@ -205,7 +207,8 @@ function kvmx_create_custom {
# Initial system install.
echo "Installing base system..."
- kvmx_sudo_run LC_ALL=C DEBIAN_FRONTEND=noninteractive debootstrap --arch=$arch $version $WORK/ $mirror
+ kvmx_sudo_run LC_ALL=C DEBIAN_FRONTEND=noninteractive debootstrap \
+ --arch=$arch --include linux-image-$kernel_arch $version $WORK/ $mirror
# Initial configuration.
echo "Applying initial configuration..."
@@ -224,84 +227,17 @@ function kvmx_create_custom {
# Fstab
echo "/dev/vda2 / ext4 errors=remount-ro 0 1" | $SUDO tee $WORK/etc/fstab > /dev/null
- # Locale
- $APT_INSTALL locales
- echo "LANG=$LANG" | $SUDO tee $WORK/etc/default/locale > /dev/null
- echo "$LANG UTF-8" | $SUDO tee -a $WORK/etc/locale.gen > /dev/null
- kvmx_sudo_run chroot $WORK/ locale-gen
-
- # Initial upgrade
- echo "Applying initial upgrades..."
- kvmx_sudo_run chroot $WORK/ apt-get update
- kvmx_sudo_run chroot $WORK/ apt-get upgrade -y
-
- if [ "$arch" == "i386" ]; then
- kernel_arch="686"
- else
- kernel_arch="$arch"
- fi
-
- # Basic packages
- $APT_INSTALL screen cron lsb-release openssl rsync -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
-
- # 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
-
- if [ "$ssh_support" == "y" ]; then
- if [ "$ssh_custom" == "y" ]; then
- privkey="`dirname $image`/ssh/$hostname.key"
- pubkey="${privkey}.pub"
- mkdir -p "`dirname $privkey`"
- __kvmx_ssh_keygen $privkey "$user@$hostname"
- else
- pubkey="$DIRNAME/share/ssh/insecure_private_key.pub"
- fi
-
- kvmx_sudo_run chroot $WORK/ mkdir -p /home/user/.ssh
- kvmx_sudo_run chroot $WORK/ chmod 700 /home/user/.ssh
- kvmx_sudo_run cp $pubkey $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
- fi
-
- kvmx_sudo_run chroot $WORK/ chown -R user.user /home/user
- echo "$user:$password" | 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
-
# Mount auxiliary filesystems needed by the bootloader
kvmx_sudo_run mount none -t proc $WORK/proc
kvmx_sudo_run mount none -t sysfs $WORK/sys
kvmx_sudo_run mount -o bind /dev/ $WORK/dev
if [ "$bootloader" == "grub" ]; then
- # Possible alternative: https://packages.debian.org/jessie/grub-firmware-qemu
$APT_INSTALL grub-pc
- # GRUB install is not working for jessie and this was an attempt to fix it
- #kvmx_sudo_run sed -i -e 's|^#GRUB_DISABLE_LINUX_UUID=true|GRUB_DISABLE_LINUX_UUID=true|' $WORK/etc/default/grub
kvmx_sudo_run chroot $WORK/ update-grub
kvmx_sudo_run chroot $WORK/ grub-install $device
- # Alternative: install from the host directly into the image
+ # Possible alternatives:
+ # https://packages.debian.org/jessie/grub-firmware-qemu
# https://superuser.com/questions/130955/how-to-install-grub-into-an-img-file
#kvmx_sudo_run grub-install --boot-directory=$WORK/boot $image
elif [ "$bootloader" == "extlinux" ]; then
@@ -328,6 +264,9 @@ EOF
kvmx_sudo_run umount $WORK/sys
kvmx_sudo_run umount $WORK/dev
+ # Run basic provision
+ __kvmx_create_custom_second_stage
+
# Umount image
kvmx_sudo_run umount $WORK
kvmx_sudo_run rmdir $WORK
@@ -345,6 +284,75 @@ EOF
kvmx_sudo_run chown -R `whoami`. `dirname $image`
}
+# Second stage procedure
+function __kvmx_create_custom_second_stage {
+ # Networking: eth0
+ cat <<-EOF | $SUDO tee $WORK/etc/network/interfaces.d/eth0 > /dev/null
+allow-hotplug eth0
+iface eth0 inet dhcp
+EOF
+
+ # Networking: ens3
+ # See #799253 - virtio ens3 network interface
+ # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=799253
+ cat <<-EOF | $SUDO tee $WORK/etc/network/interfaces.d/ens3 > /dev/null
+allow-hotplug ens3
+iface ens3 inet dhcp
+EOF
+
+ # Locale
+ $APT_INSTALL locales
+ echo "LANG=$LANG" | $SUDO tee $WORK/etc/default/locale > /dev/null
+ echo "$LANG UTF-8" | $SUDO tee -a $WORK/etc/locale.gen > /dev/null
+ kvmx_sudo_run chroot $WORK/ locale-gen
+
+ # Initial upgrade
+ #echo "Updating list of packages..."
+ #kvmx_sudo_run chroot $WORK/ apt-get update
+ #kvmx_sudo_run chroot $WORK/ apt-get dist-upgrade -y
+
+ # Basic packages
+ $APT_INSTALL screen cron lsb-release openssl rsync
+ $APT_INSTALL spice-vdagent qemu-guest-agent
+
+ # 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
+
+ # Root password
+ echo 'root:root' | kvmx_sudo_run chroot $WORK/ chpasswd
+
+ # Initial user
+ if ! grep -q "^$user:" $WORK/etc/passwd; then
+ kvmx_sudo_run chroot $WORK/ useradd $user -G sudo -s /bin/bash
+ fi
+
+ if [ "$ssh_support" == "y" ]; then
+ if [ "$ssh_custom" == "y" ]; then
+ privkey="`dirname $image`/ssh/$hostname.key"
+ pubkey="${privkey}.pub"
+ mkdir -p "`dirname $privkey`"
+ __kvmx_ssh_keygen $privkey "$user@$hostname"
+ else
+ pubkey="$DIRNAME/share/ssh/insecure_private_key.pub"
+ fi
+
+ kvmx_sudo_run chroot $WORK/ mkdir -p /home/user/.ssh
+ kvmx_sudo_run chroot $WORK/ chmod 700 /home/user/.ssh
+ kvmx_sudo_run cp $pubkey $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
+ fi
+
+ kvmx_sudo_run chroot $WORK/ chown -R user.user /home/user
+ echo "$user:$password" | kvmx_sudo_run chroot $WORK/ chpasswd
+}
+
# Dispatch
if [ "$method" == "custom" ]; then
kvmx_create_custom