磁盘管理实验文档(my version)


一、磁盘分区常用命令

首先看看当前的硬盘信息

[root@localhost ~]# fdisk -l

Disk /dev/sda: 85.9 GB, 85899345920 bytes, 167772160 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00025137

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   167772159    83373056   8e  Linux LVM
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: gpt


#         Start          End    Size  Type            Name
 1         2048     10487807      5G  Linux filesyste Linux filesystem
 2     10487808     20973567      5G  Linux filesyste 

Disk /dev/sdd: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/sde: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-root: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-home: 29.5 GB, 29469179904 bytes, 57556992 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@localhost ~]#

可以在/proc/partitions这个文件查看当前的分区

[root@localhost ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0   83886080 sda
   8        1     512000 sda1
   8        2   83373056 sda2
   8       16   20971520 sdb
   8       17    5242880 sdb1
   8       18    5242880 sdb2
  11        0    7587840 sr0
   8       48   20971520 sdd
   8       64   20971520 sde
   8       32   20971520 sdc
 253        0   52428800 dm-0
 253        1    2097152 dm-1
 253        2   28778496 dm-2
[root@localhost ~]#

先试试MBR格式的分区,fdisk选项如下所示

[root@localhost ~]# fdisk /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): m
Command action
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   w   write table to disk and exit

Command (m for help):

输入n,可以创建新的MBR分区,然后输入p可以显示当前的分区状态

[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x3d04855b.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): p

Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x3d04855b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048    20973567    10485760   83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#

重复n的操作,添加其他分区。 注:MBR格式磁盘最多可以创建4个主分区或3个主分区1个扩展分区,在扩展分区中可以创建若干个逻辑分区。

注意id代表了磁盘的用途,可以通过t来改变

[root@localhost ~]# fdisk /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition number (1-128, default 1): 
First sector (34-41943006, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943006, default 41943006): +5G
Created partition 1


Command (m for help): t
Selected partition 1
Partition type (type L to list all types): L
  1 EFI System                     C12A7328-F81F-11D2-BA4B-00A0C93EC93B
  2 MBR partition scheme           024DEE41-33E7-11D3-9D69-0008C781F39F
  3 BIOS boot partition            21686148-6449-6E6F-744E-656564454649
  4 Microsoft reserved             E3C9E316-0B5C-4DB8-817D-F92DF00215AE
  5 Microsoft basic data           EBD0A0A2-B9E5-4433-87C0-68B6B72699C7
  6 Microsoft LDM metadata         5808C8AA-7E8F-42E0-85D2-E1E90434CFB3
  7 Microsoft LDM data             AF9B60A0-1431-4F62-BC68-3311714A69AD
  8 Windows recovery evironmnet    DE94BBA4-06D1-4D40-A16A-BFD50179D6AC
  9 IBM General Parallel Fs        37AFFC90-EF7D-4E96-91C3-2D7AE055B174
 10 HP-UX data partition           75894C1E-3AEB-11D3-B7C1-7B03A0000000
 11 HP-UX service partition        E2A1E728-32E3-11D6-A682-7B03A0000000
 12 Linux filesystem               0FC63DAF-8483-4772-8E79-3D69D8477DE4
 13 Linux RAID                     A19D880F-05FC-4D3B-A006-743F0F84911E
 14 Linux swap                     0657FD6D-A4AB-43C4-84E5-0933C84B4F4F
 15 Linux LVM                      E6D6D379-F507-44C2-A23C-238F2A3DF928
 16 Linux reserved                 8DA63339-0007-60C0-C436-083AC8230908
 17 FreeBSD data                   516E7CB4-6ECF-11D6-8FF8-00022D09712B
 18 FreeBSD boot                   83BD6B9D-7F41-11DC-BE0B-001560B84F0F
 19 FreeBSD swap                   516E7CB5-6ECF-11D6-8FF8-00022D09712B
 20 FreeBSD UFS                    516E7CB6-6ECF-11D6-8FF8-00022D09712B
 21 FreeBSD ZFS                    516E7CBA-6ECF-11D6-8FF8-00022D09712B
 22 FreeBSD Vinum                  516E7CB8-6ECF-11D6-8FF8-00022D09712B
 23 Apple HFS/HFS+                 48465300-0000-11AA-AA11-00306543ECAC
 24 Apple UFS                      55465300-0000-11AA-AA11-00306543ECAC
 25 Apple RAID                     52414944-0000-11AA-AA11-00306543ECAC
 26 Apple RAID offline             52414944-5F4F-11AA-AA11-00306543ECAC
 27 Apple boot                     426F6F74-0000-11AA-AA11-00306543ECAC
 28 Apple label                    4C616265-6C00-11AA-AA11-00306543ECAC
 29 Apple TV recovery              5265636F-7665-11AA-AA11-00306543ECAC
 30 Apple Core storage             53746F72-6167-11AA-AA11-00306543ECAC
 31 Solaris boot                   6A82CB45-1DD2-11B2-99A6-080020736631
 32 Solaris root                   6A85CF4D-1DD2-11B2-99A6-080020736631
 33 Solaris /usr & Apple ZFS       6A898CC3-1DD2-11B2-99A6-080020736631
 34 Solaris swap                   6A87C46F-1DD2-11B2-99A6-080020736631
 35 Solaris backup                 6A8B642B-1DD2-11B2-99A6-080020736631
 36 Solaris /var                   6A8EF2E9-1DD2-11B2-99A6-080020736631
 37 Solaris /home                  6A90BA39-1DD2-11B2-99A6-080020736631
 38 Solaris alternate sector       6A9283A5-1DD2-11B2-99A6-080020736631
 39 Solaris reserved 1             6A945A3B-1DD2-11B2-99A6-080020736631
 40 Solaris reserved 2             6A9630D1-1DD2-11B2-99A6-080020736631
 41 Solaris reserved 3             6A980767-1DD2-11B2-99A6-080020736631
 42 Solaris reserved 4             6A96237F-1DD2-11B2-99A6-080020736631
 43 Solaris reserved 5             6A8D2AC7-1DD2-11B2-99A6-080020736631
 44 NetBSD swap                    49F48D32-B10E-11DC-B99B-0019D1879648
 45 NetBSD FFS                     49F48D5A-B10E-11DC-B99B-0019D1879648
 46 NetBSD LFS                     49F48D82-B10E-11DC-B99B-0019D1879648
 47 NetBSD concatenated            2DB519C4-B10E-11DC-B99B-0019D1879648
 48 NetBSD encrypted               2DB519EC-B10E-11DC-B99B-0019D1879648
 49 NetBSD RAID                    49F48DAA-B10E-11DC-B99B-0019D1879648
 50 ChromeOS kernel                FE3A2A5D-4F32-41A7-B725-ACCC3285A309
 51 ChromeOS root fs               3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC
 52 ChromeOS reserved              2E0A753D-9E48-43B0-8337-B15192CB1B5E
 53 MidnightBSD data               85D5E45A-237C-11E1-B4B3-E89A8F7FC3A7
 54 MidnightBSD boot               85D5E45E-237C-11E1-B4B3-E89A8F7FC3A7
 55 MidnightBSD swap               85D5E45B-237C-11E1-B4B3-E89A8F7FC3A7
 56 MidnightBSD UFS                0394Ef8B-237C-11E1-B4B3-E89A8F7FC3A7
 57 MidnightBSD ZFS                85D5E45D-237C-11E1-B4B3-E89A8F7FC3A7
 58 MidnightBSD Vinum              85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7
[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 

看看分区记录

[root@localhost ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0   83886080 sda
   8        1     512000 sda1
   8        2   83373056 sda2
   8       16   20971520 sdb
   8       17    5242880 sdb1
   8       18    5242880 sdb2
  11        0    7587840 sr0
   8       48   20971520 sdd
   8       64   20971520 sde
   8       32   20971520 sdc
   8       33   10485760 sdc1
 253        0   52428800 dm-0
 253        1    2097152 dm-1
 253        2   28778496 dm-2
[root@localhost ~]#

gdisk和fdisk非常类似

[root@localhost ~]# gdisk 
GPT fdisk (gdisk) version 0.8.6

Type device filename, or press  to exit: 
[root@localhost ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.6

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): ?
b    back up GPT data to a file
c    change a partition's name
d    delete a partition
i    show detailed information on a partition
l    list known partition types
n    add a new partition
o    create a new empty GUID partition table (GPT)
p    print the partition table
q    quit without saving changes
r    recovery and transformation options (experts only)
s    sort partitions
t    change a partition's type code
v    verify disk
w    write table to disk and exit
x    extra functionality (experts only)
?    print this menu

Command (? for help): n
Partition number (1-128, default 1): 1
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +10G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 7C54ABBD-43F6-4748-8BB0-24C9333F1F84
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 20971453 sectors (10.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        20973567   10.0 GiB    8300  Linux filesystem

Command (? for help): t
Using 1
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): L
0700 Microsoft basic data  0c01 Microsoft reserved    2700 Windows RE          
4200 Windows LDM data      4201 Windows LDM metadata  7501 IBM GPFS            
7f00 ChromeOS kernel       7f01 ChromeOS root         7f02 ChromeOS reserved   
8200 Linux swap            8300 Linux filesystem      8301 Linux reserved      
8e00 Linux LVM             a500 FreeBSD disklabel     a501 FreeBSD boot        
a502 FreeBSD swap          a503 FreeBSD UFS           a504 FreeBSD ZFS         
a505 FreeBSD Vinum/RAID    a580 Midnight BSD data     a581 Midnight BSD boot   
a582 Midnight BSD swap     a583 Midnight BSD UFS      a584 Midnight BSD ZFS    
a585 Midnight BSD Vinum    a800 Apple UFS             a901 NetBSD swap         
a902 NetBSD FFS            a903 NetBSD LFS            a904 NetBSD concatenated 
a905 NetBSD encrypted      a906 NetBSD RAID           ab00 Apple boot          
af00 Apple HFS/HFS+        af01 Apple RAID            af02 Apple RAID offline  
af03 Apple label           af04 AppleTV recovery      af05 Apple Core Storage  
be00 Solaris boot          bf00 Solaris root          bf01 Solaris /usr & Mac Z
bf02 Solaris swap          bf03 Solaris backup        bf04 Solaris /var        
bf05 Solaris /home         bf06 Solaris alternate se  bf07 Solaris Reserved 1  
bf08 Solaris Reserved 2    bf09 Solaris Reserved 3    bf0a Solaris Reserved 4  
bf0b Solaris Reserved 5    c001 HP-UX data            c002 HP-UX service       
ed00 Sony system partitio  ef00 EFI System            ef01 MBR partition scheme
ef02 BIOS boot partition   fb00 VMWare VMFS           fb01 VMWare reserved     
fc00 VMWare kcore crash p  fd00 Linux RAID            
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.

创建新分区的时候可以看见可以有128个分区

parted,和前两个相比,更灵活,可以自行设定MBR或GPT格式和分区

[root@localhost ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help                                                             
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all
        found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU
        Parted
(parted)

通过mklabel msdos可以设定为MBR格式,然后可以通过mkpart来划分分区

[root@localhost ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel msdos                                                    
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do
you want to continue?
Yes/No? yes                                                               
(parted) mkpart primary 0 10G
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I                                                          
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End     Size    Type     File system  Flags
 1      512B   10.0GB  10.0GB  primary

(parted) set 1 lvm on                                                     
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End     Size    Type     File system  Flags
 1      512B   10.0GB  10.0GB  primary               lvm

(parted) quit                                                             
Information: You may need to update /etc/fstab.
  • msdos设定为MBR格式
  • gpt设定为GPT格式
  • primary代表主分区
  • extended代表扩展分区
  • logical代表逻辑分区
  • set number flag state用于设置分区的用途,flag:boot、lvm、raid。State:on/off表示开启或关闭。

parted工具分完区后无需保存,输入q退出即可。 划分好分区之后,还需要格式化才能使用。可以通过 mkfs/mkswap来格式化文件系统

#mkfs.xfs  /dev/分区设备名或#mkfs  -t  xfs  /dev/分区设备名
[root@localhost ~]# mkfs -t xfs /dev/sdb1
meta-data=/dev/sdb1              isize=256    agcount=4, agsize=610352 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=2441406, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]#

可以修改fstab实现自动加载

# /etc/fstab
# Created by anaconda on Sun Jul 10 08:31:13 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=6fc778fd-78b9-4602-9b5c-41c9032df718 /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sdb1/      /data1  xfs     defaults        0 0

测试是否能自动挂载

[root@localhost ~]# mount -a
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G  3.6G   47G   8% /
devtmpfs                 474M     0  474M   0% /dev
tmpfs                    489M  144K  489M   1% /dev/shm
tmpfs                    489M  7.0M  482M   2% /run
tmpfs                    489M     0  489M   0% /sys/fs/cgroup
/dev/mapper/centos-home   28G   36M   28G   1% /home
/dev/sda1                497M  158M  340M  32% /boot
/dev/sr0                 7.3G  7.3G     0 100% /mnt
tmpfs                    489M   20K  489M   1% /tmp
tmpfs                     98M   24K   98M   1% /run/user/0
/dev/sdb1                9.4G   33M  9.3G   1% /data1
[root@localhost ~]#

通过df -h查看已经挂载了的设备

  • -T选项可以显示设备的文件系统类
  • 有的挂载点路径比较长,自动分2行显示,可以-P强制一行显示
  • 如同进程有pid,用户有uid,每个文件系统也有自己的id,称为uuid,但是不是每个分区都有;如果某个分区没有文件系统,那么这个分区是没有uuid的。
  • 可以通过 blkid(block id)来查看。注意uuid标记的是文件系统,而不是分区。uuid的好处在于可以通过uuid这个唯一值来挂载系统,这样可以避免因为删除硬盘造成的错位,sda6变成了sda5等等
[root@localhost ~]# blkid
/dev/sda1: UUID="6fc778fd-78b9-4602-9b5c-41c9032df718" TYPE="xfs" 
/dev/sda2: UUID="IAX19G-FdKG-utRi-04N7-rx81-SFP3-zt6lDi" TYPE="LVM2_member" 
/dev/sdb1: UUID="ad56ea35-bcd5-409e-abfc-03958f7f249a" TYPE="xfs" 
/dev/sr0: UUID="2015-12-09-22-36-30-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/mapper/centos-root: UUID="9fbe9b5c-4b64-477e-a906-f67cf77e7c61" TYPE="xfs" 
/dev/mapper/centos-swap: UUID="0bbdad7b-4f55-4596-b9e1-a919809399d0" TYPE="swap" 
/dev/mapper/centos-home: UUID="07a05880-2ba7-4966-8e9a-4d687568d68b" TYPE="xfs" 
[root@localhost ~]#

我们可以通过xfs_admin -U 来手动更改文件系统的uuid

[root@localhost ~]# umount /dev/sdb1
[root@localhost ~]# uuidgen
8758e9f7-3f46-443f-b910-d2d37d2ec3ea
[root@localhost ~]# xfs_admin -U 8758e9f7-3f46-443f-b910-d2d37d2ec3ea /dev/sdb1
Clearing log and setting UUID
writing all SBs
new UUID = 8758e9f7-3f46-443f-b910-d2d37d2ec3ea
[root@localhost ~]# blkid
/dev/sda1: UUID="6fc778fd-78b9-4602-9b5c-41c9032df718" TYPE="xfs" 
/dev/sda2: UUID="IAX19G-FdKG-utRi-04N7-rx81-SFP3-zt6lDi" TYPE="LVM2_member" 
/dev/sdb1: UUID="8758e9f7-3f46-443f-b910-d2d37d2ec3ea" TYPE="xfs" 
/dev/sr0: UUID="2015-12-09-22-36-30-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/mapper/centos-root: UUID="9fbe9b5c-4b64-477e-a906-f67cf77e7c61" TYPE="xfs" 
/dev/mapper/centos-swap: UUID="0bbdad7b-4f55-4596-b9e1-a919809399d0" TYPE="swap" 
/dev/mapper/centos-home: UUID="07a05880-2ba7-4966-8e9a-4d687568d68b" TYPE="xfs" 
[root@localhost ~]#

附:在目录里面可以通过 ls -ld查看目录的属性,ls -la查看内容的属性,但是-ld显示的目录大小只有4K,这仅仅是目录本身的大小,要想查看目录和其内容的整个大小,可以通过du来查看,如果只想看最终结果,使用-s(summary)就行了

[root@localhost ~]# du -h /boot/
4.0K    /boot/grub
0    /boot/grub2/themes/system
0    /boot/grub2/themes
2.4M    /boot/grub2/i386-pc
3.3M    /boot/grub2/locale
2.5M    /boot/grub2/fonts
8.1M    /boot/grub2
132M    /boot/
[root@localhost ~]# du -hs /boot
132M    /boot
[root@localhost ~]#

二、创建swap分区

下面我们来看看swap分区如何手动创建。swap类似windows的虚拟内存/page file,当内存不足的时候,数据保存在swap中。 有两种方式可以使用:

第一种单独用一个分区来作为swap

[root@localhost ~]# cat /proc/swaps 
Filename                Type        Size    Used    Priority
/dev/dm-1                               partition    2097148    12680    -1
[root@localhost ~]#

创建一个分区(如:/dev/sdb3),并更改分区ID为82

[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 3
First sector (19531251-41943039, default 19531776): 
Using default value 19531776
Last sector, +sectors or +size{K,M,G} (19531776-41943039, default 41943039): +5G
Partition 3 of type Linux and of size 5 GiB is set

Command (m for help): t
Partition number (1,3, default 3): 3
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00065a96

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1    19531250     9765625   8e  Linux LVM
/dev/sdb3        19531776    30017535     5242880   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.
[root@localhost ~]# partx -a /dev/sdb
partx: /dev/sdb: failed to read partition table
[root@localhost ~]# ls /dev/sdb
/dev/sdb
[root@localhost ~]# partprobe /dev/sdb
sdb   sdb1  
[root@localhost ~]# kpartx /dev/sdb
sdb1 : 0 19531250 /dev/sdb 1
sdb3 : 0 10485760 /dev/sdb 19531776
[root@localhost ~]# partx -u /dev/sdb
partx: /dev/sdb: failed to read partition table
[root@localhost ~]# partprobe /dev/sdb
sdb   sdb1  
[root@localhost ~]# partprobe /dev/sdb
[root@localhost ~]# partprobe /dev/sdb
sdb   sdb1  sdb3  
[root@localhost ~]# partprobe /dev/sdb

执行partx –a /dev/sdb命令,使分区修改生效

在分区上创建swap文件系统

[root@localhost ~]# mkswap /dev/sdb3
Setting up swapspace version 1, size = 5242876 KiB
no label, UUID=77f07660-3757-4d74-aaf7-26f551746ffe
[root@localhost ~]# swapon /dev/sdb3
[root@localhost ~]# cat /proc/swaps 
Filename                Type        Size    Used    Priority
/dev/dm-1                               partition    2097148    12960    -1
/dev/sdb3                               partition    5242876    0    -2
[root@localhost ~]#

修改fstab实现自动加载

/dev/sdb3       swap    swap    defaults        0 0

第二种方式是创建一个文件块,这个文件所占有的空间作为swap使用

[root@localhost ~]# dd if=/dev/zero of=/swapfile bs=1M count=2048
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 41.4544 s, 51.8 MB/s
[root@localhost ~]# mkswap /swapfile 
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=18e57fc8-a288-4a84-83ab-ecf3a85b25b2
[root@localhost ~]# ls -l /swapfile
-rw-r--r--. 1 root root 2147483648 Jul 15 22:09 /swapfile
[root@localhost ~]# chmod 600 /swapfile 
[root@localhost ~]# swapon /swapfile
[root@localhost ~]# cat /proc/swaps 
Filename                Type        Size    Used    Priority
/dev/dm-1                               partition    2097148    13268    -1
/dev/sdb3                               partition    5242876    0    -2
/swapfile                               file        2097148    0    -3
[root@localhost ~]#

对于普通的分区,扩展度不高,一旦分区格式化完成,很难灵活的再增加或者减少分区大小。为了解决这个问题,可以使用LVM(逻辑卷)。基本过程是把物理磁盘或者分区初始化称为物理卷(PV),然后把PV加入VG(卷组),最后在VG上划分逻辑的分区(LVM),LVM可以当做普通的分区进行格式化和挂载。


三、创建LVM

将准备的磁盘或分区创建PV

[root@localhost ~]# pvcreate /dev/sdb1
WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created
[root@localhost ~]# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created
[root@localhost ~]#

可以执行pvdisplay查看PV的详细信息,pvremove删除PV

[root@localhost ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               79.51 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              20354
  Free PE               16
  Allocated PE          20338
  PV UUID               IAX19G-FdKG-utRi-04N7-rx81-SFP3-zt6lDi

  "/dev/sdc1" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               hjETI4-pPlT-zhJ0-WTvt-ZRu3-YbwP-pcYBBt

  "/dev/sdb1" is a new physical volume of "5.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               5.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               i6RXwU-0A0u-LS40-mlfB-WUBa-ReNQ-TRsSCU

[root@localhost ~]#

创建完PV,之后,需要创建VG,然后添加PV到VG中

可以通过vgdisplay查看具体的信息,注意PE的Size是4M,这个是增减的最小计算单位

[root@localhost ~]# vgcreate vg00 /dev/sdb1 /dev/sdc1
  Volume group "vg00" successfully created
[root@localhost ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "centos" using metadata type lvm2
  Found volume group "vg00" using metadata type lvm2
[root@localhost ~]# vgdisplay vg00
  --- Volume group ---
  VG Name               vg00
  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               14.99 GiB
  PE Size               4.00 MiB
  Total PE              3838
  Alloc PE / Size       0 / 0   
  Free  PE / Size       3838 / 14.99 GiB
  VG UUID               qvD13H-7Me2-aoCS-fqVZ-5puw-1sUn-8MUwc1

[root@localhost ~]#

注:创建VG时:使用–s选项的作用是在创建时指定PE块(物理扩展单元)的大小,默认是4M。 如:# vgcreate volGroup03 -s 8M /dev/sdb[12])

我们可以继续往vg里面添加新的分区

[root@localhost ~]# vgextend vg00 /dev/sdc2
  Volume group "vg00" successfully extended
[root@localhost ~]# vgdisplay vg00
  --- Volume group ---
  VG Name               vg00
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  2
  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               19.99 GiB
  PE Size               4.00 MiB
  Total PE              5117
  Alloc PE / Size       0 / 0   
  Free  PE / Size       5117 / 19.99 GiB
  VG UUID               qvD13H-7Me2-aoCS-fqVZ-5puw-1sUn-8MUwc1

[root@localhost ~]#

若事先没有把sdb3转化为pv,而是直接添加到vg里面,不过一旦添加了他自动就初始化成pv了。

[root@localhost ~]# vgextend vg00 /dev/sde1
  Physical volume "/dev/sde1" successfully created
  Volume group "vg00" successfully extended
[root@localhost ~]# vgdisplay vg00
  --- Volume group ---
  VG Name               vg00
  System ID             
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               29.98 GiB
  PE Size               4.00 MiB
  Total PE              7676
  Alloc PE / Size       0 / 0   
  Free  PE / Size       7676 / 29.98 GiB
  VG UUID               qvD13H-7Me2-aoCS-fqVZ-5puw-1sUn-8MUwc1

[root@localhost ~]#

可以添加当然也可以减少pv。 #vgreduce vg00 /dev/sdb3

VG准备就绪,可以创建了LVM了

[root@localhost ~]# lvcreate -L 110M -n lv00 vg00
  Rounding up size to full physical extent 112.00 MiB
  Logical volume "lv00" created.
[root@localhost ~]# lvscan
  ACTIVE            '/dev/centos/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/centos/home' [27.45 GiB] inherit
  ACTIVE            '/dev/centos/root' [50.00 GiB] inherit
  ACTIVE            '/dev/vg00/lv00' [112.00 MiB] inherit
[root@localhost ~]#

注意看他的大小其实是112M,因为PE的大小是4M,这个4M是最小单位,不能破开,因此28个PE就是112M

[root@localhost ~]# echo 112/4 | bc
28
[root@localhost ~]# lvdisplay /dev/vg00/lv00 
  --- Logical volume ---
  LV Path                /dev/vg00/lv00
  LV Name                lv00
  VG Name                vg00
  LV UUID                qqs0eu-O2vN-pL1V-16ta-R4bE-1hLa-fzhB3I
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-07-15 22:25:09 +0800
  LV Status              available
  # open                 0
  LV Size                112.00 MiB
  Current LE             28
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3

[root@localhost ~]#

注:大L可以直接指定大小,小l是指定多少个PE的值 也可以设置剩余空间的百分比

[root@localhost ~]# lvcreate -l 10%free -n lv01 vg00
  Logical volume "lv01" created.
[root@localhost ~]#

删除逻辑卷 #lvremove /dev/vg00/lv01

[root@localhost ~]# lvremove /dev/vg00/lv01
Do you really want to remove active logical volume lv01? [y/n]: y
  Logical volume "lv01" successfully removed
[root@localhost ~]#

对已经创建的逻辑卷,可以当做普通分区一样格式化和挂载

[root@localhost ~]# mkfs.xfs /dev/vg00/lv00 
meta-data=/dev/vg00/lv00         isize=256    agcount=4, agsize=7168 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=28672, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]#

修改/etc/fstab文件实现开机自动挂载。

/dev/vg00/lv00  /data   xfs     defaults        0 0
[root@localhost /]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G  5.6G   45G  12% /
devtmpfs                 474M     0  474M   0% /dev
tmpfs                    489M  144K  489M   1% /dev/shm
tmpfs                    489M  7.0M  482M   2% /run
tmpfs                    489M     0  489M   0% /sys/fs/cgroup
/dev/mapper/centos-home   28G   36M   28G   1% /home
/dev/sda1                497M  158M  340M  32% /boot
/dev/sr0                 7.3G  7.3G     0 100% /mnt
tmpfs                    489M   20K  489M   1% /tmp
tmpfs                     98M   24K   98M   1% /run/user/0
/dev/mapper/vg00-lv00    109M  5.8M  103M   6% /data

扩展一个逻辑卷,增加300M,首先要确保卷组有大于300M的空闲空间。

[root@localhost /]# vgdisplay vg00
  --- Volume group ---
  VG Name               vg00
  System ID             
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  6
  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               29.98 GiB
  PE Size               4.00 MiB
  Total PE              7676
  Alloc PE / Size       28 / 112.00 MiB
  Free  PE / Size       7648 / 29.88 GiB
  VG UUID               qvD13H-7Me2-aoCS-fqVZ-5puw-1sUn-8MUwc1

[root@localhost /]#

执行lvextend扩展逻辑卷大小

[root@localhost /]# lvextend -L +200M /dev/vg00/lv00 
  Size of logical volume vg00/lv00 changed from 112.00 MiB (28 extents) to 312.00 MiB (78 extents).
  Logical volume lv00 successfully resized.
[root@localhost /]# lvscan
  ACTIVE            '/dev/centos/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/centos/home' [27.45 GiB] inherit
  ACTIVE            '/dev/centos/root' [50.00 GiB] inherit
  ACTIVE            '/dev/vg00/lv00' [312.00 MiB] inherit
[root@localhost /]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        50G  5.6G   45G  12% /
devtmpfs                devtmpfs  474M     0  474M   0% /dev
tmpfs                   tmpfs     489M  144K  489M   1% /dev/shm
tmpfs                   tmpfs     489M  7.0M  482M   2% /run
tmpfs                   tmpfs     489M     0  489M   0% /sys/fs/cgroup
/dev/mapper/centos-home xfs        28G   36M   28G   1% /home
/dev/sda1               xfs       497M  158M  340M  32% /boot
/dev/sr0                iso9660   7.3G  7.3G     0 100% /mnt
tmpfs                   tmpfs     489M   20K  489M   1% /tmp
tmpfs                   tmpfs      98M   24K   98M   1% /run/user/0
/dev/mapper/vg00-lv00   xfs       109M  5.8M  103M   6% /data
[root@localhost /]#

注意逻辑卷的文件系统仍然是109M没有改变,我们还需要填充文件系统的空白。 RHEL7可以用xfs_growfs来扩大XFS文件系统,也可以直接用resize2fs 来处理设备

  • 注意的是 XFS系统只能增长,不能减少!因此如果需要减少LVM的话,分区只能使用ext4了
[root@localhost /]# xfs_growfs /dev/vg00/lv00 
meta-data=/dev/mapper/vg00-lv00  isize=256    agcount=4, agsize=7168 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=28672, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 28672 to 79872
[root@localhost /]#

执行df查看扩展后的文件系统

[root@localhost /]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        50G  5.6G   45G  12% /
devtmpfs                devtmpfs  474M     0  474M   0% /dev
tmpfs                   tmpfs     489M  144K  489M   1% /dev/shm
tmpfs                   tmpfs     489M  7.0M  482M   2% /run
tmpfs                   tmpfs     489M     0  489M   0% /sys/fs/cgroup
/dev/mapper/centos-home xfs        28G   36M   28G   1% /home
/dev/sda1               xfs       497M  158M  340M  32% /boot
/dev/sr0                iso9660   7.3G  7.3G     0 100% /mnt
tmpfs                   tmpfs     489M   20K  489M   1% /tmp
tmpfs                   tmpfs      98M   24K   98M   1% /run/user/0
/dev/mapper/vg00-lv00   xfs       309M  6.1M  303M   2% /data
[root@localhost /]#

四、逻辑卷快照

LVM提供一个极妙的设备,它是snaphot。允许管理员创建一个新的块装置,在某个时间点提供了一个精确的逻辑卷副本,快照提供原始卷的静态视图LVM 快照通过把文件系统的改变记录到一个快照分区,因此当你创建一个快照分区时,你不需要使用和你正创建快照的分区一样大小的分区,所需的空间大小取决于快照的使用,所以没有可循的方法来设置此大小。如果快照的大小等于原始卷的大小那么快照永远可用。

快照是特殊的逻辑卷,只可以对逻辑卷做快照。逻辑卷快照和需要做快照的逻辑卷必须在同一个卷组里面

现在在我们的系统中有个逻辑卷/dev/vg00/lv00,我们用lvdisplay来查询一下这个逻辑卷

[root@localhost /]# lvdisplay /dev/vg00/lv00 
  --- Logical volume ---
  LV Path                /dev/vg00/lv00
  LV Name                lv00
  VG Name                vg00
  LV UUID                qqs0eu-O2vN-pL1V-16ta-R4bE-1hLa-fzhB3I
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-07-15 22:25:09 +0800
  LV Status              available
  # open                 1
  LV Size                312.00 MiB
  Current LE             78
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3

[root@localhost /]#
[root@localhost /]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        50G  5.6G   45G  12% /
devtmpfs                devtmpfs  474M     0  474M   0% /dev
tmpfs                   tmpfs     489M  144K  489M   1% /dev/shm
tmpfs                   tmpfs     489M  7.0M  482M   2% /run
tmpfs                   tmpfs     489M     0  489M   0% /sys/fs/cgroup
/dev/mapper/centos-home xfs        28G   36M   28G   1% /home
/dev/sda1               xfs       497M  158M  340M  32% /boot
/dev/sr0                iso9660   7.3G  7.3G     0 100% /mnt
tmpfs                   tmpfs     489M   20K  489M   1% /tmp
tmpfs                   tmpfs      98M   24K   98M   1% /run/user/0
/dev/mapper/vg00-lv00   xfs       309M  6.1M  303M   2% /data
[root@localhost /]#

可以看到,这个逻辑卷/dev/vg00/lv00的大小是309M。我们将这个逻辑卷/dev/vg00/lv00挂载到/data下面。复制一些数据到/data里面去。方便等下做试验

[root@localhost /]# cp /etc/passwd /etc/shadow /etc/group /data/
[root@localhost /]# ls -l /data
total 12
-rw-r--r--. 1 root root  984 Jul 15 22:37 group
-rw-r--r--. 1 root root 2315 Jul 15 22:37 passwd
----------. 1 root root 1282 Jul 15 22:37 shadow
[root@localhost /]#

现在我们就为逻辑卷/dev/vg00/lv00来做快照

[root@localhost /]# lvcreate --size 300M --snapshot --name lvsp00 /dev/vg00/lv00
  Logical volume "lvsp00" created.

执行lvscan查看创建好的逻辑卷

[root@localhost /]# lvscan 
  ACTIVE            '/dev/centos/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/centos/home' [27.45 GiB] inherit
  ACTIVE            '/dev/centos/root' [50.00 GiB] inherit
  ACTIVE   Original '/dev/vg00/lv00' [312.00 MiB] inherit
  ACTIVE   Snapshot '/dev/vg00/lvsp00' [300.00 MiB] inherit
[root@localhost /]#

可以看到/dev/vg00/lv00是原始逻辑卷,而/dev/vg00/lvsp00是快照

执行lvdisplay或lvs命令查看逻辑信息

[root@localhost /]# lvdisplay /dev/vg00/lvsp00 
  --- Logical volume ---
  LV Path                /dev/vg00/lvsp00
  LV Name                lvsp00
  VG Name                vg00
  LV UUID                Q8gjdc-NL2q-DdX9-qVLz-8AZ6-16wA-crS52f
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-07-15 22:39:31 +0800
  LV snapshot status     active destination for lv00
  LV Status              available
  # open                 0
  LV Size                312.00 MiB
  Current LE             78
  COW-table size         300.00 MiB
  COW-table LE           75
  Allocated to snapshot  0.01%
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:6

[root@localhost /]#

可以看到逻辑卷快照创建成功了, 注意:这个快照卷建好之后,是不需要格式化也不需要进行挂载的。格式化或挂载都会出现的错误提示的。

模拟将原逻辑卷中的数据删除

[root@localhost /]# rm -rf /data/*
[root@localhost /]# ls /data/
[root@localhost /]#

如何恢复原逻辑卷的数据?有两方式可以恢复删除的数据

方式一 是先将原逻辑卷卸除挂载#umount /dev/vg00/lv00 然后挂载逻辑卷快照即可 #mount /dev/vg00/lvsp00 /data,就可以正常访问数据了

[root@localhost /]# umount /dev/vg00/lv00 
[root@localhost /]# mount /dev/vg00/lvsp00 /data
[root@localhost /]# ls /data
group  passwd  shadow
[root@localhost /]#

方式二

可以通过 lvconvert把快照的内容重新写回原有的lvm 先将原逻辑卷卸除挂载#umount /dev/vg00/lv00 执行lvconvert将快照的数据合并到原逻辑卷 #lvconvert --merge /dev/vg00/lvsp00 最后挂载原逻辑卷,查看数据是否恢复成功

[root@localhost /]# umount /dev/vg00/lvsp00 
[root@localhost /]# ls /data
[root@localhost /]# lvconvert --merge /dev/vg00/lvsp00 
  Merging of volume lvsp00 started.
  lv00: Merged: 100.0%
[root@localhost /]# mount /dev/vg00/lv00 /data
[root@localhost /]# ls /data
group  passwd  shadow
[root@localhost /]#

注:当我们把原逻辑卷里面的数据给删除了,逻辑卷快照里面的数据还在,所以可以用快照恢复数据。而当我们在逻辑卷里面添加数据,快照是不会发生改变的,是没有这个文件的。因为快照只会备份当时逻辑卷的一瞬间。


五、使用ssm(系统存储管理器)进行逻辑管理

逻辑卷管理器(LVM)是一种极其灵活的磁盘管理工具,它让用户可以从多个物理硬驱创建逻辑磁盘卷,并调整大小,根本没有停机时间。最新版本的CentOS/RHEL 7现在随带系统存储管理器(又叫ssm),这是一种统一的命令行界面,由红帽公司开发,用于管理各种各样的存储设备。目前,有三种可供ssm使用的卷管理后端:LVM、Btrfs和Crypt

准备ssm,在CentOS/RHEL 7上,你需要首先安装系统存储管理器。可以通过rpm或yum工具安装

[root@localhost Packages]# rpm -ivh system-storage-manager-0.4-5.el7.noarch.rpm 
warning: system-storage-manager-0.4-5.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:system-storage-manager-0.4-5.el7 ################################# [100%]
[root@localhost Packages]#

首先我们来检查关于可用硬盘和LVM卷的信息。下面这个命令将显示关于现有磁盘存储设备、存储池、LVM卷和存储快照的信息。

#ssm  list
[root@localhost Packages]# ssm list
--------------------------------------------------------------
Device         Free       Used      Total  Pool    Mount point
--------------------------------------------------------------
/dev/sda                         80.00 GB          PARTITIONED
/dev/sda1                       500.00 MB          /boot      
/dev/sda2  64.00 MB   79.45 GB   79.51 GB  centos             
/dev/sdb                         20.00 GB                     
/dev/sdb1   4.69 GB  312.00 MB    5.00 GB  vg00               
/dev/sdb3                         5.00 GB          SWAP       
/dev/sdc                         20.00 GB                     
/dev/sdc1  10.00 GB    0.00 KB   10.00 GB  vg00               
/dev/sdc2   5.00 GB    0.00 KB    5.00 GB  vg00               
/dev/sdd                         20.00 GB                     
/dev/sde                         20.00 GB                     
/dev/sde1  10.00 GB    0.00 KB   10.00 GB  vg00               
--------------------------------------------------------------
----------------------------------------------------
Pool    Type  Devices      Free       Used     Total  
----------------------------------------------------
centos  lvm   1        64.00 MB   79.45 GB  79.51 GB  
vg00    lvm   4        29.68 GB  312.00 MB  29.98 GB  
----------------------------------------------------
-------------------------------------------------------------------------------------
Volume            Pool    Volume size  FS     FS size       Free  Type    Mount point
-------------------------------------------------------------------------------------
/dev/centos/root  centos     50.00 GB  xfs   49.98 GB   46.40 GB  linear  /          
/dev/centos/swap  centos      2.00 GB                             linear             
/dev/centos/home  centos     27.45 GB  xfs   27.43 GB   27.40 GB  linear  /home      
/dev/vg00/lv00    vg00      312.00 MB  xfs  308.67 MB  308.38 MB  linear  /data      
/dev/sda1                   500.00 MB  xfs  496.67 MB  339.69 MB  part    /boot      
-------------------------------------------------------------------------------------
[root@localhost Packages]#

在这个例子中,有两个物理设备(“/dev/sda”和“/dev/sdb”)、二个存储池(“rhel和vg00”),以及存储池rhel中创建的两个LVM卷(“dev/rhel/root”和“/dev/rhel/swap”),存储池vg00中创建的一个LVM卷(/dev/vg00/lv00)。 下面来讲解如何通过ssm创建、管理逻辑卷和逻辑卷快照

至少新添加一块磁盘,执行ssm命令显示现有磁盘存储设备、存储池、LVM卷的信息

可以看到有两块空闲磁盘(sdc、sdd) 创建新的LVM池/卷 在这个示例中,不妨看一下如何在物理磁盘驱动器上创建新的存储池和新的LVM卷。如果使用传统的LVM工具,整个过程相当复杂,需要准备分区,需要创建物理卷、卷组、逻辑卷,最后还要建立文件系统。不过,若使用ssm,整个过程一蹴而就! 下面这个命令的作用是,创建一个名为mypool的存储池,创建存储池中名为lv01的500MB大小的LVM卷,使用XFS文件系统格式化卷,并将它挂载到/mnt/test下。

[root@localhost ~]# ssm create -s 500M -n lv01 --fstype xfs -p mypool /dev/sdc /test
File descriptor 7 (/dev/urandom) leaked on lvm invocation. Parent PID 51084: /usr/bin/python
  WARNING: Device for PV 5bFQEC-fww9-q1ZT-Vm4K-ioRI-uC0d-I2rgtK not found or rejected by a filter.
  WARNING: Device for PV hjETI4-pPlT-zhJ0-WTvt-ZRu3-YbwP-pcYBBt not found or rejected by a filter.
  WARNING: Device for PV 5bFQEC-fww9-q1ZT-Vm4K-ioRI-uC0d-I2rgtK not found or rejected by a filter.
  WARNING: Device for PV hjETI4-pPlT-zhJ0-WTvt-ZRu3-YbwP-pcYBBt not found or rejected by a filter.
  WARNING: Device for PV 5bFQEC-fww9-q1ZT-Vm4K-ioRI-uC0d-I2rgtK not found or rejected by a filter.
WARNING: dos signature detected on /dev/sdc at offset 510. Wipe it? [y/n]: y
  Wiping dos signature on /dev/sdc.
  Physical volume "/dev/sdc" successfully created
  Volume group "mypool" successfully created
File descriptor 7 (/dev/urandom) leaked on lvm invocation. Parent PID 51084: /usr/bin/python
WARNING: LVM2_member signature detected on /dev/mypool/lv01 at offset 536. Wipe it? [y/n]: y
  Wiping LVM2_member signature on /dev/mypool/lv01.
  Logical volume "lv01" created.
meta-data=/dev/mypool/lv01       isize=256    agcount=4, agsize=32000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=128000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]#

验证ssm创建的结果

[root@localhost ~]# lvscan
  ACTIVE            '/dev/centos/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/centos/home' [27.45 GiB] inherit
  ACTIVE            '/dev/centos/root' [50.00 GiB] inherit
  ACTIVE            '/dev/mypool/lv01' [500.00 MiB] inherit
  WARNING: Device for PV hjETI4-pPlT-zhJ0-WTvt-ZRu3-YbwP-pcYBBt not found or rejected by a filter.
  WARNING: Device for PV 5bFQEC-fww9-q1ZT-Vm4K-ioRI-uC0d-I2rgtK not found or rejected by a filter.
  ACTIVE            '/dev/vg00/lv00' [312.00 MiB] inherit
[root@localhost ~]#

恢复快照后

[root@localhost ~]# ssm create -s 500M -n lv01 --fstype xfs -p mypool /dev/sdc /test
File descriptor 7 (/dev/urandom) leaked on lvm invocation. Parent PID 49329: /usr/bin/python
  Physical volume "/dev/sdc" successfully created
  Volume group "mypool" successfully created
File descriptor 7 (/dev/urandom) leaked on lvm invocation. Parent PID 49329: /usr/bin/python
  Logical volume "lv01" created.
meta-data=/dev/mypool/lv01       isize=256    agcount=4, agsize=32000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=128000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]#

或执行ssm list

[root@localhost ~]# ssm list
--------------------------------------------------------------
Device         Free       Used      Total  Pool    Mount point
--------------------------------------------------------------
/dev/sda                         80.00 GB          PARTITIONED
/dev/sda1                       500.00 MB          /boot      
/dev/sda2  64.00 MB   79.45 GB   79.51 GB  centos             
/dev/sdb                         20.00 GB                     
/dev/sdb1                         5.00 GB          /data1     
/dev/sdb2                         5.00 GB                     
/dev/sdc   19.51 GB  500.00 MB   20.00 GB  mypool             
/dev/sdd                         20.00 GB                     
/dev/sde                         20.00 GB                     
--------------------------------------------------------------
----------------------------------------------------
Pool    Type  Devices      Free       Used     Total  
----------------------------------------------------
centos  lvm   1        64.00 MB   79.45 GB  79.51 GB  
mypool  lvm   1        19.51 GB  500.00 MB  20.00 GB  
----------------------------------------------------
-------------------------------------------------------------------------------------
Volume            Pool    Volume size  FS     FS size       Free  Type    Mount point
-------------------------------------------------------------------------------------
/dev/centos/root  centos     50.00 GB  xfs   49.98 GB   46.40 GB  linear  /          
/dev/centos/swap  centos      2.00 GB                             linear             
/dev/centos/home  centos     27.45 GB  xfs   27.43 GB   27.40 GB  linear  /home      
/dev/mypool/lv01  mypool    500.00 MB  xfs  496.67 MB  496.54 MB  linear  /test      
/dev/sda1                   500.00 MB  xfs  496.67 MB  339.69 MB  part    /boot      
/dev/sdb1                     5.00 GB  xfs    4.99 GB    4.99 GB          /data1     
-------------------------------------------------------------------------------------
[root@localhost ~]#

将物理磁盘(sdd)添加到LVM池

[root@localhost ~]# ssm add -p mypool /dev/sdd
File descriptor 7 (/dev/urandom) leaked on lvm invocation. Parent PID 49527: /usr/bin/python
  Physical volume "/dev/sdd" successfully created
  Volume group "mypool" successfully extended
[root@localhost ~]#

新设备添加到存储池后,存储池会自动扩大,扩大多少取决于设备的大小。

检查名为centos的存储池的大小执行ssm list查看

-------------------------------------------------------------------------------------
Volume            Pool    Volume size  FS     FS size       Free  Type    Mount point
-------------------------------------------------------------------------------------
/dev/centos/root  centos     50.00 GB  xfs   49.98 GB   46.40 GB  linear  /          
/dev/centos/swap  centos      2.00 GB                             linear             
/dev/centos/home  centos     27.45 GB  xfs   27.43 GB   27.40 GB  linear  /home      
/dev/mypool/lv01  mypool    500.00 MB  xfs  496.67 MB  496.54 MB  linear  /test      
/dev/sda1                   500.00 MB  xfs  496.67 MB  339.69 MB  part    /boot      
/dev/sdb1                     5.00 GB  xfs    4.99 GB    4.99 GB          /data1     
-------------------------------------------------------------------------------------
[root@localhost ~]#

接下来,我们来扩大现有的LVM卷 扩大LVM卷,不妨将/dev/mypool/lv01卷的大小增加300MB。 如果你在存储池中有额外空间,可以扩大存储池中现有的磁盘卷。

为此,使用ssm命令的resize选项

[root@localhost ~]# ssm resize -s +300M /dev/mypool/lv01 
File descriptor 7 (/dev/urandom) leaked on lvm invocation. Parent PID 49681: /usr/bin/python
  Size of logical volume mypool/lv01 changed from 500.00 MiB (125 extents) to 800.00 MiB (200 extents).
  Logical volume lv01 successfully resized.
meta-data=/dev/mapper/mypool-lv01 isize=256    agcount=4, agsize=32000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=128000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 128000 to 204800
[root@localhost ~]#

执行ssm list查看扩大后逻辑卷

-------------------------------------------------------------------------------------
Volume            Pool    Volume size  FS     FS size       Free  Type    Mount point
-------------------------------------------------------------------------------------
/dev/centos/root  centos     50.00 GB  xfs   49.98 GB   46.40 GB  linear  /          
/dev/centos/swap  centos      2.00 GB                             linear             
/dev/centos/home  centos     27.45 GB  xfs   27.43 GB   27.40 GB  linear  /home      
/dev/mypool/lv01  mypool    800.00 MB  xfs  496.67 MB  496.54 MB  linear  /test      
/dev/sda1                   500.00 MB  xfs  496.67 MB  339.69 MB  part    /boot      
/dev/sdb1                     5.00 GB  xfs    4.99 GB    4.99 GB          /data1     
-------------------------------------------------------------------------------------
[root@localhost ~]#

可以看到逻辑卷扩大到800M,即在原来的基础上增加了300M,但文件系统大小(Fs size)还没有改变,仍然是原来的大小。 为了让文件系统识别增加后的卷大小,你需要“扩大”现有的文件系统本身。有不同的工具可用来扩大现有的文件系统,这取决于你使用哪种文件系统。比如说,有面向EXT2/EXT3/EXT4的resize2fs、面向XFS的xfs_growfs以及面向Btrfs的btrfs,不一而足。 在这个例子中,我们使用CentOS 7,XFS文件系统在默认情况下创建。

因而,我们使用xfs_growfs来扩大现有的XFS文件系统。

[root@localhost ~]# xfs_growfs /dev/mypool/lv01 
meta-data=/dev/mapper/mypool-lv01 isize=256    agcount=7, agsize=32000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=204800, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]#

扩大XFS文件系统后,查看结果

-------------------------------------------------------------------------------------
Volume            Pool    Volume size  FS     FS size       Free  Type    Mount point
-------------------------------------------------------------------------------------
/dev/centos/root  centos     50.00 GB  xfs   49.98 GB   46.40 GB  linear  /          
/dev/centos/swap  centos      2.00 GB                             linear             
/dev/centos/home  centos     27.45 GB  xfs   27.43 GB   27.40 GB  linear  /home      
/dev/mypool/lv01  mypool    800.00 MB  xfs  796.67 MB  771.48 MB  linear  /test      
/dev/sda1                   500.00 MB  xfs  496.67 MB  339.69 MB  part    /boot      
/dev/sdb1                     5.00 GB  xfs    4.99 GB    4.99 GB          /data1     
-------------------------------------------------------------------------------------

或执行#df -hT

[root@localhost ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        50G  3.6G   47G   8% /
devtmpfs                devtmpfs  474M     0  474M   0% /dev
tmpfs                   tmpfs     489M  144K  489M   1% /dev/shm
tmpfs                   tmpfs     489M  7.0M  482M   2% /run
tmpfs                   tmpfs     489M     0  489M   0% /sys/fs/cgroup
/dev/mapper/centos-home xfs        28G   36M   28G   1% /home
/dev/sda1               xfs       497M  158M  340M  32% /boot
/dev/sr0                iso9660   7.3G  7.3G     0 100% /mnt
tmpfs                   tmpfs     489M   20K  489M   1% /tmp
tmpfs                   tmpfs      98M   24K   98M   1% /run/user/0
/dev/sdb1               xfs       5.0G   33M  5.0G   1% /data1
/dev/mapper/mypool-lv01 xfs       797M   26M  772M   4% /test
[root@localhost ~]#

可以看到LVM扩展成功 逻辑卷快照 对现有的LVM卷(比如/dev/mypool/lv01)生成快照

一旦快照生成完毕,它将作为一个特殊的快照卷存储起来,存储了原始卷中生成快照时的所有数据

[root@localhost ~]# cp /etc/{passwd,shadow,group} /test
[root@localhost ~]# ls /test
group  passwd  shadow
[root@localhost ~]# ssm snapshot /dev/mypool/lv01 
File descriptor 7 (/dev/urandom) leaked on lvm invocation. Parent PID 49992: /usr/bin/python
  Logical volume "snap20160715T230814" created.
[root@localhost ~]# ssm list snapshots
-----------------------------------------------------------------------------
Snapshot                         Origin  Pool    Volume size     Size  Type  
-----------------------------------------------------------------------------
/dev/mypool/snap20160715T230814  lv01    mypool    160.00 MB  0.00 KB  linear
-----------------------------------------------------------------------------
[root@localhost ~]#

每次原LVM中的数据更改,都可以手动执行ssm snapshot生成快照 当原LVM数据损坏就可以用快照恢复了 方式一是先将原逻辑卷卸除挂载 #umount /dev/vg00/lv00

然后挂载逻辑卷快照即可 #mount /dev/vg00/lvsp00 /data,就可以正常访问数据了

[root@localhost ~]# rm -rf /test/*
[root@localhost ~]# ls /test
[root@localhost ~]# umount /dev/mypool/lv01 
[root@localhost ~]# mount /dev/mypool/snap20160715T230814 /test/
[root@localhost ~]# ls /test
group  passwd  shadow
[root@localhost ~]#

方式二可以通过 lvconvert把快照的内容重新写回原有的lvm

先将原逻辑卷卸除挂载#umount /dev/vg00/lv00 执行lvconvert将快照的数据合并到原逻辑卷 #lvconvert --merge /dev/vg00/lvsp00 最后挂载原逻辑卷,查看数据是否恢复成功

[root@localhost ~]# umount /dev/mypool/snap20160715T230814 
[root@localhost ~]# ls /test
[root@localhost ~]# lvconvert --merge /dev/mypool/snap20160715T230814 
  Merging of volume snap20160715T230814 started.
  lv01: Merged: 100.0%
  lv01: Merged: 100.0%
[root@localhost ~]# mount /dev/mypool/lv01 /test/
[root@localhost ~]# ls /test/
group  passwd  shadow
[root@localhost ~]#

有磁ssm的具体用法可以参考ssm的帮助手删页 如:删除LVM卷#ssm remove 删除存储池#ssm remove


results matching ""

    No results matching ""