-
Notifications
You must be signed in to change notification settings - Fork 0
User Mode Linux
NOTE: As of 2021-04-08 there is no shell implemented. Therefore it is required to install a 3rd-party shell (like dash). Please note that binaries, that are not statically build require additional libraries to be included in the final rootfs.
User-mode Linux is a port of Linux to Linux. In other words, it is a binary executable instead of a kernel that runs on hardware. User-mode Linux runs as a standard executable on top of a running Linux instance. Just imagine Linux running in a virtual machine on a Linux computer, just without the virtual machine.
- binutils
- gcc (GNU C compiler)
- make
- autoconf
- automake
- wget
- git
apt-get update
apt-get install build-essential wget git
pacman -Syu
pacman -S base-devel wget git
echo "sys-libs/glibc static-libs" >> /etc/portage/package.use/custom
emerge glibc gcc wget git
This guide shows how to create a simple operating system based on the Linux kernel.
The default editor is ed
and the default shell is dash
.
- Create and format the virtual drive
dd if=/dev/zero of=ubda.img bs=1M count=512
mkfs.ext4 ubda.img
- Mount the virtual drive
mkdir ubda
sudo mount ubda.img ubda
- Change the ownership of the virtual drive
sudo chown $USER ubda
- Create a basic filesystem-layout
mkdir -p ubda/{proc,sys,dev,boot,etc,tmp,home,root,bin}
mkdir -p ubda/lib/modules
mkdir -p ubda/var/{log,lock,cache}
mkdir -p ubda/usr{,/local}/{bin,lib,share,libexec,include}
ln -sf bin ubda/sbin
ln -sf bin ubda/usr/sbin
ln -sf bin ubda/usr/local/sbin
ln -sf ../tmp ubda/var/tmp
ln -sf /proc/self/mounts ubda/etc/mtab
chmod 1777 ubda/tmp
- (Optional) Configuring the operating system
echo 'umlinux' > ubda/etc/hostname
echo 'root::0:0:root:/root:/bin/sh' > ubda/etc/passwd
echo 'root::0:' > ubda/etc/group
echo 'export HOSTNAME="$(uname -n)"' > ubda/etc/profile
echo 'export PS1="\$USER@\$HOSTNAME:\$PWD \$ "' >> ubda/etc/profile
echo 'export PWD="$HOME"' >> ubda/etc/profile
echo 'alias ls="ls -p"' >> ubda/etc/profile
touch ubda/etc/shadow
chmod 600 ubda/etc/shadow
- Download, extract the Linux kernel from kernel.org and cd into it.
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.11.13.tar.xz
tar -xf linux-5.11.13.tar.xz
cd linux-5.11.13
- Download the pre-configured kernel configuration
wget -O .config https://raw.githubusercontent.com/Benni3D/microcoreutils/master/.wiki/kconfig
- (Optional) Configure the kernel to your liking
make ARCH=um menuconfig
- Build the kernel.
make ARCH=um -j$(nproc)
- (Optional) Installing the kernel modules
If you selected Loadable Module support:
make INSTALL_MOD_PATH="$PWD/../ubda" modules_install
- Installing the kernel
cp linux ../linux
cd ..
It is recommended to install a x86_64-linux-musl toolchain, or else login and other umight not work correctly.
- Download binutils, gcc, musl
wget https://ftp.gnu.org/gnu/binutils/binutils-2.36.1.tar.xz
wget https://ftp.gnu.org/gnu/gcc/gcc-10.3.0/gcc-10.3.0.tar.xz
wget https://musl.libc.org/releases/musl-1.2.2.tar.gz
- Extract
tar -xf binutils-2.36.1.tar.xz
tar -xf gcc-10.3.0.tar.xz
tar -xf musl-1.2.2.tar.gz
- Build and install binutils
mkdir binutils-2.36.1/build
cd binutils-2.36.1/build
../configure --prefix=/usr/local --target=x86_64-linux-musl --disable-nls --disable-multilib --with-system-zlib
make -j$(nproc)
sudo make install
cd ../..
- Install kernel-headers
cd linux-5.11.13
make clean
sudo make ARCH=x86 INSTALL_HDR_PATH=/usr/local/x86_64-linux-musl headers_install
cd ..
- Build C compiler
mkdir gcc-10.3.0/build
cd gcc-10.3.0
./contrib/download_prerequisites
cd build
../configure --prefix=/usr/local --target=x86_64-linux-musl --disable-nls --disable-multilib --with-system-zlib --enable-languages=c
make -j$(nproc) all-gcc
sudo make install-gcc
cd ../..
- Install C Headers and Startup files
cd musl-1.2.2
./configure --prefix=/usr/local/x86_64-linux-musl --build=$MACHTYPE --target=x86_64-linux-musl --enable-static
sudo make install-headers
sudo rm -rf lib obj
make lib/crt1.o lib/crti.o lib/crtn.o
sudo install lib/crt1.o lib/crti.o lib/crtn.o /usr/local/x86_64-linux-musl/lib
sudo x86_64-linux-musl-gcc -nostdlib -nostartfiles -shared -x c /dev/null -o /usr/local/x86_64-linux-musl/lib/libc.so
cd ..
- Build and install Compiler Support Library
cd gcc-10.3.0/build
make -j$(nproc) all-target-libgcc
sudo make install-target-libgcc
cd ../..
- Build and install C library
cd musl-1.2.2
make -j$(nproc)
sudo make install
cd ..
- Build additional gcc libraries
cd gcc-10.3.0/build
make -j$(nproc)
sudo make install
cd ../..
- Clone this repository and cd into it
git clone https://github.com/Benni3D/microcoreutils.git
cd microcoreutils
- Build microcoreutils
./autogen.sh
CFLAGS=-static ./configure --host=x86_64-linux-musl
make -j$(nproc)
- Install microcoreutils
make DESTDIR="$PWD/../ubda" install
cd ..
- Download & extract dash from here and cd into it
wget https://git.kernel.org/pub/scm/utils/dash/dash.git/snapshot/dash-0.5.11.3.tar.gz
tar -xf dash-0.5.11.3.tar.gz
cd dash-0.5.11.3
- Configure and build dash
./autogen.sh
CC=x86_64-linux-musl-gcc ./configure --prefix=/ --datarootdir=/usr/share --enable-static --enable-silent-rules
make -j$(nproc)
- Install dash
make DESTDIR="$PWD/../ubda" install
cd ..
- Link sh to dash
ln -sf dash ubda/bin/sh
- (Optional) Stripping the binaries
strip ubda/bin/*
- Change ownership of the virtual drive
sudo chown -R root:root ubda
- Umount the virtual drive
sudo umount ubda.img
- Delete the toolchain source code
rm -rf musl-* gcc-* binutils-*
- Delete the Linux kernel source code
rm -rf linux-*
- Delete the dash source code
rm -rf dash*
- Delete the microcoreutils source code
rm -rf microcoreutils
- Start the newly created operating system
./linux mem=16M ubda=ubda.img rw
- Login with root
umlinux login: root
- Stop the operating system
halt