del %tmp%
mkdir %tmp%

exec qemu-img convert -O qcow2 -c %img%.img %img%.qcow2
exec qemu-img convert -O vmdk -o subformat=streamOptimized %img%.img %img%.vmdk

del %img%.gns3a

file-text %img%.gns3a
{
    "appliance_id": "691f6552-725f-4dff-a2ba-220f5e3ab6a6",
    "name": "freeRouter",
    "category": "router",
    "description": "networking swiss army knife - it speaks routing protocols, and (re)encapsulates packets on interfaces",
    "vendor_name": "freeRouter",
    "vendor_url": "http://www.freertr.org/",
    "documentation_url": "http://www.freertr.org/",
    "product_name": "freeRouter",
    "product_url": "http://www.freertr.org/",
    "registry_version": 3,
    "status": "stable",
    "maintainer": "GNS3 Team",
    "maintainer_email": "developers@gns3.net",
    "usage": "there is no default password. a default configuration is present.",
    "port_name_format": "ethernet{port1}",
    "qemu": {
        "adapter_type": "virtio-net-pci",
        "adapters": 8,
        "ram": 1024,
        "hda_disk_interface": "ide",
        "arch": "%unam%",
        "console_type": "telnet",
        "kvm": "require"
    },
    "images": [
        {
            "filename": "rtr-%unam%.qcow2",
.

file-vers %img%.qcow2
file-line %img%.gns3a             "version": "%find%",
file-hash md5 %img%.qcow2
file-line %img%.gns3a             "md5sum": "%find%",
file-size %img%.qcow2
file-line %img%.gns3a             "filesize": %find%,

file-text %img%.gns3a
            "download_url": "http://www.freertr.org/",
            "direct_download_url": "http://dl.nop.hu/rtr-%unam%.qcow2"
        }
    ],
    "versions": [
        {
.

file-vers %img%.qcow2
file-line %img%.gns3a             "name": "%find%",
file-text %img%.gns3a
            "images": {
                "hda_disk_image": "rtr-%unam%.qcow2"
            }
        }
    ]
}
.

file-line %tmp%/package.ver 1.0

file-text %tmp%/package.vsh
<domain type='kvm'>
  <name>rtr-dsk</name>
  <uuid>41bffa92-a072-44ca-8073-455b82c990b5</uuid>
  <title>freeRouter</title>
  <description>http://www.freertr.org/</description>
  <memory unit='KiB'>1048576</memory>
  <currentMemory unit='KiB'>1048576</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <os>
    <type arch='%unam%' machine='pc-i440fx-2.11'>hvm</type>
  </os>
  <features>
    <acpi/>
    <apic/>
    <vmport state='off'/>
  </features>
  <cpu mode='host-model' check='partial'>
    <model fallback='allow'/>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='rtr-%unam%.qcow2'/>
      <target dev='vda' bus='virtio'/>
      <boot order='1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </controller>
    <interface type='direct'>
      <mac address='52:54:00:97:12:a0'/>
      <source dev='eth0' mode='passthrough'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='tcp'>
      <source mode='bind' host='0.0.0.0' service='4555'/>
      <protocol type='raw'/>
      <target type='isa-serial' port='0'>
        <model name='isa-serial'/>
      </target>
    </serial>
    <channel type='unix'>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <channel type='spicevmc'>
      <target type='virtio' name='com.redhat.spice.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='2'/>
    </channel>
    <input type='tablet' bus='usb'>
      <address type='usb' bus='0' port='1'/>
    </input>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='spice' autoport='yes'>
      <listen type='address'/>
      <image compression='off'/>
    </graphics>
    <sound model='ich6'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </sound>
    <video>
      <model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <redirdev bus='usb' type='spicevmc'>
      <address type='usb' bus='0' port='2'/>
    </redirdev>
    <redirdev bus='usb' type='spicevmc'>
      <address type='usb' bus='0' port='3'/>
    </redirdev>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
    </memballoon>
    <rng model='virtio'>
      <backend model='random'>/dev/urandom</backend>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
    </rng>
  </devices>
</domain>
.

file-vers %img%.qcow2
file-text %tmp%/package.yaml
manifest-version: 1.0

info:
  name: freeRouter
  description: "freeRouter"
  version: %find%

app:
  apptype: vm

  resources:
   cpu: 30
   memory: 1024000
   vcpu: 1

   disk:
    - target-dev: hdc
      file: rtr-%unam%.qcow2

   interfaces:
    - target-dev: net1
    - target-dev: net2
    - target-dev: net3
    - target-dev: net4

   serial:
    - console
    - aux

  startup:
    runtime: kvm
    boot-dev: hd
.

file-text %tmp%/package.ovf
<?xml version="1.0"?>
<Envelope ovf:version="1.0" xml:lang="en-US" xmlns="http://schemas.dmtf.org/ovf/envelope/1" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vbox="http://www.virtualbox.org/ovf/machine">
  <References>
    <File ovf:id="file1" ovf:href="rtr-%unam%.vmdk"/>
  </References>
  <DiskSection>
    <Info>List of the virtual disks used in the package</Info>
    <Disk ovf:capacity="2147483648" ovf:diskId="vmdisk1" ovf:fileRef="file1" ovf:format="http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized" vbox:uuid="2ff74630-0377-4198-89e8-c8f3236029f5"/>
  </DiskSection>
  <NetworkSection>
    <Info>Logical networks used in the package</Info>
    <Network ovf:name="NAT">
      <Description>Logical network used by this appliance.</Description>
    </Network>
  </NetworkSection>
  <VirtualSystem ovf:id="rtr">
    <Info>A virtual machine</Info>
    <ProductSection>
      <Info>Meta-information about the installed software</Info>
      <Product>freeRouter</Product>
      <ProductUrl>http://www.freertr.org/</ProductUrl>
    </ProductSection>
    <AnnotationSection>
      <Info>A human-readable annotation</Info>
      <Annotation>freeRouter distribution
