Skip to content

Instantly share code, notes, and snippets.

@mgerdts
Created January 25, 2018 19:35
Show Gist options
  • Save mgerdts/3977929422ac822fb603d0567c97bd61 to your computer and use it in GitHub Desktop.
Save mgerdts/3977929422ac822fb603d0567c97bd61 to your computer and use it in GitHub Desktop.
nvlist from xml
<?xml version="1.0"?>
<nvlist>
<nvpair name="hostid">
<uint32 value="0xabc123"/>
</nvpair>
<nvpair name="vcpus">
<int64 value="4"/>
</nvpair>
<nvpair name="ram">
<int64 value="8192"/>
</nvpair>
<nvpair name="resolvers">
<string value="8.8.8.8,8.8.4.4"/>
</nvpair>
<nvpair name="devices">
<nvlist_array>
<nvlist>
<nvpair name="path">
<string value="/dev/zvol/rdsk/zones/b2-disk0"/>
</nvpair>
<nvpair name="boot">
<boolean value="true"/>
</nvpair>
<nvpair name="media">
<string value="disk"/>
</nvpair>
<nvpair name="model">
<string value="virtio"/>
</nvpair>
</nvlist>
</nvlist_array>
</nvpair>
<nvpair name="networks">
<nvlist_array>
<nvlist>
<nvpair name="nic">
<string value="net0"/>
</nvpair>
<nvpair name="gateway">
<string value="172.26.17.1"/>
</nvpair>
<nvpair name="gateways">
<string value="172.26.17.1"/>
</nvpair>
<nvpair name="netmask">
<string value="255.255.255.0"/>
</nvpair>
<nvpair name="ip">
<string value="172.26.17.202"/>
</nvpair>
<nvpair name="ips">
<string value="172.26.17.202/24"/>
</nvpair>
<nvpair name="model">
<string value="virtio"/>
</nvpair>
<nvpair name="primary">
<boolean value="true"/>
</nvpair>
</nvlist>
</nvlist_array>
</nvpair>
</nvlist>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="zone">
<xsl:element name="nvlist">
<!-- hostid -->
<xsl:element name="nvpair">
<xsl:attribute name="name">hostid</xsl:attribute>
<xsl:element name="uint32">
<xsl:attribute name="value">
<xsl:value-of select="concat('0x', @hostid)" />
</xsl:attribute>
</xsl:element>
</xsl:element>
<!-- vcpus -->
<xsl:element name="nvpair">
<xsl:attribute name="name">vcpus</xsl:attribute>
<xsl:element name="int64">
<xsl:attribute name="value">
<xsl:value-of select='/zone/attr[@name="vcpus"]/@value' />
</xsl:attribute>
</xsl:element>
</xsl:element>
<!-- ram (in MB) -->
<xsl:element name="nvpair">
<xsl:attribute name="name">ram</xsl:attribute>
<xsl:element name="int64">
<xsl:attribute name="value">
<xsl:value-of select='/zone/attr[@name="ram"]/@value' />
</xsl:attribute>
</xsl:element>
</xsl:element>
<!-- resolvers -->
<xsl:element name="nvpair">
<xsl:attribute name="name">resolvers</xsl:attribute>
<xsl:element name="string">
<xsl:attribute name="value">
<xsl:value-of select='/zone/attr[@name="resolvers"]/@value' />
</xsl:attribute>
</xsl:element>
</xsl:element>
<!-- devices -->
<xsl:element name="nvpair">
<xsl:attribute name="name">devices</xsl:attribute>
<xsl:element name="nvlist_array">
<xsl:for-each select="device">
<xsl:element name="nvlist">
<!-- match -> path -->
<xsl:element name="nvpair">
<xsl:attribute name="name">path</xsl:attribute>
<xsl:element name="string">
<xsl:attribute name="value">
<xsl:value-of select='@match' />
</xsl:attribute>
</xsl:element>
</xsl:element>
<!-- boot -->
<xsl:element name="nvpair">
<xsl:attribute name="name">boot</xsl:attribute>
<xsl:element name="boolean">
<xsl:attribute name="value">
<xsl:value-of select='net-attr[@name="boot"]/@value' />
</xsl:attribute>
</xsl:element>
</xsl:element>
<!-- media -->
<xsl:element name="nvpair">
<xsl:attribute name="name">media</xsl:attribute>
<xsl:element name="string">
<xsl:attribute name="value">
<xsl:value-of select='net-attr[@name="media"]/@value' />
</xsl:attribute>
</xsl:element>
</xsl:element>
<!-- model -->
<xsl:element name="nvpair">
<xsl:attribute name="name">model</xsl:attribute>
<xsl:element name="string">
<xsl:attribute name="value">
<xsl:value-of select='net-attr[@name="model"]/@value' />
</xsl:attribute>
</xsl:element>
</xsl:element>
</xsl:element> <!-- name="nvlist" -->
</xsl:for-each> <!-- device -->
</xsl:element> <!-- name="nvlist_array" -->
</xsl:element> <!-- name="nvpair" -->
<!-- networks -->
<xsl:element name="nvpair">
<xsl:attribute name="name">networks</xsl:attribute>
<xsl:element name="nvlist_array">
<xsl:for-each select="network">
<xsl:element name="nvlist">
<!-- physical -> nic -->
<xsl:element name="nvpair">
<xsl:attribute name="name">nic</xsl:attribute>
<xsl:element name="string">
<xsl:attribute name="value">
<xsl:value-of select='@physical' />
</xsl:attribute>
</xsl:element>
</xsl:element>
<!-- gateway -->
<xsl:element name="nvpair">
<xsl:attribute name="name">gateway</xsl:attribute>
<xsl:element name="string">
<xsl:attribute name="value">
<xsl:value-of select='net-attr[@name="gateway"]/@value' />
</xsl:attribute>
</xsl:element>
</xsl:element>
<!-- gateways -->
<xsl:element name="nvpair">
<xsl:attribute name="name">gateways</xsl:attribute>
<xsl:element name="string">
<xsl:attribute name="value">
<xsl:value-of select='net-attr[@name="gateways"]/@value' />
</xsl:attribute>
</xsl:element>
</xsl:element>
<!-- netmask -->
<xsl:element name="nvpair">
<xsl:attribute name="name">netmask</xsl:attribute>
<xsl:element name="string">
<xsl:attribute name="value">
<xsl:value-of select='net-attr[@name="netmask"]/@value' />
</xsl:attribute>
</xsl:element>
</xsl:element>
<!-- ip -->
<xsl:element name="nvpair">
<xsl:attribute name="name">ip</xsl:attribute>
<xsl:element name="string">
<xsl:attribute name="value">
<xsl:value-of select='net-attr[@name="ip"]/@value' />
</xsl:attribute>
</xsl:element>
</xsl:element>
<!-- ips -->
<xsl:element name="nvpair">
<xsl:attribute name="name">ips</xsl:attribute>
<xsl:element name="string">
<xsl:attribute name="value">
<xsl:value-of select='net-attr[@name="ips"]/@value' />
</xsl:attribute>
</xsl:element>
</xsl:element>
<!-- model -->
<xsl:element name="nvpair">
<xsl:attribute name="name">model</xsl:attribute>
<xsl:element name="string">
<xsl:attribute name="value">
<xsl:value-of select='net-attr[@name="model"]/@value' />
</xsl:attribute>
</xsl:element>
</xsl:element>
<!-- primary -->
<xsl:element name="nvpair">
<xsl:attribute name="name">primary</xsl:attribute>
<xsl:element name="boolean">
<xsl:attribute name="value">
<xsl:value-of select='net-attr[@name="primary"]/@value' />
</xsl:attribute>
</xsl:element>
</xsl:element>
</xsl:element> <!-- name="nvlist" -->
</xsl:for-each> <!-- device -->
</xsl:element> <!-- name="nvlist_array" -->
</xsl:element> <!-- name="nvpair" -->
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT THIS FILE. Use zonecfg(1M) instead.
-->
<!DOCTYPE zone PUBLIC "-//Sun Microsystems Inc//DTD Zones//EN" "file:///usr/share/lib/xml/dtd/zonecfg.dtd.1">
<zone name="b2" zonepath="/zones/b2" autoboot="false" brand="bhyve" limitpriv="default" ip-type="exclusive" debugid="14" hostid="abc123">
<device match="/dev/zvol/rdsk/zones/b2-disk0">
<net-attr name="boot" value="true"/>
<net-attr name="model" value="virtio"/>
<net-attr name="media" value="disk"/>
<net-attr name="image-size" value="10240"/>
<net-attr name="image-uuid" value="6aac0370-56b8-11e7-a29b-df1de8b42795"/>
</device>
<rctl name="zone.cpu-shares">
<rctl-value priv="privileged" limit="100" action="none"/>
</rctl>
<rctl name="zone.max-lwps">
<rctl-value priv="privileged" limit="2000" action="deny"/>
</rctl>
<rctl name="zone.max-msg-ids">
<rctl-value priv="privileged" limit="4096" action="deny"/>
</rctl>
<rctl name="zone.max-sem-ids">
<rctl-value priv="privileged" limit="4096" action="deny"/>
</rctl>
<rctl name="zone.max-shm-ids">
<rctl-value priv="privileged" limit="4096" action="deny"/>
</rctl>
<rctl name="zone.max-shm-memory">
<rctl-value priv="privileged" limit="1610612736" action="deny"/>
</rctl>
<rctl name="zone.zfs-io-priority">
<rctl-value priv="privileged" limit="100" action="none"/>
</rctl>
<rctl name="zone.max-physical-memory">
<rctl-value priv="privileged" limit="8858370048" action="deny"/>
</rctl>
<rctl name="zone.max-locked-memory">
<rctl-value priv="privileged" limit="8858370048" action="deny"/>
</rctl>
<rctl name="zone.max-swap">
<rctl-value priv="privileged" limit="8858370048" action="deny"/>
</rctl>
<attr name="resolvers" type="string" value="8.8.8.8,8.8.4.4"/>
<attr name="ram" type="string" value="8192"/>
<attr name="vcpus" type="string" value="4"/>
<attr name="vm-autoboot" type="string" value="false"/>
<attr name="dataset-uuid" type="string" value="fbd0d30b-65e1-ea38-ec78-8dad1c41d143"/>
<attr name="vm-version" type="string" value="1"/>
<dataset name="zones/b2/data"/>
<network allowed-address="172.26.17.202" physical="net0" mac-addr="02:08:20:84:61:a2" vlan-id="3317" global-nic="external">
<net-attr name="gateway" value="172.26.17.1"/>
<net-attr name="gateways" value="172.26.17.1"/>
<net-attr name="netmask" value="255.255.255.0"/>
<net-attr name="ip" value="172.26.17.202"/>
<net-attr name="ips" value="172.26.17.202/24"/>
<net-attr name="model" value="virtio"/>
<net-attr name="primary" value="true"/>
</network>
</zone>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment