public lockableVMSpec createChildVM(SQLiteConnection conn, hostDB db, VMHardwareSpec reqhw, VMSoftwareSpec reqsw, string newOwner) { if ((permittedAccessRead & bladeLockType.lockVMCreation) == bladeLockType.lockNone) { throw new Exception("lockVMCreation is needed when calling .createChildVM"); } vmserverTotals totals = db.getVMServerTotals(this); int indexOnServer = totals.VMs + 1; string newBladeName = xdlClusterNaming.makeVMName(bladeIP, indexOnServer); // If we set the debugger port automatically, make sure we reset it to zero before we return. bool needToResetReqSWDebugPort = false; if (reqsw.debuggerPort == 0) { reqsw.debuggerPort = xdlClusterNaming.makeVMKernelDebugPort(bladeIP, indexOnServer); needToResetReqSWDebugPort = true; } vmSpec newVM = new vmSpec(conn, newBladeName, reqsw, bladeLockType.lockAll, bladeLockType.lockAll); newVM.parentBladeIP = bladeIP; newVM.state = bladeStatus.inUseByDirector; newVM.currentOwner = "vmserver"; // We own the blade until we are done setting it up newVM.nextOwner = newOwner; newVM.parentBladeID = bladeID.Value; newVM.memoryMB = reqhw.memoryMB; newVM.cpuCount = reqhw.cpuCount; newVM.indexOnServer = indexOnServer; newVM.VMIP = xdlClusterNaming.makeVMIP(bladeIP, newVM); newVM.iscsiIP = xdlClusterNaming.makeiSCSIIP(bladeIP, newVM); newVM.eth0MAC = xdlClusterNaming.makeEth0MAC(bladeIP, newVM); newVM.eth1MAC = xdlClusterNaming.makeEth1MAC(bladeIP, newVM); // VMs always have this implicit snapshot. newVM.currentSnapshot = "vm"; if (needToResetReqSWDebugPort) { reqsw.debuggerPort = 0; } lockableVMSpec toRet = new lockableVMSpec(newVM.VMIP, bladeLockType.lockAll, bladeLockType.lockAll); toRet.setSpec(newVM); return(toRet); }
public lockableVMSpec getVMByIP(string bladeName, bladeLockType readLock, bladeLockType writeLock) { // We need to lock IP addressess, since we're searching by them. readLock = readLock | bladeLockType.lockIPAddresses; lockableVMSpec toRet = new lockableVMSpec(bladeName, readLock, writeLock); try { string sqlCommand = "select * from bladeOwnership " + "join VMConfiguration on ownershipKey = VMConfiguration.ownershipID " + "where VMConfiguration.VMIP = $VMIP"; using (SQLiteCommand cmd = new SQLiteCommand(sqlCommand, conn)) { cmd.Parameters.AddWithValue("$VMIP", bladeName); using (SQLiteDataReader reader = cmd.ExecuteReader()) { if (reader.Read()) { toRet.setSpec(new vmSpec(conn, reader, readLock, writeLock)); return(toRet); } // No records returned. toRet.Dispose(); return(null); } } } catch (Exception) { toRet.Dispose(); throw; } }
public disposingList <lockableVMSpec> getVMByVMServerIP(lockableBladeSpec blade, bladeLockType readLock, bladeLockType writeLock) { disposingList <lockableVMSpec> toRet = new disposingList <lockableVMSpec>(); if ((blade.getCurrentLocks().read & bladeLockType.lockVMCreation) == 0) { throw new Exception("lockVMCreation required on vmserver passed to getVMByVMServerIP"); } // We need to lock IP addressess on the VMs, since we lock by them. readLock = readLock | bladeLockType.lockIPAddresses; // Since we hold lockVMCreation, we can assume no new VMs will be added or removed to/from this blade. We assume that // VM IP addresses will never change, except during initialization, when they go from null - we just drop any with a // NULL IP address. Dictionary <string, lockableVMSpec> VMNames = new Dictionary <string, lockableVMSpec>(); string sqlCommand = "select VMIP from vmConfiguration " + "join bladeConfiguration on parentbladeID = bladeConfigKey " + "where bladeIP = $vmServerIP"; using (SQLiteCommand cmd = new SQLiteCommand(sqlCommand, conn)) { cmd.Parameters.AddWithValue("$vmServerIP", blade.spec.bladeIP); using (SQLiteDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { string VMName = reader[0].ToString(); if (!String.IsNullOrEmpty(VMName)) { VMNames.Add(VMName, new lockableVMSpec(VMName, readLock, writeLock)); } } } } try { // Now read each from the DB, now that we hold the lock for each. foreach (KeyValuePair <string, lockableVMSpec> kvp in VMNames) { string vmName = kvp.Key; lockableVMSpec vmSpec = kvp.Value; string sql_getVM = "select bladeOwnership.*, vmConfiguration.* from vmConfiguration " + " join bladeOwnership on bladeOwnership.ownershipKey = vmConfiguration.ownershipID " + " join bladeConfiguration on parentbladeID = bladeConfigKey " + " where VMIP = $vmIP"; using (SQLiteCommand cmd = new SQLiteCommand(sql_getVM, conn)) { cmd.Parameters.AddWithValue("$vmIP", vmName); using (SQLiteDataReader reader = cmd.ExecuteReader()) { if (!reader.Read()) { throw new Exception("VM disappeared, even though we hold lockVMCreation on the parent!"); } vmSpec.setSpec(new vmSpec(conn, reader, readLock, writeLock)); toRet.Add(vmSpec); } } } } catch (Exception) { foreach (KeyValuePair <string, lockableVMSpec> kvp in VMNames) { kvp.Value.Dispose(); } throw; } return(toRet); }