Starting a Lab: Behind the Scenes

netlab up looks like a single command, but it orchestrates a dozen steps, from transforming your lab topology to configuring the lab devices. This tutorial walks you through those steps, describes the files netlab creates in your lab directory, and explains how the other netlab commands find the lab you started.

AI-Generated Content

This documentation was written by AI and adjusted/edited by Ivan

We’ll use this simple lab topology (saved in topology.yml) throughout the tutorial:

provider: clab
defaults.device: frr
module: [ ospf ]

nodes: [ r1, r2 ]
links: [ r1-r2 ]

Starting the Lab

Sanity Checks

Before doing anything else, netlab up checks whether a lab is already running:

  • If it finds the netlab.lock file in the current directory, it assumes another lab is running in that directory and refuses to start a new one (more details).

  • It reads the netlab status file to check whether the same lab instance is already running in another directory.

Creating the Configuration Files

netlab up then runs the netlab create process with the same CLI arguments to:

  • Read the lab topology and the project-, user-, and system defaults

  • Transform the lab topology into a detailed devices-and-links data model (that’s where the IP addresses, routing protocol parameters, and interface names get calculated)

  • Create the configuration files using the default output modules, including saving the transformed data into the netlab.snapshot.pickle file.

This is what you’ll see when you start the above lab topology:

$ netlab up
Creating configuration files
[CONFIG]  r1: initial,ospf,hosts
[CONFIG]  r2: initial,ospf,hosts (shared)
Created provider configuration file: clab.yml
Created pickled transformed topology data into netlab.snapshot.pickle
Created group_vars for all
Created group_vars for modules
Created group_vars for frr
Created host_vars for r1, r2
Created minimized Ansible inventory hosts.yml
Created Ansible configuration file: ansible.cfg

Tip

You can run netlab create on its own to inspect the generated files before starting the lab. The netlab up –snapshot command skips this step and starts the lab from the existing configuration files.

Checking the Virtualization Provider

Next, netlab up runs provider-specific checks (for example, containerlab version or vagrant plugin list) to verify that the virtualization provider is installed and usable, checks that the container images or Vagrant boxes used by the lab devices are available, and checks the software (for example, Ansible collections) needed to configure the lab devices.

Starting the Lab Devices

Once the checks are complete, netlab up:

  • Records the lab instance (with its directory) in the netlab status file and creates the netlab.lock file in the current directory

  • Executes the provider-specific preparation tasks, for example, creating the Linux bridges for multi-access links or the libvirt management network (more details)

  • Starts the lab devices with the provider-specific command (containerlab deploy for clab, vagrant up for libvirt). In a multi-provider lab, it starts the secondary provider (usually containerlab) after the primary one.

  • Configures link impairment if needed

Configuring the Lab Devices

After the lab devices have been started, netlab up runs the netlab initial process, which waits for the devices to become ready and deploys the initial device configurations, the configurations of the configuration modules (OSPF in our example), and the custom configuration templates.

You can skip this step with the --no-config option, or use saved device configurations with the --reload-config option (more details).

Wrapping Up

Finally, netlab up starts the external tools defined in the lab topology, changes the lab status to started, and displays the lab topology message (if there is one). Use the --validate option to run the lab validation tests after the lab has started.

Tip

  • You can execute your own commands during the netlab up (and netlab down) process with the CLI hooks.

  • Use the netlab up –dry-run command to display the commands that would be executed without starting the lab.

Files Created in the Lab Directory

After starting the above lab topology in the /home/user/labs/ospf directory, you’ll find these files in the lab directory:

$ ls
ansible.cfg  clab-ospf  clab.yml  group_vars  host_vars  hosts.yml
netlab.lock  netlab.snapshot.pickle  node_files  topology.yml

File or directory

Created by

Contents and usage

netlab.snapshot.pickle

netlab create

The transformed lab topology. Commands that need the transformed topology data use it to get information about the running lab.

clab.yml or Vagrantfile

netlab create

Virtualization provider configuration file used to start and stop the lab devices. Multi-provider labs also have clab-augment.yml.

node_files

netlab create

Device configuration snippets deployed by netlab initial, and files mapped into containers

hosts.yml, ansible.cfg, host_vars, group_vars

netlab create

Ansible inventory used by netlab initial and other commands that use Ansible

Tool directories (for example, graphite)

netlab create

Configuration files of the external tools

netlab.lock

netlab up

A marker that a lab is running in this directory

config

netlab collect

Device configurations collected from the running lab

Virtualization providers create their own files as well, for example, the containerlab lab directory (clab-ospf in our example; the lab name is derived from the name of the directory containing the lab topology) or the Vagrant .vagrant directory.

The netlab.lock file prevents subsequent netlab create commands from overwriting the configuration files of the running lab[1].

netlab also keeps a few files outside of the lab directory:

  • The lab status file (default: ~/.netlab/status.yaml) that tracks the running lab instances

  • The usage statistics file (default: ~/.netlab/stats.json)

Stopping the Lab

netlab down does not use your lab topology. It reads the transformed lab topology from the netlab.snapshot.pickle file, so it stops the lab you started even if you changed the lab topology in the meantime. Then it:

  • Checks the netlab status file to verify that the lab instance is running in the current directory[2] and asks you whether to proceed if it’s not.

  • Stops the external tools

  • Stops the lab devices with the provider-specific command (containerlab destroy or vagrant destroy), starting with the secondary providers

  • Removes the lab instance from the netlab status file and removes the netlab.lock file

netlab down leaves the configuration files in the lab directory, so you can use them to troubleshoot a failed lab or restart the lab with netlab up –snapshot. Use the --cleanup option to remove them.

Warning

netlab down –cleanup removes all files netlab created in the lab directory, including the config directory with the device configurations you collected with netlab collect. Use the --output option of the ‌netlab collect command to store the device configurations in another (sub)directory.

Tip

Use the netlab restart command to shut down the lab and start it again from the same lab topology (for example, after changing the lab topology).

How Do netlab Commands Find the Running Lab?

The operational netlab commands (for example, netlab connect, netlab initial, or netlab validate) do not read your lab topology; they need the data about the running lab, including the IP addresses netlab assigned to the lab devices. They read that data from the netlab.snapshot.pickle file in the current directory and fail when they cannot find it:

$ netlab connect r1
[FATAL]   connect: The topology snapshot file netlab.snapshot.pickle does not exist
[HINT]    Looks like no lab was started from this directory
          Use 'netlab status --all' to display labs running on this system

You’ll usually get this error when you execute a netlab command in a directory other than the one in which you started the lab, or after removing the snapshot file with netlab down –cleanup.

Tip

  • netlab warns you when you change the lab topology after starting the lab. The operational commands keep using the snapshot of the lab topology taken when you started the lab; use netlab restart to apply the changes.

  • You can use the --instance parameter to specify the lab instance the current command should work on.

The netlab Status File

netlab up records every lab it starts in the netlab status file (default: ~/.netlab/status.yaml[3]). Each lab instance has an identifier (default unless you use the multilab plugin), and the status file records its directory, its status, and a log of the lab lifecycle events. netlab down removes the lab instance from the status file.

Displaying the Running Labs

The netlab status command uses the status file instead of the snapshot file:

  • netlab status displays the lab instance started in the current directory, its devices, and their state. It reports an error if there’s no lab running in the current directory.

  • netlab status –all displays all lab instances running on your server and their directories:

$ netlab status --all
Active lab instance(s)

┏━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━┓
┃ id      ┃ directory            ┃ status  ┃ providers ┃
┡━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━┩
│ default │ /home/user/labs/ospf │ started │ clab      │
└─────────┴──────────────────────┴─────────┴───────────┘

Working with a Lab Started in Another Directory

You could change the current directory to the lab directory displayed by netlab status –all. Alternatively, use the --instance option of the operational commands. netlab finds the lab instance in the status file, changes the current directory to the lab directory, and executes the command there:

$ netlab connect --instance default r1
[CHANGED] Selected instance default, current directory changed to /home/user/labs/ospf

You can use the --instance option (or its -i shortcut) with most operational commands, including netlab down, netlab status, netlab config, netlab connect, netlab capture, and netlab validate. netlab initial and netlab tc accept only the long --instance option because they use -i for something else.

The reporting commands (netlab graph, netlab inspect, and netlab report) can use a running lab instance, the snapshot file in the current directory, or a lab topology as their data source (more details).

Tip

Want to run more than one lab on the same server? Read Starting Multiple Lab Instances.