Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[HOWTO] Building SmallGentoo
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
Geese
n00b
n00b


Joined: 14 Dec 2003
Posts: 24
Location: Fairfax, VA

PostPosted: Thu Aug 28, 2008 10:30 pm    Post subject: [HOWTO] Building SmallGentoo Reply with quote

Disclaimer
This howto is provided with no warranty whatsover. It may or may not work for you, and you may make a mistake and break something critical to you. Do this on a test machine first. Also, I offer no support on this guide and do not actively monitor PMs in my forum account. So, really, take this as-is. Things worked for me as listed below on 8-27-2008. I can't guarantee that this guide will remain correct as of this moment.

With that said, let's move on. :wink:

Objective
To provide guidance for creating a very minimalistic build of gentoo using glibc (not uclibc).

Goals
1) Minimize total size on disk for uses on slower storage like laptop harddrives, CF disks, USB drives, and CDs.
2) Provide the fastest boot times possible
3) Maintain as much compatibility as possible for adding in normal packages on top of this base for your custom needs
4) Satisfy one of the primary urges of a lot of Gentoo users (tweak-freaks) in making the smallest, fastest possible system capable of accomplishing specific objectives.

Assumptions
You are an experienced Linux user who understands the finer points of umasking packages, working in chroots, and building kernels. A lot of the skills from the TinyGentoo Wiki are used here with some minor differences. This project is for the patient folks out there.

Solution Overview
Use a stage3 (or greater) system to build a chrooted environment much like TinyGentoo but using glibc instead of uclibc for certain compatibility reasons. Use baselayout-2, sysvinit, openrc, and some creativity. Ignore a lot of dependencies where appropriate, and use busybox to fill in the rest.

Backround and Assumptions
My needs were to make a very small, very fast-booting gentoo system capable of supporting Xorg and Crossover Office (Wine) to be able to run navigation software on a Via Epia MII12000 in a VoomPC2 case. In other words, a Gentoo-based CarPC. As I have not formally worked out the details for how to best implement X on top of this, this guide gets you to the point of a fully-functioning command line system. In my case, I am able to cold boot to prompt in 18.3 seconds (pressing the power switch to seeing the word "login" show up). 8-9 of those seconds are the BIOS. Pretty good, IMO.

This howto will approach this build process with the following assumptions:
1) I'm using a laptop hard drive now
2) I'm dual booting the stage3 install (in my case, /dev/hda5) and the smallgentoo system (/dev/hda3) off the same laptop drive with a shared /boot partition (/dev/hda1). /dev/hda2 is swap.
3) I'm eventually going to change the fstab to be more CF card friendly and put the contents of the smallgentoo system on a IDE/CF setup.

Solution Details
Because I've installed a full gentoo build from stage3 on /dev/hda5, I can mount /dev/hda3 in a chroot and build the system in there. When I want to test, I can simply reboot and pick the different grub line and boot off of /dev/hda3. Pretty convenient and a whole lot faster than trying to use a build system and copy the entire image over to the target system. I realize that embedded users will have to do that, but desktop users can use my methods all on their same disk.

1) If you haven't already, make the filesystem and then mount it:
Code:
mke2fs -j /dev/hdX && mount /dev/hdX /your/mount/point


2) After making a partition for your chroot and mounting it in your build system's filesystem, add something like this to your /etc/fstab:
Code:
/dev/hdX              /your/mount/point          ext3       noatime         0 0


3) Create some of the directories that baselayout-2 does not provide:
Code:
mkdir -p ${ROOT}/etc/portage
mkdir -p ${ROOT}/dev/pts
mkdir -p ${ROOT}/proc
mkdir -p ${ROOT}/sys
mkdir -p ${ROOT}/boot/grub
mkdir -p ${ROOT}/home
mkdir -p ${ROOT}/root
mkdir -p ${ROOT}/mnt/cdrom
mkdir -p ${ROOT}/var/cache/edb
mkdir -p ${ROOT}/var/run
mkdir -p ${ROOT}/var/tmp


