fudonghai

导航

 

原理是把原分区删除但记住其开始位置,然后从这个位置新建一个更大的分区,这样原来的数据就不会丢失。

原文地址: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

 贴一个实际操作的样张

posted on 2019-03-01 08:18  fudonghai  阅读(4029)  评论(0编辑  收藏  举报