linux · gentoo · 2016-12-26 · yuex

Introduction

I got a MacBook Pro recently, the mid 2015 15" version. I have to admit that MacOs and Homebrew are cool. But still I want Gentoo Portage back. Actually I doubt if anyone can still date with other package system after you have seen the true beauty of Gentoo Portage. So I tried Gentoo Prefix. But unfortunately, at this point, due to the new release of MacOS Sierra and LLVM, bintutils-apple failed to compile with clang-3.9. I tried to use the system clang (Apple 8.0.0) and clang-3.8. binutils-apple could compile. But later on glib failed to compile. It looks like glib used some new features supported in clang-3.9. That was where I gave up. Maybe I will try it again later.

Anyway, here I am, going to install Gentoo natively on this MacBook Pro mid 2015 15" laptop. Its product number is MJLT2LL/A if you want details. Believe me, we're gonna have a lot of details here. I think there are not too many differences between installation on Mac or on PC. The biggest one comes from the kernel configuration since hardware always varies from laptop to laptop, even under same brand same product number. So generally, the installation guide below also applies to PC except the kernel options configuration part.

Installation of Gentoo is not easy. You may have successfully installed ArchLinux once or two. Gentoo installation is a little bit harder than that. But not too much, just that you need to configure the kernel yourself using some Gentoo-specific tools. But if your only Linux installation exprience comes from Ubuntu, Fedora, or CentOS with a nice graphics installation interface, then you're gonna have troubles. You can still try. But my advice is to find yourself two or three days and follow Sakaki's Guide. Because there are gonna be a lot new concepts.

Acknoledgements

Thanks to Artem and, parts of this guide are adapted from their installation guide. In case you have troubles in following this guide, you may want to check out theirs. BTW, they use 2013 models.

And of course, I want to thank Sakaki. My first Gentoo installation is following his guide. But the problem is that he uses his own tools like buildkernel and showem which are provided by adding a portage overlay. I think that's too much for a Gentoo newbie and obscure the original installation process and the relationships among Gentoo Portage, Kernel, Xorg, and Gnome or KDE. So here I will stick with genkernel.

Expectation

In this guide, I will introduce step by step how to install a dual-boot Gentoo on MacBook Pro 15" (mid 2015). The filesystem will be encrypted by using luks and lvm. We use systemd and grub2 here. After the installation, you should be able to boot into Gentoo to see a console command line or into MacOs to see GUI. In following posts, I will introduce how to install KDE from there. Once you have a bootable Gentoo, it's pretty easy to install KDE or Gnome.

Besides, following guide assuems that you have a wireless network (you don't have Ethernet port on Mac, right?). Though not necessary, another laptop with SSH client at your hands will be much pretty handier.

Prepare Boot Media and Disk

Unlike PC, Mac is an UEFI-only system. We cannot just dd an ISO image to an USB stick and boot. We have to make an UEFI-bootable USB stick. It's just a little bit more than dd. You need to install and configure a bootloader like grub or syslinux on that USB sticks. That's a little bit tedious. So here I will use some tool.

Here, you may be interested of the difference between BIOS and UEFI, and perhaps Linus's comments on UEFI

In UEFI Gentoo Quick Install Gudie, it is recommended to use SystemRescureCD. It's a Gentoo-based rescue system. You need to download it from their website.

To make the USB stick, we will use [UNetbootin][] here. It supports all three platform. After download, the burning is easy. Just remeber, before burning, you have to format the USB stick as a FAT filesystem. If you are using Linux, you need to mount it then.

Suppose your USB stick is /dev/sdb1

mkfs.fat /dev/sdb1
mount /dev/sdb1 /media

Then, use unetbootin to make the USB stick.

Then, if you want dual boot to MacOS, you need to resize the disk to save some space for Gentoo. It can be done by using Disk Utility. The filesystem format doesn't matter. We will re-format it later. The label doesn't matter right too because we can set it later. But I recommend you set it now, just name it as Gentoo HD or some thing else you like. Usually, fresh Gentoo needs 20GiB. But if you are planning to use it afterwards, I recommend no less than 100GiB.

Boot from USB Stick

Plug in your USB stick and reboot. Hold Alt (or Option) key after you hear the boot sound. Then choose to boot from USB stick. Press Enter until you see the welcome message to command line and the prompt is ready.

Setup Remote Access

You can install by using the console command line. But probably you have found that the fonts are too small. Even if they were large enough, typing commands there is still painful. So here we are gonna set up remote access so that you can login remotely by using SSH.

First, we need to connect to the WIFI

nmcli dev wifi connect "ap-name" password "pass"

If you are using Ethernet or you want GUI, you can try

net-setup

But personally, I prefer nmcli.

Next, modify

# /etc/ssh/sshd_config
PasswordAuthentication yes
PermitRootLogin yes

Then, since the resuce system is using OpenRC, you need to restart sshd by

/etc/init.d/sshd restart

And last, don't forget to setup the password for root by

passwd

Create the Disk

To create some encrypted disk, we need to load the encryption modules first

modprobe dm-crypt
modprobe aes
# error means no hardware cryptographic device
# run modprobe aes_generic
modprobe sha256

Next, check out your partition for Gentoo

blkid

Make sure which one you want to format. You should be able to identify the label when you resize the disk for Gentoo. In my case it is /dev/sda4

cryptsetup luksFormat /dev/sda4 # enter your passphrase
cryptsetup luksOpen /dev/sda4 gentoo

Here, we formated the disk and opened it as /dev/mapper/gentoo.

Then, create lvm which enable you to adjust your disk's size without reformating. This is the thing that Windows still doesn't support.

pvcreate /dev/mapper/gentoo
vgcreate vg /dev/mapper/gentoo

Here, we created a virtual group named as vg

Finally, we create a partion for root and format it as Ext4

lvcreate -l 100%FREE -n root vg
mkfs.ext4 /dev/vg/root

Since we are using SSD with 8 or 16 GiB memory, I think swap is not necessary. People may argue that we need swap to support hibernation. But usually for laptop with a buit-in battery, what we need is sleep instead of hibernation.

Please checkout

Prepare Portage

Gentoo's package system is Portage. You can simply think Portage as a collections of recipes defining how to accquire, compile, and install some source code. It use tools like emerge to install packages. Like Debian, emerge uses other tools to search. To compiler kernel, we need to setup Portage first.

Synchronize time is not necessary, but it doesn't hurt

ntpdate -s time.nist.gov

Mount the file system we created

mount /dev/vg/root /mnt/gentoo
cd /mnt/gentoo

Get the Gentoo stage3 snapshot, you should check Gentoo Mirror to get the latest snapshot for systemd.

wget http://distfiles.gentoo.org/releases/amd64/autobuilds/current-stage3-amd64-systemd/stage3-amd64-systemd-20161218.tar.bz2
tar xvf stage3*

For what is stage3, please checkout Gentoo Stage. Actually, if you try Gentoo Prefix, all stages will be went through. But here, stage3 is enough.

Get the latest Portage

wget http://distfiles.gentoo.org/releases/snapshots/current/portage-latest.tar.xz
tar xvf portage* -C /mnt/gentoo/usr

But still Portage needs some configuration. First we change root to our future file system. But before that, we need to mount it.

mount -t proc none /mnt/gentoo/proc
mount -o bind /dev /mnt/gentoo/dev
mount -o bind /sys /mnt/gentoo/sys

Next, copy the name resolution file

cp -L /etc/resolve.conf /mnt/gentoo/etc

Then change root, and up

chroot /mnt/gentoo /bin/bash
env-update && source /etc/profile

Now, we need to edit /etc/portage/make.conf. But first, you may want to install vim

emerge vim
vim /etc/portage/make.conf

For now, the most important thing is to add some compile options.

/etc/portage/make.conf
# Doc https://wiki.gentoo.org/wiki//etc/portage/make.conf

CFLAGS="-O2 -pipe -march=native"
CXXFLAGS="${CFLAGS}"
CHOST="x86_64-pc-linux-gnu"

# cores + 1
MAKEOPTS="-j5"
EMERGE_DEFAULT_OPTS="--jobs 4"

ACCEPT_LICENSE="*"
ACCEPT_KEYWORDS="amd64"

VIDEO_CARDS="radeon radeonsi intel"
INPUT_DEVICES="evdev synaptics mtrack mouse"

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

FEATURES="binpkg-logs clean-logs split-log"

USE="unicode nls bluetooth alsa infinality cjk"

Another thing is to define CPU_FLAGS_X86 so that emerge can use these CPU-specific flags to optimize the generated code. There is a tool to generate these flags for you

emerge cpuid2cpuflags -1
cpuinfo2cpuflags-x86 >> /etc/portage/make.conf

Kernel

Before configuring kernel, we modify the /etc/fstab

vim /etc/fstab
/dev/sda1               /boot/efi       vfat            defaults,noauto,noatime,discard 1 2
/dev/vg/root            /               ext4            defaults,noatime,discard        0 1

Here, /dev/sda1 is Mac's original EFI partition.

Then, we install kernel sources and build tool genkernel-next. Since we want to use an encrypted disk, genkernel-next needs to pull in some support.

mkdir -p /etc/portage/package.use
touch -p /etc/portage/package.use/zzz_via_automask
echo "sys-kernel/genkernel-next cryptsetup" > /etc/portage/package.use/genkernel-next
emerge gentoo-sources
emerge genkernel-next
emerge linux-firmwares

genkernel-next also needs some configuration

INSTALL="yes"
OLDCONFIG="yes"
MENUCONFIG="yes"
NCONFIG="no"
CLEAN="yes"
MRPROPER="no"
MOUNTBOOT="no"
SAVE_CONFIG="yes"
USECOLOR="yes"
MAKEOPTS="-j5"
LVM="yes"
LUKS="yes"
GPG="no"
DMRAID="no"
BUSYBOX="yes"
UDEV="yes"
MDADM="no"
ISCSI="no"
E2FSPROGS="no"
FIRMWARE="yes"
FIRMWARE_DIR="/lib/firmware"
BOOTLOADER="grub"

Next, we configure the kernel. Though this is interesting, it is also very tedious. For example, you need to configure kernel options for

  • Systemd https://wiki.gentoo.org/wiki/Systemd#Kernel
  • Thunderbolt
  • Thunderbolt Ethernet
  • SD Card
  • Others Misc https://wiki.gentoo.org/wiki/Apple_Macbook_Pro_Retina_(early_2013)
  • Wireless https://wiki.gentoo.org/wiki/Wifi
  • Bluetooth https://wiki.gentoo.org/wiki/Bluetooth
  • Radeon https://wiki.gentoo.org/wiki/Radeon

I won't go into details here. If you like, you can use my configuration . It's for 4.4.26. Save it under /etc/kernels/.

Now, compile and install the kernel

genkernel all

It takes some time. You should be able to hear the roar of your fans.

Bootloader

First, mount Mac's EFI partition

mkdir -p /boot/efi
mount /dev/sda1 /boot/efi

Then, we need to install Grub. Since we are using lvm and luks, we need Grub to support device mapper.

echo "sys-boot/grub device-mapper" >> /etc/portage/package.use/grub 
emerge grub

Lvm should be pulled in by grub. We need some configurations

vim /etc/lvm/lvm.conf
use_lvmetad = 0

Then restart

/etc/init.d/lvm restart

Next, we configure Grub

vim /etc/default/grub
GRUB_CMDLINE_LINUX="init=/usr/lib/systemd/systemd crypt_root=/dev/sda4 root=/dev/mapper/vg-root dolvm rootfstype=ext4"
GRUB_ENABLE_CRYPTODISK=y

Add an entry to boot MacOS from Grub

vim /etc/grub.d/40_custom
menuentry "Mac OS X" {
    insmod hfsplus
    set root='(hd1,gpt3)'
    chainloader ($root)/System/Library/CoreServices/boot.efi
}

You can use efibootmgr to check out EFI entries

# check
efibootmgr -v

# add an entry
efibootmgr --create --label Gentoo --loader "\EFI\gentoo\grubx64.efi"

# remove an entry
efibootmgr -B -b <hex>

Install Grub

grub-install --efi-directory=/boot/efi

Generate Grub configuration

grub-mkconfig -o /boot/grub/grub.cfg

Ready to Boot

Now, we are just one step away from boot into Gentoo. Before that, we need to configure some profiles for Gentoo

eselect profile set default/linux/amd64/13.0/systemd
emerge --ask --update --deep --newuse @world

Then install NetworkManager to manage network after we boot into Gentoo

emerge networkmanager

Setup a password for root so that you can login

passwd

Now, try to reboot

reboot

Systemd Configuration

If everything goes fine, you should be able to boot into a command line console after entering the passphrase for decrypting disk and password for logining.

Now, we need to configure Systemd to finalize our installation

systemd-machine-id-setup

Auto start Network Manager

systemctl enable NetworkManager
systemctl start NetworkManager

Connect to network

nmcli dev wifi connect "ap-name" password "pass"

Setup hostname

hostnamectl set-hostname macbookpro

Setup locales

/etc/locale.conf
en_US.UTF-8 UTF-8
locale-gen
localectl set-locale LANG=en_US.UTF-8
localectl set-keymap us

Setup timezone

timedatectl set-timezone US/Eastern
timedatectl set-ntp 1

If you like, you can also add a normal user now.