libux创建逻辑卷及扩展

如果你磁盘分区是用LVM的话可以实现动态分配,直接用lvextend;
如果用传统分区的话,可以在分区上面再fdisk出需要的容量,然后挂载mount到主磁盘就行了。

此处讲的是lvm的lvextend
一、创建逻辑卷

[linux@localhost Desktop]$ su                                     //切换root
Password: 
[root@localhost Desktop]# fdisk -l                                //显示详解

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x00053377

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2358    18631680   83  Linux
/dev/sda3            2358        2611     2031616   82  Linux swap / Solaris

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x00000000




#创建分区

[root@localhost Desktop]# fdisk /dev/sdb                                  //对sdb磁盘进行操作

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): m                                                    //m查看文档
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n                                                   //n新建
Command action
   e   extended                             //扩展区
   p   primary partition (1-4)              //主分区
p                          
Partition number (1-4): 1                                                   //编号1
First cylinder (1-2610, default 1):                             //直接回车  什么都不甜  避免空间浪费
Using default value 1           
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +3G        //多大容量

Command (m for help): p                                                      //查看情况

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x609a0236

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         393     3156741   83  Linux

Command (m for help): w                                                       //保存  否则以上操作不管用
The partition table has been altered!

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



#查看

[root@localhost Desktop]# df -h                                          //磁盘空间占用情况
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        18G  4.0G   13G  24% /
tmpfs           491M  372K  491M   1% /dev/shm
/dev/sda1       291M   34M  242M  13% /boot

[root@localhost Desktop]# fdisk -l                                       //显示详解

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x00053377

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2358    18631680   83  Linux
/dev/sda3            2358        2611     2031616   82  Linux swap / Solaris

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x5b4abd65

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         393     3156741   83  Linux


#物理卷

[root@localhost Desktop]# pvcreate /dev/sdb1                                //创建物理卷
  Physical volume "/dev/sdb1" successfully created
[root@localhost Desktop]# pvscan                                            //扫描物理卷
  PV /dev/sdb1                      lvm2 [3.01 GiB]
  Total: 1 [3.01 GiB] / in use: 0 [0   ] / in no VG: 1 [3.01 GiB]
[root@localhost Desktop]# pvdisplay                                         //显示物理卷
  "/dev/sdb1" is a new physical volume of "3.01 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               3.01 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               J4aKkI-9cja-HVDC-NYlF-exvn-W80z-3qOpJ6


#卷组
[root@localhost Desktop]# vgscan                                           //扫描卷组
  Reading all physical volumes.  This may take a while...
  No volume groups found
[root@localhost Desktop]# vgdisplay                                        //显示卷组
  No volume groups found
[root@localhost Desktop]# vgcreate vg_data1 /dev/sdb1                      //创建卷组
  Volume group "vg_data1" successfully created
[root@localhost Desktop]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "vg_data1" using metadata type lvm2
[root@localhost Desktop]# vgdisplay
  --- Volume group ---
  VG Name               vg_data1
  System ID             
  Format                lvm2
  Metadata Areas        1
  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                1
  Act PV                1
  VG Size               3.01 GiB
  PE Size               4.00 MiB
  Total PE              770
  Alloc PE / Size       0 / 0   
  Free  PE / Size       770 / 3.01 GiB
  VG UUID               dxDgwz-jipx-UNZT-XZg7-cLpD-xaOv-wqh4yJ



 #逻辑卷   
 [root@localhost Desktop]# lvscan                                          //扫描逻辑卷
[root@localhost Desktop]# lvdisplay                                        //显示逻辑卷
[root@localhost Desktop]# lvcreate -L 3G -nlv_box vg_data1                  //创建指定大小和名称的逻辑卷
  Logical volume "lv_box" created
[root@localhost Desktop]# lvscan
  ACTIVE            '/dev/vg_data1/lv_box' [3.00 GiB] inherit
[root@localhost Desktop]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg_data1/lv_box
  LV Name                lv_box
  VG Name                vg_data1
  LV UUID                jzTiCF-RgnF-qHLo-2yLu-eAS5-L19n-Dtx4Pv
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2017-12-12 07:25:36 -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



  #格式化逻辑卷
  [root@localhost Desktop]# mkfs.ext3 /dev/vg_data1/lv_box
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
196608 inodes, 786432 blocks
39321 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=805306368
24 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 29 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.




#uuid和挂载
[root@localhost Desktop]# mkdir /boxs                                       //创建文件夹
[root@localhost Desktop]# blkid /dev/vg_data1/lv_box                        //blkid查看分区的UUID
/dev/vg_data1/lv_box: UUID="4a583261-6b27-4cf1-a152-91a9e9a6961f" SEC_TYPE="ext2" TYPE="ext3" 
[root@localhost Desktop]# mount /dev/vg_data1/lv_box /boxs                 //
[root@localhost Desktop]# df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/sda2                     18G  4.0G   13G  24% /
tmpfs                        491M  372K  491M   1% /dev/shm
/dev/sda1                    291M   34M  242M  13% /boot
/dev/mapper/vg_data1-lv_box  3.0G   69M  2.8G   3% /boxs



#添加UUID="4a583261-6b27-4cf1-a152-91a9e9a6961f" /boxs ext3 defaults 0 0 

[root@localhost Desktop]# vim /etc/fstab                                     //将逻辑卷设置为开机自动挂载
[root@localhost Desktop]# df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/sda2                     18G  4.0G   13G  24% /
tmpfs                        491M  372K  491M   1% /dev/shm
/dev/sda1                    291M   34M  242M  13% /boot
/dev/mapper/vg_data1-lv_box  3.0G   69M  2.8G   3% /boxs



