延伸系統空間

啟動系統後,可以透過指令「df -m」檢查目前的磁碟空間的狀態,所使用的sd卡可能是8G、16G、32G或者更大,但系統初始的狀態,根目錄「/」只有6G多的空間,並且使用了4G的空間,其他空間未啟用,所以要把被隱藏的空間都設定在根目錄「/」中。

df 磁碟空間狀態

  • 系統初始狀態的磁碟空間:此sd卡為8G,進入系統後雖然後還可做延伸,但處理完也沒有多少空間可以使用

pi@bpi-iot-ros-ai:~$ df -m
Filesystem     1M-blocks  Used Available Use% Mounted on
/dev/mmcblk0p2      6831  4466      2002  70% /
devtmpfs             866     0       866   0% /dev
tmpfs                994     1       994   1% /dev/shm
tmpfs                994    10       985   1% /run
tmpfs                  5     1         5   1% /run/lock
tmpfs                994     0       994   0% /sys/fs/cgroup
tmpfs                199     1       199   1% /run/user/1000
/dev/mmcblk0p1       256    32       225  13% /media/pi/BPI-BOOT
pi@bpi-iot-ros-ai:~$ df -m
Filesystem     1M-blocks  Used Available Use% Mounted on
/dev/mmcblk0p2     29613  6176     22154  22% /
devtmpfs             866     0       866   0% /dev
tmpfs                994     1       994   1% /dev/shm
tmpfs                994    10       985   1% /run
tmpfs                  5     1         5   1% /run/lock
tmpfs                994     0       994   0% /sys/fs/cgroup
tmpfs                199     1       199   1% /run/user/1000
/dev/mmcblk0p1       256    33       224  13% /media/pi/BPI-BOOT
pi@bpi-iot-ros-ai:~$ df -m
Filesystem     1M-blocks  Used Available Use% Mounted on
/dev/mmcblk0p2     29613 13472     14858  48% /
devtmpfs             866     0       866   0% /dev
tmpfs                994     1       994   1% /dev/shm
tmpfs                994    10       985   1% /run
tmpfs                  5     1         5   1% /run/lock
tmpfs                994     0       994   0% /sys/fs/cgroup
tmpfs                199     1       199   1% /run/user/1000
/dev/mmcblk0p1       256    33       224  13% /media/pi/BPI-BOOT

經以上的動作,把要燒錄到emmc的linux image檔放在sd卡中,最少需要使用16G的sd卡,接下來就開始把一張32G的sd卡做延伸的動作

fdisk 重建分割區

執行指令sudo fdisk /dev/mmcblk0,過程如下

  • p:列出磁碟狀態,可以看出目前的/dev/mmcblk0是有29.7G,為一張32g的sd卡,目前有/dev/mmcblk0p1及/dev/mmcblk0p2兩個分割區。

  • d:刪除分割區,在partition number選擇2。

  • n:新增分割區,選擇分割區的型態為p(primary),partition number一樣選2,first sector這邊要注意一下,要看一開始用指令p列出的磁碟狀態中,/dev/mmcblk0p2的start為「729088」,所以這邊也要填入「729088」,last sector就使用預設值(default)也就是最大值,接著就會顯示新增的分割區內容。

  • w:將以下所執行的動作儲存寫入。

pi@bpi-iot-ros-ai:~$ sudo fdisk /dev/mmcblk0

Welcome to fdisk (util-linux 2.27.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: 29.7 GiB, 31914983424 bytes, 62333952 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: 0xf692356d

Device         Boot  Start      End  Sectors  Size Id Type
/dev/mmcblk0p1      204800   729087   524288  256M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      729088 15267839 14538752    7G 83 Linux

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

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

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

pi@bpi-iot-ros-ai:~$

resize2fs 改變分割區大小

完成以上動作並且儲存完成後,請重新開機,開機完成後,要再執行sudo resize2fs /dev/mmcblk0p2 指令改變分割區大小,執行過程如下:

pi@bpi-iot-ros-ai:~$ sudo resize2fs /dev/mmcblk0p2
[sudo] password for pi:
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/mmcblk0p2 is now 7700608 (4k) blocks long.

最後再使用df -m確認一下是否有修改完成

pi@bpi-iot-ros-ai:~$ df -m
Filesystem     1M-blocks  Used Available Use% Mounted on
/dev/mmcblk0p2     29613  4503     23826  16% /
devtmpfs             866     0       866   0% /dev
tmpfs                994     1       994   1% /dev/shm
tmpfs                994    10       985   1% /run
tmpfs                  5     1         5   1% /run/lock
tmpfs                994     0       994   0% /sys/fs/cgroup
tmpfs                199     1       199   1% /run/user/1000
/dev/mmcblk1p2      6831  4453      2015  69% /media/pi/BPI-ROOT
/dev/mmcblk1p1       256    32       225  13% /media/pi/BPI-BOOT
/dev/mmcblk0p1       256    33       224  13% /media/pi/BPI-BOOT1

Last updated