磁盘管理(SWAP、dd、quota、RAID、LVM)

2016-08-26:

授课内容:

1、SWAP交换分区的创建

2、dd命令的使用

3、设定文件系统配额

4、设定和管理软RAID设备

5、配置逻辑卷、逻辑卷快照

1、swap

(1)SWAP分区:模拟内存,当物理内存不足时,进程需要内存资源是,内存会把一部分没有在用的进程分页挪到硬盘的模拟内存中,腾出空间被现在需要使用内存资源的进程

即其作用是可以允许内存过载使用,windows系统也有类似的机制,由于虚拟内存空间是建立在硬盘之上,所以其速度和性能会大打折扣,所以适合临时使用

(2)创建SWAP分区:

相关命令:mkswap、swapon

示例:

[19:42 root@Centos6.8/usr/local/http2]# fdisk /dev/sda 【创建分区】
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): n
First cylinder (9460-26109, default 9460): 
Using default value 9460
Last cylinder, +cylinders or +size{K,M,G} (9460-26109, default 26109): +2G
Command (m for help): t
Partition number (1-6): 6
Hex code (type L to list codes): L
0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx         
5  Extended        42  SFS             86  NTFS volume set da  Non-FS data    
6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility   
8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt         
9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access     
a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O        
b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor      
c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs        
e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT            
f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1
Hex code (type L to list codes): 82 【选择swap模式】
Changed system type of partition 6 to 82 (Linux swap / Solaris)
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 or after you run partprobe(8) or kpartx(8)
Syncing disks.
[19:43 root@Centos6.8/usr/local/http2]# mkswap /dev/sda6 【格式化交换分区】
Setting up swapspace version 1, size = 2100024 KiB
no label, UUID=ae9d3eee-1908-47fe-9b32-2a777d0b28b1
[19:43 root@Centos6.8/usr/local/http2]# swapon -a 【挂载swap】
[19:44 root@Centos6.8/usr/local/http2]# free 【查看已经挂载成功】
total       used       free     shared    buffers     cached
Mem:       1907300    1059764     847536       1336      65000     724408
-/+ buffers/cache:     270356    1636944
Swap:      4095996          0    4095996
[19:44 root@Centos6.8/usr/local/http2]# cat /proc/swaps 
FilenameTypeSizeUsedPriority
/dev/sda5                               partition40959960-1

2、dd命令的使用;

dd:convert and copy a file,转换并复制文件,与cp命令相似,只是用法格式有不一样,dd复制的是数据流,工作在vfs之下

用法:

dd if=/PATH/FROM/SRC of=/PATH/TO/DEST bs=#:block size, 复制单元大小 count=#:复制多少个bs

of=file写到所命名的文件而不是到标准输出

if=file从所命名文件读取而不是从标准输入

bs=size指定块大小(既是是ibs也是obs)

ibs=size一次读size个byte

obs=size 一次写size个byte

cbs=size 一次转化size个byte

skip=blocks从开头忽略blocks个ibs大小的块

seek=blocks从开头忽略blocks个obs大小的块

count=n只拷贝n个记录

cbs=size 一次转换多少byte

说明:

磁盘拷贝:

dd if=/dev/sda of=/dev/sdb

备份MBR:

dd if=/dev/sda of=/tmp/mbr.bak bs=512 count=1

破坏MBR中的bootloader:

dd if=/dev/zero of=/dev/sda bs=64 count=1 seek=446

有一个大与2K的二进制文件fileA。现在想从第64个字节位置开始读取,需要读取的大小是128Byts。又有fileB, 想把上面读取到的128Bytes写到第32个字节开始的位置,替换128Bytes,请问如何实现?

dd if=fileA of=fileB bs=1 count=128 skip=63 seek=31 conv=notrunc

备份:

dd if=/dev/sdx of=/dev/sdy

将本地的/dev/sdx整盘备份到/dev/sdy

dd if=/dev/sdx of=/path/to/image

将/dev/sdx全盘数据备份到指定路径的image文件

dd if=/dev/sdx | gzip >/path/to/image.gz

备份/dev/sdx全盘数据,并利用gzip工具进行压缩,保存到指定路径

恢复:

dd if=/path/to/image of=/dev/sdx

将备份文件恢复到指定盘

gzip -dc /path/to/image.gz | dd of=/dev/sdx

将压缩的备份文件恢复到指定盘

3、文件系统配额:以文件系统(分区)为单位启用

分区配额管理:在一个分区内进行管理

(1)、启用磁盘配额:

分区挂载选项(在fstab修改挂载属性:usrquota,grpquota):

usrquota:控制单个用户

grpquota:控制组用户

(2)、创建配额数据库

quotacheck -cug /filesystem 成功后会在文件系统有两个aquota的文件

  [root@localhost home]# ll
total 36
-rw-------. 1 root root  7168 Aug 26 15:04 aquota.group
-rw-------. 1 root root  7168 Aug 26 15:04 aquota.user
drwxr-xr-x. 4 hill root  4096 Aug 26 15:00 hill
drwx------. 2 root root 16384 Aug 26 14:50 lost+found
[root@localhost home]# getenforce
Enforcing
[root@localhost home]# setenforce 0
[root@localhost home]# getenforce
Permissive

(3)、启用

quotaon /filesystem

(4)、设置用户配额:

edqupta USERNAME

磁盘管理(SWAP、dd、quota、RAID、LVM)

 

根据文件所有者来定义用户使用了多大

(5)、查看、测试

        [root@localhost home]# repquota -a
*** Report for user quotas on device /dev/sda6
Block grace time: 7days; Inode grace time: 7days
Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --  141468       0       0              4     0     0       
hill      --      36   50000   60000              9     0     0

4、RAID:(raid的最小存储单位是chunk,即每次平均分到物理磁盘的大小)

(1)RAID:Redundant Array of Inexpensive Disks:廉价磁盘冗余阵列

   :Redundant Array of Independent Disks:独立磁盘冗余阵列

【注意】:由于在用户层面看来,raid组成一块磁盘,所以要对磁盘进行分区、格式化才能使用

(2)RAID级别:仅代表磁盘组织方式不同,没有上下之分;

0: 条带

性能提升: 读,写

冗余能力(容错能力): 无

空间利用率:nS

至少2块盘

1: 镜像

性能表现:写性能下降,读性能提升

冗余能力:有

空间利用率:1/2

至少2块盘

2

3

4: 

5: 带基偶校验位

性能表现:读,写提升

冗余能力:有

空间利用率:(n-1)/n

至少需要3块

10:

性能表现:读、写提升

冗余能力:有

空间利用率:1/2

至少需要4块

01:

性能表现:读、写提升

冗余能力:有

空间利用率:1/2

至少需要4块

50:

性能表现:读、写提升

冗余能力:有

空间利用率:(n-2)/n

至少需要6块

jbod:

性能表现:无提升

冗余能力:无

空间利用率:100%

至少需要2块

(3)实现RAID方式:

硬RAID(实际生产环节):

外接专门的存储机箱,机箱里接上多个硬盘

BIOS中进行设置

软RAID(系统演示环节,并不建议在生产环节使用):mdadm

RAID设备(软raid)可命名为/dev/md0、/dev/md1、/dev/md2、/dev/md3等等

(4)软件raid

命令的语法格式:mdadm[mode] <raiddevice> [options] <component-devices>

模式:

创建:-C

装配: -A

监控: -F

管理:-f, -r, -a

<raiddevice>: /dev/md#

<component-devices>: 任意块设备

-C: 创建模式

-n #: 使用#个块设备来创建此RAID

-l #:指明要创建的RAID的级别

-a {yes|no}:自动创建目标RAID设备的设备文件

-c CHUNK_SIZE: 指明块大小

-x #: 指明空闲盘的个数

-D:显示raid的详细信息;

mdadm -D /dev/md#

管理模式:

-f: 标记指定磁盘为损坏

-a: 添加磁盘

-r: 移除磁盘

观察md的状态:

cat /proc/mdstat

使用mdadm创建并定义RAID设备

			#mdadm -C /dev/md0 -a yes -l 5 -n 3 -x 1 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1

用文件系统对每个RAID设备进行格式化

#mke2fs -j /dev/md0

测试RAID设备

使用mdadm检查RAID设备的状况

#mdadm –detail|D /dev/md0

增加新的成员

#mdadm –G /dev/md0 –n4 -a /dev/sdf1

模拟磁盘故障

#mdadm /dev/md0 -f /dev/sda1

移除磁盘

#mdadm /dev/md0 –r /dev/sda1

从软件RAID磁盘修复磁盘故障

