[更新中] #RaspberryPi に #Gentoo Linuxをセットアップする

## 参考

* [Raspberry Pi - Gentoo Wiki](https://wiki.gentoo.org/wiki/Raspberry_Pi)

## セットアップ

### SDカードにパーティションを切る

今回は16GBのSDHCカードを使用した([Amazonで売ってるこれ](http://amazon.jp/dp/B003VNKNEQ/mazgi-22/ref=nosim/))

```bash
GreenLeft / # fdisk -l /dev/sdb

Disk /dev/sdb: 15.8 GB, 15811477504 bytes, 30881792 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
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       67583       32768    c  W95 FAT32 (LBA)
/dev/sdb2           67584    14747647     7340032   83  Linux
/dev/sdb3        14747648    15796223      524288   82  Linux swap / Solaris
/dev/sdb4        15796224    30881791     7542784    c  W95 FAT32 (LBA)
```

```bash
GreenLeft / # mkfs.vfat -F 16 /dev/sdb1
mkfs.fat 3.0.26 (2014-03-07)
GreenLeft / # mkfs.ext4 /dev/sdb2
mke2fs 1.42.7 (21-Jan-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
458752 inodes, 1835008 blocks
91750 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1879048192
56 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

GreenLeft / # mkswap /dev/sdb3
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=143d8bce-e8e0-46cb-8256-a1d3f5b78eb9
GreenLeft / # mkfs.vfat -F 32 /dev/sdb4
mkfs.fat 3.0.26 (2014-03-07)
```

Macからマウントされたときに分かりやすいよう、VFATなパーティションにはラベルを付ける

```bash
GreenLeft / # emerge -pv mtools

These are the packages that would be merged, in order:

Calculating dependencies... done!
[ebuild   R    ] sys-fs/mtools-4.0.15  USE="-X" 0 kB

Total: 1 package (1 reinstall), Size of downloads: 0 kB
```

```shell-session
GreenLeft / # mlabel -i /dev/sdb1 ::RPI-BOOT
GreenLeft / # mlabel -i /dev/sdb4 ::RPI-MEDIA
GreenLeft / # mlabel -s -i /dev/sdb1 ::
 Volume label is RPI-BOOT   
GreenLeft / # mlabel -s -i /dev/sdb4 ::
 Volume label is RPI-MEDIA  
```

```shell-session
GreenLeft / # mount /dev/sdb2 /mnt/gentoo/
GreenLeft / # cd /mnt/gentoo
GreenLeft gentoo # mkdir -p boot media
GreenLeft gentoo # mount /dev/sdb1 boot/
GreenLeft gentoo # mount /dev/sdb4 media/
```

```shell-session
GreenLeft / # cd /
GreenLeft / # curl -LO -s ftp://ftp.iij.ad.jp/pub/linux/gentoo/releases/arm/autobuilds/current-stage3-armv6j_hardfp/stage3-armv6j_hardfp-20140113.tar.bz2.DIGESTS
GreenLeft / # curl -LO -s ftp://ftp.iij.ad.jp/pub/linux/gentoo/releases/arm/autobuilds/current-stage3-armv6j_hardfp/stage3-armv6j_hardfp-20140113.tar.bz2
GreenLeft / # curl -LO -s ftp://ftp.iij.ad.jp/pub/linux/gentoo/releases/arm/autobuilds/current-stage3-armv6j_hardfp/stage3-armv6j_hardfp-20140113.tar.bz2.CONTENTS
GreenLeft / # sha1sum --check stage3-armv6j_hardfp-20140113.tar.bz2.DIGESTS
stage3-armv6j_hardfp-20140113.tar.bz2: OK
stage3-armv6j_hardfp-20140113.tar.bz2.CONTENTS: OK
sha1sum: WARNING: 6 lines are improperly formatted
GreenLeft / # curl -LO -s ftp://ftp.iij.ad.jp/pub/linux/gentoo/snapshots/portage-latest.tar.xz.md5sum
GreenLeft / # curl -LO -s ftp://ftp.iij.ad.jp/pub/linux/gentoo/snapshots/portage-latest.tar.xz
GreenLeft / # md5sum portage-latest.tar.xz.md5sum
ac9e1b3f7698b064c4bb82fb61610172  portage-latest.tar.xz.md5sum
GreenLeft / # md5sum --check portage-latest.tar.xz.md5sum
portage-latest.tar.xz: OK
```

```shell-session
GreenLeft gentoo # cd /mnt/gentoo
GreenLeft gentoo # tar xfp /stage3-armv6j_hardfp-20140113.tar.bz2
GreenLeft gentoo # tar xf /portage-latest.tar.xz -C usr/
```

```shell-session
GreenLeft src # pwd
/usr/src
GreenLeft src # git clone --depth=1 git://github.com/raspberrypi/firmware.git raspberrypi-firmware
Cloning into 'raspberrypi-firmware'...
remote: Counting objects: 1910, done.
remote: Compressing objects: 100% (1473/1473), done.
remote: Total 1910 (delta 358), reused 1702 (delta 302)
Receiving objects: 100% (1910/1910), 50.56 MiB | 2.86 MiB/s, done.
Resolving deltas: 100% (358/358), done.
Checking connectivity... done
```

```shell-session
GreenLeft gentoo # pwd
/mnt/gentoo
GreenLeft gentoo # cp -Rp /usr/src/raspberrypi-firmware/boot/ .
GreenLeft gentoo # cp -Rp /usr/src/raspberrypi-firmware/modules/ lib/
```

```shell-session
GreenLeft gentoo # cat boot/cmdline.txt
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
```

```shell-session
GreenLeft gentoo # cat boot/config.txt
arm_freq=900
core_freq=333
sdram_freq=450
over_voltage=2
framebuffer_depth=24
hdmi_drive=2
```

```shell-session
GreenLeft gentoo # grep -E '^root' etc/passwd
root::0:0:root:/root:/bin/bash
```

```shell-session
GreenLeft gentoo # grep -Ev '^\s*($|#)' etc/fstab
/dev/mmcblk0p1          /boot           auto            defaults        1 2
/dev/mmcblk0p2          /               ext4            defaults        0 1
/dev/mmcblk0p3          none            swap            sw              0 0
/dev/mmcblk0p4          /media          auto            defaults        0 1
```

```shell-session
localhost ~ # passwd
New password: 
Retype new password: 
passwd: password updated successfully
localhost ~ # cd /etc/init.d/
localhost init.d # ln -fs net.lo net.eth0 
localhost init.d # /etc/init.d/net.eth0 start
 * Bringing up interface eth0
 *   Caching network module dependencies
need firewalld
 *   No configuration specified; defaulting to DHCP
 *   dhcp ...
 *     Running udhcpc …                                            [ ok ]
 *     received address 172.x.y.z/16                               [ ok ]
localhost ~ # /etc/init.d/sshd start
ssh-keygen: generating new host keys: RSA1 RSA DSA
 * Starting sshd …                                                 [ ok ]
```

```shell-session
localhost ~ # locale-gen 
 * Generating 2 locales (this might take a while) with 1 jobs
 *  (1/2) Generating en_US.UTF-8 ... [ ok ]
 *  (2/2) Generating ja_JP.UTF-8 ... [ ok ]
 * Generation complete
```

```shell-session
localhost ~ # cat /etc/env.d/02locale 
LC_ALL=C
LANG=C
localhost ~ # env-update 
>>> Regenerating /etc/ld.so.cache...
```

### Wi-Fi

[これ](http://amazon.jp/dp/B003NSAMW2/mazgi-22/ref=nosim/)を使う

```shell-session
[mazgi@Ardbeg] $ ssh -lroot 172.30.255.19 'mkdir -p /lib/firmware'
[mazgi@Ardbeg] $ scp ~/Downloads/RT2870_Firmware_V22/rt2870.bin root@172.30.255.19:/lib/firmware/
rt2870.bin                                 100% 8192     8.0KB/s   00:00    
```

```shell-session
localhost ~ # /etc/init.d/swclock start
 * Setting the local clock based on last shutdown time ...         [ ok ]
localhost ~ # rc-update del hwclock boot
 * service hwclock removed from runlevel boot
localhost ~ # rc-update add swclock boot
 * service swclock added to runlevel boot
```

```shell-session
localhost ~ # grep -E '^#*s[0-9]+:' /etc/inittab
#s0:12345:respawn:/sbin/agetty -L 9600 ttyS0 vt100
#s1:12345:respawn:/sbin/agetty -L 9600 ttyS1 vt100
```