Difference between pages "Metro Quick Start Tutorial" and "Metro Data Model"

From Funtoo
(Difference between pages)
Jump to: navigation, search
(Installing Metro)
 
 
Line 1: Line 1:
[[Metro]] is the build system for [[Funtoo Linux]] and [[Gentoo Linux]] stages. It automates the bootstrapping process.
+
== Goals ==
  
This tutorial will take you through installing, setting up and running [[Metro]].
+
The Metro Data Model has been designed to provide you with an optimal way to organize build data.
  
= Preface =
+
Here are the primary goals for the data model:
  
== How Metro Works ==
+
# Provide useful ways to organize data
 +
# Use mechanisms and syntax that maximize maintainability of the data over time
 +
# Reduce and (ideally) eliminate side-effects at every opportunity
  
You may be wondering how [[Metro]] creates its first stage tarball. As you may have guessed, [[Metro]] cannot create a stage tarball out of thin air. To build a new stage tarball, [[Metro]] must use an existing, older stage tarball called a "seed" stage. This "seed" stage typically is used as the ''build environment'' for creating the stage we want.
+
To attain these goals, I've used a functional data model, where an element (variable) can be defined only once, and cannot be redefined.
  
[[Metro]] can use two kinds of seed stages. Traditionally, [[Metro]] has used a stage3 as a seed stage. This stage3 is then used to build a new stage1, which in turn is used to build a new stage2, and then a new stage3. This is generally the most reliable way to build [[Gentoo Linux]] or [[Funtoo Linux]], so it's the recommended approach.
+
''By default, the Metro parser operates in "strict" mode, which means that it will throw an error if a variable has been referenced that has not been defined. This "strict" mode is actually very useful in catching errors that might otherwise go unnoticed and result in broken builds.''
{{fancyimportant|'''After switching metro builds to Funtoo profile, Gentoo stages are no longer provided'''!}}
+
  
== Seeds and Build Isolation ==
+
In addition, the Metro parser was designed so that the order in which data elements are defined is not important, even if they reference one another. This was done to eliminate side-effects related to data ordering, where changing the order in which things are defined in a file can change the behavior of or break your code.
  
Another important concept to mention here is something called ''build isolation''. Because [[Metro]] creates an isolated build environment, and the build environment is explicitly defined using existing, tangible entities -- a seed stage and a portage snapshot -- you will get consistent, repeatable results. In other words, the same seed stage, portage snapshot and build instructions will generate an essentially identical result, even if you perform the build a month later on someone else's workstation.
+
Versions of Metro prior to 1.4 contained limited support for conditional logic. After some experimentation, I've decided that the conditional support is not necessary, and it is not used by Metro 1.4. However, support for conditionals still exist in the parser, but will be removed when the parser is rewritten.
  
== Local Build ==
 
  
Say you wanted to build a new <tt>pentium4</tt> stage3 tarball. The recommended method of doing this would be to grab an existing <tt>pentium4</tt> stage3 tarball to use as your seed stage. [[Metro]] will be told to use this existing <tt>pentium4</tt> stage3 to build a new stage1 for the same <tt>pentium4</tt>. For this process, the generic <tt>pentium4</tt> stage3 would provide the ''build environment'' for creating our new stage1. Then, the new stage1 would serve as the build environment for creating the new <tt>pentium4</tt> stage2. And the new <tt>pentium4</tt> stage2 would serve as the build environment for creating the new <tt>pentium4</tt> stage3.
+
== First Look ==
  
In the [[Metro]] terminology this is called a '''local build''', which means a stage3 of a given architecture is used to seed a brand new build of the same architecture. Incidentally this will be the first exercise we are going to perform in this tutorial.
+
Here is some sample Metro data:
  
A week later, you may want to build a brand new <tt>pentium4</tt> stage3 tarball. Rather than starting from the original <tt>pentium4</tt> stage3 again, you'd probably configure [[Metro]] to use the most-recently-built <tt>pentium4</tt> stage3 as the seed. [[Metro]] has built-in functionality to make this easy, allowing it to easily find and track the most recent stage3 seed available.
+
<pre>path: /usr/bin</pre>
 +
Above, we have defined the element <tt>path</tt> to have the value <tt>/usr/bin</tt>. <tt>path</tt> is a single-line element, and the Metro parser takes care of trimming any trailing whitespace that may be on the line. You can also define single-line elements that have values that consist of multiple whitespace-separated values:
  
== Remote Build ==
+
<pre>options: ccache replace</pre>
 +
Sometimes, you need to define an element but leave it blank. To do this, don't specify any values after the colon:
  
[[Metro]] can also perform '''remote build''', where a stage3 of a different, but binary compatible, architecture is used as a seed to build a different architecture stage3. Consequentiality the second exercise we are going to perform in this tutorial will be to build a <tt>core2 32bit</tt> stage3 tarball from the <tt>pentium4</tt> stage3 tarball we have just built.
+
<pre>options:</pre>
 +
In Metro, the <tt>/</tt> character is used to delineate various classes of elements, as follows:
  
TODO: add caveats about what archs can be seeded and what can be not (maybe a table?)
+
<pre>path/mirror: /home/mirror/linux
 +
path/mirror/snapshot: /home/mirror/linux/snapshots
 +
path/metro: /usr/lib/metro</pre>
 +
Above, we see the proper Metro convention for specifying paths. Each path has a prefix of <tt>path/</tt>. We have a <tt>path/mirror</tt> element but also have a <tt>path/mirror/snapshot</tt> element. The <tt>/</tt> is used to organize our data into logical groups. This is not enforced by Metro but is presented here as a best practice.
  
== Tailored Build ==
+
The data above could also be represented using a ''section annotation'', as follows:
  
Last, it's also worthy noting that both in <tt>local</tt> and <tt>remote builds</tt>, [[Metro]] can be configured to add and/or remove individual packages to the final tarball.
+
<pre>[section path]
Let's say you can't live without <tt>app-misc/screen</tt>, at the end of this tutorial, we will show how to have your tailored stage3 to include it.
+
  
== Installing Metro ==
+
mirror: /home/mirror/linux
 +
mirror/snapshot: /home/mirror/linux/snapshots
 +
metro: /usr/lib/metro</pre>
 +
Above, the <tt>[section path]</tt> line is a section annotation, and it tells the Metro parser that the <tt>path/</tt> prefix should be applied to all following data elements. A section annotation is in effect until another section annotation is encountered by the parser.
  
'''The recommended and supported method''' is to use the Git repository of [[Metro]].  First, ensure you remove the {{Package|dev-util/metro}} package if you had installed it:
+
While our data above is getting more organized, there is some redundancy in our data, which generally isn't a good thing. Here's an example of how to make our data a bit more compact:
  
<console>
+
<pre>[section path]
# ##i##emerge -aC dev-util/metro
+
</console>
+
  
Then ensure that {{Package|dev-vcs/git}} and {{Package|dev-python/boto}} are installed on your system:
+
mirror: /home/mirror/linux
 +
mirror/snapshot: $[path/mirror]/snapshots
 +
metro: /usr/lib/metro</pre>
 +
Above, we have used an ''element reference'' of <tt>$[path/mirror]</tt> to reference our <tt>path/mirror</tt> element. What this means is that <tt>path/snapshot</tt> will have a value of <tt>/home/mirror/linux/snapshots</tt>.
  
<console>
+
Also, it's worth pointing out that we could just have well written:
# ##i##emerge dev-vcs/git
+
# ##i##emerge dev-python/boto
+
</console>
+
  
Next, clone the master git repository as follows:
 
  
<console>
+
<pre>[section path]
# ##i##install -d /root/git
+
# ##i##cd /root/git
+
# ##i##git clone git://github.com/funtoo/metro.git
+
# ##i##ln -s /root/git/metro /usr/lib/metro
+
# ##i##ln -s /root/git/metro/metro /usr/bin/metro
+
</console>
+
  
You will now have a directory called <tt>/root/git/metro</tt> that contains all the [[Metro]] source code.
+
mirror/snapshot: $[path/mirror]/snapshots
Installation complete!
+
mirror: /home/mirror/linux
 +
metro: /usr/lib/metro</pre>
 +
In other words, it's perfectly OK to use the element reference of <tt>$[path/mirror]</tt> on a line ''before'' the actual definition of <tt>path/mirror</tt>. Metro doesn't care about the order in which data is defined.
  
= Configuring Metro =
+
Metro provides another way to organize your data in an efficient way. Supposing that you had a lot of <tt>path/mirror</tt>-related data, then it might be useful to organize your data as follows:
  
[[User:Drobbins|Daniel Robbins]] maintains [[Metro]], so it comes pre-configured to successfully build both [[Gentoo Linux]] and [[Funtoo Linux]] releases. Before reading farther, you might want to customize some basic settings like the number of concurrent jobs to fit your hardware's capabilities or the directory to use for produced stage archives. This is accomplished by editing <tt>/root/git/metro/etc/metro.conf</tt> which is the [[Metro]]'s master configuration file. The <tt>[path/mirror]</tt> section defines where [[Metro]] will look for things it needs and also dump things it creates. As initially configured, [[Metro]] is set up to build [[Funtoo Linux]] and [[Gentoo Linux]] stage tarballs and place them in the <tt>/home/mirror/linux</tt> directory:
+
<pre>[section path]
  
<pre>
+
metro: /usr/lib/metro
# Mirror Paths - where to find required files and where to put created files
+
  
 
[section path/mirror]
 
[section path/mirror]
  
 
: /home/mirror/linux
 
: /home/mirror/linux
</pre>
+
snapshot: $[]/snapshot
 +
source: $[]/$[source/subarch]/funtoo-$[source/subarch]-$[source/version]/$[source/name].tar.bz2</pre>
 +
Above, we have used two new parser features. Inside <tt>[section path/mirror]</tt>, we can define the <tt>path/mirror</tt> element itself by using a blank element name, followed by a <tt>:</tt>. The next parser feature we see above is that we can use <tt>$[]</tt> to reference the value of the <tt>path/mirror</tt> value. <tt>$[]</tt> will always reference the value of the element specified in the section annotation. Also note that as of Metro 1.1, <tt>$[:]</tt> can be used as an alternate form of <tt>$[]</tt>. In addition, as of Metro 1.2.4, <tt>$[:foo]</tt> can be used as an alternate form of <tt>$[section-name/foo]</tt>.
  
If you want to change the location of your mirror on disk, then edit the <tt>/home/mirror/linux</tt> line (which defines the path/mirror variable) to point to another directory.
+
== Collect Annotations ==
  
For the purpose of the following steps, set an environment variable:
+
Many scripting languages have the notion of an &quot;include&quot; file, or &quot;importing&quot; additional data from a remote file. Metro has this concept as well, but it is implemented in a somewhat different way. You can tell Metro to include data from another file by using a ''collect annotation''.
  
<console>
+
A collect annotation looks like this:
# ##i##export METRO_MIRROR=/home/mirror/linux
+
</console>
+
  
Of course, set the environment variable to whatever location you used in the configuration file.
+
<pre>[collect $[path/metro]/myfile.txt]</pre>
 +
Now, we called these things &quot;collect annotations&quot; for a reason - in Metro, they work slightly differently than most languages implement <tt>include</tt> and <tt>import</tt>. The main difference is that in Metro, a collect annotation does not happen right away. Instead, Metro will add the file to be collected (in this case, that would be the file <tt>/usr/lib/metro/myfile.txt</tt>, or whatever <tt>$[path/metro]/myfile.txt</tt> evaluates to) to a ''collection queue''. This means that Metro will read in the contents of the file at some point in time, and the data in the file will be available to you by the time the parsing is complete. But because Metro doesn't care about the order in which data is defined, it doesn't have the same concept of &quot;read in the data - right now!&quot; that an include or import statement does in other languages.
  
== Arch and Subarch ==
+
=== Conditional Collect Annotations ===
  
In the following example we are creating a pentium4 stage 3 compiled for x86-32bit binary compatibility. Pentium4 is a subarch of the x86-32bit architecture. Once you have metro installed you may find a full list of each subarch in your <tt>/usr/lib/metro/subarch</tt> directory each subarch will have the file extension .spec
+
Metro no longer officially supports conditional collect annotations; however, simple collect annotations can be used to make conditional decisions in Metro, as follows:
Example:
+
<console>
+
###i## ls /usr/lib/metro/subarch/
+
amd64-k10.spec        athlon-tbird.spec  generic_32.spec      native_32.spec  pentium-mmx.spec
+
amd64-k8_32.spec      athlon-xp.spec    generic_64.spec      native_64.spec  pentium-m.spec
+
amd64-k8.spec          atom_32.spec      generic_sparcv9.spec  niagara2.spec    pentiumpro.spec
+
amd64-k8+sse3_32.spec  atom_64.spec      geode.spec            niagara.spec    pentium.spec
+
amd64-k8+sse3.spec    btver1_64.spec    i486.spec            nocona.spec      prescott.spec
+
armv7a.spec            btver1.spec        i686.spec            opteron_64.spec  ultrasparc3.spec
+
athlon-4.spec          core2_32.spec      k6-2.spec            pentium2.spec    ultrasparc.spec
+
athlon-mp.spec        core2_64.spec      k6-3.spec            pentium3.spec    xen-pentium4+sse3_64.spec
+
athlon.spec            corei7.spec        k6.spec              pentium4.spec    xen-pentium4+sse3.spec
+
</console>
+
{{Fancynote|For an example list of Architectures, take a look at the directory listing on the [http://ftp.osuosl.org/pub/funtoo/funtoo-current/ Funtoo-Current Mirror].}}
+
  
= First stages build (local build) =
+
<pre>[collect ./snapshots/$[snapshot/type]]</pre>
 +
Above, Metro will collect from a file based on the value of the <tt>$[snapshot/type]</tt> element. This allows for varying definitions of elements to exist dependent on the value of <tt>$[snapshot/type]</tt>.
  
To get this all started, we need to bootstrap the process by downloading an initial seed stage3 to use for building and place it in its proper location in <tt>/home/mirror/linux</tt>, so that [[Metro]] can find it. We will also need to create some special &quot;control&quot; files in <tt>/home/mirror/linux</tt>, which will allow [[Metro]] to understand how it is supposed to proceed.
+
Above, Metro will raise an exception if <tt>$[snapshot/type]</tt> is undefined or has a value that does not map to a file on disk. If it is possible that <tt>$[snapshot/type]</tt> may not be defined, use the following format:
  
== Step 0: Optional Quick Copy of Portage Tree ==
+
<pre>[collect ./snapshots/$[snapshot/type:zap]]</pre>
 +
Using the <tt>:zap</tt> modifier, the entire collect argument will be replaced with the empty string if <tt>$[snapshot/type]</tt> is undefined. If Metro is asked to collect an empty string, it will not throw an exception. So this is a handy way to conditionally disable collection of a file. But please note that for all non-null values of <tt>$[snapshot/type]</tt>, a corresponding file must exist on disk in <tt>./snapshots/</tt> or Metro will throw an exception. <tt>:zap</tt> is explained in more detail in the &quot;Special Variable Expansion&quot; section, below.
  
There is a quick step you can perform to avoid having Metro re-clone the entire Portage tree. Perform this as root:
+
== Multi-line elements ==
  
<console>
+
Metro supports multi-line elements and they are the foundation of Metro's ''template'' engine. A multi-line element can be defined as follows, by using square brackets to delimit multi-line data:
# ##i##cd /usr/portage; git checkout origin/master
+
# ##i##install -d /var/tmp/metro/cache/cloned-repositories
+
# ##i##cat /root/git/metro/etc/builds/funtoo-current/build.conf  | grep name
+
# ##i##name: ports-2012
+
# ##i##cp -a /usr/portage /var/tmp/metro/cache/cloned-repositories/ports-2012
+
# ##i##cd /usr/portage; git checkout funtoo.org
+
</console>
+
  
== Step 1: Set up pentium4 repository (local build) ==
+
<pre>myscript: [
 +
#!/bin/bash
 +
echo $*
 +
]</pre>
 +
The terminating closing square bracket should be on a line all by itself.
  
Assuming we're following the basic steps outlined in the previous section, and building an unstable funtoo (<tt>funtoo-current</tt>) build for the <tt>pentium4</tt>, using a generic <tt>pentium4</tt> stage3 as a seed stage, then here the first set of steps we'd perform:
+
One of the very useful things about multi-line elements is that they support Metro element references:
  
<console>
+
<pre>myscript: [
# ##i##install -d "${METRO_MIRROR}/funtoo-current/x86-32bit/pentium4"
+
#!/bin/bash
# ##i##cd "${METRO_MIRROR}/funtoo-current/x86-32bit/pentium4"
+
echo Metro's path/metro setting is $[path/metro].
# ##i##install -d 2011-12-13
+
]</pre>
# ##i##cd 2011-12-13
+
In the above multi-line element, the <tt>$[path/metro]</tt> reference will be expanded to contain the appropriate value of the element. It is possible to expand single-line elements inside multi-line elements simply by referencing them using a dollar sign and square brackets.
# ##i##wget -c http://ftp.osuosl.org/pub/funtoo/funtoo-current/x86-32bit/pentium4/2011-12-13/stage3-pentium4-funtoo-current-2011-12-13.tar.xz
+
# ##i##cd ..
+
# ##i##install -d .control/version
+
# ##i##echo &quot;2011-12-13&quot; &gt; .control/version/stage3
+
# ##i##install -d .control/strategy
+
# ##i##echo &quot;local&quot; &gt; .control/strategy/build
+
# ##i##echo &quot;stage3&quot; &gt; .control/strategy/seed
+
</console>
+
  
OK, let's review the steps above. First, we create the directory <tt>"${METRO_MIRROR}/funtoo-current/x86-32bit/pentium4"</tt>, which is where Metro will expect to find unstable <tt>funtoo-current</tt> pentium4 builds -- it is configured to look here by default. Then we create a specially-named directory to house our seed x86 stage3. Again, by default, Metro expects the directory to be named this way. We enter this directory, and download our seed x86 stage3 from funtoo.org. Note that the <tt>2010-12-24</tt> version stamp matches. Make sure that your directory name matches the stage3 name too. Everything has been set up to match Metro's default filesystem layout.
+
Metro also allows you to expand multi-line elements inside other multi-line elements. Here's an example of how that works:
  
Next, we go back to the <tt>"${METRO_MIRROR}/funtoo-current/x86-32bit/pentium4"</tt> directory, and inside it, we create a <tt>.control</tt> directory. This directory and its subdirectories contain special files that Metro references to determine certain aspects of its behavior. The <tt>.control/version/stage3</tt> file is used by Metro to track the most recently-built stage3 for this particular build and subarch. Metro will automatically update this file with a new version stamp after it successfully builds a new stage3. But because Metro didn't actually ''build'' this stage3, we need to set up the <tt>.control/version/stage3</tt> file manually. This will allow Metro to find our downloaded stage3 when we set up our pentium4 build to use it as a seed. Also note that Metro will create a similar <tt>.control/version/stage1</tt> file after it successfully builds an pentium4 funtoo-current stage1.
+
<pre>myscript: [
 +
#!/bin/bash
 +
$[[steps/setup]]
 +
echo Hi There :)
 +
]</pre>
  
We also set up <tt>.control/strategy/build</tt> and <tt>.control/strategy/seed</tt> files with values of <tt>local</tt> and <tt>stage3</tt> respectively. These files define the building strategy Metro will use when we build pentium4 funtoo-current stages. With a build strategy of <tt>local</tt>, Metro will source its seed stage from funtoo-current pentium4, the current directory. And with a seed strategy of <tt>stage3</tt>, Metro will use a stage3 as a seed, and use this seed to build a new stage1, stage2 and stage3.
 
 
== Step 2: Building the pentium4 stages ==
 
 
Incidentally, if all you wanted to do at this point was to build a new pentium4 funtoo-current stage1/2/3 (plus openvz and vserver templates). You would begin the process by typing:
 
 
<console>
 
# ##i##cd /root/git/metro
 
# ##i##scripts/ezbuild.sh funtoo-current pentium4
 
</console>
 
 
If you have a slow machine, it could take several hours to be completed because several "heavy" components like gcc or glibc have to be recompiled in each stage. Once a stage has been successfully completed, it is placed in the <tt>"${METRO_MIRROR}/funtoo-current/x32-bit/pentium4/YYYY-MM-DD"</tt> subdirectory, where <tt>YYYY-MM-DD</tt> is today's date at the time the <tt>ezbuild.sh</tt> script was started or the date you put on the ezscript.sh command line.
 
 
== Step 3: The next build ==
 
 
At this point, you now have a new pentium4 stage3. If you'd like, you can reconfigure Metro to use the most recently-built pentium4 stage3 as a seed for any pentium4 builds. To do this, simply type:
 
 
<console> # ##i##echo &quot;local&quot; &gt; /home/mirror/linux/funtoo-current/x86-32bit/pentium4/.control/strategy/build</console>
 
Now, Metro will use the most recentpentium4 stage3 as a seed. The <tt>.control/remote</tt> files you created will be ignored by Metro, since it's no longer going to perform a remote build.
 
 
= Building for another binary compatible architecture (remote build) =
 
 
As written above, [[Metro]] is able to perform '''remote build''' building different architecture stage3 from a binary compatible seeding stage3 (e.g. using a pentium4 stage3 to seed a <tt>Intel Core2 32bits</tt> stage3).
 
 
In the Metro terminology this is called a '''remote build''' (a stage 3 of a different, but binary compatible, architecture is used as a seed).
 
What's not compatible? You can't use a <tt>Sparc</tt> architecture to generate an <tt>x86</tt> or <tt>ARM</tt> based stage and vice-versa. If you use a 32bit stage then you don't want to seed a 64bit build from it. Be sure that you are using a stage from the same architecture that you are trying to seed. Check [http://ftp.osuosl.org/pub/funtoo/funtoo-current/ Funtoo-current FTP Mirror] for a stage that is from the same Architecture that you will be building. 
 
 
== Step 1: Set up Core_2 32bit repository ==
 
 
In this example, we're going to use this pentium4 funtoo-current stage3 to seed a new Core_2 32bit funtoo-current build. To get that done, we need to set up the pentium4 build directory as follows:
 
 
<console>
 
# ##i## cd "${METRO_MIRROR}/funtoo-current/x86-32bit"
 
# ##i##install -d core2_32
 
# ##i##cd core2_32
 
# ##i##install -d .control/strategy
 
# ##i##echo &quot;remote&quot; &gt; .control/strategy/build
 
# ##i##echo &quot;stage3&quot; &gt; .control/strategy/seed
 
# ##i##install -d .control/remote
 
# ##i##echo &quot;funtoo-current&quot; &gt; .control/remote/build
 
# ##i##echo &quot;pentium4&quot; &gt; .control/remote/subarch
 
</console>
 
 
The steps we follow are similar to those we performed for a ''local build'' to set up our pentium4 directory for local build. However, note the differences. We didn't download a stage, because we are going to use the pentium4 stage to build a new Core_2 32bit stage. We also didn't create the <tt>.control/version/stage{1,3}</tt> files because Metro will create them for us after it successfully builds a new stage1 and stage3. We are still using a <tt>stage3</tt> seed strategy, but we've set the build strategy to <tt>remote</tt>, which means that we're going to use a seed stage that's not from this particular subdirectory. Where are we going to get it from? The <tt>.control/remote</tt> directory contains this information, and lets Metro know that it should look for its seed stage3 in the <tt>"${METRO_MIRROR}/home/mirror/linux/funtoo-current/x86-32bit/pentium4"</tt> directory. Which one will it grab? You guessed it -- the most recently built ''stage3'' (since our seed strategy was set to <tt>stage3</tt>) that has the version stamp of <tt>2010-12-24</tt>, as recorded in <tt>"${METRO_MIRROR}/funtoo-current/x86-32bit/pentium4/.control/version/stage3"</tt>. Now you can see how all those control files come together to direct Metro to do the right thing.
 
 
== Step 2: Building the Core_2 32bit stages ==
 
 
Now, you could start building your new Core_2 32bit stage1/2/3 (plus openvz and vserver templates) by typing the following:
 
 
<console>
 
# ##i##/root/git/metro/scripts/ezbuild.sh funtoo-current core2_32
 
</console>
 
 
In that case, the produced stages are placed in the <tt>"${METRO_MIRROR}/funtoo-current/x32-bit/core2_32/YYYY-MM-DD"</tt> subdirectory.
 
 
== Step 3: The Next Build ==
 
 
At this point, you now have a new Core_2 32bit stage3. If you'd like, you can reconfigure Metro to use the most recently-built Core_2 32bit stage3 as a seed for any new Core_2 32bit builds.
 
 
In the Metro terminology this is called a '''local build''' (a stage 3 of a a given architecture is used to seed a brand new build of the same architecture).
 
 
To do this, simply type:
 
 
<console>
 
# ##i##echo &quot;local&quot; &gt; /home/mirror/linux/funtoo-current/x86-32bit/core2_32/.control/strategy/build
 
</console>
 
 
Now, Metro will use the most recent Core_2 32bit stage3 as a seed. The <tt>.control/remote</tt> files you created will be ignored by Metro, since it's no longer going to perform a remote build.
 
 
= Build your own tailored stage3 =
 
 
Metro can be easily configured for building custom stage3 by including additional packages. Notice that including packages with heavy dependencies such as gnome, kde, xorg-server is not recommended (not tested so far). Well tested packages are <tt>app-misc/mc</tt>, <tt>app-misc/screen</tt>, <tt>sys-process/htop</tt>, <tt>sys-apps/dstat</tt>. An example for funtoo-current stage. Edit the following configuration file <tt>/root/git/metro/etc/builds/funtoo-current/build.conf</tt>:
 
<pre>
 
[collect ../../fslayouts/funtoo/layout.conf]
 
 
[section local]
 
 
author: Daniel Robbins <drobbins@funtoo.org>
 
 
[section target]
 
 
build: funtoo-current
 
compression: xz
 
 
[section portage]
 
 
stable: ~
 
MAKEOPTS: -j12
 
FEATURES: mini-manifest
 
SYNC: $[snapshot/source/remote]
 
USE:
 
profile: default/linux/$[target/arch:zap]/2008.0
 
 
[section emerge]
 
 
options: --jobs=4 --load-average=3 --keep-going=n
 
packages: [
 
dev-vcs/git
 
net-misc/dhcpcd
 
sys-fs/reiserfsprogs
 
net-misc/bridge-utils
 
sys-devel/bc
 
sys-apps/pciutils
 
app-portage/gentoolkit
 
        app-misc/mc
 
        app-misc/screen
 
        sys-process/htop
 
        sys-apps/dstat
 
           
 
]
 
 
[section snapshot]
 
</pre>
 
 
As you can see MAKEOPTS and emerge default options are additional strings to tweak, --keep-going=n is recommended, if something breaking during the stage building process, you can quickly diagnose the problem.
 
 
Distfiles location can be specified in <tt>/root/git/metro/etc/metro.conf</tt>
 
<pre>
 
# Main metro configuration file
 
#
 
# Path configuration:
 
 
[collect ../targets/$[metro/target]/$[target:zap].spec]
 
[collect ./builds/$[metro/build]/build.conf]
 
[collect ../subarch/$[target/subarch:zap].spec]
 
 
# General Paths
 
 
[section path]
 
tmp: /var/tmp/metro
 
distfiles: /var/portage/distfiles
 
work: $[path/tmp]/work/$[metro/build]/$[target/name]
 
 
[section path/cache]
 
</pre>
 
 
= Building Gentoo stages =
 
 
[[Category:HOWTO]]
 
 
[[Category:Metro]]
 
[[Category:Metro]]

Revision as of 16:02, 8 January 2012

Goals

The Metro Data Model has been designed to provide you with an optimal way to organize build data.

Here are the primary goals for the data model:

  1. Provide useful ways to organize data
  2. Use mechanisms and syntax that maximize maintainability of the data over time
  3. Reduce and (ideally) eliminate side-effects at every opportunity

To attain these goals, I've used a functional data model, where an element (variable) can be defined only once, and cannot be redefined.

By default, the Metro parser operates in "strict" mode, which means that it will throw an error if a variable has been referenced that has not been defined. This "strict" mode is actually very useful in catching errors that might otherwise go unnoticed and result in broken builds.

In addition, the Metro parser was designed so that the order in which data elements are defined is not important, even if they reference one another. This was done to eliminate side-effects related to data ordering, where changing the order in which things are defined in a file can change the behavior of or break your code.

Versions of Metro prior to 1.4 contained limited support for conditional logic. After some experimentation, I've decided that the conditional support is not necessary, and it is not used by Metro 1.4. However, support for conditionals still exist in the parser, but will be removed when the parser is rewritten.


First Look

Here is some sample Metro data:

path: /usr/bin

Above, we have defined the element path to have the value /usr/bin. path is a single-line element, and the Metro parser takes care of trimming any trailing whitespace that may be on the line. You can also define single-line elements that have values that consist of multiple whitespace-separated values:

options: ccache replace

Sometimes, you need to define an element but leave it blank. To do this, don't specify any values after the colon:

options:

In Metro, the / character is used to delineate various classes of elements, as follows:

path/mirror: /home/mirror/linux
path/mirror/snapshot: /home/mirror/linux/snapshots
path/metro: /usr/lib/metro

Above, we see the proper Metro convention for specifying paths. Each path has a prefix of path/. We have a path/mirror element but also have a path/mirror/snapshot element. The / is used to organize our data into logical groups. This is not enforced by Metro but is presented here as a best practice.

The data above could also be represented using a section annotation, as follows:

[section path]

mirror: /home/mirror/linux
mirror/snapshot: /home/mirror/linux/snapshots
metro: /usr/lib/metro

Above, the [section path] line is a section annotation, and it tells the Metro parser that the path/ prefix should be applied to all following data elements. A section annotation is in effect until another section annotation is encountered by the parser.

While our data above is getting more organized, there is some redundancy in our data, which generally isn't a good thing. Here's an example of how to make our data a bit more compact:

[section path]

mirror: /home/mirror/linux
mirror/snapshot: $[path/mirror]/snapshots
metro: /usr/lib/metro

Above, we have used an element reference of $[path/mirror] to reference our path/mirror element. What this means is that path/snapshot will have a value of /home/mirror/linux/snapshots.

Also, it's worth pointing out that we could just have well written:


[section path]

mirror/snapshot: $[path/mirror]/snapshots
mirror: /home/mirror/linux
metro: /usr/lib/metro

In other words, it's perfectly OK to use the element reference of $[path/mirror] on a line before the actual definition of path/mirror. Metro doesn't care about the order in which data is defined.

Metro provides another way to organize your data in an efficient way. Supposing that you had a lot of path/mirror-related data, then it might be useful to organize your data as follows:

[section path]

metro: /usr/lib/metro

[section path/mirror]

: /home/mirror/linux
snapshot: $[]/snapshot
source: $[]/$[source/subarch]/funtoo-$[source/subarch]-$[source/version]/$[source/name].tar.bz2

Above, we have used two new parser features. Inside [section path/mirror], we can define the path/mirror element itself by using a blank element name, followed by a :. The next parser feature we see above is that we can use $[] to reference the value of the path/mirror value. $[] will always reference the value of the element specified in the section annotation. Also note that as of Metro 1.1, $[:] can be used as an alternate form of $[]. In addition, as of Metro 1.2.4, $[:foo] can be used as an alternate form of $[section-name/foo].

Collect Annotations

Many scripting languages have the notion of an "include" file, or "importing" additional data from a remote file. Metro has this concept as well, but it is implemented in a somewhat different way. You can tell Metro to include data from another file by using a collect annotation.

A collect annotation looks like this:

[collect $[path/metro]/myfile.txt]

Now, we called these things "collect annotations" for a reason - in Metro, they work slightly differently than most languages implement include and import. The main difference is that in Metro, a collect annotation does not happen right away. Instead, Metro will add the file to be collected (in this case, that would be the file /usr/lib/metro/myfile.txt, or whatever $[path/metro]/myfile.txt evaluates to) to a collection queue. This means that Metro will read in the contents of the file at some point in time, and the data in the file will be available to you by the time the parsing is complete. But because Metro doesn't care about the order in which data is defined, it doesn't have the same concept of "read in the data - right now!" that an include or import statement does in other languages.

Conditional Collect Annotations

Metro no longer officially supports conditional collect annotations; however, simple collect annotations can be used to make conditional decisions in Metro, as follows:

[collect ./snapshots/$[snapshot/type]]

Above, Metro will collect from a file based on the value of the $[snapshot/type] element. This allows for varying definitions of elements to exist dependent on the value of $[snapshot/type].

Above, Metro will raise an exception if $[snapshot/type] is undefined or has a value that does not map to a file on disk. If it is possible that $[snapshot/type] may not be defined, use the following format:

[collect ./snapshots/$[snapshot/type:zap]]

Using the :zap modifier, the entire collect argument will be replaced with the empty string if $[snapshot/type] is undefined. If Metro is asked to collect an empty string, it will not throw an exception. So this is a handy way to conditionally disable collection of a file. But please note that for all non-null values of $[snapshot/type], a corresponding file must exist on disk in ./snapshots/ or Metro will throw an exception. :zap is explained in more detail in the "Special Variable Expansion" section, below.

Multi-line elements

Metro supports multi-line elements and they are the foundation of Metro's template engine. A multi-line element can be defined as follows, by using square brackets to delimit multi-line data:

myscript: [
#!/bin/bash
echo $*
]

The terminating closing square bracket should be on a line all by itself.

One of the very useful things about multi-line elements is that they support Metro element references:

myscript: [
#!/bin/bash
echo Metro's path/metro setting is $[path/metro].
]

In the above multi-line element, the $[path/metro] reference will be expanded to contain the appropriate value of the element. It is possible to expand single-line elements inside multi-line elements simply by referencing them using a dollar sign and square brackets.

Metro also allows you to expand multi-line elements inside other multi-line elements. Here's an example of how that works:

myscript: [
#!/bin/bash
$[[steps/setup]]
echo Hi There :)
]