替换出故障的磁盘然后开机

在备用驱动器上重建分区

#mdadm /dev/md0 -a /dev/sda1

5、逻辑卷管理器(LVM)【最小的存储单位PE(Physical Extend)】

(1)允许在多个物理设备间重新组织文件系统

LVM可以弹性的更改LVM的容量:通过交换PE来进行资料的转换,将原来LV内的PE转移到其他的设备中以降低LV的容量,或将其他设备中的PE加到LV中以加大容量

一个VG最多只能有35565个PE,一个PE默认是4M,所以一个VG默认最大是35565*4=256G,要想调整VG的默认大小,就要调整PE的大小。

(2)LVM管理分为三层来管理:

 

LV(logical volume)

VG(volume group)

      PV(physical volume)                                        

                                                                        磁盘管理(SWAP、dd、quota、RAID、LVM)

 

创建LV逻辑卷的过程:要先把物理磁盘创建为物理卷PV,再由多个物理卷创建为卷组VG(相当于扩展分区的概念,不能直接使用),再在VG卷组上创建需要的逻辑卷LV

(3)相关命令(相似):

PV:pvcreate、pvremove、pvmove、pvextend、pvdisplay

VG:vgcreate、vgreduce、vgextend、vgdisplay

LV:lvcreate、lvremove、lvextend、lvdisplay

pv管理工具

显示pv信息

pvs:简要pv信息显示

pvdisplay

   创建pv

pvcreate /dev/DEVICE

vg管理工具

显示卷组

vgs

vgdisplay

创建卷组

vgcreate [-s #[kKmMgGtTpPeE]] VolumeGroupName PhysicalDevicePath [PhysicalDevicePath…]

管理卷组

vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath…]

vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath…]

删除卷组

先做pvmove,再做vgremove

lv管理工具

显示逻辑卷

lvs

Lvdisplay

创建逻辑卷

lvcreate-L #[mMgGtT] -n NAME VolumeGroup

删除逻辑卷

lvremove/dev/VG_NAME/LV_NAME

重设文件系统大小

fsadm[options] resize device [new_size[BKMGTEP]]

resize2fs [-f] [-F] [-M] [-P] [-p] device [new_size]

实例:创建LV过程:

简述:    一、分区

fdisk /dev/sda t  8e

partx -a /dev/sda

lsblk

2、

                         pvcreate  /dev/sd{a7,b}

pvs

pvdisplay 

3、

                        vgcreate vg0 /dev/sd{a7,b}

vgs;pvs;pvdisplay;vgdisplay 

4、lvcreate  -n lv0 -L 10G vg0 

5、mkfs.ext4 /dev/vg0/lv0 

6、

                        vi /etc/fstab 

mkdir /mnt/lv0;mount -a

        

                   二、扩展逻辑卷lv(可以在没有卸载的情况下进行扩展)

                        1、lvdisplay

                        2、lvextend -L +1G /dev/vg0/lv0  #先物理扩展

                        3、resize2fs /dev/vg0/lv0 #后逻辑扩展

                        4、lvdisplay

                   三、扩展卷组VG

                        1、vgdisplay

                        2、pvcreat /dev/sdb

                        3、vgextend vg0 /dev/sdb

                   四、缩减逻辑卷

注意:1、不能在线缩减,得先卸载;

   2、确保缩减后的空间大小依然能存储原有的所有数据;

   3、在缩减之前应该先强行检查文件,以确保文件系统处于一至性状态;

df -lh

umount 

e2fsck -f

resize2fs /PATH/TO/PV 3G  #先逻辑缩减

lvreduce -L [-]# /PATH/TO/LV #后物理缩减

mount

   

1、创建LV

