Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Useful bash script to emerge a minimal rootfs from binpkgs
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
HoJi
n00b
n00b


Joined: 21 May 2015
Posts: 12

PostPosted: Fri Nov 25, 2016 4:23 pm    Post subject: Useful bash script to emerge a minimal rootfs from binpkgs Reply with quote

Hi all,

I thought I'd share a cut down version of a script I use to build a Gentoo rootfs for an embedded environment. It's a minimal rootfs with most of the bloat stripped out, which I subsequently package into an initramfs for running as an embedded OS running a kiosk program in X. After lzma compression my fullly functional initramfs comes out at about 60MB, but the one below should be significantly less than that. The packages are prebuilt on a build PC, then this is emerged to a separate folder with this script. Not complicated, but someone else might want this, one day. Or maybe not. Anyway, have fun!

Jason

[Edit] Included a script run in the chroot to set stuff up

Bash Script:
Code:
#!/bin/bash
# 24 November 2016 - emerge a system root into $EMBEDDEDDIR.

EMBEDDEDDIR=/tmp/root/
STORAGE=/media/serverimages/gentoo/
VERSION="productiontest"
DATE=$(date +"%Y%m%d")

# Set up Portage environment variables
export ROOT=${EMBEDDEDDIR}
export EMERGE_DEFAULT_OPTS="--quiet-build --getbinpkgonly"

# Clean up current directory and create folders
rm -r ${EMBEDDEDDIR}*
mkdir -p ${EMBEDDEDDIR}{proc,dev,sys,run,boot,etc,root}

# Set up timezone
echo "Europe/London" > ${EMBEDDEDDIR}etc/timezone

# Emerge root folder structure
emerge -k sys-apps/baselayout

# Emerge all the things
emerge -ak \
      sys-libs/glibc \
      sys-libs/timezone-data \
      sys-apps/coreutils \
      sys-apps/util-linux \
      sys-apps/openrc \
      sys-apps/busybox \
      sys-apps/grep \
      sys-apps/shadow \
      sys-apps/sed \
      sys-apps/gawk \
      sys-apps/iproute2 \
      sys-apps/net-tools \
      sys-apps/pciutils \
      sys-apps/findutils \
      sys-fs/sysfsutils \
      sys-power/acpid \
      sys-process/procps \
      sys-process/lsof \
      app-shells/bash \
      app-arch/tar \
      app-arch/gzip \
      net-analyzer/iptraf-ng \
      net-analyzer/tcpdump \
      net-analyzer/traceroute \
      net-dns/bind-tools \
      net-misc/dhcpcd \
      net-misc/iperf \
      net-misc/iputils \
      net-misc/ntp \
      net-misc/openssh \
      net-misc/utelnetd \
      x11-base/xorg-server \
      x11-libs/libva \
      x11-libs/cairo \
      x11-libs/libXrandr \
      x11-apps/xset \
      dev-libs/libusb \
      dev-util/strace \
      app-editors/nano \
      app-admin/syslog-ng

# Emerge things without deps
emerge -kO sys-boot/syslinux

# Copy across gcc libraries
cp /usr/lib64/gcc/x86_64-pc-linux-gnu/4.9.3/libgcc_s.so.1   ${EMBEDDEDDIR}usr/lib64/
cp /usr/lib64/gcc/x86_64-pc-linux-gnu/4.9.3/libstdc++.so.6   ${EMBEDDEDDIR}usr/lib64/

# Clean up libs, docs, objects etc....
rm -r ${EMBEDDEDDIR}var/db/pkg
rm -r ${EMBEDDEDDIR}var/lib/gentoo
rm -r ${EMBEDDEDDIR}var/lib/portage

rm -r ${EMBEDDEDDIR}usr/include

rm -r ${EMBEDDEDDIR}usr/lib64/*.a
rm -r ${EMBEDDEDDIR}usr/lib64/*.o

rm -r ${EMBEDDEDDIR}usr/share/doc
rm -r ${EMBEDDEDDIR}usr/share/man
rm -r ${EMBEDDEDDIR}usr/share/misc
rm -r ${EMBEDDEDDIR}usr/share/gtk-doc

# ... including unused locales...
mv ${EMBEDDEDDIR}usr/share/locale/en_GB   ${EMBEDDEDDIR}usr/share/
rm -r ${EMBEDDEDDIR}usr/share/locale/*
mv ${EMBEDDEDDIR}usr/share/en_GB   ${EMBEDDEDDIR}usr/share/locale/

mv ${EMBEDDEDDIR}usr/share/i18n/locales/en_GB   ${EMBEDDEDDIR}usr/share/
rm -r ${EMBEDDEDDIR}usr/share/i18n/locales/*
mv ${EMBEDDEDDIR}usr/share/en_GB   ${EMBEDDEDDIR}usr/share/i18n/locales/

# ... and unused terminfo descriptors...
mv ${EMBEDDEDDIR}usr/share/terminfo/l/linux   ${EMBEDDEDDIR}usr/share/
mv ${EMBEDDEDDIR}usr/share/terminfo/v/vt100   ${EMBEDDEDDIR}usr/share/
mv ${EMBEDDEDDIR}usr/share/terminfo/x/xterm   ${EMBEDDEDDIR}usr/share/
rm -r ${EMBEDDEDDIR}usr/share/terminfo/*

mkdir -p ${EMBEDDEDDIR}usr/share/terminfo/{v,l,x}
mv ${EMBEDDEDDIR}usr/share/vt100   ${EMBEDDEDDIR}usr/share/terminfo/v/
mv ${EMBEDDEDDIR}usr/share/xterm   ${EMBEDDEDDIR}usr/share/terminfo/x/
mv ${EMBEDDEDDIR}usr/share/linux   ${EMBEDDEDDIR}usr/share/terminfo/l/

# ... and unused dns shiatzu...
rm -r ${EMBEDDEDDIR}usr/bin/{dnssec*,delv,host,nsupdate}

# Generate tarball
cd ${EMBEDDEDDIR}
tar -cpf ${STORAGE}sysroot-${VERSION}-${DATE}.tar .



Profile is default/linux/amd64/13.0/no-multilib
make.conf from the build PC:
Code:
EMERGE_DEFAULT_OPTS="--ask --tree --quiet-build"

LINGUAS="en_GB"
L10N="en_GB"

CHOST="x86_64-pc-linux-gnu"

CFLAGS="-march=nehalem -O2 -pipe"
CXXFLAGS="${CFLAGS}"

CPU_FLAGS_X86="bindist mmx sse sse2 X"
USE="${CPU_FLAGS_X86} -cracklib -ipv6 -systemd lzo lzma"

PORTDIR="/usr/portage"
DISTDIR="${PORTDIR}/distfiles"
PKGDIR="${PORTDIR}/packages"

FEATURES="buildpkg"

VIDEO_CARDS="i915"

MAKEOPTS="-j8"



package.accept_keywords from the build PC:
Code:
=sys-devel/gcc-4.9.2         ~amd64
>=x11-libs/xorg-drivers-1.16      ~amd64
>=net-misc/utelnetd-0.1.9-r1      ~amd64


package.use from build PC:
Code:
sys-apps/busybox static
sys-apps/groff -X
net-misc/iputils -caps -filecaps
sys-fs/mtools -X
net-misc/openssh -X
app-editors/xemacs -X -berkdb
net-misc/ntp ssl -readline


This script is run inside the chrooted file system; chroot after mounting the following:
Code:
echo "Mounting proc sys dev..."
mount -o bind /proc ${ROOTDIR}proc
mount -o bind /sys ${ROOTDIR}sys
mount -o bind /dev ${ROOTDIR}dev
mount -o bind /dev/pts ${ROOTDIR}dev/pts


Code:
#!/bin/bash
# JP: 11 June 2016
# Post installation script to be run in the new chroot

# Set root password
echo root:'hashed password goes here' | chpasswd -e

# NTP
groupadd ntp
useradd -g ntp -s /sbin/nologin -d /var/empty/ntp -c 'NTP Daemon' ntp
echo "Europe/London" > /etc/timezone
mkdir -p /var/empty/ntp

# SSHD
groupadd sshd
useradd -g sshd -s /sbin/nologin -d /var/empty/sshd -c 'SSH Daemon' sshd
mkdir -p /var/empty/sshd
rm -r /etc/ssh
ln -s /etc/permstorage/ssh /etc/ssh

rc-update add acpid default
rc-update add utelnetd default
rc-update add sshd default
rc-update add watchdog default
rc-update add ntpd default
rc-update del netmount default
rc-update del keymaps boot


Last edited by HoJi on Mon Nov 28, 2016 9:07 am; edited 3 times in total
Back to top
View user's profile Send private message
brendlefly62
Tux's lil' helper
Tux's lil' helper


Joined: 19 Dec 2009
Posts: 133

PostPosted: Mon Nov 28, 2016 6:08 am    Post subject: Reply with quote

Hi, HoJi/Jason -- thanks for sharing. I'm curious what kind of kiosk project you've got going, running on a nehalem proc? Was that choice necessitated by the complexity of the kiosk application? or was it to avoid cross-compiling for arm - with a non-x86_64 board like a raspberry pi for example?
Back to top
View user's profile Send private message
HoJi
n00b
n00b


Joined: 21 May 2015
Posts: 12

PostPosted: Mon Nov 28, 2016 6:25 am    Post subject: Reply with quote

Thanks brendlefly62. The nehalem flag is an ugly//neat hack I used to change the instruction set; the build PC is an i7-4790 whereas the kiosk PCs are G1820 Celerons which don't use the AVX instruction set. Therefore with the default -march=native flag the compiler on the build PC generated code which wouldn't run on the kiosk PCs. D'oh!

Running this on the target PC:
Code:
gcc -march=native -Q --help=target


and rummaging through this:
https://gcc.gnu.org/onlinedocs/gcc-4.9.4/gcc/i386-and-x86-64-Options.html#i386-and-x86-64-Options

helped me determine that the nehalem march flag was probably the way to go.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum