CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/scripts/new_vm_image.py
Views: 687
1
#!/usr/bin/env python
2
###############################################################################
3
#
4
# CoCalc: Collaborative Calculation
5
#
6
# Copyright (C) 2016, Sagemath Inc.
7
#
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#
21
###############################################################################
22
23
import argparse, os, sys, time
24
VM_PATH = os.path.join(os.environ['HOME'], 'vm/images/base3/')
25
26
27
def cmd(s):
28
print(s)
29
if os.system(s):
30
raise RuntimeError("error executing '%s'" % s)
31
32
33
cmd("ls -lth %s|head -5" % VM_PATH)
34
35
parser = argparse.ArgumentParser(description="Create a new VM image.")
36
parser.add_argument(
37
"--prev", dest="prev", type=str, help="previous vm image name", default="")
38
parser.add_argument(
39
"--next", dest="next", type=str, help="new vm image name", default="")
40
args = parser.parse_args()
41
42
prev = args.prev
43
next = args.next
44
45
if prev == "":
46
# make it the most recent image
47
prev = os.popen(
48
"ls -1t %s/*.img|head -1" % VM_PATH).read().strip().rstrip('.img')
49
50
if next == "":
51
# make image name salvus-date
52
next = time.strftime("salvus-%Y-%m-%d-%H%M")
53
54
defined_machines = os.popen("virsh_list").read()
55
56
for machine in [prev, next]:
57
if machine in defined_machines:
58
print("%s is currently defined. Please undefine it before proceeding further, in order to avoid any possible corruption." % machine)
59
sys.exit(1)
60
61
prev_path = os.path.join(VM_PATH, prev + '.img')
62
next_path = os.path.join(VM_PATH, next + '.img')
63
64
if not os.path.exists(prev_path):
65
raise ValueError("previous vm image doesn't exist -- " + prev_path)
66
67
if os.path.exists(next_path):
68
raise ValueError("next vm image already exists -- " + next_path)
69
70
cmd("qemu-img create -b %s -f qcow2 %s" % (prev_path, next_path))
71
cmd("chgrp kvm %s; chmod g+rw %s" % (next_path, next_path))
72
73
cmd("virt-install --connect qemu:///system --cpu host --network network:default,model=virtio --name %s --vcpus=12 --ram 4000 --import --disk %s,device=disk,bus=virtio,format=qcow2,cache=writeback --noautoconsole --graphics vnc,port=12101"
74
% (next, next_path))
75
76
print("Booting...")
77
78
while True:
79
ip = os.popen("kvm_addresses.py %s" % next).read().strip()
80
if not ip:
81
print("waiting for ip address...")
82
time.sleep(2)
83
else:
84
print("The ip address is: '%s'" % ip)
85
break
86
87
print("""
88
You probably want to do something like this:
89
90
sshvm %s
91
sudo su
92
./update_salvus
93
apt-get update; apt-get upgrade
94
95
reboot -h now
96
sshvm %s
97
sudo shutdown -h now
98
99
Then
100
101
virsh undefine %s
102
cd vm/images/base/
103
./push
104
105
""" % (next, next, next))
106
107