在线调整ext3磁盘容量


在线调整ext3磁盘容量
 
调整前先用 tar cf /disk/home.tar /home 进行打包备份。
 
1、查看磁盘分区情况
fdisk -l
Disk /dev/sda: 291.9 GB, 291999055872 bytes
255 heads, 63 sectors/track, 35500 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes 
磁盘大小,块大小,柱面情况 
 
[root@localhost ~]# df -B 4k  
Filesystem           4K-blocks      Used Available Use% Mounted on  
/dev/sda2               990087    500195    438786 54% /  
/dev/sda5              3575306     41828   3348932   2% /home  
/dev/sda1                11659      2638      8419 24% /boot  
tmpfs                    67424         0     67424   0% /dev/shm  
df –B 4k 以4k为以单位查看,方便计算后面分区修改后的大小
 
[root@localhost ~]# df -h  
Filesystem            Size Used Avail Use% Mounted on  
/dev/sda2             3.8G 2.0G 1.7G 54% /  
/dev/sda5              14G 164M   13G   2% /home  
/dev/sda1              46M   11M   33M 24% /boot  
tmpfs                 264M     0 264M   0% /dev/shm
磁盘分区及目录的挂载情况
 
2、记录磁盘柱面情况以及需要划分的分区大小
   
计算终止柱面号
e.g: 原先/usr/local的大小为5000 MB, 起始柱面号为2550, 终止柱面号为3157. 那么我现在要将它的大小改为1000MB, 应该这样计算终止柱面号( [3.2] = 4 ):
终止柱面号 = 2550 + [(1000 * 1024 * 1024 ) / 8225280] = 2678
使用fdisk删除原来的分区(以前分区的数据不会丢失!). 然后新建一个分区, 新分区于旧分区唯一的区别就在于分区大小不一样, 这表现在他们终止柱面号不同.
增加分区大小: 终止柱面号设置为空闲空间的最后一个柱面号.
减小分区大小: 终止柱面号由计算得出.
 
3、调整步骤
umount /dev/sda5    #卸载分区
fsck -n /dev/sda5   # -n参数只输出磁盘信息,不修复
tune2fs -O ^has_journal /dev/sda5   #将ext3转换为ext2,因为只有ext2支持调整分区大小
e2fsck -f /dev/sda5 #检查ext2文件系统是否正常
接着调整分区大小,但是要保证调整后的分区大小要大于当前分区已经使用的空间大小。
比如当前分区已使用180M,至少要保证这个分区预留1G的空间。
resize2fs /dev/sda5 5000M  #调整分区为5G
fdisk /dev/sda   #注意此处是sda,非sda5。  
 
重新建立分区信息(注意分区标识符一定要选对,此处是sda5,所以删除的时候选择5)
[root@localhost ~]# fdisk /dev/sda  
注意这里是/dev/sda而非/dev/sda5  
The number of cylinders for this disk is set to 2610.  
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): m  
Command action  
   a   toggle a bootable flag  
   b   edit bsd disklabel  
   c   toggle the dos compatibility flag  
   d   delete a partition  
   l   list known partition types  
   m   print this menu  
   n   add a new partition  
   o   create a new empty DOS partition table  
   p   print the partition table  
   q   quit without saving changes 
   s   create a new empty Sun disklabel  
   t   change a partition's system id  
   u   change display/entry units  
   v   verify the partition table  
   w   write table to disk and exit  
   x   extra functionality (experts only)  
Command (m for help):  
删除分区(/dev/sda5)  
Command (m for help): d  
Partition number (1-5): 5  
创建新分区  
Command (m for help): n  
First cylinder (773-2610, default 773): 773  
这里会被询问填写新分区的大小,开始柱面默认即可,现在关键介绍结束柱面的计算。 
 由前面数据可知,文件系统的大小为1280000*4K,为了确保我们的分区足够大以包含文件系统,这里可以增加文件系统大小的3%——5%来作为分区大小。 
 
因此分区大小为Size=1280000*4K*1.05=5376000K  
Last cylinder or +size or +sizeM or +sizeK (773-2610, default 2610): +5376000K  
接着使用参数a  
Command (m for help): a  
Partition number (1-5): 5  
选项a的解释为    toggle a bootable flag 对于这个选项不是很理解,但一个明显的作用就是为新的分区分配区号,这里选择5(/dev/sda5)。 
 
接着将修改写入分区表并退出fdisk  
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
重启系统。 
reboot
fsck -n /dev/sda5    #检查调整后的分区信息
tune2fs -j /dev/sda5   #将ext2转换成ext3
reboot #再次重启系统
启动后看看/home的大小
df -B 4k
df -h
fdisk -l
 
如果没有被挂载,编辑 /etc/fstab 文件后,执行 mount -a 进行挂载。
对于分割出来的空闲分区可以用 fdisk /dev/sda 创建新的分区。注意磁盘柱面号的启始与截止位置。

相关内容

    暂无相关文章