Bootstrapping Linux Distributions
Tags: linux, devops, programming, tutorial
Published: 2022-04-19 11:30:20

We're going to use Linux OS trees and images throughout this guide. 

First, it's important to understand that you don't need to use the standard distribution installer to install Linux on your disk.  

Typically, when you're using the Linux distribution installer, once you're done with setting up your partitions, the installer will mount partitions and start the process of "bootstrapping" OS tree into the directory where your root has been partition mounted.

Once you understand how this works, you can have much greater freedom and control over your installation process. If you ever tried to install distribution like Gentoo and Arch, this will feel familiar.

So, the basic idea is to create a base OS tree of your distribution in some directory on your filesystem. We can then execute the commands inside that tree, which will see that directory as a root directory. That tree is often called the "chroot tree" (and the basic command for working with them is called `chroot`).

Besides doing full distribution installations, these chroot trees are also useful for more lightweight usage, i.e. various types of containers.

Here are some quick examples of bootstrapping various Linux distributions:

## Gentoo
This concept of "bootstrapping" is very natural for Gentoo, and in fact, it's the official installation procedure. 

You can just download the stage3 tar archive (see https://www.gentoo.org/downloads/), and unpack it:
```
wget https://bouncer.gentoo.org/fetch/root/all/releases/amd64/autobuilds/20220412T191925Z/stage3-amd64-openrc-20220412T191925Z.tar.xz
tar xzvf stage3-amd64-openrc-20220412T191925Z.tar.xz
```

## Arch Linux

Arch can be bootstrapped in a similar way, you can download and extract archlinux-bootstrap tarball (see https://archlinux.org/download/) from the official mirrors (and go from there):
```
wget https://mirror.fcix.net/archlinux/iso/2022.04.05/archlinux-bootstrap-2022.04.05-x86_64.tar.gz
tar xzvf archlinux-bootstrap-2022.04.05-x86_64.tar.gz
```

Alternatively, you can install arch-install-scripts (either from your distro repos or manually) and use pacstrap:
```
pacstrap -d /target_dir base
```

Or, if you already have pacman:
```
pacman -r /target_dir -Syy
```

## Debian based distributions

Debian based distros also have a great support for bootstrapping through the offical tool called `debootstrap`.

i.e. to bootstrap minbase variant of Debian 11 (bullseye), you can do something like this:
```
debootstrap --variant=minbase bullseye /target_dir
```

`debotstrap` is just a shell script with common dependencies (it depends only on `/bin/sh`, `wget`, `ar` and basic Linux/Unix tools), so even if you don't have it in your distro repos, it's very easy to install it.

## Alpine Linux

To bootstrap Alpine Linux from other distributions, you can use statically compiled apk tool (which you can can download from here: http://dl-cdn.alpinelinux.org/alpine/): 

```
wget http://dl-cdn.alpinelinux.org/alpine/v3.15/main/x86_64/apk-tools-static-2.12.7-r3.apk
tar zxf apk-tools-static-*.apk
```

And then run something like this (as root):
```
./sbin/apk.static --arch $(arch) -X http://dl-cdn.alpinelinux.org/alpine/latest-stable/main/ -U --allow-untrusted --root /target_dir --initdb add alpine-base
```

## rpm based distros

rpm based distros are the worst candidate for bootstrapping, but it's doable.

In the past, there was a similar tool for rpm based distributions: `febootstrap`, but it has mutated into a new tool called `supermin`. You can try to use that, or use dnf/yum from the the existing rpm-based distribution, i.e. something like this:
```
dnf --releasever=30 --installroot=/target_dir groupinstall core
```

## Next steps

Once you have that base tree, you should be able to chroot into that directory (as root):
```
chroot /target_dir
```

If everything went well, you should get a root shell prompt from your target distro.

In the next pages, we're going to see why this is important and where we can go from here. I'm going to use Debian as an example.

---
<small>Note: this is a snapshot of a (WIP) topic from the Understanding Simplicity wiki.<br>You can read the latest (better formatted) version here: [Bootstrapping Linux Distributions](http://devsimplicity.com/bootstrapping-linux-distributions)
<br>All suggestions (and reactions) are welcome.</small>