[root@localhost Desktop]# ls -l /dev/vg_data1/lv_box
lrwxrwxrwx. 1 root root 7 Dec 12 07:37 /dev/vg_data1/lv_box -> ../dm-0

# /dev/mapper/vg_data1-lv_box是/dev/vg_data1/lv_box的链接

二、扩展逻辑卷

#新创建一个分区
[root@localhost Desktop]# fdisk /dev/sdb

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): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 4
First cylinder (394-2610, default 394): 
Using default value 394
Last cylinder, +cylinders or +size{K,M,G} (394-2610, default 2610): +4G

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x5b4abd65

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         393     3156741   83  Linux
/dev/sdb4             394         916     4200997+  83  Linux

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 Desktop]# pvcreate /dev/sdb4
  dev_is_mpath: failed to get device for 8:20
  Physical volume "/dev/sdb4" successfully created
[root@localhost Desktop]# pvscan
  PV /dev/sdb1   VG vg_data1        lvm2 [3.01 GiB / 8.00 MiB free]
  PV /dev/sdb4                      lvm2 [4.01 GiB]
  Total: 2 [7.01 GiB] / in use: 1 [3.01 GiB] / in no VG: 1 [4.01 GiB]
[root@localhost Desktop]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "vg_data1" using metadata type lvm2


#扩展卷组
[root@localhost Desktop]# vgextend vg_data1 /dev/sdb4
  Volume group "vg_data1" successfully extended
[root@localhost Desktop]# vgdisplay
  --- Volume group ---
  VG Name               vg_data1
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               7.01 GiB
  PE Size               4.00 MiB
  Total PE              1795
  Alloc PE / Size       768 / 3.00 GiB
  Free  PE / Size       1027 / 4.01 GiB
  VG UUID               dxDgwz-jipx-UNZT-XZg7-cLpD-xaOv-wqh4yJ


#扩展逻辑卷      
[root@localhost Desktop]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg_data1/lv_box
  LV Name                lv_box
  VG Name                vg_data1
  LV UUID                jzTiCF-RgnF-qHLo-2yLu-eAS5-L19n-Dtx4Pv
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2017-12-12 07:25:36 -0800
  LV Status              available
  # open                 1
  LV Size                3.00 GiB
  Current LE             768
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

[root@localhost Desktop]# lvextend -L +2G /dev/vg_data1/lv_box
  Extending logical volume lv_box to 5.00 GiB
  Logical volume lv_box successfully resized

接着用 dh –f,发现实际容量并没有变化,

[root@localhost Desktop]# df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/sda2                     18G  4.0G   13G  24% /
tmpfs                        491M   80K  491M   1% /dev/shm
/dev/sda1                    291M   34M  242M  13% /boot
/dev/mapper/vg_data1-lv_box  3.0G   69M  2.8G   3% /boxs

因为我们的系统还不认识刚刚添加进来的磁盘的文件系统,所以还需要对文件系统进行扩容。

[root@localhost Desktop]# resize2fs /dev/vg_data1/lv_box             //让系统重新识别逻辑卷的大小
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_data1/lv_box is mounted on /boxs; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg_data1/lv_box to 1310720 (4k) blocks.
The filesystem on /dev/vg_data1/lv_box is now 1310720 blocks long.

[root@localhost Desktop]# df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/sda2                     18G  4.0G   13G  24% /
tmpfs                        491M   80K  491M   1% /dev/shm
/dev/sda1                    291M   34M  242M  13% /boot
/dev/mapper/vg_data1-lv_box  5.0G   71M  4.7G   2% /boxs

扩展逻辑卷的格式:
lvextend -L +需要增加的大小 /dev/卷组名/逻辑卷名
lvextend -L 扩展后总的大小 /dev/卷组名/逻辑卷名
lvextend -l PE的数量 /dev/卷组名/逻辑卷名

过程中遇到的问题:
1、逻辑卷已经有了之后,再创建物理卷时,会出现以下情况

pvcreate /dev/sdb2
Device /dev/sdb2 not found (or ignored by filtering).

此时,可以通过“partprobe;”命令或者重启虚拟机
试过几次partprobe后不管用就直接重启了,再创建一次就好了

2、用命令“ fdisk -l |grep lvm”查看,没有lvm的分区

将分区类型改为8e—-LVM

[root@localhost Desktop]# fdisk /dev/sdb

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): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): 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

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

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): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x5b4abd65

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         393     3156741   8e  Linux LVM
/dev/sdb4             394         916     4200997+  83  Linux

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.

查看lvm

[root@localhost Desktop]# fdisk -l 

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x00053377

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2358    18631680   83  Linux
/dev/sda3            2358        2611     2031616   82  Linux swap / Solaris

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x5b4abd65

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         393     3156741   8e  Linux LVM
/dev/sdb4             394         916     4200997+  83  Linux

Disk /dev/mapper/vg_data1-lv_box: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x00000000

[root@localhost Desktop]# fdisk -l |grep LVM   //查看磁盘占用情况并且是lvm的
/dev/sdb1               1         393     3156741   8e  Linux LVM

参考:http://blog.51cto.com/9265519/1566962
和http://blog.csdn.net/valage/article/details/73332147

1、vgs查看下服务器上是否还存在多余的卷组资源
2、lvextend -l +100%FREE /dev/mapper/VolGroup-lv_root # 此操作是把卷组中空闲的资源都分给root
3、 resize2fs /dev/mapper/VolGroup-lv_root
4、xfs_growfs /dev/mapper/VolGroup-lv_root

你可能感兴趣的:(linux)