-
-
Save Lennie/35ae648347ccb9d45962 to your computer and use it in GitHub Desktop.
| This is based on http://serverascode.com/2014/03/13/boot2docker-qemu.html | |
| After running the script, you should be able to login using: | |
| ssh-keygen -R $IP # if the host-key is already know for that IP-address | |
| ssh-keyscan -p 22 $IP > /var/lib/libvirt/images/boot2docker1.hostfile | |
| ssh-keygen -H -f /var/lib/libvirt/images/boot2docker1.hostfile | |
| rm -f /var/lib/libvirt/images/boot2docker1.hostfile.old | |
| ssh -p 22 -o UserKnownHostsFile=/var/lib/libvirt/images/boot2docker1.hostfile -i /var/lib/libvirt/images/boot2docker1.key docker@$IP | |
| #!/bin/bash | |
| vmtype=boot2docker | |
| num_vms=1 | |
| backing_image=boot2docker.img | |
| for ((i=1; i<=num_vms; i++)); do | |
| virsh destroy ${vmtype}$i > /dev/null | |
| virsh undefine ${vmtype}$i > /dev/null | |
| rm -f ./${vmtype}0${i}.xml > /dev/null | |
| rm -f /var/lib/libvirt/images/${vmtype}$i.img > /dev/null | |
| rm -f /var/lib/libvirt/images/${vmtype}$i-persist.img > /dev/null | |
| rm -f /var/lib/libvirt/images/boot2docker$i.key | |
| rm -f /var/lib/libvirt/images/boot2docker$i.key.pub | |
| # | |
| # Setup partitions for image | |
| # | |
| cat <<-SFDISKOUT > /var/tmp/sfdisk.out.${vmtype}${i} | |
| # partition table of boot2docker1-persist.img | |
| unit: sectors | |
| ${vmtype}${i}-persist.img1 : start= 2048, size= 10483712, Id=83 | |
| ${vmtype}${i}-persist.img2 : start= 0, size= 0, Id= 0 | |
| ${vmtype}${i}-persist.img3 : start= 0, size= 0, Id= 0 | |
| ${vmtype}${i}-persist.img4 : start= 0, size= 0, Id= 0 | |
| SFDISKOUT | |
| # | |
| # Create images | |
| # | |
| pushd /var/lib/libvirt/images > /dev/null | |
| qemu-img create -f qcow2 -b ${backing_image} ${vmtype}${i}.img > /dev/null | |
| qemu-img create -f raw ${vmtype}${i}-persist.img 5G | |
| #sfdisk --force ${vmtype}${i}-persist.img < /var/tmp/sfdisk.out.${vmtype}${i} | |
| #losetup --offset 1048576 /dev/loop0 ${vmtype}${i}-persist.img | |
| losetup /dev/loop0 ${vmtype}${i}-persist.img | |
| #mkfs.ext4 -F -L boot2docker-data /dev/loop0 | |
| mkdir b2d-data | |
| mkdir -p .ssh | |
| ssh-keygen -f boot2docker$i.key -N "" | |
| cat boot2docker$i.key.pub > .ssh/authorized_keys | |
| chown 1000:50 .ssh -R | |
| touch "boot2docker, please format-me" | |
| tar --numeric-owner -cf userdata.tar "boot2docker, please format-me" .ssh/ | |
| dd if=userdata.tar of=/dev/loop0 | |
| rm -f .ssh/authorized_keys | |
| rmdir .ssh | |
| rm -f "boot2docker, please format-me" | |
| rm -f userdata.tar | |
| sync | |
| rmdir b2d-data | |
| losetup -d /dev/loop0 | |
| popd > /dev/null | |
| rm -f /var/tmp/sfdisk.out.${vmtype}${i} | |
| chown libvirt-qemu:kvm /var/lib/libvirt/images/${vmtype}$i.img | |
| chown libvirt-qemu:kvm /var/lib/libvirt/images/${vmtype}$i-persist.img | |
| vm_uuid=`uuid` | |
| # | |
| # Build the libvirt xml file | |
| # | |
| cat <<-LIBVIRTXML > ${vmtype}${i}.xml | |
| <domain type='kvm'> | |
| <uuid>${uuid}</uuid> | |
| <name>${vmtype}${i}</name> | |
| <memory>4194304</memory> | |
| <os> | |
| <type>hvm</type> | |
| <boot dev="hd" /> | |
| </os> | |
| <features> | |
| <acpi/> | |
| </features> | |
| <vcpu>1</vcpu> | |
| <devices> | |
| <disk type='file' device='disk'> | |
| <driver type='qcow2' cache='none'/> | |
| <source file='/var/lib/libvirt/images/${vmtype}${i}.img'/> | |
| <target dev='vda' bus='virtio'/> | |
| </disk> | |
| <disk type='file' device='disk'> | |
| <driver type='raw' cache='none'/> | |
| <source file='/var/lib/libvirt/images/${vmtype}${i}-persist.img'/> | |
| <target dev='vdb' bus='virtio'/> | |
| </disk> | |
| <interface type='network'> | |
| <source network='default'/> | |
| <model type='virtio'/> | |
| <mac address='fa:16:3e:18:89:0${i}'/> | |
| </interface> | |
| </devices> | |
| </domain> | |
| LIBVIRTXML | |
| # | |
| # Define and start the vm | |
| # | |
| virsh define ${vmtype}${i}.xml > /dev/null | |
| if virsh start ${vmtype}${i} > /dev/null; then | |
| echo "${vmtype}${i} started" | |
| fi | |
| sleep 1 | |
| done | |
| virsh list --all | |
| exit 0 | |
Update: OK, so the reason it doesn't work is, because the partition created by boot2docker isn't mounted the second time the VM starts.
This isn't working for some reason: blkid -o device -l -t LABEL=boot2docker-data
For some reason mkfs fails: https://github.com/boot2docker/boot2docker/blob/master/rootfs/rootfs/etc/rc.d/automount#L32
I tried doing it the same way boot2docker does it normally. And just mount the .iso. So there are not 2 disks.
But it doesn't solve the problem.
When the filesystem isn't created. the label isn't set, the filesystem isn't mounted and a tmpfs is mounted under /mnt/vda1/var/lib/docker/aufs. There is nothing mounted at /mnt/vda1. /mnt/vda1/var/lib/boot2docker/userdata.tar does contain the SSH-key.
/home/docker/log.log does contain:
NEW boot2docker managed disk image (/dev/vda): formatting it for use
hacked up a quick connect script:
#! /bin/bash
VM=1
IP=""
while [ "$IP" == "" ]; do
IP=`grep -i "fa:16:3e:18:89:0$VM" /var/lib/libvirt/dnsmasq/default.leases | awk '{print $3}'`
if [ "$IP" == "" ]; then
sleep 1
fi
done
echo $IP
rm /var/lib/libvirt/images/boot2docker$VM.hostfile
LINES=0
while [ "$LINES" == "0" ]; do
ssh-keyscan -p 22 $IP 2> >(grep $IP) > /var/lib/libvirt/images/boot2docker$VM.hostfile
LINES=`cat /var/lib/libvirt/images/boot2docker$VM.hostfile | wc -l`
echo $LINES
if [ "$LINES" == "0" ]; then
sleep 1
fi
done
ssh-keygen -H -f /var/lib/libvirt/images/boot2docker$VM.hostfile
rm -f /var/lib/libvirt/images/boot2docker$VM.hostfile.old
ssh -p 22 -o UserKnownHostsFile=/var/lib/libvirt/images/boot2docker$VM.hostfile -i /var/lib/libvirt/images/boot2docker$VM.key docker@$IP
Darn, I thought when I would do it the boot2docker way. It would persist, but the SSH-key in /home/docker is gone after shutdown.