Last active
January 5, 2024 10:37
-
-
Save smithclay/b0a7b7e7d93b1e7a38cf1829e20a3708 to your computer and use it in GitHub Desktop.
ubuntu bootstrap for user mode linux: minimal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# This script creates a user-mode linux machine based on Ubuntu. | |
# Created by Clay Smith, May 2017 | |
# | |
# based on: https://gist.github.com/aputs/6247216 | |
# and https://gist.github.com/AVGP/5410903 | |
set -x | |
control_c() | |
# run if user hits control-c | |
{ | |
echo -en "\n*** Ouch! Exiting ***\n" | |
exit $? | |
} | |
trap control_c SIGINT | |
## warning!!! | |
## sudo access | |
if [ "`id -u`" != "0" ]; then | |
echo "I won't run as user. I need root." | |
exit 1 | |
fi | |
[ ! -e $(which debootstrap 2> /dev/null) ] && echo "error: debootstrap not found" && exit 1 | |
arch=amd64 | |
release=${1:-xenial} | |
dest_path=$(readlink -f -- "$(dirname $0)") | |
target_path=$(mktemp --directory) | |
hostname="$release-$arch" | |
extra_packages="iproute,net-tools,ca-certificates,apt,apt-utils" | |
[ ! -e "$target_path" ] && echo "error: error generating temporary directory" && exit 1 | |
sudo rm -fr $target_path | |
sudo mkdir -p $target_path | |
root_fs_path=$(mktemp /tmp/root_fs_XXXXXXX) | |
size=160 | |
echo dd if=/dev/zero of=$root_fs_path count=128 bs=1M | |
dd if=/dev/zero of=$root_fs_path count=$size bs=1M || f | |
mkfs.ext4 -T small $root_fs_path | |
mount -o loop $root_fs_path $target_path | |
sudo mkdir -p $target_path/etc/apt/apt.conf.d $target_path/etc/dpkg/dpkg.cfg.d | |
# minimize installation configs | |
cat << EOF | sudo tee $target_path/etc/apt/apt.conf.d/02compress-indexes | |
Acquire::GzipIndexes "true"; | |
Acquire::CompressionTypes::Order:: "gz"; | |
EOF | |
cat << EOF | sudo tee $target_path/etc/apt/apt.conf.d/02nocache | |
Dir::Cache { | |
srcpkgcache ""; | |
pkgcache ""; | |
} | |
EOF | |
cat << EOF | sudo tee $target_path/etc/apt/apt.conf.d/97norecommends | |
APT | |
{ | |
Install-Recommends "false"; | |
}; | |
EOF | |
cat << EOF | sudo tee $target_path/etc/dpkg/dpkg.cfg.d/01_nodoc | |
path-exclude /usr/share/doc/* | |
# we need to keep copyright files for legal reasons | |
path-include /usr/share/doc/*/copyright | |
path-exclude /usr/share/man/* | |
path-exclude /usr/share/groff/* | |
path-exclude /usr/share/info/* | |
# lintian stuff is small, but really unnecessary | |
path-exclude /usr/share/lintian/* | |
path-exclude /usr/share/linda/* | |
# lang | |
path-exclude /usr/share/locale/* | |
path-include /usr/share/locale/en* | |
# landscape | |
path-exclude /usr/share/pyshared/twisted/test* | |
path-exclude /usr/lib/python*/dist-packages/twisted/test* | |
path-exclude /usr/share/pyshared/twisted/*/test* | |
path-exclude /usr/lib/python*/dist-packages/twisted/*/test* | |
EOF | |
sudo debootstrap --verbose --arch=$arch --variant=minbase --include=$extra_packages $release $target_path http://archive.ubuntu.com/ubuntu/ | |
#sudo rm -rf $target_path/dev | |
#sudo mkdir -p $target_path/dev | |
#sudo mknod -m 0666 $target_path/dev/null c 1 3 | |
#sudo mknod -m 0666 $target_path/dev/zero c 1 5 | |
#sudo mknod -m 0666 $target_path/dev/random c 1 8 | |
#sudo mknod -m 0666 $target_path/dev/urandom c 1 9 | |
#sudo mkdir -m 0755 $target_path/dev/pts | |
#sudo mkdir -m 1777 $target_path/dev/shm | |
#sudo mknod -m 0666 $target_path/dev/tty c 5 0 | |
#sudo mknod -m 0666 $target_path/dev/tty0 c 4 0 | |
#sudo mknod -m 0666 $target_path/dev/tty1 c 4 1 | |
#sudo mknod -m 0600 $target_path/dev/console c 5 1 | |
#sudo mknod -m 0666 $target_path/dev/full c 1 7 | |
#sudo mknod -m 0600 $target_path/dev/initctl p | |
#sudo mknod -m 0666 $target_path/dev/ptmx c 5 2 | |
sudo chroot $target_path /bin/bash -x <<'EOF' | |
# LC_LANG errors | |
locale-gen en_US en_US.utf8 | |
touch /etc/resolv.conf | |
apt-get -y update | |
apt-get -y upgrade | |
# cleanup | |
apt-get -y clean all | |
apt-get -y autoremove | |
rm -rf /var/lib/apt/lists/* | |
find /usr/share/doc -depth -type f ! -name copyright | xargs rm || true | |
find /usr/share/doc -empty | xargs rmdir || true | |
rm -rf /usr/share/man /usr/share/groff /usr/share/info /usr/share/lintian /usr/share/linda /var/cache/man | |
find /usr/share/locale -mindepth 1 -maxdepth 1 ! -name 'en' | xargs rm -r | |
rm -rf /var/cache/apt/*.bin | |
# apt-get install fixes | |
mkdir -p /usr/share/man/man{1,2,3,4,5,6,7,8,9} | |
EOF | |
echo "Created filesystem: $root_fs_path" | |
sudo umount $target_path | |
#sudo rm -rf $target_path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment