Resize the root partition

本文介绍如何通过命令行工具fdisk及图形界面工具Gparted来扩大Banana Pi设备上的Linux根文件系统分区,确保有足够的空间安装更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Most of the Linux distributions available for the Banana Pi can be downloaded as a disk image from Lemaker's web site.  Most of these images take up 4GB when unpacked and copied to an SD card.  There's a small Fat16 partition used for storing boot files, and a 3.7GB ext4 partition for storing the root file system.  

When you install updates on a new disk image, you may find that the Linux partition fills up before all the updates have installed.  It's a good idea to use an 8GB card (or larger), and resize the partition so that it fills the entire card.

If there's any important data on your SD card, you should back it up before you make any changes.

The card shouldn't be mounted while you resize it, so you need to put it in an SD card reader and plug it in to a Linux PC.  If you have a spare SD card, you can boot a Banana Pi with one SD card, and use it to resize the Linux partition on a second SD card.

Use this command to view the disks and their partions:

$ sudo fdisk -l
Disk /dev/mmcblk0: 7.4 GiB, 7948206080 bytes, 15523840 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: 0x59d0bd13

Device         Boot     Start       End  Blocks  Id System
/dev/mmcblk0p1           2048    104447   51200  83 Linux
/dev/mmcblk0p2         104448  15523839 7709696  83 Linux

Disk /dev/sda: 7.5 GiB, 7998537728 bytes, 15622144 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: 0x59d0bd13

Device    Boot     Start       End  Blocks  Id System
/dev/sda1           2048    104447   51200  83 Linux
/dev/sda2         104448   7167999 3531776  83 Linux

Expand the partition with fdisk

If you're not using a desktop environment, you can resize the partition from the command line using the fdisk command:

sudo fdisk /dev/sda

Fdisk will display a prompt where you can enter simple commands.  You're going to need to delete the second partition, create a new larger one, and write the changes to disk.  The partition table will be updated, but the data in the partition will be untouched.  

When you create the new partition you'll be prompted for a type, primary or extended.  Type 'p' for primary, and enter '2' when prompted for the partition number.  When prompted for the first and last sector numbers, just press enter to use the default values selected by fdisk.

The next step is to type 'w' to write the changes to the card.  Fdisk will automatically quit after this.

[bananapi@lemaker ~]$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.24.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
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
Select (default p): p
Partition number (2-4, default 2): 2
First sector (104448-15622143, default 104448): 
Last sector, +sectors or +size{K,M,G,T,P} (104448-15622143, default 15622143): 

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

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Reboot your Banana Pi so that the partition table gets reloaded.  At this point, the partition is much larger, but the file system only occupies half of it.  The final step is to enlarge the file system so that it fills the partition.  The next two commands check the integrity of the file system and then resize it:

[bananapi@lemaker ~]$ sudo e2fsck -f /dev/sda2
e2fsck 1.42.8 (20-Jun-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sda2: 82882/220752 files (0.1% non-contiguous), 724877/882944 blocks

[bananapi@lemaker ~]$ sudo resize2fs /dev/sda2
resize2fs 1.42.8 (20-Jun-2013)
Resizing the filesystem on /dev/sda2 to 1939712 (4k) blocks.
The filesystem on /dev/sda2 is now 1939712 blocks long.

The partition and its file system should now occupy the whole SD card. 

Expand the partition with Gparted

Some Linux distributions are supplied with a graphical tool called Gparted, which is available in the Fedora image for the Banana Pi.  It's available on many PC Linux distributions by default.  If you're using Fedora on your Banana Pi, you can use Gparted instead of fdisk.

Before you start, make sure the card is plugged into an SD card reader, and make sure the card isn't automatically mounted.  Launch Gparted by going to the Application Menu -> System -> Gparted.  

Select the card that you want to edit from the drop down menu on the right hand side of the tool bar.  In this case it's /dev/sda.  Click on the partition /dev/sda2, go to the Partition menu, and select Resize/Move.

Resize the partition
In the resize window, drag the right hand edge of the partition as far to the right as it will go, and click on the Resize button. 

Click the Apply button on the Gparted tool bar to write the changes to the card.  The partition will be enlarged, and the file system is automatically expanded to fill the partition.

 

http://banoffeepiserver.com/resize-the-root-file-system-partition.html

### 解决GPT分区表与MBR冲突的问题 在处理GPT分区表与MBR冲突时,可以使用`gdisk`或`sgdisk`工具来修复和转换分区表格式。以下是具体方法: #### 检测当前磁盘分区表类型 首先,确认磁盘当前使用的分区表类型(GPT或MBR): ```bash lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL,PARTUUID,PARTTYPE ``` 如果磁盘同时存在GPT和MBR分区表,则需要进行清理或转换。 #### 使用`gdisk`修复GPT分区表 `gdisk`是一个交互式工具,能够检测并修复GPT分区表问题。以下命令用于清理MBR备份并保留GPT分区表: ```bash sudo gdisk /dev/vda x # 进入专家模式 z # 销毁分区表并退出 y # 确认销毁现有分区表 n # 不保存更改到备用引导头 ``` 此操作将清除MBR备份,仅保留GPT分区表[^1]。 #### 使用`sgdisk`自动修复GPT分区表 `sgdisk`是`gdisk`的非交互式版本,适合脚本自动化。以下命令用于修复GPT分区表中的错误: ```bash sudo sgdisk --zap-all /dev/vda # 清除所有分区表信息 sudo sgdisk --mbrtogpt /dev/vda # 将MBR转换为GPT ``` 上述命令会清除磁盘上的所有分区信息,并重新创建GPT分区表[^2]。 --- ### 修复分区表重叠警告 当分区表出现重叠警告时,通常是因为分区边界定义不正确或某些分区覆盖了其他分区的空间。以下是解决方法: #### 使用`parted`检查分区重叠 运行以下命令以检查磁盘分区是否存在重叠: ```bash sudo parted /dev/vda print ``` 如果发现分区重叠,记录受影响的分区编号。 #### 使用`gdisk`调整分区边界 进入`gdisk`交互模式,手动调整分区边界以消除重叠: ```bash sudo gdisk /dev/vda d # 删除重叠的分区 n # 创建新分区并重新定义边界 w # 写入更改并退出 ``` 在重新定义分区时,确保每个分区的起始和结束扇区不与其他分区冲突。 #### 使用`fsck`检查文件系统完整性 调整分区后,运行文件系统检查以确保数据完整性: ```bash sudo fsck /dev/vdaX # 替换X为分区号 ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值