more info at http://www.freertr.org/
</Annotation>
    </AnnotationSection>
    <OperatingSystemSection ovf:id="95">
      <Info>The kind of installed guest operating system</Info>
      <Description>Debian</Description>
      <vbox:OSType ovf:required="false">Debian</vbox:OSType>
    </OperatingSystemSection>
    <VirtualHardwareSection>
      <Info>Virtual hardware requirements for a virtual machine</Info>
      <System>
        <vssd:ElementName>Virtual Hardware Family</vssd:ElementName>
        <vssd:InstanceID>0</vssd:InstanceID>
        <vssd:VirtualSystemIdentifier>rtr</vssd:VirtualSystemIdentifier>
        <vssd:VirtualSystemType>virtualbox-2.2</vssd:VirtualSystemType>
      </System>
      <Item>
        <rasd:Caption>2 virtual CPU</rasd:Caption>
        <rasd:Description>Number of virtual CPUs</rasd:Description>
        <rasd:ElementName>2 virtual CPU</rasd:ElementName>
        <rasd:InstanceID>1</rasd:InstanceID>
        <rasd:ResourceType>3</rasd:ResourceType>
        <rasd:VirtualQuantity>2</rasd:VirtualQuantity>
      </Item>
      <Item>
        <rasd:AllocationUnits>MegaBytes</rasd:AllocationUnits>
        <rasd:Caption>1024 MB of memory</rasd:Caption>
        <rasd:Description>Memory Size</rasd:Description>
        <rasd:ElementName>1024 MB of memory</rasd:ElementName>
        <rasd:InstanceID>2</rasd:InstanceID>
        <rasd:ResourceType>4</rasd:ResourceType>
        <rasd:VirtualQuantity>1024</rasd:VirtualQuantity>
      </Item>
      <Item>
        <rasd:Address>0</rasd:Address>
        <rasd:Caption>ideController0</rasd:Caption>
        <rasd:Description>IDE Controller</rasd:Description>
        <rasd:ElementName>ideController0</rasd:ElementName>
        <rasd:InstanceID>3</rasd:InstanceID>
        <rasd:ResourceSubType>PIIX4</rasd:ResourceSubType>
        <rasd:ResourceType>5</rasd:ResourceType>
      </Item>
      <Item>
        <rasd:Address>1</rasd:Address>
        <rasd:Caption>ideController1</rasd:Caption>
        <rasd:Description>IDE Controller</rasd:Description>
        <rasd:ElementName>ideController1</rasd:ElementName>
        <rasd:InstanceID>4</rasd:InstanceID>
        <rasd:ResourceSubType>PIIX4</rasd:ResourceSubType>
        <rasd:ResourceType>5</rasd:ResourceType>
      </Item>
      <Item>
        <rasd:Address>0</rasd:Address>
        <rasd:Caption>usb</rasd:Caption>
        <rasd:Description>USB Controller</rasd:Description>
        <rasd:ElementName>usb</rasd:ElementName>
        <rasd:InstanceID>5</rasd:InstanceID>
        <rasd:ResourceType>23</rasd:ResourceType>
      </Item>
      <Item>
        <rasd:AddressOnParent>0</rasd:AddressOnParent>
        <rasd:Caption>disk1</rasd:Caption>
        <rasd:Description>Disk Image</rasd:Description>
        <rasd:ElementName>disk1</rasd:ElementName>
        <rasd:HostResource>/disk/vmdisk1</rasd:HostResource>
        <rasd:InstanceID>6</rasd:InstanceID>
        <rasd:Parent>3</rasd:Parent>
        <rasd:ResourceType>17</rasd:ResourceType>
      </Item>
      <Item>
        <rasd:AutomaticAllocation>true</rasd:AutomaticAllocation>
        <rasd:Caption>Ethernet adapter on 'NAT'</rasd:Caption>
        <rasd:Connection>NAT</rasd:Connection>
        <rasd:ElementName>Ethernet adapter on 'NAT'</rasd:ElementName>
        <rasd:InstanceID>7</rasd:InstanceID>
        <rasd:ResourceType>10</rasd:ResourceType>
      </Item>
      <Item>
        <rasd:AutomaticAllocation>true</rasd:AutomaticAllocation>
        <rasd:Caption>Ethernet adapter on 'NAT'</rasd:Caption>
        <rasd:Connection>NAT</rasd:Connection>
        <rasd:ElementName>Ethernet adapter on 'NAT'</rasd:ElementName>
        <rasd:InstanceID>8</rasd:InstanceID>
        <rasd:ResourceType>10</rasd:ResourceType>
      </Item>
      <Item>
        <rasd:AutomaticAllocation>true</rasd:AutomaticAllocation>
        <rasd:Caption>Ethernet adapter on 'NAT'</rasd:Caption>
        <rasd:Connection>NAT</rasd:Connection>
        <rasd:ElementName>Ethernet adapter on 'NAT'</rasd:ElementName>
        <rasd:InstanceID>9</rasd:InstanceID>
        <rasd:ResourceType>10</rasd:ResourceType>
      </Item>
      <Item>
        <rasd:AutomaticAllocation>true</rasd:AutomaticAllocation>
        <rasd:Caption>Ethernet adapter on 'NAT'</rasd:Caption>
        <rasd:Connection>NAT</rasd:Connection>
        <rasd:ElementName>Ethernet adapter on 'NAT'</rasd:ElementName>
        <rasd:InstanceID>10</rasd:InstanceID>
        <rasd:ResourceType>10</rasd:ResourceType>
      </Item>
    </VirtualHardwareSection>
    <vbox:Machine ovf:required="false" version="1.15-linux" uuid="{61722d0c-5197-4d81-b9c4-99e656182c6e}" name="rtr" OSType="Debian" snapshotFolder="Snapshots" lastStateChange="2018-03-02T06:02:55Z">
      <ovf:Info>Complete VirtualBox machine configuration in VirtualBox format</ovf:Info>
      <Description>rtros distribution
