温故而希望有知新――调整磁盘分区(基于ext文件系统)

CentOS、Fedora等redhat的分支,默认是不支持 ReiserFS这种文件系统的,所以如果要调整磁盘分区,像reiserfsprogs软件包是不可用的。若需要调整磁盘分区,人们首先想到的正是这个工具,网上的大部分教程都是针对这个文件系统。对于ext3可以说是非常少介绍的。

对于CentOS系统,若要支持,则需升级内核,相对麻烦。

一直以来,ext2/3/4搭配上LVM,对于redhat系列的系统是可行的了。

事实上,redhat一直自带有类似reiserfsprogs的工具的集合。

下面,我在CentOS环境下复习一遍。(PS:涉及磁盘的操作通常都是属于危险级的,请尽量慎重,最好在虚拟机下熟练操作后,再进行实体机的操作)

准备:对所要操作的分区进行备份。如果调整的是类似/、/boot等重要的分区,涉及到安全和卸载分区,所以强烈建议:备份后,在single模式下操作,或者在便携式系统里挂载所有调整的分区进行操作。

以下,对/dev/sdb1进行操作。

[root@huazi ~]# df                    
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda6              6234004   1558348   4358984  27% /
tmpfs                   255748       420    255328   1% /dev/shm
/dev/sda1                99150     29109     64921  31% /boot
/dev/sda5              1007896    113564    843132  12% /home
/dev/sda2              4031680   2667816   1159064  70% /usr
/dev/sdb1              2063504    132068   1826616   7% /mnt/sdb
[root@huazi ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda6             6.0G  1.5G  4.2G  27% /
tmpfs                 250M  420K  250M   1% /dev/shm
/dev/sda1              97M   29M   64M  31% /boot
/dev/sda5             985M  111M  824M  12% /home
/dev/sda2             3.9G  2.6G  1.2G  70% /usr
/dev/sdb1             2.0G  129M  1.8G   7% /mnt/sdb
 
因为分区很少使用,于是决定给/mnt/sdb 1G的容量,剩下的大约1G从/dev/sdb1分区中划分出来。
记下/mnt/sdb所在磁盘分区 /dev/sdb1
 
卸载磁盘分区,然后检测文件系统,留意文件数。调整后,查看文件是否完整最直观的就是查看文件数。
 
[root@huazi ~]# umount /dev/sdb1
[root@huazi ~]# fsck -n /dev/sdb1
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
/dev/sdb1: clean, 289/131072 files, 41174/524112 blocks
 
 
调整文件系统大小,提示必须先对文件进行check。
注意:调整的文件系统大小(1024M)必须要大于原来的已使用容量(129M)
否则,原来文件就基本上不能完全保留下来。
 
[root@huazi ~]# resize2fs /dev/sdb1 1024M
resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/sdb1' first.
[root@huazi ~]# e2fsck -f /dev/sdb1
e2fsck 1.41.12 (17-May-2010)
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/sdb1: 289/131072 files (0.0% non-contiguous), 41174/524112 blocks
 
对文件系统大小进行调整,调整成功。
 
[root@huazi ~]# resize2fs /dev/sdb1 1024M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/sdb1 to 262144 (4k) blocks.
The filesystem on /dev/sdb1 is now 262144 blocks long.
 
删除分区,重新进行分区。
关键:重新调整的分区要大于等于文件系统大小(1024M)
查看磁盘分区情况
 
 
[root@huazi ~]# fdisk /dev/sdb 
 
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
 
Command (m for help): p
 
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xe3f7864f
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         261     2096451   83  Linux
 
计算说明:
所需要的最小柱面数:文件系统的大小/Units(单位都是bytes)
                    1024*1024*1024/8225280=130.5416,取131以上即可。
 
结束的柱面数:      Start+所需要的最小柱面数
                    1+131=132
 
Command (m for help): d
Selected partition 1
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-261, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): 132
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
嘎嘎,到这里其实差不多成功了(差不多就是还没成功咯)
为确定已经调整分区成功,reboot吧。
 
 
 
启动后,检查调整结果。
[root@huazi ~]# mount /dev/sdb1 /mnt/sdb
[root@huazi ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda6             6.0G  1.5G  4.2G  27% /
tmpfs                 250M  420K  250M   1% /dev/shm
/dev/sda1              97M   29M   64M  31% /boot
/dev/sda2             3.9G  2.6G  1.2G  70% /usr
/dev/sda5             985M  111M  824M  12% /home
/dev/sdb1            1008M  129M  829M  14% /mnt/sdb
 
 
[root@huazi ~]# fdisk /dev/sdb
 
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
 
Command (m for help): p
 
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xe3f7864f
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
 
 
[root@huazi ~]# umount /dev/sdb1
[root@huazi ~]# fsck -f /dev/sdb1
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
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/sdb1: 289/65536 files (0.0% non-contiguous), 36933/262144 blocks
 
 
好吧,经过以上的检测推断,调整分区成功啦。
调整出来的1G可以随意用啦(剩下的工作不用说了吧,分区,格式化,挂载)。
 
 
 
嘎嘎,就此打住。。。
 
 

本文出自 “华子―伪IT伪文艺真发呆” 博客,谢绝转载!

你可能感兴趣的:(ext,调整磁盘分区,ext3文件系统)