Linux 下在线扩容的几种方式及说明


在生产环境中磁盘空间不足,但又不方便停机维护的状况不可避免.
以下操作可以在部分情况下在线扩容而不需要停机.
扩容操作从空间来源上来说有两种方式,原磁盘增加空间和新增磁盘.
Linux下磁盘分区也主要会分两种,LVM和EXT[3/4]
从空间目的地来说也会有两种,扩展原有分区和新增分区并挂载.
但总的流程基本一致,即 追加空间 --> 让kernel 识别刷新容量 --> 对新加空间进行分区操作 --> 交付文件系统使用 .

流程圖

Created with Raphaël 2.2.0 開始 新增硬盤? 掃描新增硬盤 為新硬盤分區 是否使用LVM? 指定分區為LVM格式 查看PV/新硬盤建立為PV/調整PV大小 查看VG/將PV擴展進VG 查看VG/擴展LV 調整文件系統大小 重掃變更的磁盤 重新分區該磁盤 yes no yes no

在VM中新增磁盘并交付系统使用

和Windows不一样,Linux下新增磁盘或对原磁盘增加容量并不会自动被kernel识别到.

VM 中加入新的磁盘

过程略

扫描SCSI 总线以识别到新加入的设备

echo “- - -”  > /sys/class/scsi_host/host0/scan

此处的host0,指的是第一个scsi host adapter ,对应ESXI中磁盘的设备节点SCSI(0:1)中的0.
传送的"- - -" 中的三个"-" 表示扫描所有 channel (first -), every target (second -), and every device i.e. disk/lun(third -)

为识别到的新磁盘分区

[root]# fdisk /dev/sdb
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 or + size or +sizeM or +sizeK(1-261, default 261):
Using default value 261
Command (m for help): w
The partition table has been altered!
Calling ioctr() to re-read partition table.
Syncing disks

为新分区准备文件系统

[root]# msfs -t ext3 /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type:Linux
Block size=4096 (log=2)
Fragment size=4096(log=2)
...
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 23 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.

建立目录,并将新空间挂载

[root]#mkdir /tmp/dd
[root]#mount /dev/sdb1 /tmp/dd

为以后自动挂载,编辑/etc/fstab

/dev/sdb1			/tmp/dd	ext3				defaults 		0 				0
设备						挂载点		文件系统		挂载规则		dump		order

挂载规则:
auto 开机自动挂载
default 按照大多数永久文件系统的缺省值设置挂载定义
noauto 开机不自动挂载
nouser 只有超级用户可以挂载
ro 按只读权限挂载
rw 按可读可写权限挂载
user 任何用户都可以挂载
order 指fsck(启动时fsck检查的顺序)。为0就表示不检查,(/)分区永远都
是1,其它的分区只能从2开始,当数字相同就同时检查(但不能有两1)。

在VM中对已有磁盘扩容

VM中增加现有磁盘大小

过程略
注意:如果此磁盘存在快照,则无法操作.原因显而易见.

重新扫描改变大小的磁盘

[root@ksyoweb01 ~]# echo 1> /sys/class/block/sda/device/rescan  #kernel 3.x
[root@ksyoweb01 ~]#echo 1>/sys/class/scsi_device/0:0:1:0/device/block:sdb/device/rescan #kernel 2.x

重新分区以使用新增的空间

[root@ksyoweb01 ~]#  fdisk /dev/sdb
命令 (m 以獲得說明):d
分割區編號 (1,2, default 2): 2
Partition 2 is deleted
命令 (m 以獲得說明):n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
分割區編號 (2-4, default 2): 2
起初 sector (1026048-92274687, 預設 1026048):
使用預設值 1026048
最後 sector, +sectors 或 +大小{K,M,G} (1026048-92274687, 預設 92274687):
使用預設值 92274687
Partition 2 of type Linux and of size 43.5 GiB is set
**如果是分配给LVM的则需要指定type**
命令 (m 以獲得說明):t
分割區編號 (1,2, default 2): 2
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
呼叫 ioctl() 以重新讀取分割表。

WARNING: Re-reading the partition table failed with error 16: 裝置或系統資源忙碌中.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
同步磁碟。

注意:
较旧版核心在写入分区表的提示为 The new table will be used at the next reboot.
这表明在线扩容梦想破灭,只能重启.

通知系统分区表的变化 partprobe:

[root@ksyoweb01 ~]# partprobe 
Error: /dev/sda 上的分割區 2 已被寫入,但是我們並無法將變更通報內核,或許是因為它/它們正在使用中,結果造成繼續使用舊的分割區。在進行進一步的變更之前,您應該立刻重新開機。

如果出现上面的提示,表示很不幸必须得重启.

改变文件系统的大小

root@test:~# resize2fs /dev/sdb2
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/sdb2 is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 4
The filesystem on /dev/vda1 is now 12976128 blocks long.

LVM 情况下操作

[root@ksyoweb01 ~]# pvdisplay
— Physical volume —
PV Name /dev/sda2
VG Name centos
PV Size 39.51 GiB / not usable 2.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 10114
Free PE 11
Allocated PE 10103
PV UUID JLiFFR-TWvr-E6uW-x77q-dZl1-MfrZ-T2PhUw

[root@ksyoweb01 ~]# pvresize /dev/sda2
Physical volume “/dev/sda2” changed
1 physical volume(s) resized / 0 physical volume(s) not resized
[root@ksyoweb01 ~]# pvdisplay
— Physical volume —
PV Name /dev/sda2
VG Name centos
PV Size 43.51 GiB / not usable 2.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 11138
Free PE 1035
Allocated PE 10103
PV UUID JLiFFR-TWvr-E6uW-x77q-dZl1-MfrZ-T2PhUw
[root@ksyoweb01 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- 43.51g 4.04g
[root@ksyoweb01 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz–n- 43.51g 4.04g
[root@ksyoweb01 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 35.59g
swap centos -wi-ao---- 3.88g
[root@ksyoweb01 ~]# lvextend -L +4G -f -r /dev/centos/root
Size of logical volume centos/root changed from 35.59 GiB (9111 extents) to 39.59 GiB (10135 extents).
Logical volume centos/root successfully resized.
meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=2332416 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=9329664, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=4555, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 9329664 to 10378240

[root@ksyod03 kyosfcs]# resize2fs -p /dev/VolGroup00/LogVol02
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol02 is mounted on /oracle; on-line resizing r
Performing an on-line resize of /dev/VolGroup00/LogVol02 to 144433152 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol02 is now 144433152 blocks long.
#注: -p 是指打印進度

所以,很明显.如果后续可能会有在线空间扩展需求的,请使用LVM,不然的话,在线空间扩展只是个梦.

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值