linux动态调整磁盘的容量

LVM:logical volume manager(逻辑磁盘管理),lvm屏蔽了底层磁盘布局,方便动态的调整磁盘的容量

一,创建步骤:

         1,通过fdisk工具将磁盘转换为linux的分区

        2,通过pvcreate命令将linux分区转换为物理卷(pv)

       3,通过vgcreate命令将建好的物理卷处理成组卷(vg)

      4,通过lvcreate命令将卷组分成若干个逻辑卷(lv)

      5,对逻辑卷进行格式化,挂载,动态调整逻辑卷的大小,不会影响逻辑卷的数据

二,具体的操作步骤

 查看linux分区fdisk -l,将未使用的空间转换为linux物理卷

[root@rh ~]# fdisk /dev/sdo
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.


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)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-2610, default 2610): 500m

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (501-2610, default 501):
Using default value 501
Last cylinder or +size or +sizeM or +sizeK (501-2610, default 2610): 500m
Value out of range.
Last cylinder or +size or +sizeM or +sizeK (501-2610, default 2610): 200m
Value out of range.
Last cylinder or +size or +sizeM or +sizeK (501-2610, default 2610):
Using default value 2610

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

--查看linux的分区情况
[root@rh ~]# fdisk -l

Disk /dev/sda: 85.8 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        8924    71681998+  83  Linux
/dev/sda2            8925        9179     2048287+  82  Linux swap / Solaris

Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        2610    20964793+  83  Linux

Disk /dev/sdc: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        2610    20964793+  83  Linux

Disk /dev/sdd: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1               1        2610    20964793+  83  Linux

Disk /dev/sde: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sde1               1        2610    20964793+  83  Linux

Disk /dev/sdf: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdf1               1        2610    20964793+  83  Linux

Disk /dev/sdg: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdg1               1        2610    20964793+  83  Linux

Disk /dev/sdh: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdh doesn't contain a valid partition table

Disk /dev/sdi: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdi doesn't contain a valid partition table

Disk /dev/sdj: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdj doesn't contain a valid partition table

Disk /dev/sdk: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdk doesn't contain a valid partition table

Disk /dev/sdl: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdl1               1        2610    20964793+  83  Linux

Disk /dev/sdm: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdm1               1        2610    20964793+  83  Linux

Disk /dev/sdn: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdn1               1        2610    20964793+  83  Linux

Disk /dev/sdo: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdo1               1         500     4016218+  83  Linux
/dev/sdo2             501        2610    16948575   83  Linux
[root@rh ~]# pvcreate /dev/sdo
sdo   sdo1  sdo2 

将linux物理分区转换为物理卷

将物理分区/dev/sdo{1,2}转换为物理卷
[root@rh ~]# pvcreate /dev/sdo{1,2}
  Physical volume "/dev/sdo1" successfully created
  Physical volume "/dev/sdo2" successfully created

使用pvscan查看物理卷的信息

--查看物理卷的信息,会显示所有的物理信息
[root@rh ~]# pvscan
  PV /dev/sdo1                      lvm2 [3.83 GB]
  PV /dev/sdo2                      lvm2 [16.16 GB]
  Total: 2 [19.99 GB] / in use: 0 [0   ] / in no VG: 2 [19.99 GB]

使用pvdisplay查看物理卷的详细参数

--查看各物理卷的详细信息
[root@rh ~]# pvdisplay
  "/dev/sdo1" is a new physical volume of "3.83 GB"
  --- NEW Physical volume ---
  PV Name               /dev/sdo1
  VG Name              
  PV Size               3.83 GB
  Allocatable           NO
  PE Size (KByte)       0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               8Kq0Ct-q0Hh-KKK5-Ooh2-0U3w-BDov-2oLr39
  
  "/dev/sdo2" is a new physical volume of "16.16 GB"
  --- NEW Physical volume ---
  PV Name               /dev/sdo2
  VG Name              
  PV Size               16.16 GB
  Allocatable           NO
  PE Size (KByte)       0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               z2kGU1-wI2g-7N2B-EnLd-bVJP-hnqV-v9rpvF

当物理卷没有使用的时候我们可以删除物理卷

--使用pvremove /dev/sd01删除物理卷

三,卷组(vg)将物理卷转换为卷组

--将已经是物理卷的/dev/sd0[1,2}转化为卷组名实vg01的卷组
  
[root@rh ~]# vgcreate vg01 /dev/sdo
sdo   sdo1  sdo2 
[root@rh ~]# vgcreate vg01 /dev/sdo{1,2}
  Volume group "vg01" successfully created
[root@rh ~]# vgdisplay
  --- Volume group ---
  VG Name               vg01
  System ID            
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               19.99 GB
  PE Size               4.00 MB
  Total PE              5117
  Alloc PE / Size       0 / 0  
  Free  PE / Size       5117 / 19.99 GB
  VG UUID               8Am3ag-cMB5-OL3s-Z2w3-owmo-L087-TMaz1r

以上未加参数,扩展块pe大小默认是4m,若通过vgcreate -s 8m vg01 /dev/sd0{1,2}则指定了扩展块大小为8m

使用vgdisplay查看卷组的详细信息

--查看所有物理卷的信息
   [root@rh ~]# vgdisplay
  --- Volume group ---
  VG Name               vg01
  System ID            
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  11
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               59.97 GB
  PE Size               4.00 MB
  Total PE              15353
  Alloc PE / Size       1667 / 6.51 GB
  Free  PE / Size       13686 / 53.46 GB
  VG UUID               8Am3ag-cMB5-OL3s-Z2w3-owmo-L087-TMaz1r
   也可以通过vgdisplay -v /dev/vg01查看某一具体的卷组的详细信息

使用vgscan查看卷组信息

--查看卷组的信息
[root@rh ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "vg01" using metadata type lvm2
[root@rh ~]# pvcreate /dev/sd
sda   sdb   sdc1  sde   sdf1  sdh   sdk   sdm   sdn1  sdo2 
sda1  sdb1  sdd   sde1  sdg   sdi   sdl   sdm1  sdo  
sda2  sdc   sdd1  sdf   sdg1  sdj   sdl1  sdn   sdo1 

扩展卷组vgextend,将某个物理卷添加到一个已存在的卷组

创建一个新的物理卷
[root@rh ~]# pvcreate /dev/sdc1
  Wiping software RAID md superblock on /dev/sdc1
  Physical volume "/dev/sdc1" successfully created

--将新增的一个物理卷添加到vg01卷组中

[root@rh ~]# vgextend vg01 /dev/sdc1
  Volume group "vg01" successfully extended

四,逻辑卷lv的创建及管理

从vg01上面划分6g的空间诶逻辑卷data
[root@rh ~]# lvcreate -L 6g -n data vg01
  Logical volume "data" created

对逻辑卷进行格式化
[root@rh ~]# mkfs -t ext3 /dev/vg01/data
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
786432 inodes, 1572864 blocks
78643 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1610612736
48 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done                           
Creating journal (32768 blocks):
done
Writing superblocks and filesystem accounting information: done

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

也可以通过mkfs.ext3 /dev/vg01/data格式化

使用lvscan查看逻辑卷的信息
[root@rh ~]# lvscan
  ACTIVE            '/dev/vg01/data' [6.00 GB] inherit

使用lvdisplay查看逻辑卷的参数
[root@rh ~]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg01/data
  VG Name                vg01
  LV UUID                KwarPL-APks-8VhN-hSVB-WgVG-m0EP-6H6ifd
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                6.00 GB
  Current LE             1536
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

也可以使用lvdisplay -v /dev/vg01/data查看某一逻辑卷的详细信息
如果想要删除卷组可以使用

vgremove /dev/vg01
使用lvextend增大逻辑卷的大小,在线扩容

从卷组vg01上对逻辑卷进行扩容,逻辑卷大小变为7g
[root@rh ~]# lvextend -L +1g /dev/vg01/data
  Extending logical volume data to 7.00 GB
  Logical volume data successfully resized
[root@rh ~]# lvscan
  ACTIVE            '/dev/vg01/data' [7.00 GB] inherit

使用resize2fs更新系统识别的文件系统大小,立即生效

是增加的逻辑卷立即生效
[root@rh ~]# resize2fs /dev/vg01/data
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg01/data to 1835008 (4k) blocks.
The filesystem on /dev/vg01/data is now 1835008 blocks long.

使用lvreduce减少逻辑卷的大小,必须是离线的方式-->先卸载文件系统

[root@rh ~]# lvreduce -L -500m /dev/vg01/data
  WARNING: Reducing active logical volume to 6.51 GB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce data? [y/n]: y
  Reducing logical volume data to 6.51 GB
  Logical volume data successfully resized
[root@rh ~]# lvscan
  ACTIVE            '/dev/vg01/data' [6.51 GB] inherit

使减少的逻辑卷立即生效

缩小逻辑卷通常要先卸载文件系统,并且缩小后的空间必须大于文件当前占用的容量,否则信息丢失
[root@rh ~]# resize2fs /dev/vg01/data
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg01/data to 1707008 (4k) blocks.
resize2fs: Can't read an block bitmap while trying to resize /dev/vg01/data

查看逻辑卷的大小
[root@rh ~]# lvscan
  ACTIVE            '/dev/vg01/data' [6.51 GB] inherit

删除逻辑卷

lvremove /dev/vg01/data

五,挂载逻辑卷
[root@rh ~]# mkdir /quota
[root@rh ~]# mount /dev/vg01/data /quota/
[root@rh ~]# df -hT
文件系统      类型    容量  已用 可用 已用% 挂载点
/dev/sda1     ext3     67G   20G   44G  32% /
tmpfs        tmpfs    357M     0  357M   0% /dev/shm
/dev/mapper/vg01-data
              ext3    6.9G  142M  6.5G   3% /quota

开机自动挂载
[root@rh ~]# vi /etc/fstab

LABEL=/                 /                       ext3    defaults        1 1
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
LABEL=SWAP-sda2         swap                    swap    defaults        0 0
/dev/md0                /raid                   ext3    defaults        0 0
/dev/md11               /traid                  ext3    defaults        0 0
/dev/vg01/data          /quota                  ext3    defaults        0 0
~
~
~
~
~
~
Entering Ex mode.  Type "visual" to go to Normal mode.
:wq!
"/etc/fstab" 9L, 575C written
六,逻辑卷快照管理功能,可以把卷中的数据冻结起来,类似于为这些数据拍张照片,可以永久保存建立快照当时的状态

创建卷快照,lvcreate  -L 原始逻辑卷大小的15%-20% -s -n 快照名 源逻辑卷名

跟创建逻辑卷差不多,只是加了参数-s

[root@rh ~]# lvcreate -L 1g -s -n snaplv1 /dev/vg01/data
  Logical volume "snaplv1" created

查看状态为snapshot
[root@rh ~]# lvscan
  ACTIVE   Original '/dev/vg01/data' [6.51 GB] inherit
  ACTIVE   Snapshot '/dev/vg01/snaplv1' [1.00 GB] inherit

创建挂载点,并且挂载
[root@rh ~]# mkdir /snap
[root@rh ~]# mount /dev/vg01/snaplv1 /snap/
[root@rh ~]# lvremove /dev/vg01/snaplv1
  Can't remove open logical volume "snaplv1"

卸载快照
[root@rh ~]# umount /snap/

删除快照
[root@rh ~]# lvremove /dev/vg01/snaplv1
Do you really want to remove active logical volume "snaplv1"? [y/n]: y
  Logical volume "snaplv1" successfully removed
  七,一天如果实体硬盘的分区出现错误,必须更换硬盘是,lvm提供了pvmove工具,可以将一个物理卷上的数据转移到另一个物理卷上

加载一个新的物理卷到卷组话中
[root@rh ~]# pvcreate /dev/sdb1
  Wiping software RAID md superblock on /dev/sdb1
  Physical volume "/dev/sdb1" successfully created

将新增的物理卷添加到vg01卷组中
[root@rh ~]# vgextend vg01 /dev/sdb1
  Volume group "vg01" successfully extended
[root@rh ~]# lvscan
  ACTIVE            '/dev/vg01/data' [6.51 GB] inherit

移动物理卷上的数据到新加的物理卷上
[root@rh ~]# pvmove /dev/sdb1 /dev/sdo1
  No data to move for vg01

使用vgreduce将损害的磁盘/dev/sdb1从卷组中分离

vgreduce vg01 /dev/sdb1
[root@rh ~]# pvscan
  PV /dev/sdo1   VG vg01   lvm2 [3.83 GB / 3.83 GB free]
  PV /dev/sdo2   VG vg01   lvm2 [16.16 GB / 16.16 GB free]
  PV /dev/sdc1   VG vg01   lvm2 [19.99 GB / 13.48 GB free]
  PV /dev/sdb1   VG vg01   lvm2 [19.99 GB / 19.99 GB free]
  Total: 4 [59.97 GB] / in use: 4 [59.97 GB] / in no VG: 0 [0   ]
如果要将整个lvm的磁盘迁移到另外一台计算机中

卸载该卷组中所有的逻辑卷

[root@rh ~]# umount /dev/vg01/data

使用vgchange将卷组修改为非有效的配置
[root@rh ~]# vgchange -a n vg01

使用vgexport导出该卷组
  0 logical volume(s) in volume group "vg01" now active
[root@rh ~]# vgexport vg01
  Volume group "vg01" successfully exported
将lvm磁盘安装到目的计算机上

使用pvscan扫描所有的物理卷,以便linux可以驱动这些物理卷

[root@rh ~]# pvscan
  PV /dev/sdo1    is in exported VG vg01 [3.83 GB / 3.83 GB free]
  PV /dev/sdo2    is in exported VG vg01 [16.16 GB / 16.16 GB free]
  PV /dev/sdc1    is in exported VG vg01 [19.99 GB / 13.48 GB free]
  PV /dev/sdb1    is in exported VG vg01 [19.99 GB / 19.99 GB free]
  Total: 4 [59.97 GB] / in use: 4 [59.97 GB] / in no VG: 0 [0   ]

导入卷组
[[root@rh ~]# vgimport vg01
  Volume group "vg01" successfully imported

修改卷组为有效配置
[root@rh ~]# vgchange -a y vg01
  1 logical volume(s) in volume group "vg01" now active

挂载逻辑卷
[root@rh ~]# mount /dev/vg01/data /quota/

查看磁盘挂载信息

df -hT

十,逻辑卷创建及删除推荐步骤

创建逻辑卷lv的顺序:linux分区---->物理卷pv---->卷组vg---->逻辑卷lv----->挂载到文件系统

删除逻辑卷的顺序:卸载文件系统---->逻辑卷lv---->卷组vg---->物理卷pv----->linux分区

 

整理与网络,实验与redhat5.3

你可能感兴趣的:(linux,command,dos,Access,磁盘,Allocation)