[16:18 root@Centos6.8~]# fdisk /dev/sda
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): t
Partition number (1-8): 8
Hex code (type L to list codes): L
 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx         
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data    
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility   
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1
Hex code (type L to list codes): 8e
Changed system type of partition 8 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-8): 7
Hex code (type L to list codes): 8e
Changed system type of partition 7 to 8e (Linux LVM)
Command (m for help): t
Partition number (1-8): 6
Hex code (type L to list codes): 8e
Changed system type of partition 6 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 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: 0x000e82d5
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        6400    51200000   83  Linux
/dev/sda3            6400        8950    20480000   83  Linux
/dev/sda4            8950       26109   137829376    5  Extended
/dev/sda5            8950        9460     4096000   82  Linux swap / Solaris
/dev/sda6            9460        9721     2100029   8e  Linux LVM
/dev/sda7            9722        9983     2104483+  8e  Linux LVM
/dev/sda8            9984       10376     3156741   8e  Linux LVM
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 or after you run partprobe(8) or kpartx(8)
Syncing disks.
[16:18 root@Centos6.8~]# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
BLKPG: Device or resource busy
error adding partition 6
BLKPG: Device or resource busy
error adding partition 7
BLKPG: Device or resource busy
error adding partition 8
[16:19 root@Centos6.8~]# pvdisplay 
[16:19 root@Centos6.8~]# pvcreate /dev/sda{6,7,8}
  Physical volume "/dev/sda6" successfully created
  Physical volume "/dev/sda7" successfully created
  Physical volume "/dev/sda8" successfully created
[16:20 root@Centos6.8~]# pvdisplay 
  "/dev/sda6" is a new physical volume of "2.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda6
  VG Name               
  PV Size               2.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               OcC0N8-Ojcf-6oPq-gvaS-Dny2-JVs4-Lds51R
   
  "/dev/sda7" is a new physical volume of "2.01 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda7
  VG Name               
  PV Size               2.01 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               amnydg-48Qm-tIZs-ZXnp-ngWA-4Fuk-6k53TP
   
  "/dev/sda8" is a new physical volume of "3.01 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda8
  VG Name               
  PV Size               3.01 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               mir8t7-yoTX-1pAD-A6bA-Gl4U-JYLe-pSmPSc
   
[16:20 root@Centos6.8~]# vgdisplay 
[16:20 root@Centos6.8~]# vgcreate vg0 /dev/sda{6,7,8}
  Volume group "vg0" successfully created
[16:20 root@Centos6.8~]# vgdisplay 
  --- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        3
  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                3
  Act PV                3
  VG Size               7.01 GiB
  PE Size               4.00 MiB
  Total PE              1795
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1795 / 7.01 GiB
  VG UUID               OXd09k-uQ7C-BMNr-gkeY-HeQv-NJnP-H9WWxQ
   
[16:20 root@Centos6.8~]# lvdisplay 
[16:21 root@Centos6.8~]# lvcreate -n lv0 -L 5G vg0
  Logical volume "lv0" created.
[16:21 root@Centos6.8~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg0/lv0
  LV Name                lv0
  VG Name                vg0
  LV UUID                32AMUw-xgxw-ejJ2-rps1-fm0e-feYw-OoTPS8
  LV Write Access        read/write
  LV Creation host, time Centos6.8, 2016-08-27 16:21:48 +0800
  LV Status              available
  # open                 0
  LV Size                5.00 GiB
  Current LE             1280
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
[16:22 root@Centos6.8~]# vgdisplay 
  --- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               7.01 GiB
  PE Size               4.00 MiB
  Total PE              1795
  Alloc PE / Size       1280 / 5.00 GiB
  Free  PE / Size       515 / 2.01 GiB
  VG UUID               OXd09k-uQ7C-BMNr-gkeY-HeQv-NJnP-H9WWxQ
   
[16:25 root@Centos6.8~]# mke2fs -j /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
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 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 27 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[16:26 root@Centos6.8~]# mkdir /mnt/lv0
[16:26 root@Centos6.8~]# mount /dev/vg0/lv0 /mnt/lv0/
[16:26 root@Centos6.8~]# mo
modem-manager          mogrify                mount.cifs             mountpoint             mozilla-plugin-config
modinfo                montage                mount.fuse             mountstats             
modprobe               more                   mount.nfs              mount.tmpfs            
modutil                mount                  mount.nfs4             mousetweaks

2、 扩展逻辑卷(要在挂载状态执行resize2fs)

[16:28 root@Centos6.8/mnt/lv0]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg0/lv0
  LV Name                lv0
  VG Name                vg0
  LV UUID                32AMUw-xgxw-ejJ2-rps1-fm0e-feYw-OoTPS8
  LV Write Access        read/write
  LV Creation host, time Centos6.8, 2016-08-27 16:21:48 +0800
  LV Status              available
  # open                 1
  LV Size                5.00 GiB
  Current LE             1280
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
[16:36 root@Centos6.8/mnt/lv0]# lvextend -L +1G /dev/vg0/lv0 #物理先扩展
  Size of logical volume vg0/lv0 changed from 5.00 GiB (1280 extents) to 6.00 GiB (1536 extents).
  Logical volume lv0 successfully resized.
[16:36 root@Centos6.8/mnt/lv0]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg0/lv0
  LV Name                lv0
  VG Name                vg0
  LV UUID                32AMUw-xgxw-ejJ2-rps1-fm0e-feYw-OoTPS8
  LV Write Access        read/write
  LV Creation host, time Centos6.8, 2016-08-27 16:21:48 +0800
  LV Status              available
  # open                 1
  LV Size                6.00 GiB
  Current LE             1536
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
[16:36 root@Centos6.8/mnt/lv0]# resize2fs /dev/vg0/lv0  #逻辑再扩展
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg0/lv0 is mounted on /mnt/lv0; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg0/lv0 to 1572864 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 1572864 blocks long.
[16:37 root@Centos6.8/mnt/lv0]# df
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/sda2            50264772 4865484  42839288  11% /
tmpfs                  953648      72    953576   1% /dev/shm
/dev/sda1              194241   39020    144981  22% /boot
/dev/sda3            20027260   44996  18958264   1% /testdir
/dev/mapper/vg0-lv0   6192704  141436   5736708   3% /mnt/lv0

3、扩展卷组

[16:58 root@Centos6.8/mnt/lv0]# pvcreate /dev/sda9
  Physical volume "/dev/sda9" successfully created
[16:58 root@Centos6.8/mnt/lv0]# vgextend vg0 /dev/sda9
  Volume group "vg0" successfully extended
[16:58 root@Centos6.8/mnt/lv0]# vgdisplay 
  --- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  4
  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               9.02 GiB
  PE Size               4.00 MiB
  Total PE              2308
  Alloc PE / Size       1536 / 6.00 GiB
  Free  PE / Size       772 / 3.02 GiB
  VG UUID               OXd09k-uQ7C-BMNr-gkeY-HeQv-NJnP-H9WWxQ

4、缩减逻辑卷

没有做文件系统检测和resize2fs,直接缩减,会使原来逻辑卷的数据损坏:

[17:28 root@Centos6.8~]# ll /mnt/lv0/
total 20
-rw-rw-r--. 1 hill hill     0 Aug 27 17:23 f1
drwx------. 2 root root 16384 Aug 27 16:26 lost+found
drwxr-xr-x. 2 hill hill  4096 Aug 27 17:24 pam.d
[19:00 root@Centos6.8~]# umount /mnt/lv0/
[19:00 root@Centos6.8~]# 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/n]: y
  Size of logical volume vg0/lv0 changed from 7.00 GiB (1792 extents) to 3.00 GiB (768 extents).
  Logical volume lv0 successfully resized.
[19:05 root@Centos6.8~]# cd /mnt/lv0/
[19:05 root@Centos6.8/mnt/lv0]# ll
ls: cannot access pam.d: Input/output error
total 16
-rw-rw-r--. 1 hill hill     0 Aug 27 17:23 f1
drwx------. 2 root root 16384 Aug 27 16:26 lost+found
d?????????? ? ?    ?        ?            ? pam.d
[19:05 root@Centos6.8/mnt/lv0]# cd pam.d 
-bash: cd: pam.d: Input/output error
[19:05 root@Centos6.8/mnt/lv0]#

正确步骤:

[19:20 root@Centos6.8~]# umount /mnt/lv0/ #先卸载
[19:21 root@Centos6.8~]# 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: 12/327680 files (0.0% non-contiguous), 38512/1310720 blocks
[19:21 root@Centos6.8~]# 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.
[19:22 root@Centos6.8~]# lvdisplay  #查看缩减前的LV大小
  --- Logical volume ---
  LV Path                /dev/vg0/lv0
  LV Name                lv0
  VG Name                vg0
  LV UUID                32AMUw-xgxw-ejJ2-rps1-fm0e-feYw-OoTPS8
  LV Write Access        read/write
  LV Creation host, time Centos6.8, 2016-08-27 16:21:48 +0800
  LV Status              available
  # open                 0
  LV Size                5.00 GiB
  Current LE             1280
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
[19:22 root@Centos6.8~]# lvreduce -L 3G /dev/vg0/lv0  #物理缩减LV大小
  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/n]: 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.
