Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagecell
Path: blob/master/contrib/vm/build_host.sh
447 views
1
#!/usr/bin/env bash
2
set -o errexit
3
set -o nounset
4
set -o xtrace
5
6
APTPROXY="http://your-proxy-here-or-delete-proxy-from-preseed/"
7
MAC=52:54:00:5a:9e:c1 # Must start with 52:54:00
8
9
VM_NAME=schostvm
10
OSVARIANT=ubuntu18.04
11
LOCATION="http://archive.ubuntu.com/ubuntu/dists/bionic/main/installer-amd64/"
12
13
sed "s|APTPROXY|$APTPROXY|" preseed.host > preseed.cfg
14
15
if ! [ -f ${VM_NAME}_rsa ]; then
16
ssh-keygen -q -N "" -C "sc_admin" -f ${VM_NAME}_rsa
17
fi
18
SSHKEY=`cat ${VM_NAME}_rsa.pub`
19
sed -i "s|SSHKEY|$SSHKEY|" preseed.cfg
20
21
virt-install \
22
--connect qemu:///system \
23
--name $VM_NAME \
24
--description "Host for SageCell instances." \
25
--ram=32768 \
26
--cpu host \
27
--vcpus=12 \
28
--location $LOCATION \
29
--initrd-inject=preseed.cfg \
30
--extra-args="console=ttyS0" \
31
--os-type=linux \
32
--os-variant=$OSVARIANT \
33
--disk pool=default,size=100,device=disk,bus=virtio,format=qcow2,cache=writeback \
34
--network network=default,model=virtio,mac=$MAC \
35
--graphics none \
36
--autostart \
37
--check-cpu \
38
--noreboot \
39
40
# For a dedicated partition use
41
#--disk path=/dev/???,bus=virtio \
42
43
# Make a script to SSH inside, assuming that IP will not change
44
virsh --connect qemu:///system start $VM_NAME
45
sleep 30
46
IP=`ip n|grep $MAC|grep -Eo "^[0-9.]{7,15}"`
47
cat <<EOF > ssh_host.sh
48
#!/usr/bin/env bash
49
50
if [[ \`virsh --connect qemu:///system domstate $VM_NAME\` != "running" ]]; then
51
if ! virsh --connect qemu:///system start $VM_NAME; then
52
echo "Failed to start $VM_NAME"
53
exit 1
54
fi
55
sleep 30
56
fi
57
58
ssh -i ${VM_NAME}_rsa root@$IP
59
EOF
60
chmod u+x ssh_host.sh
61
# Power cycle VM, otherwise bash completion does not work for LXC.
62
virsh --connect qemu:///system shutdown $VM_NAME
63
64