Creating logical volume in Linux

Time:2021-3-5

image

Create partition

[[email protected] lohr]# fdisk /dev/sdb
Welcome to fdisk (util Linux 2.23.2).

The changes stay in memory until you decide to write them to disk.
Think twice before using the write command.

Device does not contain a recognized partition table
Use the disk identifier 0xa7a34d5d to create a new DOS disk label.

Command (enter m for help): M
Command operation
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   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
   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 (enter 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)
Starting sector (2048-10485759, default is 2048)
The default value 2048 will be used
Last sector, + sector or + size {K, m, G} (2048-10485759, default is 10485759): + 5g
Value out of range.
Last sector, + sector or + size {K, m, G} (2048-10485759, default is 10485759): + 4G
Partition 1 is set to Linux type with a size of 4 gib

Command (enter m for help): t
Partition 1 Selected
Hex code (enter l to list all codes): l

 0 empty 24 NEC DOS 81 MINIX / old Linu BF Solaris        
 1 FAT12 27 hidden NTFS win 82 Linux exchange / 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)-
 FAT16 < 32m 40 venix 80286 85 Linux extension C7 syrinx         
 5 extend 41 PPC prep boot 86 NTFS volume set Da non file system data 
 6 FAT16 42 SFS 87 NTFS volume set DB CP / M / CTOs /
 7 HPFS / NTFS / exFAT 4D qnx4. X 88 Linux plain text de Dell tool      
 8 AIX 4E qnx4. X Part 2 8e Linux LVM DF bootit         
 9 AIX starts 4f qnx4. X Part 3 93 amoeba E1 DOS access       
 A OS / 2 boot manager 50 Ontrack DM 94 amoeba BBT E3 DOS R / O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 C w95 FAT32 (LBA) 52 CP / Ma0 IBM ThinkPad beos FS        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 F w95 extension (LBA) 54 ontrackdm6 A6 OpenBSD EF EFI (fat-12 / 16)/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC  
11 hidden FAT12 56 golden bow A8 Darwin UFS F1 speedstor      
12 Compaq diagnosis 5C Priam edit A9 NetBSD F4 speedstor      
14 hidden FAT16 < 3 61 speedstor AB Darwin starts DOS secondary       
16 hidden FAT16 63 GNU Hurd or sys AF HFS / HFS + FB VMware vmfs    
17 hidden HPFS / NTF 64 Novell Netware B7 bsdi FS FC VMware vmkcore 
18 ast intelligent sleep 65 Novell Netware B8 bsdi swap FD Linux raid automatic
1B hidden w95 fat3 70 disksecure multi boot BB wizard hidden lanstep        
1C hidden w95 fat3 75 PC / IX be Solaris boot FF BBT            
1E hidden w95 FAT1 80 old MINIX       
Hex code (input L to list all codes): 8e
Changed the type of partition 'Linux' to 'Linux LVM'

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

Calling ioctl() to re-read partition table.
Synchronizing disk.

Register partition with kernel

[[email protected] lohr]# partprobe /dev/sdb

Create physical volume

[[email protected] lohr]# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.

Create volume group

[[email protected] lohr]# vgcreate thomas /dev/sdb
  Volume group "thomas" successfully created

Creating logical volumes

[[email protected] lohr]# lvcreate -n thomas-lv -L 1G thomas 
  Logical volume "thomas-lv" created.

Creating a file system

[[email protected] lohr]# mkfs.ext4 /dev/thomas/thomas-lv 
mke2fs 1.42.9 (28-Dec-2013)
File system label=
OS type: Linux
Block size = 4096 (log = 2)
分Block size = 4096 (log = 2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block = 0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode table: done                            
Creating Journal (8192 blocks): complete
Writing superblocks and filesystem accounting information: complete

mount

[[email protected] lohr]# vi /etc/fstab 


#
# /etc/fstab
# Created by anaconda on Sun Jun  7 11:20:49 2020
#
# 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
#
.host:/VMShare /mnt/hgfs fuse.vmhgfs-fuse allow_other,defaults 0 0
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=4cd669bd-4db1-49d9-afb6-245b36db7470 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/Dev / Thomas / LV Thomas / MNT / LV Thomas ext4 defaults 0 0
mount -a

Mount successfully

image

View physical volumes

[[email protected] lohr]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               thomas
  PV Size               5.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              1279
  Free PE               1023
  Allocated PE          256
  PV UUID               pVLZ0a-1DmV-13SP-8Keq-GaW0-5gdP-PmosB2

View volume group information

[[email protected] lohr]# vgdisplay 
  --- Volume group ---
  VG Name               thomas
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <5.00 GiB
  PE Size               4.00 MiB
  Total PE              1279
  Alloc PE / Size       256 / 1.00 GiB
  Free  PE / Size       1023 / <4.00 GiB
  VG UUID               gSH0Zs-rC1U-D8PY-WaE1-nzGI-t3Jh-Xj8VXo

View logical volumes

[[email protected] lohr]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/thomas/thomas-lv
  LV Name                thomas-lv
  VG Name                thomas
  LV UUID                4enjau-4c83-tFX1-xvw5-vrMv-fsv3-S3xhNd
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-08-09 10:06:52 -0400
  LV Status              available
  # open                 1
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2