树莓派安装openwrt并扩容overlay分区

安装的参考

如何安装

步骤

  1. 使用disk genius把内存卡的分区删除,新建一个分区并采用fat32格式化
  2. 用win32diskimage写入镜像

出现问题:

  1. 其他电脑连接wifi
    后无法获取ip,而路由器插上网线后在上级路由里面也查不到ip

解决方案:

连接wifi【openwrt】后,手动修改电脑的ip:192.168.10.x,子网掩码24,网关192.168.10.1,然后就和op在同一网段了,此时访问192.168.10.1即可。

然后在op设置界面:【网络】-【接口】-【LAN】-【修改】里面,将【IPv4网关】设置为192.168.1.1,将【使用自定义的 DNS 服务器】设置为114.114.114.114,此时op便可以获取上级路由的网络(如果不清楚上级路由的情况,则改为dhcp协议,然后用ip扫描来找到op的lan地址)

扩容的参考资料

“【转载】树莓派安装openwrt后扩容-参考资料”

“【转载】openwrt的overlay分区不足解决方法”

使用fdisk扩容的尝试(使用另一台linux)

确定内存卡的盘符

拔掉openwrt的内存卡,换上linux的卡,输入命令:df -h查看硬盘情况

判断/dev/sda是装有openwrt的内存卡,其中2是装有系统的部分(猜测)

卸载盘符

umount /dev/sda2

用fdisk命令开始重新分区

  1. 进入分区程序sudo fdisk /dev/sda
  2. 查看当前分区情况:p
#终端
pi@raspberrypi:~ $ 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: 7.4 GiB, 7983857664 bytes, 15593472 sectors
Disk model: Storage Device
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: 0xf222735e
Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       8192   139263   131072   64M  c W95 FAT32 (LBA)
/dev/sda2        147456  2113535  1966080  960M 83 Linux
/dev/sda3       2113536 15593471 13479936  6.4G 83 Linux

删除分区2、3

删除分区:d

选择删除分区2:2

重复一次,删除3

重新新建分区2

然后新建分区:

分区类型选择主分区:p

分区号输入2:

起始扇区选择原来的点:147456

最后一个扇区选择默认:enter

这样就新建了一个分区2,并且分区包含了剩下的全部空间。

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       8192   139263   131072   64M  c W95 FAT32 (LBA)
/dev/sda2        147456  2113535  1966080  960M 83 Linux
/dev/sda3       2113536 15593471 13479936  6.4G 83 Linux

Command (m for help): d
Partition number (1-3, default 3): 3

Partition 3 has been deleted.

Command (m for help): d
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
First sector (2048-15593471, default 2048): 147456
Last sector, +/-sectors or +/-size{K,M,G,T,P} (147456-15593471, default 15593471):

Created a new partition 2 of type 'Linux' and of size 7.4 GiB.
Partition #2 contains a squashfs signature.

Do you want to remove the signature? [Y]es/[N]o: n

Command (m for help): w

The partition table has been altered.
Syncing disks.

使修改生效

sudo e2fsck /dev/sda2
sudo resize2fs /dev/sda2

出现错误

pi@raspberrypi:~ $ sudo e2fsck /dev/sda2
e2fsck 1.44.5 (15-Dec-2018)
ext2fs_open2: Bad magic number in super-block
e2fsck: Superblock invalid, trying backup blocks...
e2fsck: Bad magic number in super-block while trying to open /dev/sda2

The superblock could not be read or does not describe a valid ext2/ext3/ext4
filesystem.  If the device is valid and it really contains an ext2/ext3/ext4
filesystem (and not swap or ufs or something else), then the superblock
is corrupt, and you might try running e2fsck with an alternate superblock:
    e2fsck -b 8193 
 or
    e2fsck -b 32768 

/dev/sda2 contains a squashfs file system
pi@raspberrypi:~ $ sudo resize2fs /dev/sda2
resize2fs 1.44.5 (15-Dec-2018)
resize2fs: Bad magic number in super-block while trying to open /dev/sda2
Couldn't find valid filesystem superblock.

