In this post, I will show you how to quickly install Gentoo Linux on the Raspberry Pi.
After this, you’ll get a bootable Gentoo Linux on your Raspberry Pi.
Preparation
- 8 GB+ MicroSD card
- A Linux PC with MicroSD Card Reader
- The latest stage3 file for ARMv7a w/ HardFP from Gentoo Site
- Clone or download the repository on GitHub
Installation
Note: Do not enter “↵”, simply press ENTER.
First of all, you should identify what’s your disk device name. Typically, it should be /dev/mmcblkN, but in some Linux distribution, it would be /dev/sdX. In this post, I suppose your device name is /dev/mmcblk0.
Partition Disk
Raspberry Pi can only boot from the first partition on your disk, and need a vfat (aka FAT32 in Windows) file system, Linux doesn’t care which partition, but, NOT vfat. So we must provide at least 2 partitions to place boot files and system files.
Now let’s partition your disk to 2 partitions like this:
| Device | Size | Id | Type |
|---|---|---|---|
| /dev/mmcblk0p1 | 128 MB | c | W95 FAT32 (LBA) |
| /dev/mmcblk0p2 | 10 GB or all rest size | 83 | Linux |
This is my terminal include operate steps:
raspberrypi3 ~ # fdisk /dev/mmcblk0
Welcome to fdisk (util-linux 2.30.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p↵
Disk /dev/mmcblk0: 29.1 GiB, 31268519936 bytes, 61071328 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xa03cfbd1
Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 2048 61069311 61067264 29.1G 7 HPFS/NTFS/exFAT
Command (m for help): d↵
Selected partition 1
Partition 1 has been deleted.
Command (m for help): n↵
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): ↵
Using default response p.
Partition number (1-4, default 1): ↵
First sector (2048-61071327, default 2048): ↵
Last sector, +sectors or +size{K,M,G,T,P} (2048-61071327, default 61071327): +128M↵
Created a new partition 1 of type 'Linux' and of size 128 MiB.
Command (m for help): t↵
Selected partition 1
Hex code (type L to list all codes): c↵
Changed type of partition 'Linux' to 'W95 FAT32 (LBA)'.
Command (m for help): n↵
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): ↵
Using default response p.
Partition number (2-4, default 2): ↵
First sector (264192-61071327, default 264192): ↵
Last sector, +sectors or +size{K,M,G,T,P} (264192-61071327, default 61071327): +10G↵
Created a new partition 2 of type 'Linux' and of size 10 GiB.
Command (m for help): p↵
Disk /dev/mmcblk0: 29.1 GiB, 31268519936 bytes, 61071328 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xa03cfbd1
Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 2048 264191 262144 128M c W95 FAT32 (LBA)
/dev/mmcblk0p2 264192 21235711 20971520 10G 83 Linux
Command (m for help): w↵
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
Please following the Using fdisk to partition the disk on Gentoo Handbook now to learn how to using
fdisk, maybe sometime later, I’ll write a post about this.
Make File Systems
After partition, we should make file systems on each partitions:
mkfs.vfat /dev/mmcblk0p1 mkfs.ext4 /dev/mmcblk0p2
Place Files on Disk
At the moment, let’s mount the root partition (the largest one) to /mnt/gentoo/:
mount /dev/mmcblk0p2 /mnt/gentoo/
Extract the latest stage3 file (here suppose stage3-armv7a_hardfp-20161129.tar.bz2) to /mnt/gentoo/:
tar xvpf stage3-armv7a_hardfp-20161129.tar.bz2 --xattrs --numeric-owner -C /mnt/gentoo/
After this, you should see several directories under /mnt/gentoo/.
Next, mount the boot partition (the 128M one) to /mnt/gentoo/boot/:
mount /dev/mmcblk0p1 /mnt/gentoo/boot/
Copy or move files under GitHub repo’s boot directory to /mnt/gentoo/boot/, and you will find start.elf and other files under /mnt/gentoo/boot/.
Copy or move the GitHub repo’s modules directory to /mnt/gentoo/lib/, and you will get /mnt/gentoo/lib/modules/ directory.
Edit the Key Files
Now, we have the whole file structure on your disk (MicroSD card), but we still have a little works to do:
/etc/fstab:
The/mnt/gentoo/etc/fstabfile records how do partitions mount to directory, make sure only have these 2 lines uncommented:/dev/mmcblk0p1 /boot vfat noauto,noatime 1 2 /dev/mmcblk0p2 / ext4 noatime 0 1
/etc/shadow:
The/mnt/gentoo/etc/shadowfile records each users password and active state, let’s make a password shadow first:openssl passwd -1
Follow the prompting, you will get an shadow string like
$1$fGkSoMqH$177SR4upLdOJWuSTIsiBd/, place it in the root line in the/mnt/gentoo/etc/shadowfile like this:root:$1$fGkSoMqH$177SR4upLdOJWuSTIsiBd/:17210:0:::::
/boot/cmdline.txt:
Create a new file/mnt/gentoo/boot/cmdline.txtand only writeroot=/dev/mmcblk0p2 rootdelay=10in it, to make sure we can boot from MicroSD card.
Boot Up
The remaining works are eject the MicroSD card, insert it into the Raspberry Pi’s card slot, connecting the Raspberry Pi to a HDTV or monitor via HDMI cable, plug the power cord, and boot up your Gentoo Linux on your Raspberry Pi!
If you are lucky, you should see a rainbow block on your screen, after it, with some text rolling, at the end, you see a login prompt.
Congratulations, that’s your Gentoo!
Leave a Reply