13 March 2013

358. Gentoo in a virtual machine

This post took a LONG time to write, so hopefully someone, somewhere, will find it useful. My enthusiasm was also somewhat tempered during the installation. Gentoo just didn't speak to me. Maybe I'll feel differently in 6-12 months of Arch?

The post...

I'm sure that there are plenty of similar posts out there, and I admit that my main reason for writing this post isn't as much to get anyone else to play with gentoo (although it's certainly an interesting experience -- but probably a bit more fun on native hardware with a bit more oomph) as to provide myself with a written step-by-step instruction set if I should switch my mini-server to gentoo (I use it for testing/educational purposes).

I also realise that given that the most obvious advantage of gentoo is the hardware optimised binaries, running things in a virtual machine isn't going to show off the real strength of gentoo. Hopefully it might give an accurate impression of the complexity (or lack thereof) of gentoo as compared to other distros such as Arch and Debian.

Virtual machine
I'm doing this in a virtual machine. The principal reason is that I don't have any spare metal at the moment. The secondary reason is that because gentoo installation is very interactive, you will most likely not be able to do a complete installation in a single 2-hour sitting the first time (you'll have to look things up, think about modules etc. -- and compiling everything takes time), and doing things in a virtual machine makes it very easy to freeze the system until you have time to continue. Obviously you can also do it the old-fashioned way (simply chroot the system when you are ready to continue), but freezing is easier.

I probably should have switched to KVM by now, but since I'm not really that interested in using virtual machines for work, and since virtualbox is so simple to use, I'll be using Virtualbox here.

I wouldn't recommend trying Gentoo until you first feel somewhat familiar with the basic concepts on Linux (by using e.g. Debian), followed by trying a more hands-on distro like Arch, or one of the BSDs. Obviously, I'm biased since this is the path I've taken, but I still think that you're better off pushing yourself little by little, than suddenly jumping into something unfamiliar which then may turn into something that seems unfriendly and losing all interest as a consequence.

That's not to say that gentoo is difficult. What is or isn't difficult depends on your expectations and frame of reference. What I am saying is that gentoo will make a lot more sense if you have at least a conceptual idea of what is needed for a system to be bootable and useful (if you don't know that you need an X server, you won't enjoy this. If you haven't played with GRUB, you won't enjoy this).


I've followed http://www.gentoo.org/doc/
-- although I've focused on getting a working system quickly rather than spending a lot of time looking into what hardware I really have.

1. Create the virtual machine.
If you need help setting up a virtual machine you are most likely not going to enjoy gentoo (yet -- so come back in a few months), so I won't show that. Suffice to say that I created a machine with 1024 Mb RAM and 15 Gb HDD. The size of the harddisk is due to compilations normally requiring a fair amount of temporary storage space (you can probably get around it with tempfs if  you can spare it).

2. Get a gentoo cd. 
At this point we have an unpartitioned, unbootable harddrive so we need to boot our machine using some form of linux distro that can partition our virtual machine harddrive, as well as chroot gentoo. You don't need the gentoo cds for this, but it does make sense to use them.

You can use a minimal CD,a full DVD, or a stage 3 tarball. I'll use the CD.

Mirrors are found here: http://www.gentoo.org/main/en/mirrors2.xml
You will want to go to /gentoo/releases/amd64/current-iso and pick your architecture, e.g. I did

wget ftp://ftp.swin.edu.au/gentoo/releases/amd64/current-iso/install-amd64-minimal-20130110.iso
wget ftp://ftp.swin.edu.au/gentoo/releases/amd64/current-iso/install-amd64-minimal-20130110.iso.DIGESTS
sha512sum install-amd64-minimal-20130110.iso
77ab0ba00767b6d4668d0f4bf7effbf2af3f38a1bd7cef297a17076478fd46d05b15a80188da473cf7d7f8c220acbe615afd300de4af90011d54185be2697f7d install-amd64-minimal-20130110.iso
cat install-amd64-minimal-20130110.iso.DIGESTS
# SHA512 HASH 77ab0ba00767b6d4668d0f4bf7effbf2af3f38a1bd7cef297a17076478fd46d05b15a80188da473cf7d7f8c220acbe615afd300de4af90011d54185be2697f7d install-amd64-minimal-20130110.iso

3. Boot
Attach the iso to your virtual machine and boot/start.

Hit enter.
The fun begins :)

Note that everything in virtualbox 'just works' since there's a dhcp server etc. Real-world hardware may require a bit more work to get the network etc. up and running. Anyway, the gentoo manual tries to cover most eventualities, which may make it a bit more complicated to follow. We don't have to worry about most options.

4. Partition the drive
ls /dev/sd*
fdisk /dev/sda

Hit n (for new partition), p (for primary), accept 1, and 2048, set +13G, then n, p, 2, and accept the defaults. Do a, then 1 to make partition 1 bootable. Do t, 2, 82 to set sda2 as swap. Hit w to write. You've now created one bootable root and one swap partition.

mkswap /dev/sda2
mkfs.ext4 /dev/sda1

This isn't the gentoo way -- we're using a single root here instead of making separate partitions for root, usr, home etc. On the other hand, since we're just exploring we might as well keep things simple.

5. Setting up the chroot
Make sure that the date/time is right.

date -s "18:08 20130307"
mount /dev/sda1 /mnt/gentoo
cd /mnt/gentoo
wget ftp://ftp.swin.edu.au/gentoo/releases/x86/current-stage3/stage3-i686-20121213.tar.bz2
tar xvjpf stage3-i686-20121213.tar.bz2

Extracting the stage3 file creates the standard linux file structure (/var, /boot, /dev, /etc, /proc etc.).
Edit the (/mnt/gentoo)/etc/ports/make.conf
vi etc/ports/make.conf
CFLAGS="-O2 -march=native -pipe"

If you have multiple cores, change the MAKEOPTS accordingly. The USE flag seems to be 'bindist' on i686, and 'bindist mmx sse sse2' on amd64. Presumably they depend on what gentoo detects on boot, and you will probably want to keep these.

mirrorselect -i -o >> /mnt/gentoo/etc/portage/make.conf
mirrorselect -i -r -o >> /mnt/gentoo/etc/portage/make.conf
cp /etc/resolv.conf etc/resolv.conf
mount -t proc none proc/
mount --rbind /sys sys/
mount --rbind /dev dev/

6. Enter the chroot -- set-up
chroot /mnt/gentoo /bin/bash
source /etc/profile
mkdir /usr/portage

This will take a while, so don't give up if it seems stuck on 'Syncing local tree' and there's no network traffic.
emerge --sync
eselect profile list

There are seven different profiles on i686 and 13 on amd64 to choose from. I picked number 4 (default/gnome).
eselect profile set 4

I can't stand nano, so
emerge portage
emerge vim

Vim does take quite a while to install, so if you're happy with nano, stick with it.

Edit /etc/portage/make.conf
USE="bindist gnome gtk -kde -qt4"
Keep the USE flags that were there from the beginning and append gnome, gtk etc. I admit that I can't be bothered reading through /usr/portage/profiles/use.desc just in order to check out gentoo.

cp /usr/share/zoneinfo/Australia/Melbourne /etc/localtime
echo "Australia/Melbourne" > /etc/timezone

7. Compile/Install the kernel

emerge gentoo-sources
cd /usr/src/linux

Time to build a kernel! Do
make help|less

and look at the targets. Be aware that if you do 'emerge pciutils' as recommended by the gentoo guide this will take a long, long time since it pulls in 60 packages...but you need to if you want a working lspci.

My approach here is to first use make localmodconfig to get all the currently loaded modules, and then add more support manually via make menuconfig. Remember that if you screw things up you can always go back and redo it later. Build a minimal configuration, then explore what else you need to add (USB support etc.)

make localmodconfig
make menuconfig

1. Make sure to change 'Device Drivers/Serial ATA and Parallel ATA drivers' from (M) to (*).
2. Go to the submenu and make sure that AHCI SATA support is starred (i.e. not M) as well as 'Generic ATA support' Otherwise you'll probably find yourself consulting this post: http://wiki.gentoo.org/wiki/Knowledge_Base:Unable_to_mount_root_fs due to
VFS: Cannot open root device "sda1" or unknown-block(0,0)
Please append a correct "root=" boot option; here are the available partions:
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)

make modules_install
cp arch/x86_64/boot/bzimage /boot/vmlinuz-3.7.10
cp System.map /boot/System-3.7.10.map
ln -s /boot/System-3.7.10.map /boot/System.map

The first step make took me 16 minutes, which isn't bad for a single-core compile.

While you don't have to (since we have a single / partition), you might as well do
emerge genkernel
genkernel --install initramfs
mv /boot/initramfs-genkernel-x86_64-3.7.10-gentoo /boot/initramfs-3.7.10

You can edit /etc/conf.d/modules and list the modules you want to load. Have a look at the output of lsmod to get an idea. Doing 'lsmod > modules.list' might be a good idea for troubleshooting later.

8. Miscellaneous

Edit /etc/fstab
/dev/sda1 / ext4 defaults 0 2 /dev/sda2 none swap sw 0 0

Set a hostname:
echo 'HOSTNAME="turbotux"' > /etc/conf.d/hostname
echo ' turbotux localhost' > /etc/hosts

If you don't set a domain name and don't want hostname.unknown_domain to greet you on boot, run
sed -i 's,\\O,,g' /etc/issue

Edit /etc/conf.d/keymap and set keymap.

echo 'en_AU.UTF-8 UTF-8' >> /etc/locale.gen
echo 'LANG="en_AU.UTF-8"'> /etc/env.d/02locale
source /etc/profile

emerge mlocate vixie-cron sysklogd

rc-update add sshd default
emerge dhcpcd

9. GRUB2
GRUB might be a better, albeit retro, learning experience, but GRUB2 has better auto-configuration features and I'm not interested in hand-configuring grub just yet.
echo 'sys-boot/grub:2' >> /etc/portage/package.accept_keywords
emerge grub:2
mkdir /boot/grub2
grub2-mkconfig -o /boot/grub2/grub.cfg
grub2-install /dev/sda

IMPORTANT: set a root password before restarting or you won't be able to log in:

shutdown -h now
Remove the CD from the virtual machine. Start the machine again

10. Your first boot
If all went well (i.e. you did exactly what I did above) you'll be greeted with this:

You can now log in as root.

11. Installing Gnome 2

The current version of gnome in gentoo (stable) is 2.32. If you want that, just emerge gnome. If not, the easiest (not safest) way to is switch to testing which might not be the best choice for gentoo novices.
To set up gnome I'm following this post: http://www.gentoo.org/doc/en/gnome-config.xml
If you can't live a single day without gnome 3, then checkout http://en.gentoo-wiki.com/wiki/Gnome_3

Anyway, gnome 2.32:

Then do
emerge --sync
emerge --update --ask world
emerge gentoolkit
equery m gnome

Brace yourself, because this will take a while (549 packages!):
emerge --ask --autounmask-write gnome
emerge --ask gnome

Hit Yes when you run the first command (change Use params), u (use new) when you run the second command, and Yes for the third command (emerge new packages). This step took 660 minutes in a virtual machine with a single core assigned.

source /etc/profile
emerge --sync
rc-update add dbus default
/etc/init.d/dbus start

Create a user:
useradd -m verahill
emerge sudo gksu
echo 'verahill ALL=(ALL) ALL'>>/etc/sudoers
passwd verahill
su verahill
cd ~

Continue setting up gnome:
echo 'export XDG_MENU_PREFIX=-gnome' > ~/.xinitrc
echo 'exec gnome-session' >> ~/.xinitrc
ln -s /etc/xdg/menus/gnome-applications.menu /etc/xdg/menus/applications.menu 

You shouldn't have to do both the XDG_MENU_PREFIX and the symlink, but I had to in order to get a working applications menu.

Without vbox guest additions installed

To install the vbox additions, click on Devices/Install Guest Additions in the virtual machine menu. The CD will fail to mount.
sudo mount -o loop /dev/sr0 /mnt
sh /mnt/autorun.sh

Reboot, then do startx again. If you want gdm to start, then see step three here: http://www.gentoo.org/doc/en/gnome-config.xml

With vbox guest additions installed
Sounds is another story entirely...http://wiki.gentoo.org/wiki/PulseAudio

Anyway, that's enough of Gentoo for me for now.


  1. Pretty nice Instruction but if you put your grub.cfg you will get the error: "minimal bash-like line editing is supported". After I put it into /boot/grub/grub.cfg the System war booting.

    1. Thanks for the feedback.

      To other readers: I don't have a gentoo installation anymore and can't check if there are current issues with using /boot/grub2 vs /boot/grub