4) Unmask some packages that we'll use. I haven't bothered to figure out which one is actually used, so I put it in both places:
Code:
echo "sys-apps/baselayout **" > /etc/portage/package.keywords
echo "sys-apps/openrc **" >> /etc/portage/package.keywords
echo "sys-apps/baselayout **" > /your/mount/point/etc/portage/package.keywords
echo "sys-apps/openrc **" >> /your/mount/point/etc/portage/package.keywords


5) Copy a timezone and a resolv.conf in:
Code:
cp /usr/share/zoneinfo/EST5EDT /your/mount/point/etc/localtime
cp /etc/resolv.conf /your/mount/point/etc/resolv.conf


6) Creat your make.conf. I put it both in the build system and the smallgentoo's /etc. My make.conf looks like:
Code:
CFLAGS="-Os -march=i686 -pipe"
CXXFLAGS="-Os -march=i686 -pipe"
# WARNING: Changing your CHOST is not something that should be done lightly.
# Please consult http://www.gentoo.org/doc/en/change-chost.xml before changing.
CHOST="i686-pc-linux-gnu"
MAKEOPTS="-j2"
USE="fbcondecor -pam -man -locales -acl minimal via -gpm acpi mmx posix threads make-symlinks -ipv6 -perl -python -kde -doc -java -nis -nls nptl -selinux -slp -snmp -source sse2 -syslog truetype fontconfig -unicode win32codecs mp3 aac -xinetd"


7) Perform the emerges using the ROOT=/your/mount/point feature to install the compilation results into the smalllgentoo partition. This leaves all the packages, build info, portage, etc all in the build system and keeps the smallgentoo system clutter free.
Code:
ROOT="/your/mount/point" emerge --nodeps =sys-apps/baselayout-2.0.0 >> build.log
ROOT="/your/mount/point" emerge busybox >> build.log
ROOT="/your/mount/point" emerge glibc >> build.log
ROOT="/your/mount/point" emerge --nodeps ncurses bash sysvinit openrc >> build.log
ROOT="/your/mount/point" emerge dropbear e2fsprogs grub >> build.log

This lets the main emerge terminal stay pretty viewable while letting you tail -f build.log in another terminal. Note: the order is important. We want sysvinit and openrc to come AFTER busybox so that /sbin/init and /etc/inittab get overwritten and take effect.