这个方法暂时无法使用

在openwrt里面直接扩容

查看设备

输入lsblk

得到

OpenWrt# lsblk
\NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0         7:0    0 787.3M  0 loop /overlay
mmcblk0     179:0    0   7.4G  0 disk
├─mmcblk0p1 179:1    0    64M  0 part /boot
└─mmcblk0p2 179:2    0   960M  0 part /rom

得知可以扩容的id为mmcblk0

开始扩容

fdisk /dev/mmcblk0

使用p查看分区

得到:

Command (m for help): p
Disk /dev/mmcblk0: 7.45 GiB, 7983857664 bytes, 15593472 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: 0x5452574f

Device         Boot  Start     End Sectors  Size Id Type
/dev/mmcblk0p1 *      8192  139263  131072   64M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      147456 2113535 1966080  960M 83 Linux

得知还有6个多G的空间可用

mkfs.ext4 /dev/mmcblk0p3

第一次重启前全程记录

Using username "root".
root@192.168.1.177's password:
OpenWrt# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root               172.8M    172.8M         0 100% /rom
tmpfs                     1.9G      7.8M      1.8G   0% /tmp
/dev/loop0              785.3M    139.8M    645.4M  18% /overlay
overlayfs:/overlay      785.3M    139.8M    645.4M  18% /
/dev/mmcblk0p1           63.9M     17.3M     46.5M  27% /boot
tmpfs                   512.0K         0    512.0K   0% /dev
cgroup                    1.9G         0      1.9G   0% /sys/fs/cgroup
overlayfs:/overlay      785.3M    139.8M    645.4M  18% /opt/docker
OpenWrt# lsblk
\NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0         7:0    0 787.3M  0 loop /overlay
mmcblk0     179:0    0   7.4G  0 disk
├─mmcblk0p1 179:1    0    64M  0 part /boot
└─mmcblk0p2 179:2    0   960M  0 part /rom
OpenWrt# \fdisk /dev/mmcblk0

Welcome to fdisk (util-linux 2.35.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/mmcblk0: 7.45 GiB, 7983857664 bytes, 15593472 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: 0x5452574f

Device         Boot  Start     End Sectors  Size Id Type
/dev/mmcblk0p1 *      8192  139263  131072   64M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      147456 2113535 1966080  960M 83 Linux

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):
First sector (2048-15593471, default 2048): +3G
Value out of range.
First sector (2048-15593471, default 2048): +1G
Value out of range.
First sector (2048-15593471, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-15593471, default 15593471): +3G

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

Command (m for help): p
Disk /dev/mmcblk0: 7.45 GiB, 7983857664 bytes, 15593472 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: 0x5452574f

Device         Boot  Start     End Sectors  Size Id Type
/dev/mmcblk0p1 *      8192  139263  131072   64M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      147456 2113535 1966080  960M 83 Linux
/dev/mmcblk0p3        2048 6293503 6291456    3G 83 Linux

Partition table entries are not in disk order.

Command (m for help): w
The partition table has been altered.
Failed to add partition 3 to system: Resource busy

The kernel still uses the old partitions. The new table will be used at the next reboot.
Syncing disks.

OpenWrt# fdisk /dev/mmcblk0

Welcome to fdisk (util-linux 2.35.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/mmcblk0: 7.45 GiB, 7983857664 bytes, 15593472 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: 0x5452574f

Device         Boot  Start     End Sectors  Size Id Type
/dev/mmcblk0p1 *      8192  139263  131072   64M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      147456 2113535 1966080  960M 83 Linux
/dev/mmcblk0p3        2048 6293503 6291456    3G 83 Linux

Partition table entries are not in disk order.

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help): d
Partition number (1-3, default 3): 3

Partition 3 has been deleted.

