环境
服务器安装有一个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。
这样我们就已经完成了文件系统大小调整的工作,下面让我们计算一下目标分区的大小,这个大小需要根据你disk里面的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)
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)
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.
至此,整个调整分区大小及创建新分区的操作全部完成。
blockdev --rereadpt /dev/hda
kernel can re-read the partitions table.
hdparm -z
is also okay, and easier to remember
同风 于 2009-05-14 23:54:59发表:
我深深地理解了“人外有人,天外有天”这句话
hantu 于 2009-05-14 21:17:32发表:
还是用gparted来得直观
yong009003 于 2009-05-14 20:29:07发表:
按高手所说的步骤进行分区调整了,能行得通,尽管其中有些小问题,但最终还是安全如意地调整好了分区大小,兄弟,谢谢啦!
主要问题是,重启后以下命令不用执行(执行时提示错误):
umount /home/
resize_reiserfs /dev/hda*(我的是hda8)
另外,我现在终于明确地知道Hex code (type L to list codes): a5的a5的深意了----文件系统的类型!可以在fdisk命令里使用l,即Command (m for help): l(小写L)查看!
yong009003 于 2009-05-14 11:52:45发表:
哦,我好像有些明白了,Hex code (type L to list codes): a5的a5是分区的卷标,是不是啊,各位高手?
yong009003 于 2009-05-14 11:47:10发表:
我终于看到希望,这对我来说,简直能称得上“救命草”了;因为我用其它分区工具调整/Home目录(单独一个分区)时,Grub竟然被损坏了,让我进不了系统!
最后我还是靠恢复分区表才把系统弄好的也!希望上面的方法能够帮我,我就先试试再说!谢谢啦!
另外,不知上面Hex code (type L to list codes): a5的a5是不是文件系统的一个代号啊?
………………
gary168 于 2009-04-02 19:31:54发表:
问题就是在安装源代码的应用程序,而此时/usr分区空间又不足,就得如此做了!当然最好对源代码熟悉,就不愁分区的问题,安装在LVM的分区可以灵活调整。
相思爱文 于 2009-03-27 21:50:29发表:
怕空间不够用lvm比较好。此贴收藏,有空练练
jagub 于 2009-03-27 21:46:50发表:
足够了!
gary168 于 2009-03-26 19:06:35发表:
有时为了更好地管理服务器,或多或少会增加一些工具,此时原先的分区有可能空间不足,就有如此了。用过Linux下的gpart分区工具(有点像windows的PQ),没有windows的PQ好用。
wangdu2002 于 2009-03-26 16:31:27发表:
嗯,讲得很详细,不过我都是用Gparted的LiveCD来搞分区,不过只适合个人用户。服务器还是开始就分好区为好,以后不必调整了。