121121import com .cloud .agent .api .StartConsoleProxyCommand ;
122122import com .cloud .agent .api .StartRouterAnswer ;
123123import com .cloud .agent .api .StartRouterCommand ;
124+ import com .cloud .agent .api .StartSecStorageVmAnswer ;
125+ import com .cloud .agent .api .StartSecStorageVmCommand ;
124126import com .cloud .agent .api .StartupCommand ;
125127import com .cloud .agent .api .StartupRoutingCommand ;
126128import com .cloud .agent .api .StopAnswer ;
184186import com .cloud .vm .ConsoleProxyVO ;
185187import com .cloud .vm .DiskProfile ;
186188import com .cloud .vm .DomainRouter ;
189+ import com .cloud .vm .SecondaryStorageVmVO ;
187190import com .cloud .vm .State ;
188191import com .cloud .vm .VirtualMachineName ;
189192
@@ -415,6 +418,7 @@ protected String getDefaultScriptsDir() {
415418 protected String _hypervisorType ;
416419 protected String _hypervisorURI ;
417420 protected String _hypervisorPath ;
421+ protected String _sysvmISOPath ;
418422 protected String _privNwName ;
419423 protected String _privBridgeName ;
420424 protected String _linkLocalBridgeName ;
@@ -424,6 +428,7 @@ protected String getDefaultScriptsDir() {
424428 protected String _domrKernel ;
425429 protected String _domrRamdisk ;
426430 protected String _pool ;
431+ protected String _localGateway ;
427432 private boolean _can_bridge_firewall ;
428433 private Pair <String , String > _pifs ;
429434 private final Map <String , vmStats > _vmStats = new ConcurrentHashMap <String , vmStats >();
@@ -683,18 +688,7 @@ public boolean configure(String name, Map<String, Object> params)
683688 _domrArch = "i686" ;
684689 } else if (!"i686" .equalsIgnoreCase (_domrArch ) && !"x86_64" .equalsIgnoreCase (_domrArch )) {
685690 throw new ConfigurationException ("Invalid architecture (domr.arch) -- needs to be i686 or x86_64" );
686- }
687-
688- _domrKernel = (String )params .get ("domr.kernel" );
689- if (_domrKernel == null ) {
690- _domrKernel = new File ("/var/lib/libvirt/images/vmops-domr-kernel" ).getAbsolutePath ();
691- }
692-
693- _domrRamdisk = (String )params .get ("domr.ramdisk" );
694- if (_domrRamdisk == null ) {
695- _domrRamdisk = new File ("/var/lib/libvirt/images/vmops-domr-initramfs" ).getAbsolutePath ();
696- }
697-
691+ }
698692
699693 value = (String )params .get ("host.reserved.mem.mb" );
700694 _dom0MinMem = NumbersUtil .parseInt (value , 0 )*1024 *1024 ;
@@ -731,6 +725,20 @@ public boolean configure(String name, Map<String, Object> params)
731725 } catch (ClassNotFoundException e ) {
732726 throw new ConfigurationException ("Unable to find class " + "com.cloud.storage.JavaStorageLayer" );
733727 }
728+
729+ _sysvmISOPath = (String )params .get ("systemvm.iso.path" );
730+ if (_sysvmISOPath == null ) {
731+ String [] isoPaths = {"/usr/lib64/cloud/agent/vms/systemvm.iso" , "/usr/lib/cloud/agent/vms/systemvm.iso" };
732+ for (String isoPath : isoPaths ) {
733+ if (_storage .exists (isoPath )) {
734+ _sysvmISOPath = isoPath ;
735+ break ;
736+ }
737+ }
738+ if (_sysvmISOPath == null ) {
739+ throw new ConfigurationException ("Can't find system vm ISO" );
740+ }
741+ }
734742
735743 //_can_bridge_firewall = can_bridge_firewall();
736744
@@ -779,6 +787,12 @@ public boolean configure(String name, Map<String, Object> params)
779787 throw new ConfigurationException ("Failed to get public nic name" );
780788 }
781789 s_logger .debug ("Found pif: " + _pifs .first () + " on " + _privBridgeName + ", pif: " + _pifs .second () + " on " + _publicBridgeName );
790+
791+ _localGateway = Script .runSimpleBashScript ("ip route |grep default|awk '{print $3}'" );
792+ if (_localGateway == null ) {
793+ s_logger .debug ("Failed to found the local gateway" );
794+ }
795+
782796 return true ;
783797 }
784798
@@ -836,7 +850,7 @@ protected synchronized String startDomainRouter(StartRouterCommand cmd) {
836850
837851 String dataDiskPath = null ;
838852 for (diskDef disk : disks ) {
839- if (disk .getDiskLabel ().equalsIgnoreCase ("hdb " )) {
853+ if (disk .getDiskLabel ().equalsIgnoreCase ("vdb " )) {
840854 dataDiskPath = disk .getDiskPath ();
841855 }
842856 }
@@ -845,7 +859,7 @@ protected synchronized String startDomainRouter(StartRouterCommand cmd) {
845859 patchSystemVm (cmd .getBootArgs (), dataDiskPath , vmName );
846860
847861 String uuid = UUID .nameUUIDFromBytes (vmName .getBytes ()).toString ();
848- String domXML = defineVMXML (cmd .getVmName (), uuid , router .getRamSize (), 1 , _domrArch , nics , disks , router .getVncPassword (), "Fedora 12" );
862+ String domXML = defineVMXML (cmd .getVmName (), uuid , router .getRamSize (), 1 , _domrArch , nics , disks , router .getVncPassword (), cmd . getGuestOSDescription () );
849863
850864 s_logger .debug (domXML );
851865
@@ -879,19 +893,24 @@ protected synchronized String startConsoleProxy(StartConsoleProxyCommand cmd) {
879893 ConsoleProxyVO console = cmd .getProxy ();
880894 List <interfaceDef > nics = null ;
881895 try {
882- nics = createConsoleVMNetworks ( cmd );
896+ nics = createSysVMNetworks ( console . getGuestMacAddress (), console . getPrivateMacAddress (), console . getPublicMacAddress (), console . getVlanId () );
883897
884898 List <diskDef > disks = createSystemVMDisk (cmd .getVolumes ());
885899
886900 String dataDiskPath = null ;
887901 for (diskDef disk : disks ) {
888- if (disk .getDiskLabel ().equalsIgnoreCase ("hdb " )) {
902+ if (disk .getDiskLabel ().equalsIgnoreCase ("vdb " )) {
889903 dataDiskPath = disk .getDiskPath ();
890904 }
891905 }
892906
907+ String bootArgs = cmd .getBootArgs () + " zone=" + _dcId ;
908+ bootArgs += " pod=" + _pod ;
909+ bootArgs += " guid=Proxy." + console .getId ();
910+ bootArgs += " proxy_vm=" + console .getId ();
911+ bootArgs += " localgw=" + _localGateway ;
893912 String vmName = cmd .getVmName ();
894- patchSystemVm (cmd . getBootArgs () , dataDiskPath , vmName );
913+ patchSystemVm (bootArgs , dataDiskPath , vmName );
895914
896915 String uuid = UUID .nameUUIDFromBytes (vmName .getBytes ()).toString ();
897916 String domXML = defineVMXML (cmd .getVmName (), uuid , console .getRamSize (), 1 , _domrArch , nics , disks , console .getVncPassword (), "Fedora 12" );
@@ -909,12 +928,50 @@ protected synchronized String startConsoleProxy(StartConsoleProxyCommand cmd) {
909928 return null ;
910929 }
911930
931+ protected String startSecStorageVM (StartSecStorageVmCommand cmd ) {
932+ SecondaryStorageVmVO secVm = cmd .getSecondaryStorageVmVO ();
933+ List <interfaceDef > nics = null ;
934+ try {
935+ nics = createSysVMNetworks (secVm .getGuestMacAddress (), secVm .getPrivateMacAddress (), secVm .getPublicMacAddress (), secVm .getVlanId ());
936+
937+ List <diskDef > disks = createSystemVMDisk (cmd .getVolumes ());
938+
939+ String dataDiskPath = null ;
940+ for (diskDef disk : disks ) {
941+ if (disk .getDiskLabel ().equalsIgnoreCase ("vdb" )) {
942+ dataDiskPath = disk .getDiskPath ();
943+ }
944+ }
945+
946+ String vmName = cmd .getVmName ();
947+ String bootArgs = cmd .getBootArgs ();
948+ bootArgs += " zone=" + _dcId ;
949+ bootArgs += " pod=" + _pod ;
950+ bootArgs += " localgw=" + _localGateway ;
951+ patchSystemVm (bootArgs , dataDiskPath , vmName );
952+
953+ String uuid = UUID .nameUUIDFromBytes (vmName .getBytes ()).toString ();
954+ String domXML = defineVMXML (cmd .getVmName (), uuid , secVm .getRamSize (), 1 , _domrArch , nics , disks , secVm .getVncPassword (), cmd .getGuestOSDescription ());
955+
956+ s_logger .debug (domXML );
957+
958+ startDomain (vmName , domXML );
959+ } catch (LibvirtException e ) {
960+ s_logger .debug ("Failed to start domr: " + e .toString ());
961+ return e .toString ();
962+ }catch (InternalErrorException e ) {
963+ s_logger .debug ("Failed to start domr: " + e .toString ());
964+ return e .toString ();
965+ }
966+ return null ;
967+ }
968+
912969 private String defineVMXML (String vmName , String uuid , int memSize , int cpus , String arch , List <interfaceDef > nics , List <diskDef > disks , String vncPaswd , String guestOSType ) {
913970 LibvirtVMDef vm = new LibvirtVMDef ();
914971 vm .setHvsType (_hypervisorType );
915972 vm .setDomainName (vmName );
916973 vm .setDomUUID (uuid );
917- vm .setDomDescription (guestOSType );
974+ vm .setDomDescription (KVMGuestOsMapper . getGuestOsName ( guestOSType ) );
918975
919976 guestDef guest = new guestDef ();
920977 guest .setGuestType (guestDef .guestType .KVM );
@@ -1100,6 +1157,8 @@ public Answer executeRequest(Command cmd) {
11001157 return execute ((StartRouterCommand )cmd );
11011158 } else if (cmd instanceof StartConsoleProxyCommand ) {
11021159 return execute ((StartConsoleProxyCommand )cmd );
1160+ } else if (cmd instanceof StartSecStorageVmCommand ) {
1161+ return execute ((StartSecStorageVmCommand )cmd );
11031162 } else if (cmd instanceof AttachIsoCommand ) {
11041163 return execute ((AttachIsoCommand ) cmd );
11051164 } else if (cmd instanceof AttachVolumeCommand ) {
@@ -1854,18 +1913,52 @@ private Answer execute(StartConsoleProxyCommand cmd) {
18541913 _vms .put (cmd .getVmName (), State .Starting );
18551914 }
18561915 try {
1916+
18571917 result = startConsoleProxy (cmd );
18581918 if (result != null ) {
18591919 throw new ExecutionException (result , null );
18601920 }
18611921
1862- result = _virtRouterResource .connect (router .getPrivateIpAddress (), cmd .getProxyCmdPort ());
1922+ result = _virtRouterResource .connect (router .getGuestIpAddress (), cmd .getProxyCmdPort ());
1923+ if (result != null ) {
1924+ throw new ExecutionException (result , null );
1925+ }
1926+
1927+ state = State .Running ;
1928+ return new StartConsoleProxyAnswer (cmd );
1929+ } catch (final ExecutionException e ) {
1930+ return new Answer (cmd , false , e .getMessage ());
1931+ } catch (final Throwable th ) {
1932+ s_logger .warn ("Exception while starting router." , th );
1933+ return createErrorAnswer (cmd , "Unable to start router" , th );
1934+ } finally {
1935+ synchronized (_vms ) {
1936+ _vms .put (cmd .getVmName (), state );
1937+ }
1938+ }
1939+ }
1940+
1941+ private Answer execute (StartSecStorageVmCommand cmd ) {
1942+ final SecondaryStorageVmVO secVm = cmd .getSecondaryStorageVmVO ();
1943+ String result = null ;
1944+
1945+ State state = State .Stopped ;
1946+ synchronized (_vms ) {
1947+ _vms .put (cmd .getVmName (), State .Starting );
1948+ }
1949+ try {
1950+ result = startSecStorageVM (cmd );
1951+ if (result != null ) {
1952+ throw new ExecutionException (result , null );
1953+ }
1954+
1955+ result = _virtRouterResource .connect (secVm .getGuestIpAddress (), cmd .getProxyCmdPort ());
18631956 if (result != null ) {
18641957 throw new ExecutionException (result , null );
18651958 }
18661959
18671960 state = State .Running ;
1868- return new StartConsoleProxyAnswer (cmd , router . getPrivateIpAddress (), router . getPrivateMacAddress () );
1961+ return new StartSecStorageVmAnswer (cmd );
18691962 } catch (final ExecutionException e ) {
18701963 return new Answer (cmd , false , e .getMessage ());
18711964 } catch (final Throwable th ) {
@@ -3247,25 +3340,34 @@ private List<interfaceDef> createRouterVMNetworks(StartRouterCommand cmd) throws
32473340 return nics ;
32483341 }
32493342
3250- private List <interfaceDef > createConsoleVMNetworks ( StartConsoleProxyCommand cmd ) {
3343+ private List <interfaceDef > createSysVMNetworks ( String guestMac , String privMac , String pubMac , String vlanId ) throws InternalErrorException {
32513344 List <interfaceDef > nics = new ArrayList <interfaceDef >();
3252- ConsoleProxyVO console = cmd .getProxy ();
3253- String privateMac = console .getPrivateMacAddress ();
3254- String pubMac = console .getPublicMacAddress ();
3345+ String brName ;
32553346 interfaceDef pubNic = new interfaceDef ();
32563347 interfaceDef privNic = new interfaceDef ();
32573348 interfaceDef vnetNic = new interfaceDef ();
3258-
3259- /*guest network is vnet: 0 is not used, 1 is link local, 2 is pub nic*/
3260- vnetNic .defPrivateNet ("default" , null , null , interfaceDef .nicModel .VIRTIO );
3261- nics .add (vnetNic );
3262-
3263- privNic .defPrivateNet (_privNwName , null , privateMac , interfaceDef .nicModel .VIRTIO );
3264- nics .add (privNic );
3265-
3266- pubNic .defBridgeNet (_publicBridgeName , null , pubMac , interfaceDef .nicModel .VIRTIO );
3267- nics .add (pubNic );
3268-
3349+
3350+ /*nic 0: link local*/
3351+ privNic .defPrivateNet (_privNwName , null , guestMac , interfaceDef .nicModel .VIRTIO );
3352+ nics .add (privNic );
3353+
3354+ /*nic 1, priv network*/
3355+
3356+ vnetNic .defBridgeNet (_privBridgeName , null , privMac , interfaceDef .nicModel .VIRTIO );
3357+ nics .add (vnetNic );
3358+
3359+ /*nic 2: public */
3360+ if ("untagged" .equalsIgnoreCase (vlanId )) {
3361+ pubNic .defBridgeNet (_publicBridgeName , null , pubMac , interfaceDef .nicModel .VIRTIO );
3362+ } else {
3363+ String vnetId = getVnetId (vlanId );
3364+ brName = setVnetBrName (vnetId );
3365+ String vnetDev = "vtap" + vnetId ;
3366+ createVnet (vnetId , _pifs .second ());
3367+ pubNic .defBridgeNet (brName , null , pubMac , interfaceDef .nicModel .VIRTIO );
3368+ }
3369+ nics .add (pubNic );
3370+
32693371 return nics ;
32703372 }
32713373
@@ -3293,13 +3395,18 @@ private List<diskDef> createSystemVMDisk(List<VolumeVO> vols) throws InternalErr
32933395 String datadiskPath = tmplVol .getKey ();
32943396
32953397 diskDef hda = new diskDef ();
3296- hda .defFileBasedDisk (rootkPath , "hda " , diskDef .diskBus .IDE , diskDef .diskFmtType .QCOW2 );
3398+ hda .defFileBasedDisk (rootkPath , "vda " , diskDef .diskBus .VIRTIO , diskDef .diskFmtType .QCOW2 );
32973399 disks .add (hda );
32983400
32993401 diskDef hdb = new diskDef ();
3300- hdb .defFileBasedDisk (datadiskPath , "hdb " , diskDef .diskBus .IDE , diskDef .diskFmtType .RAW );
3402+ hdb .defFileBasedDisk (datadiskPath , "vdb " , diskDef .diskBus .VIRTIO , diskDef .diskFmtType .RAW );
33013403 disks .add (hdb );
33023404
3405+ diskDef hdc = new diskDef ();
3406+ hdc .defFileBasedDisk (_sysvmISOPath , "hdc" , diskDef .diskBus .IDE , diskDef .diskFmtType .RAW );
3407+ hdc .setDeviceType (diskDef .deviceType .CDROM );
3408+ disks .add (hdc );
3409+
33033410 return disks ;
33043411 }
33053412
0 commit comments