Command (m for help): p
Disk /dev/mmcblk0: 7.45 GiB, 7983857664 bytes, 15593472 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: 0x5452574f

Device         Boot  Start     End Sectors  Size Id Type
/dev/mmcblk0p1 *      8192  139263  131072   64M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      147456 2113535 1966080  960M 83 Linux

Command (m for help): w
The partition table has been altered.
Failed to remove partition 3 from system: No such device or address

The kernel still uses the old partitions. The new table will be used at the next reboot.
Syncing disks.

OpenWrt# fdisk /dev/mmcblk0

Welcome to fdisk (util-linux 2.35.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/mmcblk0: 7.45 GiB, 7983857664 bytes, 15593472 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: 0x5452574f

Device         Boot  Start     End Sectors  Size Id Type
/dev/mmcblk0p1 *      8192  139263  131072   64M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      147456 2113535 1966080  960M 83 Linux

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
First sector (2048-15593471, default 2048): 2113536
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2113536-15593471, default 15593471): +3G

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

Command (m for help): p
Disk /dev/mmcblk0: 7.45 GiB, 7983857664 bytes, 15593472 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: 0x5452574f

Device         Boot   Start     End Sectors  Size Id Type
/dev/mmcblk0p1 *       8192  139263  131072   64M  c W95 FAT32 (LBA)
/dev/mmcblk0p2       147456 2113535 1966080  960M 83 Linux
/dev/mmcblk0p3      2113536 8404991 6291456    3G 83 Linux

Command (m for help): w
The partition table has been altered.
Syncing disks.

OpenWrt# mkfs.ext4 /dev/mmcblk0p3
mke2fs 1.45.6 (20-Mar-2020)
Discarding device blocks: done
Creating filesystem with 786432 4k blocks and 196608 inodes
Filesystem UUID: 84f1fcef-0224-4409-a211-45f800551e81
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

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

OpenWrt# reboot

重启后把以前的文件复制到扩容的文件夹

mount -t ext4 /dev/mmcblk0p3 /mnt/mmcblk0p3

发现该目录还不存在

OpenWrt# mount -t ext4 /dev/mmcblk0p3 /mnt/mmcblk0p3
mount: /mnt/mmcblk0p3: mount point does not exist.

于是先使用mkdir /mnt/mmcblk0p3mount -t ext4 /dev/mmcblk0p3 /mnt/mmcblk0p3

再使用df -h查询得到

OpenWrt# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root               172.8M    172.8M         0 100% /rom
tmpfs                     1.9G      7.5M      1.8G   0% /tmp
/dev/loop0              785.3M    139.8M    645.4M  18% /overlay
overlayfs:/overlay      785.3M    139.8M    645.4M  18% /
/dev/mmcblk0p1           63.9M     17.3M     46.5M  27% /boot
tmpfs                   512.0K         0    512.0K   0% /dev
cgroup                    1.9G         0      1.9G   0% /sys/fs/cgroup
overlayfs:/overlay      785.3M    139.8M    645.4M  18% /opt/docker
/dev/mmcblk0p3            2.9G      9.0M      2.7G   0% /mnt/mmcblk0p3

说明已经挂载成功

然后看看loop0指向的是哪个目录,发现overlay目录为/overlay

于是将其内容拷贝,命令为:cp -r /overlay/* /mnt/mmcblk0p3

然后卸载umount /dev/mmcblk0p3

提示busy,可以参考linux挂载卸载不掉 umount target is busy

不过因为op上没有安装相应的命令,于是我先尝试reboot

重启之后发现不会自动挂载,再次手动挂载mount -t ext4 /dev/mmcblk0p3 /mnt/mmcblk0p3

再次手动卸载umount /dev/mmcblk0p3

成功

打开openwrt的管理界面

【挂载点】-【生成配置】(如果找不到后面的设备可以使用这一步)-【(/mnt/mmcblk0p3)修改】

重启之后,成功!(之前失败是因为忘记勾选启用此挂载点)