luobo2ni 发表于 2019-4-17 17:31:58

CentOS6 lvm配置

  1、添加一块20G的新硬盘并重启Linux,硬盘被识别为sdb。
  2、在sdb上创建两个新分区,大小为10G和5G。
  # 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-10443, default 1):
  Using default value 1
  Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): +10G
  Command (m for help): n
  Command action
  e   extended
  p   primary partition (1-4)
  p
  Partition number (1-4): 2
  First cylinder (1307-10443, default 1307):
  Using default value 1307
  Last cylinder, +cylinders or +size{K,M,G} (1307-10443, default 10443): +5G
  3、将分区系统ID设置为LVM类型,并保存。
  Command (m for help): t
  Partition number (1-4): 1
  Hex code (type L to list codes): 8e
  Changed system type of partition 1 to 8e (Linux LVM)
  Command (m for help): t
  Partition number (1-4): 2
  Hex code (type L to list codes): 8e
  Changed system type of partition 5 to 8e (Linux LVM)
  Command (m for help): w
  The partition table has been altered!
  4、让Linux内核重读分区表。(由于此硬盘为新硬盘,之前并没有分区,所于此步可以忽略。对于之前有分区的硬盘,添加了分区之后需要这一步操作。)
  # partx -a /dev/sdb
  BLKPG: Device or resource busy
  error adding partition 1
  BLKPG: Device or resource busy
  error adding partition 2
  # partx -a /dev/sdb
  BLKPG: Device or resource busy
  error adding partition 1
  BLKPG: Device or resource busy
  error adding partition 2
  5、创建物理卷PV。
  # pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
  6、查看PV信息。
  # pvs
  PV         VG   FmtAttr PSizePFree
  /dev/sdb1       lvm2 ---- 10.00g 10.00g
  # pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vg0
  PV Size               10.00 GiB / not usable 4.54 MiB
  Allocatable         yes
  PE Size               4.00 MiB
  Total PE            2560
  Free PE               2560
  Allocated PE          0
  PV UUID               8oFFsN-mdFh-kgkQ-eSmt-IBhS-HJWP-xnnUzJ
  7、创建卷组VG。
  # vgcreate vg0 /dev/sdb1
  Volume group "vg0" successfully created
  8、查看VG信息。
  # vgs
  VG   #PV #LV #SN Attr   VSizeVFree
  vg0    1   0   0 wz--n- 10.00g 10.00g
  # vgdisplay
  --- Volume group ---
  VG Name               vg0
  System ID
  Format                lvm2
  Metadata Areas      1
  Metadata Sequence No1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               10.00 GiB
  PE Size               4.00 MiB
  Total PE            2560
  Alloc PE / Size       0 / 0
  FreePE / Size       2560 / 10.00 GiB
  VG UUID               E1lTkT-ttoh-K7rG-edfj-GTNJ-3zhB-5dJSci
  9、扩展VG大小。
  # pvcreate /dev/sdb2
  Physical volume "/dev/sdb2" successfully created
  # vgextend vg0 /dev/sdb2
  Volume group "vg0" successfully extended
  # vgs
  VG   #PV #LV #SN Attr   VSizeVFree
  vg0    2   0   0 wz--n- 15.01g 15.01g
  10、缩减VG大小。
  # pvmove /dev/sdb2
  No data to move for vg0
  # vgreduce vg0 /dev/sdb2
  Removed "/dev/sdb2" from volume group "vg0"
  # vgs
  VG   #PV #LV #SN Attr   VSizeVFree
  vg0    1   0   0 wz--n- 10.00g 10.00g
  11、创建逻辑卷LV。
  # lvcreate -L 2G -n lv0 vg0
  Logical volume "lv0" created.
  12、查看LV。
  # lvs
  LV   VG   Attr       LSize Pool Origin Data%Meta%Move Log Cpy%Sync Convert
  lv0vg0-wi-a----- 2.00g
  # lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg0/lv0
  LV Name                lv0
  VG Name                vg0
  LV UUID                wqjJ5n-uXjY-0uj2-dwSE-wA4T-LfuL-3xlFTS
  LV Write Access      read/write
  LV Creation host, time centos6.localdomain, 2019-02-26 23:54:43 +0800
  LV Status            available
  # open               0
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors   auto
  - currently set to   256
  Block device         253:0
  13、格式化LV为ext4文件系统并挂载。
  # mke2fs -t ext4 /dev/vg0/lv0
  mke2fs 1.41.12 (17-May-2010)
  Filesystem label=
  OS type: Linux
  Block size=4096 (log=2)
  Fragment size=4096 (log=2)
  Stride=0 blocks, Stripe width=0 blocks
  131072 inodes, 524288 blocks
  26214 blocks (5.00%) reserved for the super user
  First data block=0
  Maximum filesystem blocks=536870912
  16 block groups
  32768 blocks per group, 32768 fragments per group
  8192 inodes per group
  Superblock backups stored on blocks:
  32768, 98304, 163840, 229376, 294912
  Writing inode tables: done
  Creating journal (16384 blocks): done
  Writing superblocks and filesystem accounting information: done
  This filesystem will be automatically checked every 28 mounts or
  180 days, whichever comes first.Use tune2fs -c or -i to override.
  # mkdir /backup
  # mount /dev/vg0/lv0 /backup
  # df -h
  Filesystem         SizeUsed Avail Use% Mounted on
  /dev/sda3             17G3.4G   13G22% /
  tmpfs                490M   0490M   0% /dev/shm
  /dev/sda1            976M   34M892M   4% /boot
  /dev/mapper/vg0-lv02.0G3.0M1.9G   1% /backup
  14、扩展LV大小。
  # lvextend -L 5G /dev/vg0/lv0
  Size of logical volume vg0/lv0 changed from 2.00 GiB (512 extents) to 5.00 GiB (1280 extents).
  Logical volume lv0 successfully resized.
  15、扩展LV中的ext4文件系统大小。
  # resize2fs /dev/vg0/lv0
  resize2fs 1.41.12 (17-May-2010)
  Filesystem at /dev/vg0/lv0 is mounted on /backup; on-line resizing required
  old desc_blocks = 1, new_desc_blocks = 1
  Performing an on-line resize of /dev/vg0/lv0 to 1310720 (4k) blocks.
  The filesystem on /dev/vg0/lv0 is now 1310720 blocks long.
  # df -h
  Filesystem         SizeUsed Avail Use% Mounted on
  /dev/sda3             17G3.4G   13G22% /
  tmpfs                490M   0490M   0% /dev/shm
  /dev/sda1            976M   34M892M   4% /boot
  /dev/mapper/vg0-lv04.9G4.0M4.7G   1% /backup
  16、缩减LV中的ext4文件系统大小。
  # umount /dev/vg0/lv0
  # e2fsck -f /dev/vg0/lv0
  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/vg0/lv0: 11/327680 files (0.0% non-contiguous), 37982/1310720 blocks
  # resize2fs /dev/vg0/lv0 3G
  resize2fs 1.41.12 (17-May-2010)
  Resizing the filesystem on /dev/vg0/lv0 to 786432 (4k) blocks.
  The filesystem on /dev/vg0/lv0 is now 786432 blocks long.
  17、缩减LV大小。
  # lvreduce -L 3G /dev/vg0/lv0
  WARNING: Reducing active logical volume to 3.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
  Do you really want to reduce vg0/lv0? : y
  Size of logical volume vg0/lv0 changed from 5.00 GiB (1280 extents) to 3.00 GiB (768 extents).
  Logical volume lv0 successfully resized.
  # mount /dev/vg0/lv0 /backup
  # df -h
  Filesystem         SizeUsed Avail Use% Mounted on
  /dev/sda3             17G3.4G   13G22% /
  tmpfs                490M   0490M   0% /dev/shm
  /dev/sda1            976M   34M892M   4% /boot
  /dev/mapper/vg0-lv02.9G3.0M2.8G   1% /backup
  18、创建快照卷。
  # cp /etc/issue /backup
  # cat /backup/issue
  CentOS release 6.10 (Final)
  Kernel \r on an \m
  # lvcreate -L 3G -p r -s /dev/vg0/lv0 -n lv0-snap
  Logical volume "lv0-snap" created.
  # lvs
  LV       VG   Attr       LSize Pool Origin Data%Meta%Move Log Cpy%Sync Convert
  lv0      vg0owi-aos--- 3.00g
  lv0-snap vg0sri-a-s--- 3.00g      lv0    0.00
  19、挂载快照卷,修改源卷文件后查看快照卷文件是否变化。
  # mount /dev/vg0/lv0-snap /mnt
  mount: block device /dev/mapper/vg0-lv0--snap is write-protected, mounting read-only
  # echo "New line." >> /backup/issue
  # cat /backup/issue
  CentOS release 6.10 (Final)
  Kernel \r on an \m
  New line.
  # cat /mnt/issue
  CentOS release 6.10 (Final)
  Kernel \r on an \m
  20、卸载及删除快照卷和源LV。
  # umount /mnt
  # lvremove /dev/vg0/lv0-snap
  Do you really want to remove active logical volume lv0-snap? : y
  Logical volume "lv0-snap" successfully removed
  # umount /backup
  # lvremove /dev/vg0/lv0
  Do you really want to remove active logical volume lv0? : y
  Logical volume "lv0" successfully removed
  21、删除VG。
  # vgremove vg0
  Volume group "vg0" successfully removed
  22、删除PV。
  # pvremove /dev/sdb{1,2}
  Labels on physical volume "/dev/sdb1" successfully wiped
  Labels on physical volume "/dev/sdb2" successfully wiped



页: [1]
查看完整版本: CentOS6 lvm配置