Skip to content

Instantly share code, notes, and snippets.

@jbweber
Created August 3, 2018 02:16
Show Gist options
  • Save jbweber/58beefab934ca3d00063b99282b64490 to your computer and use it in GitHub Desktop.
Save jbweber/58beefab934ca3d00063b99282b64490 to your computer and use it in GitHub Desktop.

root file system

For our container we want a root filesystem so we'll extract one from the busybox docker container.

We will then extract it into a folder (as root to preserve permissions).

$ id=$(docker run -d busybox /bin/true)
$ docker export $id > busyboxfs.tar
$ docker rm $id
$ mkdir busyboxfs
$ sudo tar -xf busyboxfs.tar -C busyboxfs

create our container process

unshare

First well drop ourselves into namespaces unshared from parent with the unshare command.

$ sudo unshare -fmuip

This will fork our process as a child process and create a mount, uts, ipc, and process namespace.

pivot_root

Next we'll pivot_root to our busyboxfs

mkdir -p $(pwd)/busyboxfs/old-root
mkdir -p /busyboxfs
mount --bind $(pwd)/busyboxfs /busyboxfs
pivot_root /busyboxfs /busyboxfs/old-root
mount -t proc proc /proc
cd /
umount -l /old-root
exec /bin/sh

This switches our root filesystem to the busyboxfs and then removes any access to the old rootfs. This also switches our init pid to the /bin/sh process making it pid 1

$ ps -ef
PID   USER     TIME  COMMAND
    1 root      0:00 /bin/sh
   55 root      0:00 ps -ef

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment