Skip to content

tutorial

parke edited this page Jan 4, 2022 · 32 revisions

Lxroot tutorials


Tutorial 1 - Install Lxroot

Introduction

  • In Tutorial 1, we install Lxroot by running the below commands.
  • Root access is not required.

Commands

$  wget    -O lxroot.zip  https://github.com/parke/lxroot/archive/refs/heads/master.zip  
$  unzip   lxroot.zip
$  make    -C lxroot-master  test
$  export  PATH="$PATH:$PWD/lxroot-master/bin"
$  which   lxroot
$  lxroot  --help

Commentary

  • Line 1 downloads the Lxroot source code as a zipfile.
  • Line 2 unzips the source code.
  • Line 3 builds and tests the lxroot command.
  • Line 4 adds lxroot to your $PATH. (Alternatively, you may simply copy lxroot into your $PATH.)
  • Line 5 verifies that lxroot is in your $PATH.
  • Line 6 verifies that lxroot can run.

Tutorial 2 - Manually create an Alpine Linux guest userland

Introduction

  • Tutorial 2 continues from the end of tutorial 1.
  • In tutorial 2, we manually create an Alpine Linux guest userland.
  • A guest userland is simply a directory that contains the userland's files.
  • Tutorial 2 assumes your machine is x86_64. (If not, adjust accordingly.)

Commands

$  wget    https://dl-cdn.alpinelinux.org/alpine/v3.15/releases/x86_64/alpine-minirootfs-3.15.0-x86_64.tar.gz
$  mkdir   guest
$  tar     xzf  alpine-minirootfs-3.15.0-x86_64.tar.gz  -C ./guest/
$  ls      ./guest/
$  cp      -i  /etc/resolv.conf  ./guest/etc/
$  mkdir   -p  ./guest/$HOME

Commentary

  • Line 1 downloads an Alpine Linux minirootfs tarball.
  • Line 2 creates the ./guest/ directory that will contain the guest userland.
  • Line 3 extracts the minirootfs tarball into the guest directory.
  • Line 4 shows the contents of the guest directory.
  • Line 5 copies /etc/resolv.conf into the guest directory.
  • Line 6 makes a home directory in the guest directory.

Tutorial 3 - Use Lxroot to enter the guest

Introduction

  • Tutorial 3 continues from the end of tutorial 2.
  • First, we run some basic commands that display information about host environment.
  • Then, we use the lxroot command to enter the guest.
  • Then, we run some basic commands in the guest that display information about the guest environment.
  • Then, we create the file /foo in the guest environment.
  • Then, we exit the guest.
  • Finally, we remove the ./guest/foo file from the host environment.

Commands

$  pwd
$  id
$  cat      /etc/issue
$  uname    -a
$  touch    /foo
$  lxroot   -r  ./guest/
  #  pwd
  #  id
  #  cat    /etc/issue
  #  uname  -a
  #  ps     aux
  #  touch  /foo
  #  exit
$  ls       -l  ./guest/foo
$  rm       ./guest/foo

Commentary

  • Line 1 (pwd) displays the current working directory on the host.
  • Line 2 (id) displays the current process uid/gid on the host.
  • Line 3 (cat /etc/issue) displays the contents of /etc/issue on the host.
  • Line 4 displays kernel information on the host.
  • Line 5 attempts to touch /foo. This is expected to fail.
  • Line 6 runs lxroot to enter the guest.
  • The -r on line 6 tells lxroot to simulate uid = 0 (root).
    • Line 7 (pwd) displays the current working directory in the guest.
    • Line 8 (id) displays the current (possibly simulated) process uid/gid in the guest.
    • Line 9 displays the contents of /etc/issue in the guest.
    • Line 10 (uname -a) displays information about the kernel in the guest. The guest is running directly on the host's kernel, so the output of line 10 should be the same as the output of line 4.
    • Line 11 (ps aux) displays all the processes running in the guest. Specifically, these processes are running in the guest's process namespace.
    • Line 12 (touch /foo) attempts to create or modify the file /foo in the guest.
    • Line 13 (exit) exits the interactive shell that is running in the guest.
  • Line 14 runs ls -l ./guest/foo on the host. This will display the file that was created on line 12.
  • Line 15 (rm ./guest/foo) will remove the file that was created on line 12 (touch /foo).

Tutorial 4 - Build Lxroot inside the guest

Introduction

  • Tutorial 4 continues from the end of tutorial 3.

Commands

$  lxroot   -nr  ./guest/  --  apk  update
$  lxroot   -nr  ./guest/  --  apk  add  build-base
$  lxroot   -n   ./guest/
  $  wget   -O lxroot.zip  https://github.com/parke/lxroot/archive/refs/heads/master.zip  
  $  unzip  lxroot.zip
  $  make   -C lxroot-master  unit
  $  exit

Commentary

  • Line 1 runs apk update inside the guest as simulated root.
  • The -n option grants network access inside the guest.
  • The -r option simulates uid = 0 (root) inside the guest.
  • Line 2 installs basic build tools in the guest.
  • Line 3 runs an interactive shell inside the guest (without simulating uid = 0).
  • Lines 4-7 are run inside the guest.
    • Line 4 downloads the Lxroot source code.
    • Line 5 extracts the Lxroot source code.
    • Line 6 builds and tests Lxroot.
    • Line 7 exits the guest.

Tutorial 5 - Nest a second guest inside the first guest

Introduction

  • Tutorial 5 continues from the end of tutorial 5.
  • Tutorial 5 nests a second guest inside the first guest.
  • Inside the nested guest, we will create the file hello.txt.

Commands

$  lxroot    -n  ./guest/
  $  export  PATH="$PATH:$PWD/lxroot-master/bin"
  $  wget    https://dl-cdn.alpinelinux.org/alpine/v3.15/releases/x86_64/alpine-minirootfs-3.15.0-x86_64.tar.gz
  $  mkdir   nested_guest
  $  tar     xzf  alpine-minirootfs-3.15.0-x86_64.tar.gz  -C ./nested_guest/
  $  lxroot  -n  ./nested_guest/
    $  echo 'Hello, nested_guest!' > hello.txt
    $  exit
  $  cat  ./nested_guest/$HOME/hello.txt
  $  exit
$  cat  ./guest/$HOME/nested_guest/$HOME/hello.txt

Commentary

  • Line 1 shows the process uid/gid on the host.
  • Line 2 enters the guest userland.
  • Line 3 adds lxroot to your PATH inside the guest.
  • Line 4 downloads the Apline minirootfs tarball.
  • Line 5 extracts the tarball.
  • Line 6 renames the extracted directory to nested_guest for convenience.
  • Line 7 shows the process uid/gid in the guest.
  • Line 8 runs an interactive shell in the nested guest.
  • Line 9 shows the process uid/gid in the nested guest.
  • Line 10 runs apk update in the nested guest.
  • Line 11 exits the nested guest.
  • Line 12 exits the guest.

See also: the vland tutorials

  • vland is a high-level convenience wrapper around Lxroot.
  • Therefore, you may also wish to read the vland tutorials.
Clone this wiki locally