Skip to content

chroot build steps on debian

Will Jhun edited this page Feb 7, 2023 · 6 revisions

Old steps

mkdir chroot
cd chroot/
mkdir stretch00
export CHROOT=/share/chroot/stretch00
sudo apt-get install debootstrap
sudo debootstrap --arch amd64 stretch $CHROOT http://ftp.us.debian.org/debian/
sudo mount -t proc proc $CHROOT/proc
sudo mount -t devpts devpts $CHROOT/dev/pts
sudo chroot $CHROOT /bin/bash --login
[now in chroot shell, do some setup]
echo 'PS1="CHROOT-stretch:\w# "' >> ~/.bashrc
apt-get install locales
dpkg-reconfigure locales
apt-get install vim ssh nasm gcc
apt-get install libc6-dev-i386
apt-get install gcc-multilib g++-multilib
apt-get install ca-certificates
apt-get clean

and a little script to jump in and out of chroot:

#!/bin/bash

export CHROOT=/share/chroot/stretch00
sudo mount -t proc proc $CHROOT/proc
sudo mount -t devpts devpts $CHROOT/dev/pts
sudo chroot $CHROOT /bin/bash --login
sudo umount $CHROOT/proc
sudo umount $CHROOT/dev/pts

Steps for building a ubuntu chroot for arm64

Download this Ubuntu 22.04.1 image and make a qcow2 image from it:

mkdir arm-qemu
cd arm-qemu
wget https://cloud-images.ubuntu.com/releases/22.04/release-20221101.1/ubuntu-22.04-server-cloudimg-arm64.img

Use virt-customize to set a default root password:

sudo apt install libguestfs-tools     # for virt-customize
virt-customize -a ubuntu-22.04-server-cloudimg-arm64.img --root-password password:<pass>

Optionally convert the image into a compressed format to save space:

qemu-img convert -f raw ubuntu-22.04-server-cloudimg-arm64.img -O qcow2 jammy-cloudimg-arm64.qcow2
rm ubuntu-22.04-server-cloudimg-arm64.img

Obtain qemu EFI images and compose flash drive images:

sudo apt install qemu-efi

dd if=/dev/zero of=flash1.img bs=1M count=64
dd if=/dev/zero of=flash0.img bs=1M count=64
dd if=/usr/share/qemu-efi-aarch64/QEMU_EFI.fd of=flash0.img conv=notrunc

Start qemu:

qemu-system-aarch64 -nographic -machine virt -m 2G -cpu max \
 -drive file=flash0.img,format=raw,if=pflash -drive file=flash1.img,format=raw,if=pflash \
 -object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-device,rng=rng0 \
 -device virtio-blk-device,drive=drive0,bootindex=0 -drive file=jammy-cloudimg-arm64.qcow2,if=none,id=drive0,cache=writeback \
 -device virtio-net-device,netdev=usernet -netdev user,id=usernet,hostfwd=tcp::22222-:22

Log in as root (with the password set above) and change the password for the ubuntu account. Then change to the ubuntu account and proceed to set up the chroot:

mkdir chroot
cd chroot/
mkdir jammy00
export CHROOT=/home/ubuntu/chroot/jammy00
sudo apt update
sudo apt-get install debootstrap
sudo debootstrap --arch arm64 jammy $CHROOT https://mirrors.ocf.berkeley.edu/ubuntu-ports
sudo mount -t proc proc $CHROOT/proc
sudo mount -t devpts devpts $CHROOT/dev/pts
sudo chroot $CHROOT /bin/bash --login

Once inside the chroot shell, do some setup:

echo 'PS1="CHROOT-jammy:\w# "' >> ~/.bashrc
apt-get install locales
dpkg-reconfigure locales
apt-get install vim ssh gcc
apt-get install ca-certificates
apt-get clean

Exit the shell, undo the mountpoints and tar up the chroot:

<Ctrl-D>
sudo umount $CHROOT/proc
sudo umount $CHROOT/dev/pts
cd jammy00
sudo tar cvfz ../arm64-target-root.tar.gz .

Retrieve the image via scp over qemu usernet. Note that password authentication is disabled by default on the cloud image. Comment out the following line in /etc/ssh/sshd_config:

PasswordAuthentication no
scp -P 22222 ubuntu@localhost:/home/ubuntu/chroot/riscv64\* .

Steps for building a ubuntu chroot for riscv64

Install opensbi binaries:

sudo apt install opensbi

Download this Ubuntu 22.04.1 image and (optionally) make a qcow2 image from it:

mkdir rv-qemu
cd rv-qemu
wget https://cdimage.ubuntu.com/releases/22.04/release/ubuntu-22.04.1-preinstalled-server-riscv64+unmatched.img.xz
unxz ubuntu-22.04.1-preinstalled-server-riscv64+unmatched.img.xz
qemu-img convert -f raw ubuntu-22.04.1-preinstalled-server-riscv64+unmatched.img -O qcow2 jammy-unmatched.qcow2
rm ubuntu-22.04.1-preinstalled-server-riscv64+unmatched.img

You'll need a recent u-boot image to boot it. Extract one from this Debian package:

wget http://ftp.us.debian.org/debian/pool/main/u/u-boot/u-boot-omap_2023.01+dfsg-2_armhf.deb
ar -x u-boot-omap_2023.01+dfsg-2_armhf.deb
tar xvf data.tar.xz

Start qemu:

qemu-system-riscv64 -nographic -machine virt -m 2G -cpu rv64 \
 -bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf \
 -kernel usr/lib/u-boot/qemu-riscv64_smode/uboot.elf \
 -object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-device,rng=rng0 \
 -append "console=ttyS0 rw root=/dev/vda1" \
 -device virtio-blk-device,drive=hd0 -drive file=jammy-unmatched.qcow2,if=none,id=hd0 \
 -device virtio-net-device,netdev=usernet -netdev user,id=usernet,hostfwd=tcp::22222-:22

Log in (l:ubuntu, p:ubuntu), change the password as prompted, and then proceed to set up the chroot:

mkdir chroot
cd chroot/
mkdir jammy00
export CHROOT=/home/ubuntu/chroot/jammy00
sudo apt update
sudo apt-get install debootstrap
sudo debootstrap --arch riscv64 jammy $CHROOT https://mirrors.ocf.berkeley.edu/ubuntu-ports/
sudo mount -t proc proc $CHROOT/proc
sudo mount -t devpts devpts $CHROOT/dev/pts
sudo chroot $CHROOT /bin/bash --login

Once inside the chroot shell, do some setup:

echo 'PS1="CHROOT-jammy:\w# "' >> ~/.bashrc
apt-get install locales
dpkg-reconfigure locales
apt-get install vim ssh gcc
apt-get install ca-certificates
apt-get clean

Exit the shell, undo the mountpoints and tar up the chroot:

<Ctrl-D>
sudo umount $CHROOT/proc
sudo umount $CHROOT/dev/pts
cd jammy00
sudo tar cvfz ../riscv64-target-root.tar.gz .

Retrieve the image via scp over qemu usernet:

scp -P 22222 ubuntu@localhost:/home/ubuntu/chroot/riscv64\* .

TODO

These images could be trimmed down to include a minimum of dependencies (shared libraries, headers).