(tutorial-lab-start)= # Starting a Lab: Behind the Scenes **[netlab up](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. :::{admonition} AI-Generated Content :class: note This documentation was written by AI and adjusted/edited by Ivan ::: ```eval_rst .. contents:: Table of Contents :depth: 2 :local: :backlinks: none ``` 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](tutorial-lab-files)). * It reads the [_netlab_ status file](tutorial-find-lab) to check whether the same [lab instance](tutorial-multiple-labs) is already running in another directory. ### Creating the Configuration Files **netlab up** then runs the **[netlab create](netlab-create)** process with the same CLI arguments to: * Read the lab topology and the [project-, user-, and system defaults](defaults-locations) * 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](output-formats), 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](netlab-up-restart)** 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](providers) 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](tutorial-find-lab) 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](netlab-up-provider)) * Starts the lab devices with the provider-specific command (`containerlab deploy` for *clab*, `vagrant up` for *libvirt*). In a [multi-provider lab](labs-multi-provider), it starts the secondary provider (usually *containerlab*) after the primary one. * Configures [link impairment](links-netem) if needed ### Configuring the Lab Devices After the lab devices have been started, **netlab up** runs the **[netlab initial](netlab-initial)** process, which waits for the devices to become ready and deploys the initial device configurations, the configurations of the [configuration modules](topo-modules) (OSPF in our example), and the [custom configuration templates](custom-config). You can skip this step with the `--no-config` option, or use saved device configurations with the `--reload-config` option ([more details](netlab-up-reload)). ### Wrapping Up Finally, **netlab up** starts the [external tools](tools-intro) 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](netlab-validate) after the lab has started. ```{tip} * You can execute your own commands during the **netlab up** (and **netlab down**) process with the [CLI hooks](dev-cli-hooks). * Use the **netlab up --dry-run** command to display the commands that would be executed without starting the lab. ``` (tutorial-lab-files)= ## 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 ``` {.table-wrap} | File or directory | Created by | Contents and usage | |-------------------|------------|--------------------| | `netlab.snapshot.pickle` | **netlab create** | The transformed lab topology. [Commands that need the transformed topology data](tutorial-find-lab) use it to get information about the running lab. | | `clab.yml` or `Vagrantfile` | **netlab create** | [Virtualization provider](providers) 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](outputs-ansible) used by **netlab initial** and other commands that use Ansible | | Tool directories (for example, `graphite`) | **netlab create** | Configuration files of the [external tools](tools-intro) | | `netlab.lock` | **netlab up** | A marker that a lab is running in this directory | | `config` | **[netlab collect](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[^LOCK]. [^LOCK]: If you're sure there's no lab running in the current directory, you can remove the `netlab.lock` file manually, or use **netlab down --cleanup --force** to cleanup the directory. _netlab_ also keeps a few files outside of the lab directory: * The [lab status file](tutorial-find-lab) (default: `~/.netlab/status.yaml`) that tracks the running lab instances * The usage statistics file (default: `~/.netlab/stats.json`) ## Stopping the Lab **[netlab down](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](tutorial-find-lab) to verify that the lab instance is running in the current directory[^NCSN] 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 [^NCSN]: The `netlab.snapshot.pickle` file could have been created by a **netlab create** command without you ever starting the lab in that directory. **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](netlab-restart)** command to shut down the lab and start it again from the same lab topology (for example, after changing the lab topology). ``` (tutorial-find-lab)= ## How Do netlab Commands Find the Running Lab? The operational _netlab_ commands (for example, **[netlab connect](netlab-connect)**, **[netlab initial](netlab-initial)**, or **[netlab validate](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](netlab-restart)** to apply the changes. * You can use the `--instance` parameter to specify the [lab instance](tutorial-multiple-labs) the current command [should work on](tutorial-lab-another-dir). ``` ### The netlab Status File **netlab up** records every lab it starts in the _netlab_ status file (default: `~/.netlab/status.yaml`[^LSF]). Each [lab instance](tutorial-multiple-labs) has an identifier (*default* unless you use the [*multilab* plugin](plugin-multilab)), 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. [^LSF]: You can change the location of the status file with the **defaults.lab_status_file** setting in the [user or system defaults](defaults-locations). ### Displaying the Running Labs The **[netlab status](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 │ └─────────┴──────────────────────┴─────────┴───────────┘ ``` * Like most other operational commands, **netlab status** accepts the `--instance` parameter that [specifies the lab instance you're interested in](tutorial-lab-another-dir). (tutorial-lab-another-dir)= ### 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-graph)**, **[netlab inspect](netlab-inspect)**, and **[netlab report](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](netlab-report-data-source)). ```{tip} Want to run more than one lab on the same server? Read [](tutorial-multiple-labs). ```