myguangzhou
作者myguangzhou2012-09-11 23:38
技术经理, Uniwise

原来linux不用LVM也能扩展文件系统

字数 8164阅读 14688评论 7赞 2
以前总以为UNIX/LINUX使用了lvm才能扩展或缩小卷和文件系统,今天才发现我错了:即使没有lvm,linux也能扩展卷和及其相应的文件系统而不会丢失原有数据。
以下是测试步骤,记录备查。
 
1、系统是redhat linux 6
[root@localhost ku]# uname -r
2.6.32-131.0.15.el6.x86_64
[root@localhost ku]# more /etc/redhat-release
Red Hat Enterprise Linux Server release 6.1 (Santiago)
[root@localhost ku]#
 
2、创建/dev/sdb1及其文件系统,创建文本文件
[root@localhost ku]# echo welcome to redhat world > ricky
[root@localhost ku]# more ricky
welcome to redhat world
 
3、目前/dev/sdb1对应的文件系统/ku为1.2G
[root@localhost ku]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       16G  3.7G   11G  25% /
tmpfs                 613M   88K  613M   1% /dev/shm
/dev/sda1             485M   32M  428M   7% /boot
/dev/sdb1             1.2G   34M  1.1G   3% /ku
 
4、卸载/ku
[root@localhost ku]# cd /
[root@localhost /]# umount /ku
[root@localhost /]#
 
5、用fdisk命令删除/dev/sdb1分区,然后重新创建/dev/sdb1分区,并指定大小为4G
[root@localhost /]# 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: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x54f7c6df
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         393     3156741   83  Linux
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-652, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): +4G
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost /]#
 
6、fdisk -l /dev/sdb输出可以看出,cylinder已经由393扩为523
[root@localhost /]# fdisk -l /dev/sdb
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x54f7c6df
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         523     4200966   83  Linux
 
7、先check文件系统
[root@localhost /]# 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: 12/73760 files (0.0% non-contiguous), 13234/307200 blocks
 
8、将文件系统resize为2G
[root@localhost /]# resize2fs /dev/sdb1 2G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/sdb1 to 524288 (4k) blocks.
The filesystem on /dev/sdb1 is now 524288 blocks long.
[root@localhost /]# mount /dev/sdb1 /ku
[root@localhost /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       16G  3.7G   11G  25% /
tmpfs                 613M   88K  613M   1% /dev/shm
/dev/sda1             485M   32M  428M   7% /boot
/dev/sdb1             2.0G   34M  1.9G   2% /ku
[root@localhost /]#
 
9、检查扩容前的文件是否正确无损
[root@localhost /]# cd /ku
[root@localhost ku]# more ricky
welcome to redhat world
[root@localhost ku]#
 
10、当然也可以在线扩展文件系统
[root@localhost ku]# resize2fs /dev/sdb1 2500M
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/sdb1 is mounted on /ku; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/sdb1 to 640000 (4k) blocks.
The filesystem on /dev/sdb1 is now 640000 blocks long.
[root@localhost ku]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       16G  3.7G   11G  25% /
tmpfs                 613M   88K  613M   1% /dev/shm
/dev/sda1             485M   32M  428M   7% /boot
/dev/sdb1             2.5G   34M  2.3G   2% /ku
[root@localhost ku]#
 
11、但是不可以在线缩小;要缩小文件系统必须先卸载文件系统
[root@localhost ku]# resize2fs /dev/sdb1 1G
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/sdb1 is mounted on /ku; on-line resizing required
On-line shrinking from 640000 to 262144 not supported.
[root@localhost ku]# cd /
[root@localhost /]# umount /ku
[root@localhost /]# resize2fs /dev/sdb1 1G
resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/sdb1' first.
[root@localhost /]# 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: 12/147520 files (0.0% non-contiguous), 17864/640000 blocks
[root@localhost /]# resize2fs /dev/sdb1 1G
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.
[root@localhost /]# mount /dev/sdb1 /ku
[root@localhost /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       16G  3.7G   11G  25% /
tmpfs                 613M   88K  613M   1% /dev/shm
/dev/sda1             485M   32M  428M   7% /boot
/dev/sdb1            1010M   34M  939M   4% /ku
 
12、当然,你不能将文件系统扩展为比它的逻辑卷大
[root@localhost /]# resize2fs /dev/sdb1 5G
resize2fs 1.41.12 (17-May-2010)
The containing partition (or device) is only 1050241 (4k) blocks.
You requested a new size of 1310720 blocks.
 

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

2

添加新评论7 条评论

#苗誉铧系统运维工程师, 北京天港
2019-12-03 19:55
测试是没有问题,但是在生产上估计还是不敢用
#liguaren系统运维工程师, 汽车制造业
2015-11-03 14:41
不敢试啊,靠谱吗?
#yaohongdi1987数据库运维工程师, anondba
2013-10-30 16:55
厉害
#abit2007系统工程师, 代维
2013-09-25 15:22
虽然现在不研究linux,不过还是先收藏下。
#mryang系统工程师, 浙江
2013-07-25 14:39
威武啊  有惊无险
#apsbb系统工程师, mp
2013-05-03 12:53
先删除分区,再创建,有点风险啊,需要记录开始与结束的cylinder。
#jianhuiyang2008技术经理, 铁路
2012-10-19 08:17
我以前也是这样认为的,原来可以这样子,谢谢楼主了。
Ctrl+Enter 发表

核心数据库服务器选型优先顺序调查

发表您的选型观点,参与即得50金币。