DEV Community

Cover image for Mount Namespace Practice Guide
Ajinkya Singh
Ajinkya Singh

Posted on

Mount Namespace Practice Guide

πŸš€ I'm Building My Own Container Runtime!

This is part of a complete series where I'm building Conti - a container runtime from scratch. Check it out on GitHub!

About This Series:

  • I'm sharing everything I learn while building my own container runtime
  • Most concepts come from videos, documentation, and LLM-assisted learning (for educational purposes)
  • Focus: Understanding through practice - raw Linux commands and practical implementation
  • Important: When building your own container, DON'T copy code from sources - it kills the fun! Write it yourself, break things, debug, and learn.

Why Build Your Own?

  • Deep understanding of how containers really work
  • Master low-level Linux concepts
  • Learn by doing, not just reading
  • It's incredibly fun when things finally click!

🎯 Overview

Practice creating isolated filesystem views using mount namespaces with a simple bookstore/cafe scenario.


πŸ” What is Pivot Root?

Pivot root changes the root filesystem (/) for a process and its children, providing complete filesystem isolation.

How It Works

BEFORE PIVOT ROOT:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  System Root (/)        β”‚
β”‚  β”œβ”€β”€ home/              β”‚
β”‚  β”œβ”€β”€ var/               β”‚
β”‚  β”œβ”€β”€ bookstore/         β”‚ ← Target directory
β”‚  β”‚   β”œβ”€β”€ books/         β”‚
β”‚  β”‚   β”œβ”€β”€ checkout/      β”‚
β”‚  β”‚   └── old_root/      β”‚ (empty)
β”‚  └── cafe/              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

AFTER PIVOT ROOT (from bookstore):
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  New Root (/)           β”‚
β”‚  β”œβ”€β”€ books/             β”‚ ← Bookstore content
β”‚  β”œβ”€β”€ checkout/          β”‚
β”‚  └── old_root/          β”‚ ← Old system root moved here
β”‚      β”œβ”€β”€ home/          β”‚
β”‚      β”œβ”€β”€ var/           β”‚
β”‚      └── cafe/          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

Key Requirements

βœ“ Target directory must be a mount point

βœ“ Needs a directory to move old root (e.g., old_root/)

βœ“ Must be run in a mount namespace (or as init process)


πŸ“‹ Quick Setup Exercise

Scenario

Create two isolated environments:

  • Bookstore zone with its own resources
  • Cafe zone with its own resources

πŸš€ Practice Steps

1️⃣ Initial Setup

# Create directory structures
mkdir -p bookstore/{books,checkout,lounge,old_root}
mkdir -p cafe/{menu,kitchen,seating,old_root}

# Add sample files
echo "Fiction novels" > bookstore/books/inventory.txt
echo "Coffee menu" > cafe/menu/drinks.txt
Enter fullscreen mode Exit fullscreen mode

2️⃣ Create First Namespace (Bookstore)

# Open terminal 1
sudo unshare -m -p --mount-proc /bin/bash

# Make it a mount point
sudo mount --bind bookstore bookstore

# Change root
cd bookstore
sudo pivot_root . old_root

# Verify isolation
ls /  # Should only show: books, checkout, lounge, old_root
Enter fullscreen mode Exit fullscreen mode

3️⃣ Create Second Namespace (Cafe)

# Open terminal 2
sudo unshare -m -p --mount-proc /bin/bash

# Make it a mount point
sudo mount --bind cafe cafe

# Change root
cd cafe
sudo pivot_root . old_root

# Verify isolation
ls /  # Should only show: menu, kitchen, seating, old_root
Enter fullscreen mode Exit fullscreen mode

4. Test Isolation

# In bookstore namespace
cd /
cat books/inventory.txt  # βœ“ Works
cat menu/drinks.txt      # βœ— Doesn't exist

# In cafe namespace
cd /
cat menu/drinks.txt      # βœ“ Works
cat books/inventory.txt  # βœ— Doesn't exist
Enter fullscreen mode Exit fullscreen mode

Key Commands

Command Purpose
unshare -m -p --mount-proc /bin/bash Create isolated namespace
mount --bind dir dir Make directory a mount point
pivot_root . old_root Change root directory
lsns -t mnt List mount namespaces

Quick Verification

# Check you're in a namespace
lsns -t mnt -t pid

# Verify mount point
df -a | grep bookstore

# Check current root
pwd  # Should show /
Enter fullscreen mode Exit fullscreen mode

Top comments (0)