Last active
January 17, 2020 00:48
-
-
Save Lennie/35ae648347ccb9d45962 to your computer and use it in GitHub Desktop.
boot2docker-qemu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hacked up a quick connect script: