当前位置:首页 > 嵌入式 > 嵌入式软件
[导读]介绍如何调整linux分区大小

环境

服务器安装有一个60G的硬盘,目前已经划分为/、/home、/chroot和/swap,详细的分区信息如何下所示:


Command (m for help): p

Disk /dev/hda: 61.4 GB, 61492838400 bytes
255 heads, 63 sectors/track, 7476 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 915 7349706 83 Linux
/dev/hda2 916 1414 4008217+ 83 Linux
/dev/hda3 1415 7476 48693015 5 Extended
/dev/hda5 1415 1477 506016 82 Linux swap
/dev/hda6 1478 7476 48186936 83 Linux



然后现在希望从/home分区里面分出大约4GB的空间用于安装FreeBSD。

过程

首先,我们需要调整文件系统的大小(注意是文件系统,而不是分区的大小),然后对调整完毕的文件系统进行检查,然后再进一步调整分区的大小。

文章以我自己用的系统为例子,实际操作时根据情况调整即可。

用惯了传统调整硬盘工具的人可能会认为文件系统和硬盘的分区之间是统一的,实际上他们之间并不是完整的整体(这也就是为什么BSD可以在一个硬盘分区里面制作多个文件系统分区)。想像一下带有移动式书架的书橱。你可以通过移动式书架调整书橱里面书籍的位置。把书当成文件系统,你可以在书橱里面放满书籍,也可以通过移动式书架调整书橱的空间。当然也可以通过书架调整书橱里面书籍存放的位置。如下图所示:


|<-- hda1------>|<-- hda2------>|
+---------------+---------------+
|XXXXXXXXXXXXXXX|XXXXXXXXXX| |
|XXXXXXXXXXXXXXX|XXXXXXXXXX| |
+---------------+---------------+



硬盘分区/dev/hda1中的文件系统占满了整个分区,而/dev/hda2中的文件系统只占用了2/3左右的分区。

基本的原理就是这样,现在让我们开始调整/dev/hda6中文件系统的大小:


[root@ringmail root]# umount /home
[root@ringmail root]# resize_reiserfs -s 43000M /dev/hda6

<-------------resize_reiserfs, 2002------------->
reiserfsprogs 3.6.4

You are running BETA version of reiserfs shrinker.
This version is only for testing or VERY CAREFUL use.
Backup of you data is recommended.

Do you want to continue? [y/N]:y
Processing the tree: 0%
....20%....40%....60%....80%....100% left 0, 301020 /sec

nodes processed (moved):
int 56 (0),
leaves 8124 (0),
unfm 6313240 (0),
total 6321420 (0).

check for used blocks in truncated region

ReiserFS report:
blocksize 4096
block count 11008000 (12046734)
free blocks 4678034 (5716736)
bitmap block count 336 (368)

Syncing..done



由于/home原来的大小是47057,而我们希望在/home里面划分4G的空间出来,取整数/home的目标大小就是43000。所以在resize_reiserfs时为-s提供大小为43000M。

这样我们就已经完成了文件系统大小调整的工作,下面让我们计算一下目标分区的大小,这个大小需要根据你的fdisk里面的Units值来计算,因为硬盘分区都是按照柱面来划分的,基本的计算方法为:43000*1024*1024/8225280=5481.7304699657640834111422346716,然后我们取最接近的值5482+1478=6960来作为分区结束的柱号,这里面的1478是/home分区开始的柱面号,在前面的fdisk里面可以看到。

调整完文件系统的大小以后,再使用以下命令以文件系统进行全面的检查:


[root@ringmail root]# reiserfsck --check --fix-fixable /dev/hda6



现在我们已经完成文件系统的大小调整工作,但是硬盘分区的大小仍然没有调整,还是跟先前一样大(可以想像我们从书橱里面拿走了几本书,而书架的大小并没有调整,依然占用着原来的空间)。

现在我们要开始进行整个操作里面最危险的操作:如果没有必要的话,最好不要进行下去:-D,不过不必过于担心,按照以下操作进行即可。

启动fdisk,并按照以下步骤进行:


[root@ringmail root]# fdisk /dev/hda

The number of cylinders for this disk is set to 7476.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/hda: 61.4 GB, 61492838400 bytes
255 heads, 63 sectors/track, 7476 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 915 7349706 83 Linux
/dev/hda2 916 1414 4008217+ 83 Linux
/dev/hda3 1415 7476 48693015 5 Extended
/dev/hda5 1415 1477 506016 82 Linux swap
/dev/hda6 1478 7476 48186936 83 Linux

Command (m for help): d
Partition number (1-6): 6

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (1478-7476, default 1478): 1478
Last cylinder or +size or +sizeM or +sizeK (1478-7476, default 7476): 6960

Command (m for help): p

Disk /dev/hda: 61.4 GB, 61492838400 bytes
255 heads, 63 sectors/track, 7476 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 915 7349706 83 Linux
/dev/hda2 916 1414 4008217+ 83 Linux
/dev/hda3 1415 7476 48693015 5 Extended
/dev/hda5 1415 1477 506016 82 Linux swap
/dev/hda6 1478 6960 44042166 83 Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource
busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.



这里需要说明的是,由于内核仍然使用旧的分区表,我需要通过重新启动才可以刷新刚制作的分区信息,所以在进行完重划分区操作以后,我对系统进行了重新启操作。系统重启后再继续后续的操作过程。

通过以上步骤,我们已经完成了硬盘分区大小调整的操作,接下来回到重新调整文件系统大小的问题上来。由于硬盘分区是按照柱面来计划的,我们要在resize_reiserfs步骤上就一步到位调整文件系统大小与硬盘分区大小正好相等是比较困难的事情。所以一般是调硬盘分区比文件系统要大一点点,然后再通过以下命令进行同步(当然如果你不在意硬盘分区里面存在部分空间的话也可以忽略以下步骤):


[root@ringmail root]# umount /home/
[root@ringmail root]# resize_reiserfs /dev/hda6

<-------------resize_reiserfs, 2002------------->
reiserfsprogs 3.6.4

ReiserFS report:
blocksize 4096
block count 11010541 (11008000)
free blocks 4680574 (4678034)
bitmap block count 337 (336)

Syncing..done



完成以后原有分区的大小调整操作已经结束,接下来需要创建新分区:


[root@ringmail root]# fdisk /dev/hda

The number of cylinders for this disk is set to 7476.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/hda: 61.4 GB, 61492838400 bytes
255 heads, 63 sectors/track, 7476 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 915 7349706 83 Linux
/dev/hda2 916 1414 4008217+ 83 Linux
/dev/hda3 1415 7476 48693015 5 Extended
/dev/hda5 1415 1477 506016 82 Linux swap
/dev/hda6 1478 6960 44042166 83 Linux

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (6961-7476, default 6961):
Using default value 6961
Last cylinder or +size or +sizeM or +sizeK (6961-7476, default 7476):
Using default value 7476

Command (m for help): t
Partition number (1-7): 7
Hex code (type L to list codes): a5
Changed system type of partition 7 to a5 (FreeBSD)

Command (m for help): p

Disk /dev/hda: 61.4 GB, 61492838400 bytes
255 heads, 63 sectors/track, 7476 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 915 7349706 83 Linux
/dev/hda2 916 1414 4008217+ 83 Linux
/dev/hda3 1415 7476 48693015 5 Extended
/dev/hda5 1415 1477 506016 82 Linux swap
/dev/hda6 1478 6960 44042166 83 Linux
/dev/hda7 6961 7476 4144738+ a5 FreeBSD

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource
busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.



至此,整个调整分区大小及创建新分区的操作全部完成。

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

CPU亲和度通过限制进程或线程可以运行的CPU核心集合,使得它们只能在指定的CPU核心上执行。这可以减少CPU缓存的失效次数,提高缓存命中率,从而提升系统性能。

关键字: Linux 嵌入式

在Linux系统性能优化中,内存管理与网络连接处理是两大核心领域。vm.swappiness与net.core.somaxconn作为关键内核参数,直接影响系统在高负载场景下的稳定性与响应速度。本文通过实战案例解析这两个...

关键字: Linux 内存管理

对于LLM,我使用b谷歌Gemini的免费层,所以唯一的成本是n8n托管。在使用了n8n Cloud的免费积分后,我决定将其托管在Railway上(5美元/月)。然而,由于n8n是开源的,您可以在自己的服务器上托管它,而...

关键字: 人工智能 n8n Linux

在Linux系统管理中,权限控制是安全运维的核心。本文通过解析/etc/sudoers文件配置与组策略的深度应用,结合某金融企业生产环境案例(成功拦截98.7%的非法提权尝试),揭示精细化权限管理的关键技术点,包括命令别...

关键字: Linux 用户权限 sudoers文件

Linux内核中的信号量(Semaphore)是一种用于资源管理的同步原语,它允许多个进程或线程对共享资源进行访问控制。信号量的主要作用是限制对共享资源的并发访问数量,从而防止系统过载和数据不一致的问题。

关键字: Linux 嵌入式

在云计算与容器化技术蓬勃发展的今天,Linux网络命名空间(Network Namespace)已成为构建轻量级虚拟网络的核心组件。某头部互联网企业通过命名空间技术将测试环境资源消耗降低75%,故障隔离效率提升90%。本...

关键字: Linux 云计算

在Linux内核4.18+和主流发行版(RHEL 8/Ubuntu 20.04+)全面转向nftables的背景下,某电商平台通过迁移将防火墙规则处理效率提升40%,延迟降低65%。本文基于真实生产环境案例,详解从ipt...

关键字: nftables Linux

在Linux设备驱动开发中,等待队列(Wait Queue)是实现进程睡眠与唤醒的核心机制,它允许进程在资源不可用时主动放弃CPU,进入可中断睡眠状态,待资源就绪后再被唤醒。本文通过C语言模型解析等待队列的实现原理,结合...

关键字: 驱动开发 C语言 Linux

在Unix/Linux进程间通信中,管道(pipe)因其简单高效被广泛使用,但默认的半双工特性和无同步机制容易导致数据竞争。本文通过父子进程双向通信案例,深入分析互斥锁与状态机在管道同步中的应用,实现100%可靠的数据传...

关键字: 管道通信 父子进程 Linux

RTOS :RTOS的核心优势在于其实时性。它采用抢占式调度策略,确保高优先级任务能够立即获得CPU资源,从而在最短时间内完成处理。RTOS的实时性是通过严格的时间管理和任务调度算法实现的,能够满足对时间敏感性要求极高的...

关键字: Linux RTOS
关闭