8 ) Copy device files in:
Code:
cp -r /dev/* /your/mount/point


9) Edit the smallgentoo's /etc/fstab. /boot is hda1 for me, and the smallgentoo partition is hda3 for me. E.g.:
Code:
/dev/hda1               /boot           ext2            noatime         0 0
/dev/hda3               /               ext3            noatime         0 0
/dev/cdrom              /mnt/cdrom      auto            noauto,ro       0 0
shm                     /dev/shm        tmpfs           nodev,nosuid,noexec     0 0

Change to suit your environment.

10) Create a "working" script to help you jump in and out of the chroot cleanly. This script takes care of mounting and unmounting some needed system directories. I named mine "work.sh" and made it executable:
Code:
#!/bin/bash
ROOT=/your/mount/point

# Mount bind directories needed for chroot
echo "Preparing to enter the chrooted environment"
mount -o bind /proc ${ROOT}/proc
mount -o bind /sys ${ROOT}/sys
mount -o bind /dev ${ROOT}/dev
mount -o bind /dev/pts ${ROOT}/dev/pts
echo "Chrooting into the environment"

# Chroot and provide a root shell
chroot ${ROOT} /bin/bash
echo "Exiting from the chrooted environment"

# Unmount binded directories
umount ${ROOT}/proc
umount ${ROOT}/sys
umount ${ROOT}/dev/pts
umount ${ROOT}/dev
echo "Cleanly exited the chrooted environment"


11) Use the work.sh script to chroot into the smallgentoo system and perform further modifications. This means the following for my needs:

- Set root's passwd (I do this manually)
- edit /etc/shells to be appropriate (I do this manually)
- Disable boot-up services so that only the bare minimum start
- Remove all extraneous files from the filesystem.
- Fix some issues that I ran into along the way for my system.

My script (I call it tweak.sh) that I copy into the smallgentoo system AND RUN INSIDE THE CHROOT looks like:
Code:
#/bin/bash

echo "Removing unneeded files..."
# BE CAREFUL EDITING THESE
rm -rf /usr/share/locale /usr/share/zoneinfo/* /usr/share/doc /usr/share/info /usr/share/man /usr/share/dict /usr/include/* /tmp/* /var/tmp/* /var/db/*
ls /usr/share/i18n/locales/ | grep -v en_US | xargs rm
ls /usr/share/i18n/charmaps/ | grep -v ISO- | grep -v UTF | xargs rm
ls /usr/lib/gconv | grep -v ISO | grep -v UTF | grep -v lib | grep -v gconv | xargs rm
rm /linuxrc
# Whew.  Scary deletion stuff done.
echo "Tuning startup scripts..."
rc-update del netmount default
rc-update del consolefont boot
rc-update del modules boot
rc-update del keymaps boot
rc-update del mtab boot
rc-update del fsck boot
rc-update del termencoding boot
rc-update del net.lo boot
rc-update del urandom boot
# busybox's halt doesn't take -w as an option
sed -i 's/halt -w/halt/' /etc/init.d/bootmisc
# my realtime clock on my via comes up as rtc0
ln -s /dev/rtc /dev/rtc0
# odd bug with the symlink from the ebuild
ln -s /proc/mounts /etc/mtab
rm /etc/init.d/functions.sh
ln -s /lib/rc/sh/functions.sh /etc/init.d/functions.sh
# Set some stuff up
sed -i 's/localhost/CarPC/' /etc/conf.d/hostname
sed -i 's/agetty/getty/' /etc/inittab
sed -i 's/interactive="YES/interactive="NO/' /etc/rc.conf
ln -s /etc/init.d/net.lo /etc/init.d/net.eth0
echo "config_eth0=\"dhcp\"" > /etc/conf.d/net
echo "Startup scripts tuned."


12) Build a kernel. I personally use gentoo-sources and build by hand. In the build system's /usr/src/linux, run make menuconfig. I personally created a monolithic kernel with zero modules and the bare minimum of options. Basically, it's custom tailored to my via epia box and weighs in at 1.8MB. Once it's built, copy it to the /boot partition you have and modify the grub.conf accordingly.

In my case, my grub.conf looks like:
Code:
#
timeout 1
default 0
fallback 1

#splashimage=(hd0,0)/boot/grub/splash.xpm.gz

title CarPC
root (hd0,0)
kernel /boot/kernel root=/dev/hda3 vga=789 quiet

title Builder
root (hd0,0)
kernel /boot/kernel root=/dev/hda5 vga=792 quiet

title Rescue CarPC
root (hd0,0)
kernel /boot/kernel root=/dev/hda3 init=/bin/ash

title Rescue Builder
root (hd0,0)
kernel /boot/kernel root=/dev/hda5 init=/bin/bash

Modify yours to be correct.

You should now have a system that boots really quickly, is a total of ~20MB or less (do a "du -sch /*" once logged into it or chrooted into it), and is ready for your further customizations. To get a network interface up, run /etc/init.d/net.lo start && /etc/init.d/net.eth0 start. For my uses, the carpc will most often be disconnected, so I left them out of the rc-update changes.

Code:
root@builder / # du -sch *
1.3M   bin
368.0k   boot
104.0k   dev
560.0k   etc
4.0k   home
3.4M   lib
12.0k   mnt
4.0k   proc
8.0k   root
1.8M   sbin
4.0k   sys
4.0k   tmp
4.0k   tweak.sh
8.8M   usr
72.0k   var
16.3M   total


I hope you find this useful.

Additional Resources
http://gentoo-wiki.com/TinyGentoo
http://teknowlogic.org/soft/gentoo-on-epia-5000/
https://forums.gentoo.org/viewtopic-t-327295-start-0-postdays-0-postorder-asc-highlight-usb.html
http://www.gentoo.org/proj/en/base/amd64/howtos/index.xml?part=1&chap=2


Last edited by Geese on Thu Aug 28, 2008 11:08 pm; edited 2 times in total
Back to top
View user's profile Send private message
poly_poly-man
Advocate
Advocate


Joined: 06 Dec 2006
Posts: 2477
Location: RIT, NY, US

PostPosted: Thu Aug 28, 2008 10:48 pm    Post subject: Reply with quote

thought of uclibc?
_________________
iVBORw0KGgoAAAANSUhEUgAAA

avatar: new version of logo - see topic 838248. Potentially still a WiP.
Back to top
View user's profile Send private message
Geese
n00b
n00b


Joined: 14 Dec 2003
Posts: 24
Location: Fairfax, VA

PostPosted: Thu Aug 28, 2008 10:56 pm    Post subject: Reply with quote

poly_poly-man wrote:
thought of uclibc?

Yes, in fact. I've done all of this using uclibc only to later find out that crossover office 6 (the only version of wine that can run iguidance v3 well in linux) requires glibc. I even had it sub-8MB in size and boot a few seconds faster than this. I have also made livecds using catalyst before, and I realized that a lot of the files in the stage1 spec weren't really needed. So, I kinda merged the two howtos and really worked hard to pair it down to the barebones minimum.

These forums were the sole reasons for my success with those projects, so I feel like it was only right to give back my experiences in how-to form.
Back to top
View user's profile Send private message
poly_poly-man
Advocate
Advocate


Joined: 06 Dec 2006
Posts: 2477
Location: RIT, NY, US

PostPosted: Thu Aug 28, 2008 11:00 pm    Post subject: Reply with quote

it does look good - I'll definitely be playing with your instructions...

though I'll prolly use uclibc - I'm a bit of a fan (see my www link)
_________________
iVBORw0KGgoAAAANSUhEUgAAA

avatar: new version of logo - see topic 838248. Potentially still a WiP.
Back to top
View user's profile Send private message
Geese
n00b
n00b


Joined: 14 Dec 2003
Posts: 24
Location: Fairfax, VA

PostPosted: Thu Aug 28, 2008 11:03 pm    Post subject: Reply with quote

Go for it. It's a fun project. The main differences between this howto and the great TinyGentoo howto is the use of glibc instead of uclibc and the use of sysvinit/openrc instead of writing your own little linuxrc script from scratch.
Back to top
View user's profile Send private message
Geese
n00b
n00b


Joined: 14 Dec 2003
Posts: 24
Location: Fairfax, VA

PostPosted: Wed Sep 10, 2008 4:17 pm    Post subject: Reply with quote

So I have a basic xorg up and running.

I had to do the following as next steps:
Code:
ROOT="/your/mount/point" KERNEL_DIR=/usr/src/linux emerge xorg-server xterm >> build.log
ROOT="/your/mount/point" KERNEL_DIR=/usr/src/linux emerge --nodeps libXft >> build.log


I then copied the /lib/libgcc_s.so.1 from my build system image into /your/mount/point/lib/ and I edited the bottom of /etc/X11/xinit/xinitrc to comment out the lines with twm (I didn't install it).

I then ran:
Code:
X -configure

and copied that file to be /etc/X11/xorg.conf. The only tweak was to change /dev/input/mice0 to /dev/mice0.

Finally, I edited /usr/bin/startx and replaced the
Code:
mcookie=`/usr/bin/mcookie`
with
mcookie=abbacadabbaabbacadabba

as I don't have mcookie installed and don't care about security.

A simple startx got me in! Next steps are to add a window manager and customize the GUI as needed.
Back to top
View user's profile Send private message
int2str
Guru
Guru


Joined: 29 Sep 2003
Posts: 361

PostPosted: Sat Sep 13, 2008 4:26 pm    Post subject: Reply with quote

Thanks for this well written HOWTO!
Keep up the good work.
_________________
Adpot an unanswered post today!
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