Linux分區擴容至整個SD卡


原理是把原分區刪除但記住其開始位置,然后從這個位置新建一個更大的分區,這樣原來的數據就不會丟失。

原文地址:https://wiki.somlabs.com/index.php/How_to_resize_Debian_9.2_root_partition_to_fit_SD_card

最近在玩樹莓派,發現raspex-ubuntu-64bit-18.10-lxde-1380mb-181022.zip寫到SD卡上后根分區/的使用量為100%,而還有10G的剩余空間並沒有被使用,於是找一找辦法。

我是把需要擴展的SD卡通過讀卡器掛到樹莓派上來做操作的,這樣根分區可以被刪除。如果使用這張需要擴展的SD卡啟動系統,然后把自己的根分區刪除,我猜測Linux應該不允許這種方式的操作。

 

一,查看信息

IMPORTANT!!! Even a small typo might cause data loss - be extremely careful while resizing partition!

By default size of the root partition of Debian is about 800MB. You need to resize it to use full SD card capacity. First step is to get SD card device name:

上面是安全警告

root@somlabs:~# fdisk -l
省略若干


Disk /dev/mmcblk1: 3.7 GiB, 3965714432 bytes, 7745536 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: 0x9de2d76a

Device         Boot Start     End Sectors  Size Id Type
/dev/mmcblk1p1      16384 1628159 1611776  787M 83 Linux

主要得到: Disk /dev/mmcblk1里面的Start是16384,這個例子SD卡里面只有一個Disk /dev/mmcblk1分區,實際樹莓派有兩個分區,並且分區名字叫/dev/sda,注意靈活使用。

 

二,對Disk /dev/mmcblk1進行操作

root@somlabs:~# fdisk /dev/mmcblk1

Welcome to fdisk (util-linux 2.29.2).
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/mmcblk1: 3.7 GiB, 3965714432 bytes, 7745536 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: 0x9de2d76a

Device         Boot Start     End Sectors  Size Id Type
/dev/mmcblk1p1      16384 1628159 1611776  787M 83 Linux
這里也看到Start扇區也是16384
Command (m
for help): d 把這個分區刪除,注意樹莓派刪除的是/dev/sda2
Selected partition
1 Partition 1 has been deleted. Command (m for help): p 列出分區信息
Disk
/dev/mmcblk1: 3.7 GiB, 3965714432 bytes, 7745536 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: 0x9de2d76a 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 分區序號,樹莓派是2
First sector (
2048-7745535, default 2048): 16384 這里要使用起始扇區16384這個值
Last sector,
+sectors or +size{K,M,G,T,P} (16384-7745535, default 7745535): 默認回車取最大, Created a new partition 1 of type 'Linux' and of size 3.7 GiB. Partition #1 contains a ext4 signature. Do you want to remove the signature? [Y]es/[N]o: n 不改變文件分區信號?? 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).

 

三,重啟系統,我是在宿主機上重啟系統,繼續操作

root@somlabs:~# reboot

四,resize file system

root@somlabs:~# resize2fs /dev/mmcblk1p1
resize2fs 1.43.4 (31-Jan-2017)
Filesystem at /dev/mmcblk1p1 is m[  122.795430] EXT4-fs (mmcblk1p1): resizing filesystem from 201472 to 966144 blocks
ounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
[  123.861183] EXT4-fs (mmcblk1p1): resized filesystem to 966144
The filesystem on /dev/mmcblk1p1 is now 966144 (4k) blocks long.

五,查看大小

root@somlabs:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       3.7G  499M  3.0G  15% /
devtmpfs         88M     0   88M   0% /dev
tmpfs           248M     0  248M   0% /dev/shm
tmpfs           248M  3.4M  245M   2% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           248M     0  248M   0% /sys/fs/cgroup
tmpfs            50M     0   50M   0% /run/user/0

 貼一個實際操作的樣張


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM