Skip to content

Commit b5b7fcc

Browse files
the scsh verion
1 parent 5fbcec4 commit b5b7fcc

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed

bin/createvm.scm

+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
(use-modules
2+
(scsh scsh)
3+
(scsh syntax)
4+
(scsh run-extras)
5+
(srfi srfi-8)
6+
(rnrs lists)
7+
(ice-9 match)
8+
(ice-9 pretty-print)
9+
(srfi srfi-11))
10+
11+
12+
(define guile-magic "#!/usr/bin/guile
13+
!#
14+
")
15+
(define (modulesuse)
16+
"The trunk used to load necessary module in startvm.scm"
17+
'(use-modules (scsh scsh)
18+
(scsh syntax)
19+
(scsh run-extras)
20+
(srfi srfi-8)
21+
(srfi srfi-11)
22+
(ice-9 match)
23+
(rnrs lists)
24+
(ice-9 pretty-print))
25+
)
26+
(define (xkvmf)
27+
"The trunk used to define procedure of kvm in generated startvm.scm"
28+
'(define (kvm xvmdef)
29+
(fold-right
30+
(lambda (x y)
31+
(let ((rdopt (lambda (v)
32+
(string-join
33+
(map (lambda (x)
34+
(match x
35+
((i)
36+
(format #f "~a" i))
37+
((i j)
38+
(format #f "~a=~a" i j))))
39+
v)
40+
","))))
41+
(match x
42+
((p)
43+
(cons (format #f "-~a" p) y))
44+
((p v)
45+
(cons (format #f "-~a" p)
46+
(match v
47+
((? list? xx)
48+
(cons (rdopt xx) y))
49+
(_
50+
(cons (format #f "~a" v) y))))))))
51+
'()
52+
xvmdef
53+
)))
54+
55+
(define (xvmdef hda-name macaddr memsize vmisopath)
56+
"the trunk used to define vm parameters in generated startvm.scm"
57+
`(define vmdef
58+
'((M pc)
59+
; (S)
60+
(cpu kvm64)
61+
(daemonize)
62+
(pidfile "kvm.pid")
63+
(drive ((file ,hda-name)
64+
(index 0)
65+
(if ide)
66+
(media disk)))
67+
(net ((tap)
68+
(ifname tap0)
69+
(script /bin/true)))
70+
(net ((nic)
71+
(macaddr ,macaddr)))
72+
(m ,memsize)
73+
(vnc "unix:vncsock,server")
74+
(chardev (
75+
(socket)
76+
(id mnt1)
77+
(nowait)
78+
(path mntrsock)
79+
(server)))
80+
(mon ((chardev mnt1)
81+
(mode readline)))
82+
(usb)
83+
(usbdevice tablet)
84+
(drive ((file ,vmisopath)
85+
(index 1)
86+
(if ide)
87+
(media cdrom)))
88+
))
89+
)
90+
91+
92+
(define (optbuilder* ol)
93+
"usage: (optbuilder* '((info) (text \"hello world\")))"
94+
(let ((o->s
95+
(lambda (x)
96+
(let* ((s (stringify (car x)))
97+
(l? (> (string-length s) 1))
98+
(p (if l?
99+
"--"
100+
"-"))
101+
(m (if l?
102+
"="
103+
""))
104+
(ls (if (null? (cdr x))
105+
(list p s)
106+
(list p s m (stringify (cadr x))))))
107+
(string-join ls "")))))
108+
(map o->s ol)))
109+
110+
111+
(define (zenity zdef)
112+
`(zenity ,@(optbuilder* zdef)))
113+
114+
(setenv "WINDOWID" "0")
115+
116+
(run (,@
117+
(zenity
118+
`((info)
119+
(text
120+
"Welcome Using Chaos Eternal's KVM creation utilities")
121+
(title Welcome)))))
122+
123+
(random 255 (random-state-from-platform ))
124+
125+
(let ((repo (receive
126+
(status p1 p2)
127+
(run/collecting
128+
(1 2)
129+
(,@(zenity
130+
'((file-selection)
131+
(title "Choose Repository")
132+
(directory)
133+
(text
134+
"Choose a directory to hold the repository")))))
135+
(if (> status 0)
136+
(throw 'user-cancel)
137+
(car (port->string-list p1)))))
138+
(macaddr (string-join
139+
(cons "00"
140+
(map
141+
(lambda (x)
142+
(format #f "~X" (random 255 )))
143+
(iota 5)))
144+
":")))
145+
(&& (test "!" -d ,repo)
146+
(begin (run (mkdir "-p" repo))
147+
(run (mkdir ,(string-join (list repo "vde1") "/")))))
148+
(with-cwd
149+
repo
150+
(let-values
151+
(
152+
((vmname memsize disksize disktype)
153+
[receive (status p1)
154+
(run/collecting
155+
(1 2)
156+
(,@(zenity
157+
'((forms)
158+
(title "Create Virtual Machine")
159+
(text "Input the following Definitions")
160+
(add-entry "VM Name")
161+
(add-entry "Memory Size")
162+
(add-entry "Disk Size")
163+
(add-list "Disk Type")
164+
(list-values "qcow2|raw")
165+
(column-values "QCow2")
166+
(separator "\n")))))
167+
(if (> status 0)
168+
(throw 'user-cancel)
169+
(apply values (port->string-list p1)))])
170+
((vmisopath)
171+
[receive (status p1)
172+
(run/collecting
173+
(1 2)
174+
(,@(zenity
175+
'((file-selection)
176+
(title "Choose a boot iso image, \
177+
Cancel to create vm with out a boot iso")
178+
))))
179+
(if (> status 0)
180+
"/dev/null"
181+
(values (car (port->string-list p1))))])
182+
)
183+
(let* ((vmpath (string-append repo "/" vmname))
184+
(script-path (string-append vmpath "/startvm.scm"))
185+
(hda-name "hda.img")
186+
(hda-path (string-append vmpath "/" hda-name))
187+
;(script-port (open-file script-path "w"))
188+
)
189+
(if (> (run (mkdir ,vmpath)) 0)
190+
(throw `repo-exists))
191+
(run (qemu-img create -f ,disktype ,hda-path ,disksize))
192+
(with-output-to-file
193+
script-path
194+
(lambda ()
195+
(display guile-magic)
196+
(pretty-print (modulesuse))
197+
(pretty-print (xkvmf))
198+
(pretty-print (xvmdef hda-name macaddr memsize vmisopath))
199+
(pretty-print '(setenv "LD_PRELOAD"
200+
"/usr/lib/vde2/libvdetap.so"))
201+
(pretty-print '(setenv "tap0"
202+
"../vde1"))
203+
(pretty-print '(with-cwd
204+
(dirname
205+
(car (command-line)))
206+
(let ()
207+
(run (kvm ,@(kvm vmdef)))
208+
(run (ssvncviewer ./vncsock))
209+
)
210+
)
211+
)))
212+
;(list repo vmname memsize disksize disktype vmisopath)
213+
))))

0 commit comments

Comments
 (0)