Check-in [d9a66bb196]
Not logged in
Overview
Comment:Implented snapshots
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: d9a66bb196016cdcb4cc48100259efcaf9eea4de
User & Date: vitus on 2015-12-17 12:47:38
Other Links: manifest | tags
Context
2015-12-17
14:27
Added shutdown of socket before exit check-in: a816083630 user: vitus tags: trunk
12:47
Implented snapshots check-in: d9a66bb196 user: vitus tags: trunk
2015-12-12
07:26
Hopefilly fixed problem with hangs of monitor operation. Implemented screenshot, record and stoprecord commands. Incorporated start.template inside script, improved error diagnostic on create check-in: a090f477b4 user: vitus tags: trunk
Changes

Modified vws from [0d9abda1a1] to [6f0576d6d4].

76
77
78
79
80
81
82




83
84
85
86
87
88






89
90
91
92
93
94
95
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







+
+
+
+






+
+
+
+
+
+









def cmd_start(options):
    if options.stopped:
        arg=""
        if options.cdrom:
            arg=" -cdrom "+options.cdrom[0]
        if options.snapshot:
            arg=arg+" -snapshot"
        if options.args:
            arg=arg+" "+options.args
        cwd=os.getcwd()
        os.chdir(options.dir)
        os.system("./start%s" % arg)
        os.chdir(cwd)
        time.sleep(2)
        options.sock = connect_vm(options.dir)
    else:
        if options.snapshot or options.args:
            print >>sys.stderr, "Cannot change qemu options. VM is already running"
        if options.cdrom:
            options.file = options.cdrom[0]
            cmd_cdrom(options)    
    if options.gui:
        uri = spiceurl(options)
        os.system("remote-viewer %s &" % uri)
    elif not options.stopped:
        print >>sys.stderr,"VM already running"
def cmd_stop(options):
    if options.hard:
232
233
234
235
236
237
238





































































































239
240
241
242
243
244
245
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    else:
        print send_command(options.sock,"stopcapture "+m.group(1))
def cmd_version(options):
    print VERSION

def validate_size(size):
    return re.match('\d+[KMG]',size) is not None

def get_drives(vm_dir):
    """ Return list of drive files in the VW directory """
    result=[]
    with open(vm_dir+"/start") as f:
        for line in f:
            if (re.match("\s*-drive .*",line) and
                line.find("media=disk")>-1):
                m=re.search("file=([^,\s]*)",line)
                if m:
                    result.append(m.group(1))
    return result

def cmd_snapshot(options):
    if not options.stopped:
        print >>sys.stderr,"Cannot make snapshot of running VW"
        return 1
    drives=get_drives(options.dir)
    os.chdir(options.dir)
    newnames={}
    for i in drives:
        name,ext=os.path.splitext(i)
        newnames[i]=name+"."+options.snapname+"."+ext
        if os.path.exists(newnames[i]):
            print >>sys.stderr,"Snapshot %s already exists",options.snapname
            return 1
    for i in drives:
        os.rename(i,newnames[i])
        os.system("qemu-img create -f qcow2 -b \"%s\" \"%s\"" %(i,newnames[i]))
    return 0

def cmd_snapshots(options): 
    os.chdir(options.dir)
    drives = get_drives(options.dir)
    lst=[]
    info={}
    with os.popen("qemu-img info --backing-chain "+drives[0],"r") as f:
        for line in f:
           if line.find(": ")!= -1 :
               var,val=line.strip().split(": ")
               if val != "":
                    info[var]=val
           elif line[0]=='\n':
               lst.append(info)
               info = {}
        lst.append(info)
    for d in lst:
        print "%-30s  %+8s  %+8s"%(d["image"],d["virtual size"],d["disk size"])


def get_backing(drive):
    with os.popen('qemu-img info "%s"' % drive,"r") as f:
        for line in f:
            m=re.match("backing.file: (.*)$",line)
            if (m) :
                return m.group(1)
    return None                

def cmd_revert(options):
    # Removes latest snapshot images and creates new ones instead
    if not options.stopped:
        print >>sys.stderr,"Cannot revert running VW to snapshot"
        return 1
    os.chdir(options.dir)
    for drive in get_drives(options.dir):
        # Check if first  has backing file
        backing=get_backing(drive)
        if not backing:
            print >>sys.stderr,"Drive %s has no snapshots"%drive
            continue
        # Unlink current image
        os.unlink(drive)
        # create new image with same backing file
        os.system('qemu-img create -f qcow2 -b "%s" "%s"'%(backing,drive))

def cmd_commit(options):   
    #
    # Commits last snapshot changes into it's backing file
    #
    if options.stopped:
       #
       # Stoppend vm - last snapshot is commited into its backing file.
       # Backing file is made current drive image
       #
       os.chdir(options.dir)
       for drive in get_drives(options.dir):
            backing=get_backing(drive)
            if backing is None:
                continue
            os.system('qemu-img commit "%s"'%drive)
            os.unlink(drive)
            os.rename(backing,drive)
    else:
       #
       #
       # Check if we are running in the snapshot mode
       # 
       send_command(options.sock,"commit")

    


template="""#!/bin/sh
# Get machine name from current directory name
NAME=$(basename $(pwd))
# if remote access is enabled, then there should be
# SPICE_PASSWORD=password
QEMU_AUDIO_DRV=spice
403
404
405
406
407
408
409




410
411
412
413
414
415
416
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531







+
+
+
+







# Power management
p=new_command(cmds,'start',help='Start VW and connect to console',
    description="Start VW if not running and connect to the console")
p.add_argument('--no-gui',dest='gui',action='store_const',const=False,
               default=True,help='do not open console window')
p.add_argument('--cdrom',metavar='filename.iso',dest='cdrom',nargs=1,
               help='connect specified iso image to VMs cdrom on start')
p.add_argument('--args',metavar='string',dest='args',nargs=1,default="",
                help="Specify extra QEMU options")
p.add_argument("--snapshot",action='store_const',const=True,default=False,
                help="Run without modifying disk image")
# Following commands don't need extra args
p=new_command(cmds,'stop',help='Shut down virtual machine',
         description="Terminate the VW, gracefully or ungracefully")
p.add_argument('--hard',help='Power off immediately', action='store_const',dest='hard',const=True,default=False)

new_command(cmds,'save',help='Save VW state and stop emulation',
    description="Save VW state and stop emulation")