[19:22 root@Centos6.8~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg0/lv0
  LV Name                lv0
  VG Name                vg0
  LV UUID                32AMUw-xgxw-ejJ2-rps1-fm0e-feYw-OoTPS8
  LV Write Access        read/write
  LV Creation host, time Centos6.8, 2016-08-27 16:21:48 +0800
  LV Status              available
  # open                 0
  LV Size                3.00 GiB
  Current LE             768
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
[19:23 root@Centos6.8~]# mount /dev/vg0/lv0 /mnt/lv0/ #重新挂载
[19:23 root@Centos6.8~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda2             48G  4.7G   41G  11% /
tmpfs                932M   72K  932M   1% /dev/shm
/dev/sda1            190M   39M  142M  22% /boot
/dev/sda3             20G   44M   19G   1% /testdir
/dev/mapper/vg0-lv0  3.0G   69M  2.8G   3% /mnt/lv0
[19:24 root@Centos6.8~]# ll /mnt/lv0/  #文件正常
total 20
-rw-r--r--. 1 root root    28 Aug 27 19:12 f1
drwx------. 2 root root 16384 Aug 27 19:12 lost+found

逻辑卷快照:

(1)快照是特殊的逻辑卷,它是在生成快照时存在的逻辑卷的准确拷贝

(2)对于需要备份或者复制的现有数据集临时拷贝以及其它操作来说,快照是最合适的选择。

(3)快照只有在它们和原来的逻辑卷不同时才会消耗空间,即一开始创建逻辑卷时,快照的空间其实是空的,这是为什么创建快照速度如此快的原因,快照后虽然理论上是一块空的空间,但实际上会在快照上看到原来的LV内容,但该内容不是存在快照上,而是原来LV的一个指针,只有原来LV内容更改时才会把内容拷贝到快照上。

(4)快照就是将当时的系统信息记录下来,就好像照相一般,若将来有任何数据改动了,则原始数据会被移动到快照区,没有改动的区域则由快照区和文件系统共享。

由于快照区与原本的LV共用很多PE的区块,因此快照去与被快照的LV必须要要在同一个VG上!系统恢复的时候的文件数量不能高于快照区的实际容量。

为现有逻辑卷创建快照
#lvcreate -l 64 -s -n snap-data -p r /dev/vg0/data #-p r,快照一般权限是只读
挂载快照
#mkdir -p /mnt/snap
#mount -o ro /dev/vg0/snap-data /mnt/snap
删除快照
#umount /mnt/databackup
#lvremove /dev/vg0/databackup

实例:

 [10:17 root@Centos6.8/mnt/lv1]# lvcreate -L 1G -n my_snapshot -p r -s /dev/vg1/lv1 
  Logical volume "my_snapshot" created.
[10:17 root@Centos6.8/mnt/lv1]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg1/lv1
  LV Name                lv1
  VG Name                vg1
  LV UUID                tPz7MB-ldJ7-VP0y-BN6c-aUj2-AAlM-5sDvqb
  LV Write Access        read/write
  LV Creation host, time Centos6.8, 2016-08-30 09:32:54 +0800
  LV snapshot status     source of
                         my_snapshot [active]
  LV Status              available
  # open                 1
  LV Size                3.00 GiB
  Current LE             192
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/vg1/my_snapshot
  LV Name                my_snapshot
  VG Name                vg1
  LV UUID                wLc4Sm-hmUM-Kk3R-ImgP-f7dC-Bzt1-TT4zk4
  LV Write Access        read only
  LV Creation host, time Centos6.8, 2016-08-30 10:17:24 +0800
  LV snapshot status     active destination for lv1
  LV Status              available
  # open                 0
  LV Size                3.00 GiB
  Current LE             192
  COW-table size         1.00 GiB
  COW-table LE           64
  Allocated to snapshot  0.00%
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3
   
[10:17 root@Centos6.8/mnt/lv1]# mkdir /mnt/napshottest
[10:17 root@Centos6.8/mnt/lv1]# mount /dev/vg1/my_snapshot /mnt/sda6/
mount: block device /dev/mapper/vg1-my_snapshot is write-protected, mounting read-only
[10:18 root@Centos6.8/mnt/lv1]# mount /dev/vg1/my_snapshot /mnt/sda6/
mount: block device /dev/mapper/vg1-my_snapshot is write-protected, mounting read-only
mount: /dev/mapper/vg1-my_snapshot already mounted or /mnt/sda6/ busy
mount: according to mtab, /dev/mapper/vg1-my_snapshot is already mounted on /mnt/sda6
[10:18 root@Centos6.8/mnt/lv1]# df
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/sda2             50264772 5659356  42045416  12% /
tmpfs                   953648      72    953576   1% /dev/shm
/dev/sda1               194241   39020    144981  22% /boot
/dev/sda3             20027260   44996  18958264   1% /testdir
/dev/mapper/vg1-lv1    3096336   34364   2904712   2% /mnt/lv1
/dev/mapper/vg1-my_snapshot
                       3096336   34364   2904712   2% /mnt/sda6
   
[10:19 root@Centos6.8/mnt]# cd lv1/
[10:19 root@Centos6.8/mnt/lv1]# ls
f1  inittab  lost+found  rc.sysinit
[10:19 root@Centos6.8/mnt/lv1]# echo > inittab #修改原来逻辑卷文件
[10:20 root@Centos6.8/mnt/lv1]# cat inittab  #修改原来逻辑卷文件已经被修改
[10:20 root@Centos6.8/mnt/lv1]# cat /mnt/sda6/inittab #查看快照的文件还是初始状态
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
# 
id:5:initdefault:

原创文章,作者:麦德良,如若转载,请注明出处:http://www.178linux.com/42348

(0)
上一篇 2016-09-01 08:50
下一篇 2016-09-01 08:50

相关推荐

  • rpm软件管理工具详解

    1 Linux软件安装概述 安装程序的方式: 软件包概述 RPM RPM的缺点 RPM包 RPM分包 RPM命令的使用 rpm 包管理 升级注意项 如何安装rpm软件包 如何卸载rpm软件包 如何升级rpm软件包 如何查询rpm软件包 如何查看与rpm包相关的文件和其他信息 如何校验rpm包 RPM的数据库 1 Linux软件安装概述 安装程序的方式: 通用…

    Linux干货 2016-09-06
  • vim简单实用的技巧总结

    vi 和 vim无需过多的区分,vim可认为是vi的增强版。        这篇关于vim的手册,我个人觉得实在无法把它写成文章,只能以手册的方式列举出来,因为,vim是一个动手使用的工具,只能在使用中你才能慢慢发现它的操作多么符合逻辑,我从下面这些简略的描述中,尽量将它的操作步骤写出来,但更重要的…

    Linux干货 2015-09-14
  • 功能强大的Linux文本编辑器之Vim的使用

    VIM编辑器   Vim章节的内容:    使用vi和vim的三种主要模式    移动光标,进入插入模式    改变、删除、复制文本    撤销改变    搜索文档    vim寄存器    可视化和多窗口 &…

    Linux干货 2016-08-12
  • 使用 nice、cpulimit 和 cgroups 限制 cpu 占用率

    Linux内核是一名了不起的马戏表演者,它在进程和系统资源间小心地玩着杂耍,并保持系统的正常运转。 同时,内核也很公正:它将资源公平地分配给各个进程。 但是,如果你需要给一个重要进程提高优先级时,该怎么做呢? 或者是,如何降低一个进程的优先级? 又或者,如何限制一组进程所使用的资源呢? 答案是需要由用户来为内核指定进程的优先级 大部分进程启动时的优先级是相同…

    Linux干货 2015-02-14
  • 马哥教育网络班21期+第五周课程练习

    马哥教育网络班21期+第5周课程练习 [TOC] 1. 显示/boot/grub/grub.conf中至少以一个空白字符开头的行。 [root@rhel-5 ~]# grep -E '^[[:space:]]+' /boot/grub/grub.conf  root (hd…

    Linux干货 2016-08-02
  • 软件包管理(rpm篇)

    软件包管理(rpm篇)静态和动态链接    链接主要作用是把各个模块之间相互引用的部分处理好,使得各个模块之间能够正确地衔接,分为静态链接和动态链接    静态链接        把程序对应的依赖库复制一份到包&nbsp…

    Linux干货 2017-04-24

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-02 09:30

    内容饱满,但是缺乏了逻辑性,这是作者需要好好培养的地方,同时对于raid部分如果能通过图片来展示各个级别的工作模式会更直观哦。