menu E4b9a6's blog
rss_feed
E4b9a6's blog
有善始者实繁,能克终者盖寡。

Linux下扩展分区大小

作者:E4b9a6, 创建:2024-08-12, 字数:4363, 已阅:172, 最后更新:2024-08-12

在迁移 Linux 系统到新硬盘后,使用 df -h 打印大小,发现仍然显示原硬盘的大小

假设有2张存储卡,卡1安装了一个 Linux 系统(大小8G),卡2是等待迁移的新卡(大小64G)

将它们插入到一台 PC 中,并启动 fdisk 打印信息如下:

Bash
$ sudo fdisk -l

...

Disk /dev/sda: 7.32 GiB, 7864320000 bytes, 15360000 sectors
Disk model: MassStorageClass
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: 0x96247f4d

Device     Boot Start      End  Sectors  Size Id Type
/dev/sda1       32768 15040511 15007744  7.2G 83 Linux


Disk /dev/sdb: 59.64 GiB, 64034439168 bytes, 125067264 sectors
Disk model: MassStorageClass
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: 0x00000000

Device     Boot Start       End   Sectors  Size Id Type
/dev/sdb1        2048 125067230 125065183 59.6G  c W95 FAT32 (LBA)

使用 dd/dev/sda 内的Linux系统迁移到 /dev/sdb

Bash
sudo dd if=/dev/sda of=/dev/sdb bs=4M status=progress

确保迁移数据全部写入

Bash
sudo sync

将卡2插入设备中并开机,开机无误后查看系统盘大小:

Bash
$ df -h                            
Filesystem      Size  Used Avail Use% Mounted on
udev            415M     0  415M   0% /dev
tmpfs            98M  1.9M   96M   2% /run
/dev/mmcblk0p1  7.1G  1.8G  5.2G  26% /
tmpfs           488M     0  488M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           488M  4.0K  488M   1% /tmp
/dev/zram1       47M   11M   33M  25% /var/log
tmpfs            98M     0   98M   0% /run/user/0

/dev/mmcblk0p1 依然是原始卡1的8G大小,这是因为系统是原封不动的迁移,所以分区表信息没有变化

使用 fdisk 来确认当前硬盘的真实大小:

Bash
$ fdisk -l                         
Disk /dev/mmcblk0: 59.64 GiB, 64034439168 bytes, 125067264 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: 0x96247f4d

Device         Boot Start       End   Sectors  Size Id Type
/dev/mmcblk0p1      32768 125067263 125034496 59.6G 83 Linux

...

使用 fdisk 结合 resize2fs 调整分区大小,并扩展文件系统以利用整个硬盘的空间

  1. 使用 fdisk /dev/mmcblk0 启动fdisk程序,请确保是进入整个硬盘 mmcblk0 而不是 mmcblk0p1
  2. 输入 p 查看分区表,记下起始扇区
  3. 输入 d 按确认删除分区,请不用担心,这不会删除分区上的数据,待会重新创建分区时使用相同的起始扇区即可
  4. 输入 n 创建分区,输入 p 表示是主分区,分区号默认 1,然后输入先前的起始扇区,回车确认最大值作为结束扇区
  5. 输入 w 写入上述操作

以上操作实践如下:

Bash
$ fdisk /dev/mmcblk0                    

Welcome to fdisk (util-linux 2.38.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Command (m for help): p

Disk /dev/mmcblk0: 59.64 GiB, 64034439168 bytes, 125067264 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: 0x96247f4d

Device         Boot Start      End  Sectors  Size Id Type
/dev/mmcblk0p1      32768 15040511 15007744  7.2G 83 Linux

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

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

Created a new partition 1 of type 'Linux' and of size 59.6 GiB.
Partition #1 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): w
The partition table has been altered.
Syncing disks.

再次查看系统盘分区是否变成 64G :

Bash
$ df -h                            
Filesystem      Size  Used Avail Use% Mounted on
udev            415M     0  415M   0% /dev
tmpfs            98M  2.0M   96M   3% /run
/dev/mmcblk0p1   59G  1.8G   57G   4% /
tmpfs           488M     0  488M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           488M  4.0K  488M   1% /tmp
/dev/zram1       47M   12M   32M  27% /var/log
tmpfs            98M     0   98M   0% /run/user/0

[[replyMessage== null?"发表评论":"发表评论 @ " + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageResponse.total]])

还没有可以显示的留言...
gravatar
[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[getEnviron(messageItem.m_environ)]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[getEnviron(messageItem.m_environ)]]