文章目录
1、还原备份到硬盘
这里会实现把硬盘分开30G的系统分区
和数据分区
,一些类似于小电影的数据就放在数据分区
,以后备份数据分区
的是不备份的,只备份系统分区
的数据。
1.1、硬盘分区
只有第一次在这个硬盘里面按照这个教程恢复备份的需从这里开始,之后就可以直接从 1.3
开始操作
把硬盘怼到树莓派的USB接口,我这边硬盘的磁盘是/dev/sda
。
使用fdisk
工具分区,前30G用来存放系统文件,后面剩余的用来放数据文件。
pi@raspbian:~$ sudo fdisk /dev/sda #进入磁盘工具
Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p #查看当前分区
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Disk model: 00AAKX-001CA0
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: 0x975f2ea8
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 8192 524287 516096 252M c W95 FAT32 (LBA)
/dev/sda2 524288 976773167 976248880 465.5G 83 Linux
Command (m for help): d #删除分区
Partition number (1,2, default 2): 2 #删除第2个分区
Partition 2 has been deleted.
Command (m for help): d #删除分区
Selected partition 1 #只剩下1个分区,默认删除那个分区
Partition 1 has been deleted.
Command (m for help): n #创建一个新的分区
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p #新的分区是主分区
Partition number (1-4, default 1): 1 #新分区的分区号是1
First sector (65535-976773167, default 65535): #不需要填,使用默认的65535
Last sector, +/-sectors or +/-size{K,M,G,T,P} (65535-976773167, default 976773167): +30G #新分区的大小是30G
Created a new partition 1 of type 'Linux' and of size 30 GiB.
Command (m for help): n #创建一个新的分区
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p #新的分区是主分区
Partition number (2-4, default 2): #默认新分区的分区号是2
First sector (62979135-976773167, default 62979135): #不需要填,使用默认的62979135
Last sector, +/-sectors or +/-size{K,M,G,T,P} (62979135-976773167, default 976773167): #不需要填,使用默认的到硬盘结束 976773167
Created a new partition 2 of type 'Linux' and of size 435.7 GiB.
Command (m for help): w #保存刚才的分区信息
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
1.2、格式化数据分区
pi@raspbian:~$ sudo mkfs.ext4 /dev/sda2
mke2fs 1.44.5 (15-Dec-2018)
Creating filesystem with 114224254 4k blocks and 28557312 inodes
Filesystem UUID: fd513a96-8850-47fa-9f70-46383446dec5
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
1.3、查看分区信息(重要)
pi@raspbian:~$ sudo fdisk -l
.
.
.
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Disk model: 00AAKX-001CA0
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: 0x1c9d33c6
Device Boot Start End Sectors Size Id Type
/dev/sda1 65535 62979134 62913600 30G 83 Linux
/dev/sda2 62979135 976773167 913794033 435.7G 83 Linux
这里要把系统分区 /dev/sda1
的结束End 62979134
给记下来,后面扩容的时候要用
1.4、挂载数据分区
cd ~
#创建目录HD,创建过就不需要再创建
mkdir HD
#挂载数据分区到HD目录
sudo mount /dev/sda2 HD
1.5、把备份镜像文件放到数据分区(如果数据分区本来就有这个镜像文件的话可以忽略这一步)
#把HD目录的拥有者改为pi用户
sudo chown pi.sudo HD
把备份镜像文件移动到数据分区
mv 备份文件 HD/
1.6、格式化系统分区
pi@raspbian:~$ sudo mkfs.ext4 /dev/sda1
mke2fs 1.44.5 (15-Dec-2018)
Creating filesystem with 7864200 4k blocks and 1966080 inodes
Filesystem UUID: 800e266b-c718-4f37-a162-f396daff7260
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
1.7、还原备份
pi@raspbian:~$ sudo dd bs=1M if=HD/20200727-init.img of=/dev/sda
记录了4900+0 的读入
记录了4900+0 的写出
5138022400 bytes (5.1 GB, 4.8 GiB) copied, 106.926 s, 48.1 MB/s
可以看到写速度比之前安装系统到SD卡里快多了。
2、扩容
还原备份之后,使用命令
pi@raspbian:~$ sudo fdisk -l
.
.
.
Device Boot Start End Sectors Size Id Type
/dev/sda1 8192 524287 516096 252M c W95 FAT32 (LBA)
/dev/sda2 524288 10033151 9508864 4.5G 83 Linux
可以看到,500G的硬盘只剩下这么还原的镜像的大小了,所以要把500G的容量扩充回来
2.1、取消挂载的/dev/sda2
因为之前还原系统时挂载了/dev/sda2
,所以现在要取消挂载。
sudo umount /dev/sda2
2.2、编辑分区
#进入磁盘工具
pi@raspbian:~$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p #查看当前分区
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Disk model: 00AAKX-001CA0
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: 0x1c9d33c6
Device Boot Start End Sectors Size Id Type
/dev/sda1 8192 524287 516096 252M c W95 FAT32 (LBA)
/dev/sda2 524288 10033151 9508864 4.5G 83 Linux
Command (m for help): d #删除第2个分区
Partition number (1,2, default 2): 2
Partition 2 has been deleted.
Command (m for help): n #新建分区
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p #新建主分区
Partition number (2-4, default 2): 2 #新建主分区的分区号是2
First sector (524288-976773167, default 589815): 524288 #这里输入刚刚查看的,/dev/sda2 的 Start 524288
Last sector, +/-sectors or +/-size{K,M,G,T,P} (524288-976773167, default 976773167): 62979134 #这里输入上面步骤 1.3 查看分区信息,记下的/dev/sda1 End 的数字 62979134
#这样这两个分区加起来的大小就刚好是30G
Created a new partition 2 of type 'Linux' and of size 29.8 GiB.
Partition #2 contains a ext4 signature.
Do you want to remove the signature? [Y]es/[N]o: n #刚刚分配的分区已经存在 ext4 格式的签名了,是否需要移除签名,这里就必须是n,不需要
Command (m for help): n #新建分区
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p): p #新建主分区
Partition number (3,4, default 3): 3 #新建主分区的分区号是3
First sector (62979135-976773167, default 62979135): #使用默认的62979135
Last sector, +/-sectors or +/-size{K,M,G,T,P} (62979135-976773167, default 976773167): #使用默认的 976773167
Created a new partition 3 of type 'Linux' and of size 435.7 GiB.
Partition #3 contains a ext4 signature.
Do you want to remove the signature? [Y]es/[N]o: n #刚刚分配的分区已经存在 ext4 格式的签名了,是否需要移除签名,因为我们要保留之前的数据分区的数据,所以这里是n,不需要
Command (m for help): p #查看分区信息
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Disk model: 00AAKX-001CA0
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: 0x1c9d33c6
Device Boot Start End Sectors Size Id Type
/dev/sda1 8192 524287 516096 252M c W95 FAT32 (LBA)
/dev/sda2 524288 62979134 62454847 29.8G 83 Linux
/dev/sda3 62979135 976773167 913794033 435.7G 83 Linux
Command (m for help): w #写入分区信息
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
2.3、完成扩容
编辑完分区后,使用命令
sudo resize2fs /dev/sda2
完成扩容
3、USB硬盘启动
3.1、更新固件
注意:固件要在SD卡启动的模式下更新
我只有个4代的树莓派,通过USB启动我也只是在这个4代的树莓派上面实现过,其它版本的我不知道行不行
树莓派默认的固件是不支持硬盘启动的,所以要更新固件才行。
下载最新的 固件文件,根据README.md
的命令
sudo rpi-eeprom-update -d -f ./pieeprom-xxxxxx.bin
更新固件,更新完毕之后,根据提示重启树莓派。
重启后使用命令
sudo rpi-eeprom-update
就能看到当前固件的信息。
3.2、系统启动
更新完固件后,树莓派关机->关电源->拔掉SD卡(不拔的话默认是SD卡启动的)->怼上硬盘到USB3.0接口->上电,如无意外的话就可以启动成功进入到系统了。
3.3、挂载数据分区
cd ~
#创建目录HD,创建过就不需要再创建
mkdir HD
#挂载数据分区到HD目录
sudo mount /dev/sda3 HD
#查看数据分区里面的数据,可以看到之前的文件还在的
ls HD/
3.4、开机自动挂载分区
编辑文件/etc/fstab
,在文件最后增加一行
/dev/sda3 /home/pi/HD ext4 defaults,noatime,nodiratime 0 0
然后重启