debuggers.hg

view tools/python/xen/xend/XendLocalStorageRepo.py @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children
line source
1 #!/usr/bin/python
2 #============================================================================
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of version 2.1 of the GNU Lesser General Public
5 # License as published by the Free Software Foundation.
6 #
7 # This library is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 # Lesser General Public License for more details.
11 #
12 # You should have received a copy of the GNU Lesser General Public
13 # License along with this library; if not, write to the Free Software
14 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 #============================================================================
16 # Copyright (C) 2007 XenSource Ltd.
17 #============================================================================
18 #
19 # A pseudo-StorageRepository to provide a representation for the images
20 # that can be specified by xm.
21 #
23 import commands
24 import logging
25 import os
26 import stat
27 import threading
28 import re
29 import sys
30 import struct
32 from xen.util import mkdir
33 import uuid
34 from XendError import XendError
35 from XendVDI import *
36 from XendTask import XendTask
37 from XendStorageRepository import XendStorageRepository
38 from XendStateStore import XendStateStore
39 from XendOptions import instance as xendoptions
41 MB = 1024 * 1024
43 log = logging.getLogger("xend.XendLocalStorageRepo")
45 class XendLocalStorageRepo(XendStorageRepository):
46 """A backwards compatibility storage repository so that
47 traditional file:/dir/file.img and phy:/dev/hdxx images can
48 still be represented in terms of the Xen API.
49 """
51 def __init__(self, sr_uuid, sr_type = 'local',
52 name_label = 'Local',
53 name_description = 'Traditional Local Storage Repo'):
54 """
55 @ivar images: mapping of all the images.
56 @type images: dictionary by image uuid.
57 @ivar lock: lock to provide thread safety.
58 """
60 XendStorageRepository.__init__(self, sr_uuid, sr_type,
61 name_label, name_description)
63 self.state = XendStateStore(xendoptions().get_xend_state_path()
64 + '/local_sr')
66 stored_images = self.state.load_state('vdi')
67 if stored_images:
68 for image_uuid, image in stored_images.items():
69 self.images[image_uuid] = XendLocalVDI(image)
71 def create_vdi(self, vdi_struct):
72 """ Creates a fake VDI image for a traditional image string.
74 The image uri is stored in the attribute 'uri'
75 """
76 vdi_struct['uuid'] = uuid.createString()
77 vdi_struct['SR'] = self.uuid
78 new_image = XendLocalVDI(vdi_struct)
79 self.images[new_image.uuid] = new_image
80 self.save_state()
81 return new_image.uuid
83 def save_state(self):
84 vdi_records = dict([(k, v.get_record(transient = False))
85 for k, v in self.images.items()])
86 self.state.save_state('vdi', vdi_records)
88 def destroy_vdi(self, vdi_uuid):
89 if vdi_uuid in self.images:
90 del self.images[vdi_uuid]
92 self.save_state()