Commits

Kaspar Schiess committed ac2fbda

Automatic mac address beautification

Comments (0)

Files changed (3)

lib/qemu-toolkit/vm.rb

         if model == 'virtio'
           cmd << '-device virtio-net-pci,'+
             parameter_list(
-              mac: macaddr, 
+              mac: vnic.macaddr, 
               tx: 'timer', x_txtimer: 200000, x_txburst: 128, 
               vlan: vlan) 
         else
             parameter_list(
               vlan: vlan, name: nic_name, 
               model: model,
-              macaddr: macaddr)
+              macaddr: vnic.macaddr)
         end
 
         vlan += 1
       
       # networking: net
       nets.each do |type, parameters|
+        map(parameters, :macaddr) { |a| Network::MacAddress.new(a) }
+        
         cmd << "-net #{type},"+
           parameter_list(parameters)
       end
       socket.close
     end
   
+    # Maps a key from a hash to a new value returned by the block. 
+    #
+    def map hash, key, &block
+      return unless hash.has_key? key
+      hash[key] = block.call(hash[key])
+    end
+  
     def socket_chardev(name, path)
       "-chardev socket,id=#{name},path=#{path},server,nowait"
     end

spec/fixture/etc/nic.rb

     via: 'igb1'
 
   # This is what happens below the covers.
-  net :vnic, vlan: 1, name: 'vm1', ifname: 'vm1', macaddr: '1:8:20:52:a6:7e'
-  net :nic, vlan: 1, name: "vm1", model: "virtio", macaddr: '1:8:20:52:a6:7e'
+  net :vnic, vlan: 2, name: 'vm1', ifname: 'vm1', macaddr: '1:8:20:52:a6:7e'
+  net :nic, vlan: 2, name: "vm1", model: "virtio", macaddr: '1:8:20:52:a6:7e'
 
   # And this demonstrates how to configure the built-in DHCP-server: 
   nic 'eth2', 
-    macaddr: '0:24:81:67:93:99', 
+    macaddr: '0:24:81:67:93:99', model: 'e1000g',
     via: 'igb2', 
     # Important part follows: 
     ip: '10.0.0.2', netmask: '255.255.255.0', 

spec/lib/qemu-toolkit/vm_spec.rb

       subject { nic.command }
 
       describe 'manual nic configuration' do
-        it { should include("-net vnic,vlan=1,name=vm1,ifname=vm1,macaddr=1:8:20:52:a6:7e") }
-        it { should include("-net nic,vlan=1,name=vm1,model=virtio,macaddr=1:8:20:52:a6:7e") }
+        it { should include("-net vnic,vlan=2,name=vm1,ifname=vm1,macaddr=01:08:20:52:a6:7e") }
+        it { should include("-net nic,vlan=2,name=vm1,model=virtio,macaddr=01:08:20:52:a6:7e") }
       end
       describe 'automatic vnic configuration' do
         it { should include(
-          "-device virtio-net-pci,mac=2:8:20:52:a6:7e,tx=timer,x-txtimer=200000,x-txburst=128,vlan=0")}
+          "-device virtio-net-pci,mac=02:08:20:52:a6:7e,tx=timer,x-txtimer=200000,x-txburst=128,vlan=0")}
         it { should include(
           "-net vnic,vlan=0,name=eth0,ifname=nic_1") }
       end
       describe 'DHCP server configuration' do
         it { should include(
           "-net vnic,ip=10.0.0.2,netmask=255.255.255.0,gateway_ip=10.0.0.1,hostname=foobar,dns_ip=8.8.8.8,8.8.4.4,vlan=1,name=eth2,ifname=nic_1") }
+        it { should include(
+          "-net nic,vlan=1,name=eth2,model=e1000g,macaddr=00:24:81:67:93:99")}
       end
     end
   end