summaryrefslogtreecommitdiff
path: root/global/overlay/usr/local/sbin/cosmos_vm
blob: 5eec8f712f5a845de7f80d2e7a8ac66279f6cb57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash

set -- $(getopt h?H:D?s:B:M:C:R:i:g:n:I:G:N: "$@")

src_image=""
size="1G"
dhcp="no"
hostname="default"
bridge="br0"
cpus="1"
mem="1024"
repo="https://yourhost/myproj-cosmos.git"
tag="cosmos-ops"
ip=""
gateway=""
netmask=""
resolver=""
ip6=""
gateway6=""
netmask6="64"

while [ $# -gt 0 ]; do
   case "$1" in
      (-h) echo "Usage: $0 [-h] [-H hostname] [-M <memory>] [-C <#cpus>] [-B <bridge>] [-D (dhcp)] [-i/-I <ip4/6>] [-n/-N <mask4/6>] [-g/-G <gw4/6>] [-R <resolver(s)>] [-s <src image>]"; exit 0;;
      (-H) hostname="$2"; shift;; 
      (-s) src_image="$2"; shift;;
      (-D) dhcp="yes" ;;
      (-S) size="$2"; shift;;
      (-B) bridge="$2"; shift;;
      (-M) mem="$2"; shift;;
      (-C) cpus="$2"; shift;;
      (-R) resolver="$2"; shift;;
      (-i) ip="$2"; shift;;
      (-g) gateway="$2"; shift;;
      (-n) netmask="$2"; shift;;
      (-I) ip6="$2"; shift;;
      (-G) gateway6="$2"; shift;;
      (-N) netmask6="$2"; shift;;
      (--) shift; break;;
      (-*) echo "Unknown option $1\nUsage: $0 [-h] [-H hostname] [-M <memory>] [-C <#cpus>] [-B <bridge>] [-D (dhcp)] [-i/-I <ip4/6>] [-n/-N <mask4/6>] [-g/-G <gw4/6>] [-R <resolver(s)>] [-s <src image>]"; exit 1;;
      (*) break;;
   esac
   shift
done

id=$(uuidgen)

seed=${id}_seed.img
disk=${id}.img

rm -f ${seed}
truncate --size 2M ${seed}
mkfs.vfat -n cidata ${seed} 2>/dev/null

user_data=$(mktemp)
cat > ${user_data} <<EOF
#cloud-config
password: ubuntu
chpasswd: { expire: False }
ssh_pwauth: True
ssh_authorized_keys:
   - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDVvB4gdJ6EWRmx8xUSxrhoUNnWxEf8ZwAqhzC1+7XBY/hSd/cbEotLB9gxgqt0CLW56VU4FPLTw8snD8tgsyZN6KH1Da7UXno8oMk8tJdwLQM0Ggx3aWuztItkDfBc3Lfvq5T07YfphqJO7rcSGbS4QQdflXuOM9JLi6NStVao0ia4aE6Tj68pVVb3++XYvqvbU6NtEICvkTxEY93YpnRSfeAi64hsbaqSTN4kpeltzoSD1Rikz2aQFtFXE03ZC48HtGGhdMFA/Ade6KWBDaXxHGARVQ9/UccfhaR2XSjVxSZ8FBNOzNsH4k9cQIb2ndkEOXZXnjF5ZjdI4ZU0F+t7 leifj+00060AD478D6@sunet.se
runcmd:
   - ["mkdir","/tmp/seed"]
   - ["mount","/dev/vdb","/tmp/seed"]
   - ["cp","/tmp/seed/bootstrap-cosmos.sh","/tmp/seed/cosmos_1.5-1_all.deb","/root"]
   - ["cd","/root"]
   - "cd /root && /root/bootstrap-cosmos.sh ${hostname} ${repo} ${tag}"

EOF

meta_data=$(mktemp)
cat > ${meta_data} <<EOF
#cloud-config
instance-id: iid-${id}
local-hostname: ${hostname}
EOF

if [ "x${dhcp}" = "xyes" ]; then
cat >> ${meta_data} <<EOF
network-interfaces: |
    auto eth0
    iface eth0 inet dhcp
EOF
else
  if [ "x${ip}" != "x" ]; then
     cat >> ${meta_data} <<EOF
network-interfaces: |
    auto eth0
    iface eth0 inet static
        address ${ip}
        netmask ${netmask}
        gateway ${gateway}
        dns-nameservers ${resolver}

EOF
  fi

  if [ "x${ip6}" != "x" ]; then
     cat >> ${meta_data} <<EOF
network-interfaces: |
    auto eth0
    iface eth0 inet6 static
        address ${ip6}
        netmask ${netmask6}
        gateway ${gateway6}
        dns-nameservers ${resolver}

EOF
  fi
fi

mcopy -i ${seed} ${user_data} ::user-data 2>/dev/null
mcopy -i ${seed} ${meta_data} ::meta-data 2>/dev/null
mcopy -i ${seed} /etc/cosmos/apt/bootstrap-cosmos.sh /etc/cosmos/apt/cosmos_1.5-1_all.deb ::
mv ${seed} /var/lib/libvirt/images/
virsh pool-refresh default

virsh vol-clone --pool default ${src_image} ${disk}

virt-install -r ${mem} -n ${hostname} --vcpus=${cpus} --autostart --memballoon virtio --network bridge=${bridge} --boot hd --disk vol=default/${disk},format=qcow2,bus=virtio --disk vol=default/${seed},bus=virtio

rm -f ${user_data}
rm -f ${meta_data}