Skip to content

Instantly share code, notes, and snippets.

@imylomylo
Last active June 8, 2019 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imylomylo/af1d4765a5562bc970dc47876be62f89 to your computer and use it in GitHub Desktop.
Save imylomylo/af1d4765a5562bc970dc47876be62f89 to your computer and use it in GitHub Desktop.
KVM ubuntu16.04 host

Run this install

Change the GUESTNET and GUESTIP LIST

  • ./install.sh
  • reboot
#!/bin/bash

apt-get update
apt-get install -y git
apt-get install -y qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils virtinst sudo screen tmux wget git libguestfs-tools
adduser `id -un` kvm
adduser `id -un` libvirtd
useradd -m bestmylo
echo "bestmylo ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/bestmylo
lsmod | grep bridge
brctl show
#reboot
HOSTIP=`ifconfig eth0 | grep "inet addr" | cut -d ':' -f 2 | cut -d ' ' -f 1`
HOSTNETMASK=`ifconfig eth0 | grep "inet addr" | cut -d ':' -f 4 | cut -d ' ' -f 1`
NICHEX=$(hexdump -vn3 -e '/3 "52:54:00"' -e '/1 ":%02x"' -e '"\n"' /dev/urandom)
GUESTNET="172.16.240.1/29"
GUEST1="172.16.240.2"
GUEST2="172.16.240.3"
GUEST3="172.16.240.4"
GUEST4="172.16.240.5"
GUEST5="172.16.240.6"
GUEST6="172.16.240.7"
/bin/cp /etc/network/interfaces /etc/network/interfaces.bak
cat >> /etc/network/interfaces << EOF;

#KVM bridge stuff for custom routed network
# equivalent of 
# ip link add virbr10-dummy address $NICHEX  type dummy
auto virbr10-dummy
iface virbr10-dummy inet manual
  pre-up /sbin/ip link add virbr10-dummy type dummy                                                                                                                            
  up /sbin/ip link set virbr10-dummy address $NICHEX

auto virbr10                                                                                                                                                                   
iface virbr10 inet static                                                                                                                                                      
    # Make sure bridge-utils is installed!                                                                                                                                     
    bridge_ports virbr10-dummy                                                                                                                                                 
    bridge_stp on                                                                                                                                                              
    bridge_fd 2                                                                                                                                                                
    address $HOSTIP
    netmask $HOSTNETMASK
    up route add -host $GUEST1/32 dev virbr10                                                                                                                           
    up route add -host $GUEST2/32 dev virbr10                                                                                                                           
    up route add -host $GUEST3/32 dev virbr10                                                                                                                           
    up route add -host $GUEST4/32 dev virbr10                                                                                                                           
    up route add -host $GUEST5/32 dev virbr10                                                                                                                           
    up route add -host $GUEST6/32 dev virbr10                                                                                                                           
    #up route add -host 172.16.240.40/32 dev virbr10 
EOF

echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
echo "net.ipv4.conf.all.forwarding=1" >> /etc/sysctl.conf
echo "net.ipv6.conf.all.forwarding=1" >> /etc/sysctl.conf
sysctl -p

/sbin/iptables-save > /etc/network/iptables_rules
echo "" >> /etc/iptables_rules
grep COMMIT /etc/network/iptables_rules | tail -1 | sed -i 's/COMMIT/#COMMIT/' /etc/network/iptables_rules
cat >> /etc/network/iptables_rules << EOF;

# Allow inbound traffic to the private subnet.
-A FORWARD -d $GUESTNET -o virbr10 -j ACCEPT
# Allow outbound traffic from the private subnet.
-A FORWARD -s $GUESTNET -i virbr10 -j ACCEPT
# Allow inbound traffic to the ip
#-A FORWARD -d 172.16.240.40/32 -o virbr10 -j ACCEPT
# Allow outbound traffic from the ip
#-A FORWARD -s 172.16.240.40/32 -i virbr10 -j ACCEPT
COMMIT
EOF

sed -i 's/exit 0//' /etc/rc.local
echo "/sbin/iptables-restore < /etc/network/iptables_rules" >> /etc/rc.local
echo "exit 0" >> /etc/rc.local

Guests

git clone https://github.com/imylomylo/make-vps
cd /tmp
curl -O http://releases.ubuntu.com/18.04.2/ubuntu-18.04.2-live-server-amd64.iso
cd make-vps
VPSNAME=ubuntu18 HDSIZE=100 make ubuntu18

Complete install by vnc over ssh tunnel

ssh -L5900:localhost:5900 bestmylo@hostip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment