piCore 编译 ADB 小记


在 8G 的树莓派 4B 上运行 piCore 并且编译一个 ADB. 编译好后在树莓派 3B 上运行, 且开机启动 frp. 包含了 piCore 的最基本的配置方法.

如果编译一个删一个, 而且每个都单线程编译的话, 可能可以在树莓派3B的1G内存下直接编译, 不过我也没试过~ 然后交叉编译啥的就更不会了😵

1. 烧录 piCore

Tiny Core 目前的版本是 13.X, 在官网的下载页面底部有各种移植版的下载页面. 13.X 版各种架构的下载页面在这里. 这次选择的是 armv7 架构的 piCore-13.1.0.zip.

将压缩包解压, 得到 piCore-13.1.0.img. 使用 lsblk 确定 SD 卡为设备 /dev/sdb, 使用 dd 写入. (每次写的时候都要检查好几遍, 生怕设备写成硬盘…)

sudo dd if=piCore-13.1.0.img of=/dev/sdb bs=1M status=progress

写完也可以用 lsblk 或者 fdisk 检查一下, 可以看到现在有两个分区, 第二个 Linux 的只有 16MB, 需要在开机后 expand 一下

$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 30 GiB, 32212254720 bytes, 62914560 sectors
Disk model: SD/MMC/MS PRO
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: 0xe85b7916

Device     Boot  Start    End Sectors Size Id Type
/dev/sdb1         8192 139263  131072  64M  c W95 FAT32 (LBA)
/dev/sdb2       139264 172031   32768  16M 83 Linux

树莓派需要连接网线, 上电后可以直接 ssh, 用户名为 tc, 初始密码为 piCore, 不能以 root 登录. 第一次开机时, 需要生成 SSH key, 所以会比较慢.

2. 基本配置

登录后, 有两个最重要的事情

  1. 备份刚生成的 SSH key (还有 ssh-copy-id 导入的公钥之类的)

    filetool.sh -b
    
  2. fdisk 扩展之前只有 16MB 的 SD 卡分区. 用 p 查看当前分区, 记录下第二个分区 (/dev/mmcblk0p2) 的起始块号 (StartLBA). 用 d 删除第二个分区, 再用 n 新建分区, 起始块号与之前相同, 结束块号在最后. 最后用 w 写入分区表.

    $ sudo fdisk -u /dev/mmcblk0
    
    The number of cylinders for this disk is set to 983040.
    There is nothing wrong with that, but this is larger than 1024,
    and could in certain setups cause problems with:
    1) software that runs at boot time (e.g., old versions of LILO)
    2) booting and partitioning software from other OSs
       (e.g., DOS FDISK, OS/2 FDISK)
    
    Command (m for help): p
    Disk /dev/mmcblk0: 30 GB, 32212254720 bytes, 62914560 sectors
    983040 cylinders, 4 heads, 16 sectors/track
    Units: sectors of 1 * 512 = 512 bytes
    
    Device       Boot StartCHS    EndCHS        StartLBA     EndLBA    Sectors  Size Id Type
    /dev/mmcblk0p1    128,0,1     127,3,16          8192     139263     131072 64.0M  c Win95 FAT32 (LBA)
    /dev/mmcblk0p2    128,0,1     639,3,16        139264     172031      32768 16.0M 83 Linux
    
    Command (m for help): d
    Partition number (1-4): 2
    
    Command (m for help): n
    Partition type
       p   primary partition (1-4)
       e   extended
    p
    Partition number (1-4): 2
    First sector (16-62914559, default 16): 139264
    Last sector or +size{,K,M,G,T} (139264-62914559, default 62914559):
    Using default value 62914559
    
    Command (m for help): p
    Disk /dev/mmcblk0: 30 GB, 32212254720 bytes, 62914560 sectors
    983040 cylinders, 4 heads, 16 sectors/track
    Units: sectors of 1 * 512 = 512 bytes
    
    Device       Boot StartCHS    EndCHS        StartLBA     EndLBA    Sectors  Size Id Type
    /dev/mmcblk0p1    128,0,1     127,3,16          8192     139263     131072 64.0M  c Win95 FAT32 (LBA)
    /dev/mmcblk0p2    1023,3,16   1023,3,16       139264   62914559   62775296 29.9G 83 Linux
    
    Command (m for help): w
    The partition table has been altered.
    Calling ioctl() to re-read partition table
    fdisk: WARNING: rereading partition table failed, kernel still uses old table: Device or resource busy
    

    注意, 写入后会有一个 warning, 但可以忽略.

    最后重启树莓派, resize 分区二

    sudo resize2fs /dev/mmcblk0p2
    

由于包的源在国外, 可能会比较慢, 可以用代理

export http_proxy='http://192.168.1.123:8889'

安装一些必要的包

tce-load -wi bash vim openssh file htop git

这些是编译需要的—compiletc 包含了 gcc, binutils, m4, make 等; 还需要 coreutils, cmake

tce-load -wi compiletc coreutils \
         cmake autoconf automake perl5 \
         squashfs-tools

tcroot 的 shell 设置成 bash

sudo sed -i '/root/s_/bin/sh_/bin/bash_' /etc/passwd
sudo sed -i '/tc/s_/bin/sh_/bin/bash_' /etc/passwd

弄好后需要备份一下

filetool.sh -b

3. 各种依赖

准备 ADB 需要的依赖, 有的可以用 tce-load 直接下载

tce-load -wi \
         libtool libtool-dev \
         zstd zstd-dev \
         liblz4 liblz4-dev \
         libusb libusb-dev \
         pcre2 pcre2-dev

剩下的就要自己编译了. 所有编译和安装都在 /tmp 下进行.

3.1. protobuf

安装方法见 README, 下载源码 protobuf-cpp-3.21.4.tar.gz 编译 (ps. nproc 在包 coreutils 中)

cd /tmp
tar xf protobuf-cpp-3.21.4.tar.gz
cd protobuf-3.21.4
./configure
make -j$(nproc)

然后打包

sudo make DESTDIR=/tmp/dest-protobuf install
cd /tmp
mksquashfs dest-protobuf/ tbt-protobuf-3.21.4.tcz

存入SD卡中并设置开机装载

mv tbt-protobuf-3.21.4.tcz /mnt/mmcblk0p2/tce/optional/
echo 'tbt-protobuf-3.21.4.tcz' >> /mnt/mmcblk0p2/tce/onboot.lst

3.2. brotli

repo 下载 brotli-1.0.9.tar.gz 编译打包

cd /tmp
tar xf brotli-1.0.9.tar.gz
cd brotli-1.0.9
mkdir out && cd out
../configure-cmake              # 会有几个 warning, 不用管
make
sudo make DESTDIR=/tmp/dest-brotli install
cd /tmp
mksquashfs dest-brotli/ tbt-brotli-1.0.9.tcz
mv tbt-brotli-1.0.9.tcz /mnt/mmcblk0p2/tce/optional/
echo 'tbt-brotli-1.0.9.tcz' >> /mnt/mmcblk0p2/tce/onboot.lst

3.3. GoogleTest

repo 下载 googletest-release-1.12.1.tar.gz , 根据 README 编译打包

cd /tmp
tar xf googletest-release-1.12.1.tar.gz
cd googletest-release-1.12.1
mkdir build && cd build
cmake ..
make
sudo make DESTDIR=/tmp/dest-googletest install
cd /tmp
mksquashfs dest-googletest/ tbt-googletest-1.12.1.tcz
mv tbt-googletest-1.12.1.tcz /mnt/mmcblk0p2/tce/optional/
echo 'tbt-googletest-1.12.1.tcz' >> /mnt/mmcblk0p2/tce/onboot.lst

三个依赖编译并安装完成后, 手动 load 这三个包

tce-load -i \
         tbt-protobuf-3.21.4.tcz \
         tbt-brotli-1.0.9.tcz \
         tbt-googletest-1.12.1.tcz

3.4. Go

这里下载已经编译好的 Go 语言工具链 (go1.19.linux-armv6l.tar.gz). 由于 Go 编译器只是安装时需要的依赖, 不用打包, 直接解压到 /usr/local 并修改 PATH 即可.

sudo tar -C /usr/local/ -xf go1.19.linux-armv6l.tar.gz
export PATH=/usr/local/go/bin:$PATH

之后编译 adb 时会用到 https 代理和 CA 证书

export https_proxy='http://192.168.1.123:8889'

# this is needed for golang while building adb
# https://stackoverflow.com/a/67622500/11938767
export SSL_CERT_FILE=/usr/local/etc/ssl/certs/ca-certificates.crt

4. 编译 ADB

repo 下载 release android-tools-31.0.3p2.tar.xz (ps. 不大, 只有20M+, 千万不要带着 submodule clone, 5555…)

tar xf android-tools-31.0.3p2.tar.xz
cd android-tools-31.0.3p2
mkdir build && cd build
cmake ..
make
sudo make DESTDIR=/tmp/dest-android-tools install
cd /tmp
mksquashfs dest-android-tools/ tbt-android-tools-31.0.3p2.tcz
echo 'tbt-android-tools-31.0.3p2.tcz' >> /mnt/mmcblk0p2/tce/onboot.lst
tce-load -i tbt-android-tools-31.0.3p2.tcz

然后运行 adb, 没错误就代表成功啦~

在编译时, Go 在 home 目录下会建一个文件夹 go, 可以直接删了

rm -rf ~/go

最后一定要记得备份一下文件 (虽然其实也没啥要备份的)

filetool.sh -b

最后的最后, 可以备份一下 /mnt/mmcblk0p2/tce.

此时就可以放到树莓派3上跑了, 剩下的也可以直接在3上配置.

5. 内网穿透

为了远程访问方便, 加一个 frp 的包.

下载 frp_0.44.0_linux_arm.tar.gz/tmp 下, 解压, 制作包

tar xf frp_0.44.0_linux_arm.tar.gz
mkdir -vp dest-frp/usr/local/{bin,etc/frp}
mv frp_0.44.0_linux_arm/frp{c,s} dest-frp/usr/local/bin/
mv frp_0.44.0_linux_arm/frp*.ini dest-frp/usr/local/etc/frp/

# 这里可以改一下配置
cat <<ConfigEndsHERE > dest-frp/usr/local/etc/frp/frpc.ini
[common]
server_addr = 127.0.0.1
server_port = 7000

[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000
ConfigEndsHERE

# 都改为 root
sudo chown -R root: dest-frp

mksquashfs dest-frp/ tbt-frp-0.44.0.tcz
mv tbt-frp-0.44.0.tcz /mnt/mmcblk0p2/tce/optional/
echo 'tbt-frp-0.44.0.tcz' >> /mnt/mmcblk0p2/tce/onboot.lst

目录结构如下

dest-frp
└── usr
    └── local
        ├── bin
        │   ├── frpc
        │   └── frps
        └── etc
            └── frp
                ├── frpc_full.ini
                ├── frpc.ini
                ├── frps_full.ini
                └── frps.ini

最后, 把 frpc 的开机脚本放到 /opt/bootlocal.sh 中 (ps. /opt 下有两个开机脚本: bootlocal.shbootsync.sh, 后者会把前者放到后台运行, 以免阻塞开机进程)

cat <<'CmdEndsHERE' >> /opt/bootlocal.sh
# wait for network and then start frpc
# for loop from https://stackoverflow.com/a/24897831/11938767
i=1
while [ $i -le 20 ]; do
  sleep 1
  touch /tmp/waiting-${i}s
  i=$(expr $i + 1)
  if pidof udhcpc > /dev/null; then
    /usr/local/bin/frpc -c /usr/local/etc/frp/frpc.ini >/tmp/frpc.log 2>&1 &
    break
  fi
done
CmdEndsHERE

根据 /etc/init.d/dhcp.sh, 在执行 frpc 之前 DHCP 可能还没启动, 所以需要测试 DHCP 是否启动. 由 /etc/init.d/services/dhcp, 可以使用 pidof udhcpc 测试 DHCP 状态.

再次备份, 重启, 就可以直接通过 frp 访问树莓派啦~

filetool.sh -b

6. 小结

感觉用 piCore 再自己编译打包各种东西着实有点麻烦了, 可能直接把 Raspberry Pi OS 配置好再在 raspi-config 里设置成 SD 卡只读会更简单也更靠谱一点. 不过还是挺有意思的, 而且也学到了一些东西, 算是把之前 LFS 的编译命令用了下.

由于自己编译的几个包都是开机直接 load 的, 可能导致开机速度慢了不少. 实测从上电到 frp 连接成功需要大约 25s, reboot 则需要 34s 左右.

然后如果运行期间 frp 断掉或者出其他什么状况的话是无解的, 只能重启. 准备买一个米家的智能开关, 出问题就直接远程断电重开.


Authorthebesttv
Created2022-08-09 23:51
Modified2022-12-05 22:13
Generated2024-06-11 02:39
VersionEmacs 29.3 (Org mode 9.6.15)
Rawpicore-adb.org