树莓派安装CentOS

in #cn6 years ago

之前在办公室放了块A+型的树莓派跑一些推送、隧道之类的零碎小任务,本来一直都比较稳定,但自从前几个月多加了一个定点爬虫之后就明显性能不足了。恰好这几天收拾老房子的时候找到一块2代的B型版,这简直是久旱逢甘霖啊~

想到今年发布了支持树莓派2和3的armhfp版CentOS,所以在距2018年一建考试只剩21天之际的今天,忍不住手痒要折腾下了。

051111083756523.png


准备SD卡

树莓派2用的是Micro SD卡,把一块容量大于4G的Micro SD卡插在读卡器上接入电脑上后,先找到对应的设备符号:打开「Terminal」,执行diskutil list命令。

    #diskutil list 
    /dev/disk0 (internal):
       #:                       TYPE NAME                    SIZE       IDENTIFIER
       0:      GUID_partition_scheme                         500.3 GB   disk0
       1:                        EFI EFI                     314.6 MB   disk0s1
       2:                 Apple_APFS Container disk1         500.0 GB   disk0s2
    
    /dev/disk1 (synthesized):
       #:                       TYPE NAME                    SIZE       IDENTIFIER
       0:      APFS Container Scheme -                      +500.0 GB   disk1
                                     Physical Store disk0s2
       1:                APFS Volume Macintosh HD            278.8 GB   disk1s1
       2:                APFS Volume Preboot                 21.8 MB    disk1s2
       3:                APFS Volume Recovery                519.0 MB   disk1s3
       4:                APFS Volume VM                      5.4 GB     disk1s4
    
    /dev/disk2 (external, physical):
       #:                       TYPE NAME                    SIZE       IDENTIFIER
       0:     FDisk_partition_scheme                        *15.9 GB    disk2

可以看到系统给SD卡分配的设备符是/dev/disk2。当SD卡中含有macOS能够识别的分区时(比如FAT32、extFAT之类的),会被Finder自动挂载,如果不将系统自动挂载的分区卸载掉的话,后面dd烧写操作将会提示device busy,导致无法写入。在Terminal中执行diskutil umount命令:

    #diskutil umount `mount | grep "/dev/disk2" | awk '{print $3}'`
    Volume NONAME on disk2s1 unmounted

顺便提一下的这里的坑,如果SD卡挂载的分区名称中含有空格,例如"NO NAME"(如果烧录中断重新插卡就会是这个名字),那么用上面的命令进行卸载会失败,请手动将“NO NAME”中间的空格去掉。

烧录镜像

SD卡准备好,接下来就要开始下载镜像,官方的下载地址:

http://mirror.centos.org/altarch/7/isos/armhfp/

这里我选择了 CentOS-Userland-7-armv7hl-RaspberryPI-Minimal-1804-sda.raw.xz 这个压缩包,文件名中:Minimal表示最小化(无GUI),RaspberryPI表示树莓派定制版。下载完成检查checksum后,进行解压

    xz -d  /Users/holmesian/Downloads/CentOS-Userland-7-armv7hl-RaspberryPI-Minimal-1804-sda.raw.xz

这个压缩包解压之后得到的是raw文件,和其他发行版提供的img格式不一样,如果是在windows操作,就不能用WinImage烧了。在macOS和Linux下用dd进行烧录:

    sudo dd bs=4m if=/Users/holmesian/Downloads/CentOS-Userland-7-armv7hl-RaspberryPI-Minimal-1804-sda.raw of=/dev/disk2

其中值得关注的是bs参数,bs = bytes 同时设置读/写缓冲区的字节数(等于设置ibs和obs)。正确调整bs参数可以显著提升烧录的速度。

在dd命令的执行过程中可以通过 killall -SIGINFO dd 来查看传输进度,相应的反馈是这样的:

    #sudo killall -SIGINFO dd
    17+0 records in
    16+0 records out
    67108864 bytes transferred in 26.935566 secs (2491459 bytes/sec)

    678+0 records in
    678+0 records out
    2843738112 bytes transferred in 1062.362222 secs (2676807 bytes/sec)

烧录完成之后,用 diskutil eject 命令弹出SD卡,成功弹出后将SD卡从电脑上拔下来:

    #diskutil eject /dev/disk2
    Disk /dev/disk2 ejected

配置系统

把SD卡插入树莓派,网口连接上配置好DHCP服务的网络,加上1000毫安(为5.0 W)的电源,指示灯正常闪烁的话就可以通过分配的IP地址SSH连接上CentOS,初始用户名和密码如下:

UserName:root
Password:centos

成功登陆之后首先应当将可用分区扩展到整个SD卡:

    /usr/bin/rootfs-expand

接着修改root密码,并新建一个用户holmesian:

    #useradd holmesian
    #passwd holmesian
    
    #usermod -aG root holmesian

将新建的用户加入sudo列表

    #visudo

增加内容,让holmesian用户能够免密码使用sudo:

holmesian ALL=(ALL) NOPASSWD: ALL

启用BBR换回iptables服务SELinux放行SSH端口就参照以前的内容了。

yum源配置

很遗憾,国内几乎没有可用的armfhp版CentOS源,所以只能用其自带的源,好在速度勉强可以接受。至于EPEL源的话官方提供了一个未经质检或测试的自动建设EPEL源,使用方法如下

    vi /etc/yum.repos.d/epel.repo

添加下列内容

    [epel]
    name=Epel rebuild for armhfp
    baseurl=https://armv7.dev.centos.org/repodir/epel-pass-1/
    enabled=1
    gpgcheck=0

测试之后发现,这是一个基于fedora aarch64版的源,所以可以将epel.repo的内容改为上海交大的源:

    [epel]
    name=Extra Packages for Enterprise Linux 7
    baseurl=http://ftp.sjtu.edu.cn/fedora/epel/7/aarch64/
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

鉴于两个都是没有质保的,且支持armhfp的CentOS源是在是太少,请需要的TX自己选择吧。配置好yum源之后就赶紧更新,在装点必要的编译工具吧:

    yum update
    yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel 

其他内容

后续在这里更新一点相关的Tips吧。

内核更新

对于树莓派2和树莓派3,直接yum update会将内核更新,无须做任何其他的事情,只要重新开机便能运用新的内核。

armv7hl与其他版

你也许应知道 armv7hl 平台的发行版本被称为 CentOS Userland Linux 而不是 CentOS Linux。个中原因是由于 CentOS 替换结构 SIG 有权加入其它组件,取代某些组件,或不创建上游发行版本的某些组件。内核就是最显著的例子,因为 kernel 3.10.0-*(即 CentOS 7 x86_64 发行版本用的内核)并不支持 armv7hl 底板/结构。

更多内容详见官方说明

内核版本

    # uname -a
    Linux RPi 4.14.65-v7.1.el7 #1 SMP Mon Aug 20 19:04:20 UTC 2018 armv7l armv7l armv7l GNU/Linux

发行版信息

    # cat /etc/os-release
    NAME="CentOS Linux"
    VERSION="7 (AltArch)"
    ID="centos"
    ID_LIKE="rhel fedora"
    VERSION_ID="7"
    PRETTY_NAME="CentOS Linux 7 (AltArch)"
    ANSI_COLOR="0;31"
    CPE_NAME="cpe:/o:centos:centos:7"
    HOME_URL="https://www.centos.org/"
    BUG_REPORT_URL="https://bugs.centos.org/"
    
    CENTOS_MANTISBT_PROJECT="CentOS-7"
    CENTOS_MANTISBT_PROJECT_VERSION="7"
    REDHAT_SUPPORT_PRODUCT="centos"
    REDHAT_SUPPORT_PRODUCT_VERSION="7"
Sort:  

Congratulations @holmesian! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:
SteemitBoard and the Veterans on Steemit - The First Community Badge.

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.033
BTC 63966.64
ETH 3055.32
USDT 1.00
SBD 3.87