【树莓派学习笔记】从固态硬盘启动树莓派

SD卡读写速度实在是太慢了,不堪用,决定更新硬件,从固态硬盘启动树莓派。一方面提升性能,一方面延长寿命。

SD卡速度测试

笔者的树莓派使用SanDisk 64GB micro SD卡。

SanDisk 64GB micro SD卡

使用如下指令测试SD卡写入性能(oflag=direct选项将跳过内存缓存直接读写)。

$ time dd if=/dev/zero of=test bs=8k count=10000 oflag=direct
10000+0 records in
10000+0 records out
81920000 bytes (82 MB, 78 MiB) copied, 20.9216 s, 3.9 MB/s

real    0m20.927s
user    0m0.039s
sys     0m0.904s

这速度不予置评。测试读取性能。

$ time dd if=test of=/dev/null bs=8k count=10000 iflag=direct
10000+0 records in
10000+0 records out
81920000 bytes (82 MB, 78 MiB) copied, 4.81443 s, 17.0 MB/s

real    0m4.819s
user    0m0.043s
sys     0m0.289s

测完会生成一个test文件,记得删除。

硬件更新

使用的m.2转USB拓展板型号X862 V2.0,硬盘选用Kingston A400 m.2 2280 SATA接口SSD。

X862 V2.0

另外之前购买的风扇又坏了,因为选择的液压轴承不能承受全天候工作的强度。所以也更换了滚珠轴承的新风扇。
由于笔者仍然需要铝合金的散热外壳,而官方提供的螺丝配件不能满足要求,所以另外购买了铜柱、尼龙垫片和螺栓重新组装;为了更好的散热还给SSD加装了硅脂贴片。

finished pi

固态硬盘速度测试

创建分区并格式化

安装完成后开机,查看用fdisk工具磁盘设备。

$ sudo fdisk -l

... ...

Disk /dev/mmcblk0: 59.48 GiB, 63864569856 bytes, 124735488 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: 0x44cc6dad

Device         Boot  Start       End   Sectors  Size Id Type
/dev/mmcblk0p1        8192    532479    524288  256M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      532480 124735487 124203008 59.2G 83 Linux


Disk /dev/sda: 447.13 GiB, 480103981056 bytes, 937703088 sectors
Disk model: 2115
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 33553920 bytes

注意到设备/dev/mmcblk0就是SD卡,目前系统仍然运行在这上面;设备/dev/sda就是新安装的固态硬盘。

fdisk工具建立分区并格式化硬盘。

$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xfb2bec60.

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 (65535-937703087, default 65535):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (65535-937703087, default 937703087):

Created a new partition 1 of type 'Linux' and of size 447.1 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

使用指令sudo fdisk /dev/sda将打开一个ui,n是创建新分区,w是保存分区设置并退出。具体帮助可以用m打开。
使用指令mkfs格式化分区,笔者这里将硬盘格式化为ext4文件系统。

$ sudo mkfs -t ext4 /dev/sda1
mke2fs 1.46.2 (28-Feb-2021)
Creating filesystem with 117204694 4k blocks and 29302784 inodes
Filesystem UUID: 0a859952-4dd9-4553-ad71-44e4537f5e42
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
        102400000

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

再次查看分区情况。

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 447.13 GiB, 480103981056 bytes, 937703088 sectors
Disk model: 2115
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 33553920 bytes
Disklabel type: dos
Disk identifier: 0xfb2bec60

Device     Boot Start       End   Sectors   Size Id Type
/dev/sda1       65535 937703087 937637553 447.1G 83 Linux

挂载

查看已格式化分区。

$ sudo parted -l
Model: ASMT 2115 (scsi)
Disk /dev/sda: 480GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End    Size   Type     File system  Flags
 1      33.6MB  480GB  480GB  primary  ext4


Model: SD SN64G (sd/mmc)
Disk /dev/mmcblk0: 63.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      4194kB  273MB   268MB   primary  fat32        lba
 2      273MB   63.9GB  63.6GB  primary  ext4

挂载并修改权限。

mkdir ssd
sudo mount -t ext4 /dev/sda1 /home/pi/ssd/
sudo chown -R pi:pi /home/pi/ssd

注意:ext4文件系统不支持在挂载时设置权限,需要在挂载后手动设置。

查看磁盘和挂载情况。

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        59G   12G   44G  22% /
devtmpfs        1.7G     0  1.7G   0% /dev
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           759M  2.4M  757M   1% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
/dev/mmcblk0p1  253M   53M  201M  21% /boot
tmpfs           380M  4.0K  380M   1% /run/user/1000
/dev/sda1       440G   28K  417G   1% /home/pi/ssd

读写测试

写入性能。

$ time dd if=/dev/zero of=ssd/test bs=8k count=10000 oflag=direct
10000+0 records in
10000+0 records out
81920000 bytes (82 MB, 78 MiB) copied, 1.88336 s, 43.5 MB/s

real    0m1.887s
user    0m0.040s
sys     0m0.709s

芜湖起飞。读取性能。

$ time dd if=ssd/test of=/dev/null bs=8k count=10000 iflag=direct
10000+0 records in
10000+0 records out
81920000 bytes (82 MB, 78 MiB) copied, 1.56033 s, 52.5 MB/s

real    0m1.565s
user    0m0.026s
sys     0m0.429s

💥完爆SD卡。

系统拷贝

旧的系统需要拷贝到固态硬盘上。使用工具rpi-clone,安装教程参考作者的repo

$ sudo rpi-clone sda

Booted disk: mmcblk0 63.9GB                Destination disk: sda 480.1GB
---------------------------------------------------------------------------
Part      Size    FS     Label           Part   Size    FS    Label
1 /boot   256.0M  fat32  --              1      447.1G  ext4  --
2 root     59.2G  ext4   rootfs
---------------------------------------------------------------------------
== Initialize: IMAGE partition table - partition number mismatch: 2 -> 1 ==
1 /boot               (52.0M used)   : MKFS  SYNC to sda1
2 root                (12.0G used)   : RESIZE  MKFS  SYNC to sda2
---------------------------------------------------------------------------
Run setup script       : no.
Verbose mode           : no.
-----------------------:
** WARNING **          : All destination disk sda data will be overwritten!
-----------------------:

Initialize and clone to the destination disk sda?  (yes/no): yes
Optional destination ext type file system label (16 chars max):

Initializing
  Imaging past partition 1 start.
  => dd if=/dev/mmcblk0 of=/dev/sda bs=1M count=8 ...
  Resizing destination disk last partition ...
    Resize success.
  Changing destination Disk ID ...
  => mkfs -t vfat -F 32  /dev/sda1 ...
  => mkfs -t ext4  /dev/sda2 ...

Syncing file systems (can take a long time)
Syncing mounted partitions:
  Mounting /dev/sda2 on /mnt/clone
  => rsync // /mnt/clone with-root-excludes ...
  Mounting /dev/sda1 on /mnt/clone/boot
  => rsync /boot/ /mnt/clone/boot  ...

Editing /mnt/clone/boot/cmdline.txt PARTUUID to use a678da9c
Editing /mnt/clone/etc/fstab PARTUUID to use a678da9c
===============================
Done with clone to /dev/sda
   Start - 19:35:36    End - 19:43:37    Elapsed Time - 8:01

Cloned partitions are mounted on /mnt/clone for inspection or customizing.

Hit Enter when ready to unmount the /dev/sda partitions ...
  unmounting /mnt/clone/boot
  unmounting /mnt/clone
===============================

完成后关机拔掉SD卡即可,再次启动就是SSD了。

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       439G   12G  405G   3% /
devtmpfs        1.7G     0  1.7G   0% /dev
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           759M  992K  758M   1% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
/dev/sda1       253M   53M  201M  21% /boot
tmpfs           380M  4.0K  380M   1% /run/user/1000
$ sudo fdisk -l /dev/sda
Disk /dev/sda: 447.13 GiB, 480103981056 bytes, 937703088 sectors
Disk model: 2115
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 33553920 bytes
Disklabel type: dos
Disk identifier: 0xa678da9c

Device     Boot  Start       End   Sectors   Size Id Type
/dev/sda1         8192    532479    524288   256M  c W95 FAT32 (LBA)
/dev/sda2       532480 937703087 937170608 446.9G 83 Linux

参考

linux磁盘读写速度测试

X862 V2.0

linux格式化硬盘教程 linux格式化硬盘教程是什么

mount时发生错误:wrong fs type, bad option, bad superblock on

Unrecognized mount option “uid=33” or missing value

Homelab upgrade: New Raspberry Pi Cluster rackmount

rpi-clone