树莓派-使SD卡快速扩容

在树莓派实际使用当中,我们会发现SD卡的存储并没有得到充分的利用,是否有办法让可用空间变的更大,毫无疑问肯定是有的。
准备环境
设备:树莓派 3B(其他型号也可以)
SD卡容量:16G

目录

  • 使用raspi-config扩容(建议)
  • 利用fdisk和resize2fs扩容(不推荐)
  • 增大交换分区大小(建议)

使用raspi-config扩容(建议)

首先输入"df -h"命令可以看到,当前root分区总共只用了3.1G,而可用空间仅剩288M,但实际我们的存储是16G的,所以可用空间理论上不应该这么小

pi@raspberrypi:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       3.1G  2.7G  288M  91% /
devtmpfs        310M     0  310M   0% /dev
tmpfs           438M     0  438M   0% /dev/shm
tmpfs           438M   17M  421M   4% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           438M     0  438M   0% /sys/fs/cgroup
/dev/mmcblk0p1   43M   22M   21M  51% /boot
tmpfs            88M     0   88M   0% /run/user/1000
pi@raspberrypi:~$

此时可以利用"raspi-config"对root分区进行扩容,输入如下命令:

sudo raspi-config

选择"7 Advanced Options"回车
然后选择"A1 Expand Filesystem Ensures that all of ….",回车
询问是否修改root分区,直接回车
到首页面,按"Tab"键,选择"Finish",回车
询问是否重启,选择重启
重启完后,输入"df -h",会发现root分区已经扩容成功

pi@raspberrypi:~ $ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        15G  2.9G   11G  22% /
devtmpfs        433M     0  433M   0% /dev
tmpfs           438M     0  438M   0% /dev/shm
tmpfs           438M   12M  426M   3% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           438M     0  438M   0% /sys/fs/cgroup
/dev/mmcblk0p1  253M   53M  200M  21% /boot
tmpfs            88M     0   88M   0% /run/user/1000

利用fdisk和resize2fs扩容(不推荐)

利用"fdisk"和"resize2fs"命令确认mmcblk0p2节点的其实地址为:“532480”(注:根据实际情况确认该地址,要输错了会导致分区出错,很重要!!!),并对该分区重新创建:

pi@raspberrypi:~ $ sudo  fdisk  /dev/mmcblk0

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 #查看分区信息, /dev/mmcblk0p2 start地址很重要
Disk /dev/mmcblk0: 14.6 GiB, 15681454080 bytes, 30627840 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: 0xea7d04d6

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

Command (m for help): d #删除mmcblk0p2分区
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):

Using default response p.
Partition number (2-4, default 2): 2 #分区号选择2 即mmcblk0p2分区
First sector (2048-30627839, default 2048): 532480  #刚才查看的mmcblk0p2起始地址
Last sector, +/-sectors or +/-size{K,M,G,T,P} (532480-30627839, default 30627839): #默认直接回车

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

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

The signature will be removed by a write command.

Command (m for help): p #这里重新查看发现mmcblk0p2分区变大了
Disk /dev/mmcblk0: 14.6 GiB, 15681454080 bytes, 30627840 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: 0xea7d04d6

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

Filesystem/RAID signature on partition 2 will be wiped.

Command (m for help): w #保存,如果输错了,按q不保存重新来
The partition table has been altered.
Syncing disks.


pi@raspberrypi:~ $ sudo resize2fs /dev/mmcblk0p2 #利用resize2fs调整文件系统实际大小
resize2fs 1.44.5 (15-Dec-2018) 
Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mmcblk0p2 is now 3761920 (4k) blocks long.

然后重启!

增大交换分区大小(建议)

虽然可用空间变大了,但我们在使用的实际当中会发现有些卡顿,这是因为树莓派默认的给的swap分区只要100M。

关于交换分区的解释大致如下:
Swap分区是存储上一个特殊用途的分区或文件,是当系统内存不够用的时候,把物理内存中的一部分空间释放出来,以供当前运行的程序使用。那些被释放或交换出来的内容就会被临时保存到Swap分区中,等到程序需要时,再从Swap分区中恢复数据到内存中。

修改方法如下:

通过修改"/etc/dphys-swapfile"文件的"CONF_SWAPSIZE"字段达到扩大交换分区的目的(以M为单位)

pi@raspberrypi:~ $ sudo vi /etc/dphys-swapfile #输入如下命令

# /etc/dphys-swapfile - user settings for dphys-swapfile package
# author Neil Franklin, last modification 2010.05.05
# copyright ETH Zuerich Physics Departement
#   use under either modified/non-advertising BSD or GPL license


# this file is sourced with . so full normal sh syntax applies


# the default settings are added as commented out CONF_*=* lines




# where we want the swapfile to be, this is the default
#CONF_SWAPFILE=/var/swap


# set size to absolute value, leaving empty (default) then uses computed value
#   you most likely don't want this, unless you have an special disk situation
CONF_SWAPSIZE=2048 #将该字段的100改为2048


# set size to computed value, this times RAM size, dynamically adapts,
#   guarantees that there is enough swap without wasting disk space on excess
#CONF_SWAPFACTOR=2


# restrict size (computed and absolute!) to maximally this limit
#   can be set to empty for no limit, but beware of filled partitions!
#   this is/was a (outdated?) 32bit kernel limit (in MBytes), do not overrun it
#   but is also sensible on 64bit to prevent filling /var or even / partition
#CONF_MAXSWAP=2048

然后重启下服务

pi@raspberrypi:~ $ sudo /etc/init.d/dphys-swapfile restart
[ ok ] Restarting dphys-swapfile (via systemctl): dphys-swapfile.service.
pi@raspberrypi:~

我们就把Swap分区从100M调整到了2G,可通过"free -m"命令查看

pi@raspberrypi:~ $ free -m
              total        used        free      shared  buff/cache   available
Mem:            875         101         613          11         159         712
Swap:          2047           0        2047  #这里就是交换的内容
pi@raspberrypi:~ $
更多参考

学习自–编程那些年

你可能感兴趣的:(经验分享)