背景
Raspberry Pi 4B 使用 TF(T-Flash 或 Micro SD) 卡安装操作系统,我的 TF 卡的容量为 32G,容量较小,手头刚好有一块从大学时用的笔记本(用了6年多)拆下来的机械硬盘(1TB),打算使用这块硬盘对我的树莓派进行硬盘扩容。
扩容思路
我的树莓派使用 Ubuntu 系统,扩容的思路是将系统的/目录迁移至机械硬盘,/boot目录仍保留在 TF 卡中,最终效果如下:
图中的/dev/sda1是我的机械硬盘。
扩容步骤
对硬盘进行分区与格式化
首先需要对硬盘进行分区与格式化,使用命令sudo fdisk -l查看硬盘信息,默认为/dev/sda,对硬盘进行分区,命令如下:
sudo fdisk /dev/sda
具体分区方法可输入m查阅,我只创建了一个分区,名称为/dev/sda1。
将分区/dev/sda1格式化为ext4文件系统,命令如下:
sudo mkfs.ext4 /dev/sda1
最后我的硬盘信息如下(使用命令sudo fdisk -l查看):
Disk /dev/sda: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: JPVX-75JC3T0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 8C82A3DA-4F40-404E-9C1B-B19ACE5536AF
Device Start End Sectors Size Type
/dev/sda1 2048 1953525134 1953523087 931.5G Linux filesystem
记录下Disk identifier信息(8C82A3DA-4F40-404E-9C1B-B19ACE5536AF)。
将/目录迁移至机械硬盘
将/目录迁移至机械硬盘,命令如下:
dd if=/dev/mmcblk0p2 of=/dev/sda1
在树莓派中,/目录的挂载分区默认为/dev/mmcblk0p2,可通过命令df -h查看。
修改cmdline.txt
在cmdline.txt中添加如下内容(将 TF 卡从树莓派中拔出接入电脑进行修改):
root=PARTUUID=8C82A3DA-4F40-404E-9C1B-B19ACE5536AF
修改后的cmdline.txt内容为:
console=serial0,115200 dwc_otg.lpm_enable=0 console=tty1 root=LABEL=writable root=PARTUUID=8C82A3DA-4F40-404E-9C1B-B19ACE5536AF rootfstype=ext4 rootwait fixrtc quiet splash
最后,将 TF 卡插入树莓派重新启动即可