more info at http://www.freertr.org/
</Description>
      <ExtraData>
        <ExtraDataItem name="GUI/LastCloseAction" value="PowerOff"/>
        <ExtraDataItem name="GUI/LastGuestSizeHint" value="720,400"/>
        <ExtraDataItem name="GUI/LastNormalWindowPosition" value="0,238,720,450"/>
        <ExtraDataItem name="GUI/RestrictedRuntimeDevicesMenuActions" value="HardDrives"/>
        <ExtraDataItem name="GUI/RestrictedRuntimeMachineMenuActions" value="SaveState,PowerOff"/>
        <ExtraDataItem name="GUI/SaveMountedAtRuntime" value="yes"/>
        <ExtraDataItem name="GUI/ScaleFactor" value="1"/>
        <ExtraDataItem name="GUI/StatusBar/IndicatorOrder" value="HardDisks,OpticalDisks,FloppyDisks,Network,USB,SharedFolders,Display,VideoCapture,Features,Mouse,Keyboard"/>
      </ExtraData>
      <Hardware>
        <CPU count="2">
          <PAE enabled="true"/>
          <LongMode enabled="false"/>
          <HardwareVirtExLargePages enabled="false"/>
        </CPU>
        <Memory RAMSize="1024"/>
        <HID Pointing="USBTablet"/>
        <Paravirt provider="Default"/>
        <Boot>
          <Order position="1" device="HardDisk"/>
          <Order position="2" device="None"/>
          <Order position="3" device="None"/>
          <Order position="4" device="None"/>
        </Boot>
        <Display VRAMSize="5"/>
        <VideoCapture screens="1" options="ac_enabled=false" file="." fps="25"/>
        <RemoteDisplay enabled="false">
          <VRDEProperties>
            <Property name="TCP/Ports" value="3389"/>
          </VRDEProperties>
        </RemoteDisplay>
        <BIOS>
          <IOAPIC enabled="true"/>
        </BIOS>
        <USB>
          <Controllers>
            <Controller name="OHCI" type="OHCI"/>
          </Controllers>
        </USB>
        <Network>
          <Adapter slot="0" enabled="true" MACAddress="080027EB7AA1" cable="true" type="virtio">
            <DisabledModes>
              <InternalNetwork name="intnet"/>
              <NATNetwork name="NatNetwork"/>
            </DisabledModes>
            <NAT/>
          </Adapter>
          <Adapter slot="1" enabled="true" MACAddress="080027BA27D4" cable="true" type="virtio">
            <DisabledModes>
              <InternalNetwork name="intnet"/>
              <NATNetwork name="NatNetwork"/>
            </DisabledModes>
            <NAT/>
          </Adapter>
          <Adapter slot="2" enabled="true" MACAddress="0800274F3DF3" cable="true" type="virtio">
            <DisabledModes>
              <InternalNetwork name="intnet"/>
              <NATNetwork name="NatNetwork"/>
            </DisabledModes>
            <NAT/>
          </Adapter>
          <Adapter slot="3" enabled="true" MACAddress="080027907D6D" cable="true" type="virtio">
            <DisabledModes>
              <InternalNetwork name="intnet"/>
              <NATNetwork name="NatNetwork"/>
            </DisabledModes>
            <NAT/>
          </Adapter>
          <Adapter slot="4" cable="true" type="virtio"/>
          <Adapter slot="5" cable="true" type="virtio"/>
          <Adapter slot="6" cable="true" type="virtio"/>
          <Adapter slot="7" cable="true" type="virtio"/>
        </Network>
        <UART>
          <Port slot="0" enabled="true" IOBase="0x3f8" IRQ="4" server="true" path="/tmp/rtr-con" hostMode="HostPipe"/>
        </UART>
        <LPT>
          <Port slot="1" enabled="false" IOBase="0x378" IRQ="7"/>
        </LPT>
        <AudioAdapter driver="Pulse" enabled="false"/>
        <RTC localOrUTC="UTC"/>
        <Clipboard/>
        <GuestProperties>
          <GuestProperty name="/VirtualBox/GuestAdd/Revision" value="78361" timestamp="1395308999147819000" flags=""/>
          <GuestProperty name="/VirtualBox/GuestAdd/Version" value="4.1.18" timestamp="1395308999146644000" flags=""/>
          <GuestProperty name="/VirtualBox/GuestAdd/VersionExt" value="4.1.18_Debian" timestamp="1395308999147418000" flags=""/>
          <GuestProperty name="/VirtualBox/GuestInfo/Net/0/MAC" value="08002758633D" timestamp="1395308999153077000" flags=""/>
          <GuestProperty name="/VirtualBox/GuestInfo/Net/0/Status" value="Up" timestamp="1395308999153974000" flags=""/>
          <GuestProperty name="/VirtualBox/GuestInfo/Net/0/V4/Broadcast" value="10.0.2.255" timestamp="1395308999152210000" flags=""/>
          <GuestProperty name="/VirtualBox/GuestInfo/Net/0/V4/IP" value="10.0.2.15" timestamp="1395308999151283000" flags=""/>
          <GuestProperty name="/VirtualBox/GuestInfo/Net/0/V4/Netmask" value="255.255.255.0" timestamp="1395308999152588000" flags=""/>
          <GuestProperty name="/VirtualBox/GuestInfo/Net/Count" value="1" timestamp="1395311614456867000" flags=""/>
          <GuestProperty name="/VirtualBox/GuestInfo/OS/Product" value="Linux" timestamp="1395308999144084000" flags=""/>
          <GuestProperty name="/VirtualBox/GuestInfo/OS/Release" value="3.2.0-4-%arch%" timestamp="1395308999144785000" flags=""/>
          <GuestProperty name="/VirtualBox/GuestInfo/OS/Version" value="#1 SMP Debian 3.2.54-2" timestamp="1395308999145392000" flags=""/>
          <GuestProperty name="/VirtualBox/HostInfo/GUI/LanguageID" value="en_US" timestamp="1519970436009989000" flags=""/>
        </GuestProperties>
      </Hardware>
      <StorageControllers>
        <StorageController name="IDE" type="PIIX4" PortCount="2" useHostIOCache="true" Bootable="true">
          <AttachedDevice type="HardDisk" hotpluggable="false" port="0" device="0">
            <Image uuid="{2ff74630-0377-4198-89e8-c8f3236029f5}"/>
          </AttachedDevice>
        </StorageController>
      </StorageControllers>
    </vbox:Machine>
  </VirtualSystem>
</Envelope>
.

file-hash sha1 %img%.qcow2
file-line %tmp%/package.mf SHA1(rtr-%unam%.qcow2)= %find%
file-hash sha1 %img%.vmdk
file-line %tmp%/package.mf SHA1(rtr-%unam%.vmdk)= %find%
file-hash sha1 %tmp%/package.ovf
file-line %tmp%/package.mf SHA1(package.ovf)= %find%
file-hash sha1 %tmp%/package.ver
file-line %tmp%/package.mf SHA1(package.ver)= %find%
file-hash sha1 %tmp%/package.vsh
file-line %tmp%/package.mf SHA1(package.vsh)= %find%
file-hash sha1 %tmp%/package.yaml
file-line %tmp%/package.mf SHA1(package.yaml)= %find%

exec cd ../binImg/ ; tar cf %img%.ova rtr-%unam%.qcow2 rtr-%unam%.vmdk
exec cd %tmp% ; tar rf %img%.ova package.yaml package.ovf package.vsh package.ver package.mf

del %tmp%
