35
35
package edu.ucsb.eucalyptus.storage.fs;
37
import edu.ucsb.eucalyptus.cloud.EucalyptusCloudException;
38
import edu.ucsb.eucalyptus.keys.Hashes;
37
39
import edu.ucsb.eucalyptus.storage.StorageManager;
38
import edu.ucsb.eucalyptus.util.WalrusProperties;
42
import java.util.ArrayList;
43
import java.util.List;
42
45
public class FileSystemStorageManager implements StorageManager {
44
47
public static final String FILE_SEPARATOR = "/";
45
public FileSystemStorageManager() {
48
public static final String lvmRootDirectory = "/dev";
49
private static boolean initialized = false;
51
private String rootDirectory;
52
public FileSystemStorageManager(String rootDirectory) {
53
this.rootDirectory = rootDirectory;
56
public void initialize() {
58
System.loadLibrary("fsstorage");
63
public void setRootDirectory(String rootDirectory) {
64
this.rootDirectory = rootDirectory;
48
68
public void createBucket(String bucket) throws IOException {
49
File bukkit = new File (WalrusProperties.bucketRootDirectory + FILE_SEPARATOR + bucket);
69
File bukkit = new File (rootDirectory + FILE_SEPARATOR + bucket);
50
70
if(!bukkit.exists()) {
51
71
if(!bukkit.mkdirs()) {
52
72
throw new IOException(bucket);
58
public boolean isEmpty(String bucket) {
59
return true; //To change body of implemented methods use File | Settings | File Templates.
77
public long getSize(String bucket, String object) {
78
File objectFile = new File (rootDirectory + FILE_SEPARATOR + bucket + FILE_SEPARATOR + object);
79
if(objectFile.exists())
80
return objectFile.length();
62
84
public void deleteBucket(String bucket) throws IOException {
63
File bukkit = new File (WalrusProperties.bucketRootDirectory + FILE_SEPARATOR + bucket);
85
File bukkit = new File (rootDirectory + FILE_SEPARATOR + bucket);
64
86
if(!bukkit.delete()) {
65
87
throw new IOException(bucket);
69
91
public void createObject(String bucket, String object) throws IOException {
70
File objectFile = new File (WalrusProperties.bucketRootDirectory + FILE_SEPARATOR + bucket + FILE_SEPARATOR + object);
92
File objectFile = new File (rootDirectory + FILE_SEPARATOR + bucket + FILE_SEPARATOR + object);
71
93
if (!objectFile.exists()) {
72
94
if (!objectFile.createNewFile()) {
73
95
throw new IOException(object);
124
146
public void renameObject(String bucket, String oldName, String newName) throws IOException {
125
File oldObjectFile = new File (WalrusProperties.bucketRootDirectory + FILE_SEPARATOR + bucket + FILE_SEPARATOR + oldName);
126
File newObjectFile = new File (WalrusProperties.bucketRootDirectory + FILE_SEPARATOR + bucket + FILE_SEPARATOR + newName);
127
if (!oldObjectFile.renameTo(newObjectFile)) {
128
throw new IOException(oldName);
147
File oldObjectFile = new File (rootDirectory + FILE_SEPARATOR + bucket + FILE_SEPARATOR + oldName);
148
File newObjectFile = new File (rootDirectory + FILE_SEPARATOR + bucket + FILE_SEPARATOR + newName);
149
if(oldObjectFile.exists()) {
150
if (!oldObjectFile.renameTo(newObjectFile)) {
151
throw new IOException(oldName);
132
156
public String getObjectPath(String bucket, String object) {
133
return WalrusProperties.bucketRootDirectory + FILE_SEPARATOR + bucket + FILE_SEPARATOR + object;
157
return rootDirectory + FILE_SEPARATOR + bucket + FILE_SEPARATOR + object;
160
public native String removeLoopback(String loDevName);
162
public native String createLoopback(String fileName);
164
public native String removeLogicalVolume(String lvName);
166
public native String reduceVolumeGroup(String vgName, String pvName);
168
public native String removePhysicalVolume(String loDevName);
170
public native String createVolumeFromLv(String lvName, String volumeKey);
172
public native String enableLogicalVolume(String lvName);
174
public native String disableLogicalVolume(String lvName);
176
public void deleteSnapshot(String bucket, String snapshotId, String vgName, String lvName, List<String> snapshotSet) throws EucalyptusCloudException {
177
//load the snapshot set
178
ArrayList<String> loDevices = new ArrayList<String>();
179
String snapshotLoDev = null;
180
String snapshotFileName = null;
181
for(String snapshot : snapshotSet) {
182
String fileName = rootDirectory + FILE_SEPARATOR + bucket + FILE_SEPARATOR + snapshot;
183
String loDevName = createLoopback(fileName);
184
if(loDevName.length() == 0)
185
throw new EucalyptusCloudException("could not create loopback device for " + snapshot);
186
if(snapshot.equals(snapshotId)) {
187
snapshotLoDev = loDevName;
188
snapshotFileName = fileName;
190
loDevices.add(loDevName);
192
//now remove the snapshot
193
String absoluteLVName = lvmRootDirectory + FILE_SEPARATOR + vgName + FILE_SEPARATOR + lvName;
194
String returnValue = removeLogicalVolume(absoluteLVName);
195
if(returnValue.length() == 0) {
196
throw new EucalyptusCloudException("Unable to remove logical volume " + absoluteLVName);
198
returnValue = reduceVolumeGroup(vgName, snapshotLoDev);
199
if(returnValue.length() == 0) {
200
throw new EucalyptusCloudException("Unable to remove volume group " + vgName);
202
returnValue = removePhysicalVolume(snapshotLoDev);
203
if(returnValue.length() == 0) {
204
throw new EucalyptusCloudException("Unable to remove physical volume " + snapshotLoDev);
207
//unload the snapshots
208
for(String loDevice : loDevices) {
209
returnValue = removeLoopback(loDevice);
212
//remove the snapshot backing store
214
deleteObject("", snapshotFileName);
215
} catch(Exception ex) {
216
throw new EucalyptusCloudException("could not delete snapshot file " + snapshotFileName);
220
public String createVolume(String bucket, List<String> snapshotSet, List<String> vgNames, List<String> lvNames, String snapshotId, String snapshotVgName, String snapshotLvName) throws EucalyptusCloudException {
221
String snapshotLoDev = null;
222
ArrayList<String> loDevices = new ArrayList<String>();
223
for(String snapshot : snapshotSet) {
224
String loDevName = createLoopback(rootDirectory + FILE_SEPARATOR + bucket + FILE_SEPARATOR + snapshot);
225
if(loDevName.length() == 0)
226
throw new EucalyptusCloudException("could not create loopback device for " + snapshot);
227
if(snapshot.equals(snapshotId))
228
snapshotLoDev = loDevName;
229
loDevices.add(loDevName);
234
ArrayList<String> absoluteLvNames = new ArrayList<String>();
235
String snapshotAbsoluteLvName = null;
236
for(String snapshot : snapshotSet) {
237
String absoluteLvName = lvmRootDirectory + FILE_SEPARATOR + vgNames.get(i) + FILE_SEPARATOR + lvNames.get(i);
239
enableLogicalVolume(absoluteLvName);
240
if(snapshotId.equals(snapshot))
241
snapshotAbsoluteLvName = absoluteLvName;
242
absoluteLvNames.add(absoluteLvName);
246
String volumeKey = "walrusvol-" + Hashes.getRandom(16);
247
String volumePath = rootDirectory + FILE_SEPARATOR + bucket + FILE_SEPARATOR + volumeKey;
248
createVolumeFromLv(snapshotAbsoluteLvName, volumePath);
249
if(!(new File(volumePath).exists()))
250
throw new EucalyptusCloudException("Unable to create file " + volumePath);
252
for(String absoluteLvName : absoluteLvNames) {
253
String returnValue = disableLogicalVolume(absoluteLvName);
256
//unload the snapshots
257
for(String loDevice : loDevices) {
258
String returnValue = removeLoopback(loDevice);
b'\\ No newline at end of file'