Skip to content

Instantly share code, notes, and snippets.

@mostlyobvious
Created July 21, 2012 14:08
Show Gist options
  • Save mostlyobvious/3155902 to your computer and use it in GitHub Desktop.
Save mostlyobvious/3155902 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Mostly deboostrap, but also changes hostname and configures networking
# for guest. Intended to use with lxc and bridged networks with dnsmasq.
PACKAGES=avahi-daemon,openssh-server
USER_NAME=squeeze
DISTRIBUTION=precise
VM_PATH=${1-template}
VM_NAME=$(basename $VM_PATH)
sudo debootstrap --include=$PACKAGES $DISTRIBUTION $VM_PATH
sudo chroot $VM_PATH /bin/bash -c "useradd -m -s /bin/bash -G sudo $USER_NAME"
sudo chroot $VM_PATH /bin/bash -c "echo $USER_NAME:$USER_NAME | chpasswd"
sudo chroot $VM_PATH /bin/bash -c "cat > /etc/hostname <<EOF
$VM_NAME
EOF"
sudo chroot $VM_PATH /bin/bash -c "cat > /etc/network/interfaces <<EOF
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
EOF"
sudo chroot $VM_PATH /bin/bash -c "sed -i s/rlimit-nproc=3// /etc/avahi/avahi-daemon.conf"
ssh-keygen -P "" -f $VM_NAME.key
sudo mkdir -p $VM_PATH/home/$USER_NAME/.ssh
sudo mv $VM_NAME.key.pub $VM_PATH/home/$USER_NAME/.ssh/authorized_keys
cat > $VM_NAME.xml <<EOF
<domain type='lxc'>
<name>$VM_NAME</name>
<memory>332768</memory>
<os>
<type>exe</type>
<init>/sbin/init</init>
</os>
<vcpu>1</vcpu>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/lib/libvirt/libvirt_lxc</emulator>
<filesystem type='mount'>
<source dir='$VM_PATH'/>
<target dir='/'/>
</filesystem>
<interface type='network'>
<source network='default'/>
</interface>
<console type='pty' />
</devices>
</domain>
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment