Alpine Linux in a chroot: Difference between revisions

From Alpine Linux
m (Changed mirror link. Old one was infini-spin for me. YMMV.)
 
(56 intermediate revisions by 20 users not shown)
Line 1: Line 1:
This document explains how to set up an Alpine build environment in a chroot under a "normal" Linux distro, such as Arch, Debian, Fedora, Gentoo, or Ubuntu. Once inside the chroot environment, you can build, debug, and run alpine packages.
{{TOC right}}
 
Inside the chroot environment, you can build, debug, and run Alpine packages or develop things. It's the most known way to do so without replacing your system or using a Virtual Machine.
This document explains how to set up an Alpine build environment in a chroot under a host Linux distro, that can also be used to install Alpine Linux from a non-Alpine Linux system or live environment.
 
{{Tip|You can use the [https://github.com/alpinelinux/alpine-chroot-install/ alpine-chroot-install] script to do all these steps for you.}}


== Requirements ==
== Requirements ==
You will need a few gigabytes of free disk space to have enough space for kernel compiling and storing all the binary packages.


== Creating the installation ==
* Working Linux instalation where to perform all the process
This example installation of Alpine Linux in a chroot will work with the lastest release. But it's also possible to make a chroot with '''[[Edge|edge]]''' or older releases of Alpine Linux to test backports.
* Linux kernel 2.6.22, with <code>curl</code> and <code>chroot</code> binary installed
* target media with at least 100M, 900MB for more complete solution as minimum
* internet connection
 
== Prerequisites ==
 
The variables below:
 
*'''${chroot_dir}''' = Should point to the new root directory
*'''${mirror}''' = Should be replaced with [https://dl-cdn.alpinelinux.org/alpine/MIRRORS.txt one of the available Alpine Linux mirrors].
*'''${arch}''' =  Should be the cpu architecture like x86 (i386) or amd64(x86_64)..
 
== Set up APK ==
 
Download the latest apk static package (replace <tt>${version}</tt> with actual version):
 
{{Cmd|curl -LO ${mirror}/latest-stable/main/${arch}/apk-tools-static-${version}.apk}}
 
.apk packages are just gzipped tarballs, you can unpack them using:
{{Cmd|tar -xzf apk-tools-static-*.apk}}
 
== Install the alpine base installation onto the chroot ==
 
{{Cmd|./sbin/apk.static -X ${mirror}/latest-stable/main -U --allow-untrusted -p ${chroot_dir} --initdb add alpine-base}}
 
== Seting up the chroot ==
 
Before you change root to the new directory, you need to create the required devices
 
===== Method 1.a Simple way: Using the host's /dev =====
 
{{Cmd|mount -o bind /dev ${chroot_dir}/dev}}
 
{{Note|Bind mounts can be made read-only which would limit the chroot from writing to the devices}}
 
===== Method 1.b Manual way: Creating needed nodes =====
 
{{Warning|Manually creating devices will only provide the ones that have been created}}
 
{{Cmd|mknod -m 666 ${chroot_dir}/dev/full c 1 7
mknod -m 666 ${chroot_dir}/dev/ptmx c 5 2
mknod -m 644 ${chroot_dir}/dev/random c 1 8
mknod -m 644 ${chroot_dir}/dev/urandom c 1 9
mknod -m 666 ${chroot_dir}/dev/zero c 1 5
mknod -m 666 ${chroot_dir}/dev/tty c 5 0}}
 
If you need SCSI disc access you can create the device nodes like this:
 
{{Note|Every device can have 15 sub-nodes, you should always increment by 16 for every new device}}
 
{{Cmd|mknod -m 666 ${chroot_dir}/dev/sda b 8 0
mknod -m 666 ${chroot_dir}/dev/sda1 b 8 1
mknod -m 666 ${chroot_dir}/dev/sda2 b 8 2
mknod -m 666 ${chroot_dir}/dev/sda3 b 8 3
mknod -m 666 ${chroot_dir}/dev/sdb b 8 16
mknod -m 666 ${chroot_dir}/dev/sdb1 b 8 17}}
 
==== Make the process fs and /sys fs available ====
 
{{Cmd|mount -t proc none ${chroot_dir}/proc
mount -o bind /sys ${chroot_dir}/sys}}
 
==== Set up name resolution ====
 
A resolv.conf is needed for name resolution:
 
You can either copy your host's resolv.conf:
{{Cmd|cp -L /etc/resolv.conf ${chroot_dir}/etc/}}
 
or instead you can create a new one (this example uses OpenDNS):
{{Cmd|echo -e 'nameserver 8.8.8.8\nnameserver 2620:0:ccc::2' > ${chroot_dir}/etc/resolv.conf}}
 
==== Prepare the APK repositories ====
 
Set up APK main repository (replace <tt>${branch}</tt> with the latest stable branch name, e.g. v3.3):


'''Note:''' The variables below:
{{Cmd|mkdir -p ${chroot_dir}/etc/apk
echo "${mirror}/${branch}/main" > ${chroot_dir}/etc/apk/repositories}}


*'''${build_dir}''' = You can name it whatever you like.  
{{Warning|The chroot methods are commonly used to have Alpine installed in a existing system. Because of this, the steps for preparing a bootloader are not included.}}
*'''${mirror}''' = Should be replaced with one of the available Alpine Linux mirrors.


Choose a mirror from the [http://dl-2.alpinelinux.org/alpine/MIRRORS.txt mirror list].
==== Entering your chroot ====


Let's start by getting the latest apk static package:
You then can enter your chroot by running this command as the root user (UID 0).


{{Tip|In the command below, replace x86_64 with x86 if running on a 32 bit installation}}
{{Cmd|chroot ${chroot_dir} /bin/ash -l}}


{{Warning|You will need Kernel version 2.6.22 or later to use apk-tools-static}}
==== Preparing init services ====


{{Cmd|wget http://dl-3.alpinelinux.org/alpine/v2.4/main/x86_64/apk-tools-2.3.3-r0.apk}}
If you plan to use your chroot with a init system or setup a new system on another device you should add these services:


Unpack the tarball
{{Cmd|rc-update add devfs sysinit
{{Cmd|tar -xzf apk-tools-static-2.3.3-r0.apk}}
rc-update add dmesg sysinit
rc-update add mdev sysinit


We are setting up a basic chroot:
rc-update add hwclock boot
rc-update add modules boot
rc-update add sysctl boot
rc-update add hostname boot
rc-update add bootmisc boot
rc-update add syslog boot


{{Cmd|mkdir ${build_dir}
rc-update add mount-ro shutdown
sudo ./sbin/apk.static -X ${mirror}/v2.4/main -U --allow-untrusted --root ${build_dir} --initdb add alpine-base alpine-sdk
rc-update add killprocs shutdown
mkdir -p ./${build_dir}/proc}}
rc-update add savecache shutdown}}


Lets setup our needed devices:  
<!-- TODO: Create a page dedicated to installing GRUB and Syslinux and reference them here
==== Adding a bootloader ====


{{Cmd|sudo mknod -m 666 ./${build_dir}/dev/full c 1 7
[[GRUB]]
sudo mknod -m 666 ./${build_dir}/dev/ptmx c 5 2
sudo mknod -m 644 ./${build_dir}/dev/random c 1 8
sudo mknod -m 644 ./${build_dir}/dev/urandom c 1 9
sudo mknod -m 666 ./${build_dir}/dev/zero c 1 5
sudo mknod -m 666 ./${build_dir}/dev/tty c 5 0}}
seems as /dev/null is wrong


{{Cmd|sudo rm -f ./${build_dir}/dev/null && sudo mknod -m 666 ./${build_dir}/dev/null c 1 3}}
{{Warning|Run this only if Alpine was installed to a dedicated partiton mounted at the <nowiki>${chroot_dir}</nowiki> directory.}}


We need a resolv.conf is needed for the DNS servers and the /root directory:
{{Cmd|<nowiki>apk add syslinux
dd if=/usr/share/syslinux/mbr.bin of=/dev/sda</nowiki>}}


{{Cmd|sudo cp /etc/resolv.conf ./${build_dir}/etc/
{{Warning|Make sure that the device you give the <code>dd</code> utility is the one you want to install the bootloader to. This process cannot be reverted!}}
mkdir -p ./${build_dir}/root}}
-->
= Troubleshooting =


If you don't want to copy the resolv.conf from the local machine, create this file with your DNS server entry.
== Hardened kernels or alpine as chroot host ==
{{Cmd|echo 'nameserver 8.8.8.8' >/etc/resolv.conf}}


We are setting up APK mirrors:
If you are using Alpine as a native build system you will have to make sure that you can run <code>chmod</code> from a chroot. Add the following to <code>/etc/sysctl.conf</code>


{{Cmd|sudo mkdir -p ./${build_dir}/etc/apk
<code>kernel.grsecurity.chroot_deny_chmod = 0</code>
echo "${mirror}/v2.3/main" > ./${build_dir}/etc/apk/repositories}}


== Entering your chroot ==
Then reload the sysctl configuration
After the installation you should be able to enter your chroot:


<!-- Don't we need to mount more, as in Arch instructions? Also should explain how to cleanly exit the chroot. -->
<code>sysctl -p</code>


{{Cmd|sudo mount --bind /proc ./${build_dir}/proc
== chroot: cannot run command ' ... Exec format error ==
sudo chroot ./${build_dir} /bin/sh -l}}


If you are using Alpine as a Native build system you will have to make sure that chroot can run chmod. Add following to /etc/sysctl.conf
This usually indicates that you booted with one architecture (e.g. armf) and are trying to chroot into another (e.g. x86_64). The binaries must be built for the architecture that the host runs!


kernel.grsecurity.chroot_deny_chmod = 0
Note that with '''one exception you can run 32 bit x86 chroot in x86_64, but not viceversa'''!


Then run the following command
== WARNING: Ignoring APKINDEX.xxxx.tar.gz ==


{{Cmd|sysctl -p}}
Make sure <code>${chroot_dir}/etc/apk/repositories</code> is valid and run:


== Alpine Linux in a chroot on Fedora ==
<code>apk update</code>


If you want to generate a chroot on a Fedora based system, you can use this [http://git.alpinelinux.org/cgit/fab/scripts/tree/alpine-chroot.sh script].
= External links =


{{Note|Maybe you are able to use this script on other distribution but this is not tested.}}
* You can also use script [https://github.com/alpinelinux/alpine-chroot-install/ alpine-chroot-install]
* https://web.archive.org/web/20190808203313/https://isc.sans.edu/forums/diary/Forensic+use+of+mount+bind/22854/
* Alpine Linux in a chroot on Fedora : https://git.alpinelinux.org/cgit/user/fab/scripts/tree/alpine-chroot.sh {{dead link}} script
* Alpine Linux aarch64 in a chroot on AWS Linux : https://gist.github.com/emolitor/0567e51c0ce04f4b025fc78d2cf0b4f1 script


[[Category:Installation]]
[[Category: Installation]]
[[category: System Administration]]

Latest revision as of 19:33, 30 August 2023

Inside the chroot environment, you can build, debug, and run Alpine packages or develop things. It's the most known way to do so without replacing your system or using a Virtual Machine. This document explains how to set up an Alpine build environment in a chroot under a host Linux distro, that can also be used to install Alpine Linux from a non-Alpine Linux system or live environment.

Tip: You can use the alpine-chroot-install script to do all these steps for you.

Requirements

  • Working Linux instalation where to perform all the process
  • Linux kernel 2.6.22, with curl and chroot binary installed
  • target media with at least 100M, 900MB for more complete solution as minimum
  • internet connection

Prerequisites

The variables below:

  • ${chroot_dir} = Should point to the new root directory
  • ${mirror} = Should be replaced with one of the available Alpine Linux mirrors.
  • ${arch} = Should be the cpu architecture like x86 (i386) or amd64(x86_64)..

Set up APK

Download the latest apk static package (replace ${version} with actual version):

curl -LO ${mirror}/latest-stable/main/${arch}/apk-tools-static-${version}.apk

.apk packages are just gzipped tarballs, you can unpack them using:

tar -xzf apk-tools-static-*.apk

Install the alpine base installation onto the chroot

./sbin/apk.static -X ${mirror}/latest-stable/main -U --allow-untrusted -p ${chroot_dir} --initdb add alpine-base

Seting up the chroot

Before you change root to the new directory, you need to create the required devices

Method 1.a Simple way: Using the host's /dev

mount -o bind /dev ${chroot_dir}/dev

Note: Bind mounts can be made read-only which would limit the chroot from writing to the devices
Method 1.b Manual way: Creating needed nodes
Warning: Manually creating devices will only provide the ones that have been created


mknod -m 666 ${chroot_dir}/dev/full c 1 7 mknod -m 666 ${chroot_dir}/dev/ptmx c 5 2 mknod -m 644 ${chroot_dir}/dev/random c 1 8 mknod -m 644 ${chroot_dir}/dev/urandom c 1 9 mknod -m 666 ${chroot_dir}/dev/zero c 1 5 mknod -m 666 ${chroot_dir}/dev/tty c 5 0

If you need SCSI disc access you can create the device nodes like this:

Note: Every device can have 15 sub-nodes, you should always increment by 16 for every new device

mknod -m 666 ${chroot_dir}/dev/sda b 8 0 mknod -m 666 ${chroot_dir}/dev/sda1 b 8 1 mknod -m 666 ${chroot_dir}/dev/sda2 b 8 2 mknod -m 666 ${chroot_dir}/dev/sda3 b 8 3 mknod -m 666 ${chroot_dir}/dev/sdb b 8 16 mknod -m 666 ${chroot_dir}/dev/sdb1 b 8 17

Make the process fs and /sys fs available

mount -t proc none ${chroot_dir}/proc mount -o bind /sys ${chroot_dir}/sys

Set up name resolution

A resolv.conf is needed for name resolution:

You can either copy your host's resolv.conf:

cp -L /etc/resolv.conf ${chroot_dir}/etc/

or instead you can create a new one (this example uses OpenDNS):

echo -e 'nameserver 8.8.8.8\nnameserver 2620:0:ccc::2' > ${chroot_dir}/etc/resolv.conf

Prepare the APK repositories

Set up APK main repository (replace ${branch} with the latest stable branch name, e.g. v3.3):

mkdir -p ${chroot_dir}/etc/apk echo "${mirror}/${branch}/main" > ${chroot_dir}/etc/apk/repositories

Warning: The chroot methods are commonly used to have Alpine installed in a existing system. Because of this, the steps for preparing a bootloader are not included.


Entering your chroot

You then can enter your chroot by running this command as the root user (UID 0).

chroot ${chroot_dir} /bin/ash -l

Preparing init services

If you plan to use your chroot with a init system or setup a new system on another device you should add these services:

rc-update add devfs sysinit rc-update add dmesg sysinit rc-update add mdev sysinit rc-update add hwclock boot rc-update add modules boot rc-update add sysctl boot rc-update add hostname boot rc-update add bootmisc boot rc-update add syslog boot rc-update add mount-ro shutdown rc-update add killprocs shutdown rc-update add savecache shutdown

Troubleshooting

Hardened kernels or alpine as chroot host

If you are using Alpine as a native build system you will have to make sure that you can run chmod from a chroot. Add the following to /etc/sysctl.conf

kernel.grsecurity.chroot_deny_chmod = 0

Then reload the sysctl configuration

sysctl -p

chroot: cannot run command ' ... Exec format error

This usually indicates that you booted with one architecture (e.g. armf) and are trying to chroot into another (e.g. x86_64). The binaries must be built for the architecture that the host runs!

Note that with one exception you can run 32 bit x86 chroot in x86_64, but not viceversa!

WARNING: Ignoring APKINDEX.xxxx.tar.gz

Make sure ${chroot_dir}/etc/apk/repositories is valid and run:

apk update

External links