Alpine Linux in a chroot: Difference between revisions

From Alpine Linux
(→‎Creating the installation: Maybe /dev/null was bad once, it's OK in the install I'm doing today.)
(Overhaul, not quite finished yet.)
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.
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. The guide can also be used to install Alpine Linux from a foreign livecd such as Ubuntu or Systemrescuecd.
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.


== 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.
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 ==
== Prerequisites ==
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.
The variables below:  
 
'''Note:''' The variables below:  


*'''${build_dir}''' = You can name it whatever you like.
*'''${chroot_dir}''' = Should point to the chroot directory where you  
*'''${mirror}''' = Should be replaced with one of the available Alpine Linux mirrors.
*'''${mirror}''' = Should be replaced with [http://dl-2.alpinelinux.org/alpine/MIRRORS.txt one of the available Alpine Linux mirrors].


Choose a mirror from the [http://dl-2.alpinelinux.org/alpine/MIRRORS.txt mirror list].
== Set up APK ==


Let's start by getting the latest apk static package:  
Download the latest apk static package:  


{{Tip|In the command below, replace x86_64 with x86 if running on a 32 bit installation}}
{{Tip|In the command below, replace x86_64 with x86 if running on a 32 bit installation}}
Line 22: Line 21:
{{Cmd|wget http://dl-3.alpinelinux.org/alpine/v3.0/main/x86_64/apk-tools-static-2.4.4-r0.apk}}
{{Cmd|wget http://dl-3.alpinelinux.org/alpine/v3.0/main/x86_64/apk-tools-static-2.4.4-r0.apk}}


Unpack the tarball
.apk packages are just gzipped tarballs, unpack using:
{{Cmd|tar -xzf apk-tools-static-2.4.4-r0.apk}}
{{Cmd|mkdir apk-tools-static
tar -xzf apk-tools-static-2.4.4-r0.apk -C apk-tools-static}}


We are setting up a basic chroot:
== Install the alpine base installation onto the chroot ==


{{Cmd|mkdir ${build_dir}
{{Cmd|./sbin/apk.static -X ${mirror}/v3.0/main -U --allow-untrusted --root ${chroot_dir} --initdb add alpine-base alpine-sdk}}
sudo ./sbin/apk.static -X ${mirror}/v3.0/main -U --allow-untrusted --root ${build_dir} --initdb add alpine-base alpine-sdk
mkdir -p ./${build_dir}/proc}}


Lets setup our needed devices:
== Set up the chroot ==


{{Cmd|sudo mknod -m 666 ./${build_dir}/dev/full c 1 7
Set up some devices in the chroot
sudo mknod -m 666 ./${build_dir}/dev/ptmx c 5 2
{{Tip|Manually creating devices is not needed if you choose to bind-mount hosts /dev into the chroot}}
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}}


We need a resolv.conf is needed for the DNS servers and the /root directory:
{{Cmd|sudo mknod -m 666 ${chroot_dir}/dev/full c 1 7
sudo mknod -m 666 ${chroot_dir}/dev/ptmx c 5 2
sudo mknod -m 644 ${chroot_dir}/dev/random c 1 8
sudo mknod -m 644 ${chroot_dir}/dev/urandom c 1 9
sudo mknod -m 666 ${chroot_dir}/dev/zero c 1 5
sudo mknod -m 666 ${chroot_dir}/dev/tty c 5 0}}


{{Cmd|sudo cp /etc/resolv.conf ./${build_dir}/etc/
If you need SCSI disc access:
mkdir -p ./${build_dir}/root}}


If you don't want to copy the resolv.conf from the local machine, create this file with your DNS server entry.
{{Cmd|mknod -m 666 ${chroot_dir}/dev/sda b 8 0
{{Cmd|echo 'nameserver 8.8.8.8' > ./${build_dir}/etc/resolv.conf}}
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/sda4 b 8 4
mknod -m 666 ${chroot_dir}/dev/sda5 b 8 5
mknod -m 666 ${chroot_dir}/dev/sda6 b 8 6
mknod -m 666 ${chroot_dir}/dev/sdb b 8 16
mknod -m 666 ${chroot_dir}/dev/sda1 b 8 17
mknod -m 666 ${chroot_dir}/dev/sda2 b 8 18
mknod -m 666 ${chroot_dir}/dev/sda3 b 8 19
mknod -m 666 ${chroot_dir}/dev/sda4 b 8 20
mknod -m 666 ${chroot_dir}/dev/sda5 b 8 21
mknod -m 666 ${chroot_dir}/dev/sda6 b 8 22}}


We are setting up APK mirrors:  
A resolv.conf is needed for name resolution:  


{{Cmd|sudo mkdir -p ./${build_dir}/etc/apk
{{Cmd|sudo cp /etc/resolv.conf ${chroot_dir}/etc/
echo "${mirror}/v2.3/main" > ./${build_dir}/etc/apk/repositories}}
mkdir -p ${chroot_dir}/root}}
 
If you don't want to copy the resolv.conf from the local machine, you can create a new one using Googles DNS servers:
{{Cmd|echo 'nameserver 8.8.8.8' > ${chroot_dir}/etc/resolv.conf}}
 
Set up APK mirror:
 
{{Cmd|mkdir -p ${chroot_dir}/etc/apk
echo "${mirror}/v3.0/main" > ${chroot_dir}/etc/apk/repositories}}


== Entering your chroot ==
== Entering your chroot ==
After the installation you should be able to enter your chroot:  
At this point, Alpine has been succesfully installed onto the chroot directory. Before you chroot in you
will probably want to mount /proc and /sys in the chroot:
 
{{Cmd|mount -t proc none ${chroot_dir}/proc
mount -o bind /sys ${chroot_dir}/sys}}


<!-- Don't we need to mount more, as in Arch instructions? Also should explain how to cleanly exit the chroot. -->
If you don't want to create special device files yourself, mount the hosts device directory onto the chroot:
{{Cmd|mount -o bind /dev ${chroot_dir}/dev}}


{{Cmd|sudo mount --bind /proc ./${build_dir}/proc
You can now chroot:
sudo chroot ./${build_dir} /bin/sh -l}}
{{Cmd|chroot ${chroot_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
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

Revision as of 19:36, 17 June 2014

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. The guide can also be used to install Alpine Linux from a foreign livecd such as Ubuntu or Systemrescuecd. 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 or older releases of Alpine Linux to test backports.

Requirements

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

Prerequisites

The variables below:

Set up APK

Download the latest apk static package:

Tip: In the command below, replace x86_64 with x86 if running on a 32 bit installation
Warning: You will need Kernel version 2.6.22 or later to use apk-tools-static


wget http://dl-3.alpinelinux.org/alpine/v3.0/main/x86_64/apk-tools-static-2.4.4-r0.apk

.apk packages are just gzipped tarballs, unpack using:

mkdir apk-tools-static tar -xzf apk-tools-static-2.4.4-r0.apk -C apk-tools-static

Install the alpine base installation onto the chroot

./sbin/apk.static -X ${mirror}/v3.0/main -U --allow-untrusted --root ${chroot_dir} --initdb add alpine-base alpine-sdk

Set up the chroot

Set up some devices in the chroot

Tip: Manually creating devices is not needed if you choose to bind-mount hosts /dev into the chroot

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

If you need SCSI disc access:

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/sda4 b 8 4 mknod -m 666 ${chroot_dir}/dev/sda5 b 8 5 mknod -m 666 ${chroot_dir}/dev/sda6 b 8 6 mknod -m 666 ${chroot_dir}/dev/sdb b 8 16 mknod -m 666 ${chroot_dir}/dev/sda1 b 8 17 mknod -m 666 ${chroot_dir}/dev/sda2 b 8 18 mknod -m 666 ${chroot_dir}/dev/sda3 b 8 19 mknod -m 666 ${chroot_dir}/dev/sda4 b 8 20 mknod -m 666 ${chroot_dir}/dev/sda5 b 8 21 mknod -m 666 ${chroot_dir}/dev/sda6 b 8 22

A resolv.conf is needed for name resolution:

sudo cp /etc/resolv.conf ${chroot_dir}/etc/ mkdir -p ${chroot_dir}/root

If you don't want to copy the resolv.conf from the local machine, you can create a new one using Googles DNS servers:

echo 'nameserver 8.8.8.8' > ${chroot_dir}/etc/resolv.conf

Set up APK mirror:

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

Entering your chroot

At this point, Alpine has been succesfully installed onto the chroot directory. Before you chroot in you will probably want to mount /proc and /sys in the chroot:

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

If you don't want to create special device files yourself, mount the hosts device directory onto the chroot:

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

You can now chroot:

chroot ${chroot_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

kernel.grsecurity.chroot_deny_chmod = 0

Then run the following command

sysctl -p

Alpine Linux in a chroot on Fedora

If you want to generate a chroot on a Fedora based system, you can use this script.

Note: Maybe you are able to use this script on other distribution but this is not tested.