前言
一種技術要知其然,還要知其所以然
lvm簡介
LVM是 Logical Volume Manager(邏輯卷管理)的簡寫,它是Linux環境下對磁盤分區進行管理的一種機制。普通的磁盤分區管理方式在邏輯分區劃分好之后就無法改變其大小,當一個邏輯分區存放不下某個文件時,這個文件因為受上層文件系統的限制,也不能跨越多個分區來存放,所以也不能同時放到別的磁盤上。而遇到出現某個分區空間耗盡時,解決的方法通常是使用符號鏈接,或者使用調整分區大小的工具,但這只是暫時解決辦法,沒有從根本上解決問題。隨著Linux的邏輯卷管理功能的出現,這些問題都迎刃而解,用戶在無需停機的情況下可以方便地調整各個分區大小。
lvm的基本術語
*物理存儲介質(PhysicalStorageMedia) 指系統的物理存儲設備:磁盤,如:/dev/hda、/dev/sda等,是存儲系統最底層的存儲單元。
*物理卷(Physical Volume,PV) 指磁盤分區或從邏輯上與磁盤分區具有同樣功能的設備(如RAID),是LVM的基本存儲邏輯塊,但和基本的物理存儲介質(如分區、磁盤等)比較,卻包含有與LVM相關的管理參數。
*卷組(Volume Group,VG) 類似于非LVM系統中的物理磁盤,其由一個或多個物理卷PV組成。可以在卷組上創建一個或多個LV(邏輯卷)。
*邏輯卷(Logical Volume,LV) 類似于非LVM系統中的磁盤分區,邏輯卷建立在卷組VG之上。在邏輯卷LV之上可以建立文件系統(比如/home或者/usr等)。
*物理塊(Physical Extent,PE) 每一個物理卷PV被劃分為稱為PE(Physical Extents)的基本單元,具有唯一編號的PE是可以被LVM尋址的最小單元。PE的大小是可配置的,默認為4MB。所以物理卷(PV)由大小等同的基本單元PE組成。
*邏輯塊(Logical Extent,LE) 邏輯卷LV也被劃分為可被尋址的基本單位,稱為LE。在同一個卷組中,LE的大小和PE是相同的,并且一一對應。
LVM抽象模型
圖所示LVM抽象模型,展示了PV、VG、LV三者之間關系:
和非LVM系統將包含分區信息的元數據保存在位于分區的起始位置的分區表中一樣,邏輯卷以及卷組相關的元數據也是保存在位于物理卷起始處的VGDA(卷組描述符區域)中。
VGDA包括以下內容:PV描述符、VG描述符、LV描述符、和一些PE描述符。
系統啟動LVM時激活VG,并將VGDA加載至內存,來識別LV的實際物理存儲位置。當系統進行I/O操作時,就會根據VGDA建立的映射機制來訪問實際的物理位置。
lvm的基本使用方法
pv管理工具: pvs:簡要pv信息顯示 pvdisplay:顯示pv的詳細信息 pvcreate /dev/DEVICE: 創建pv 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 擴展邏輯卷: # lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME # resize2fs /dev/VG_NAME/LV_NAME 縮減邏輯卷: # umount /dev/VG_NAME/LV_NAME # e2fsck -f /dev/VG_NAME/LV_NAME # resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT] # lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME # mount 快照:snapshot lvcreate -L #[mMgGtT] -p r -s -n snapshot_lv_name original_lv_name
實驗環境及步驟
采用centos6.7的環境。 lvm的工具包安裝 yum install -y lvm
1:創建一個至少有兩個PV組成的大小為20G的名為testvg的VG;要求PE大小為16MB, 而后在卷組中創建大小為5G的邏輯卷testlv;掛載至/users目錄;
先創建2個分區大小都為10g的pv, [root@king-liu tmp]# 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 (6602-10443, default 6602): Using default value 6602 Last cylinder, +cylinders or +size{K,M,G} (6602-10443, default 10443): +10G #增加大小為10G Command (m for help): n First cylinder (7908-10443, default 7908): Using default value 7908 Last cylinder, +cylinders or +size{K,M,G} (7908-10443, default 10443): +10G Command (m for help): T #分區類型要為8e linux LVM Partition number (1-12): 11 Hex code (type L to list codes): 8e Changed system type of partition 11 to 8e (Linux LVM) Command (m for help): t Partition number (1-12): 12 Hex code (type L to list codes): 8e Changed system type of partition 12 to 8e (Linux LVM) Command (m for help): p 查看分區 Disk /dev/sda: 85.9 GB, 85899345920 bytes 255 heads, 63 sectors/track, 10443 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: 0x0009fc3c Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 64 2614 20480000 83 Linux /dev/sda3 2614 2678 512000 82 Linux swap / Solaris /dev/sda4 2678 10443 62378373+ 5 Extended /dev/sda5 4640 5293 5253223+ fd Linux raid autodetect /dev/sda6 5294 5947 5253223+ fd Linux raid autodetect /dev/sda7 5948 6601 5253223+ 8e Linux LVM /dev/sda8 2678 3983 10488392 8e Linux LVM /dev/sda9 3984 4637 5253223+ 8e Linux LVM /dev/sda10 4638 4639 16033+ 83 Linux /dev/sda11 6602 7907 10490413+ 8e Linux LVM /dev/sda12 7908 9213 10490413+ 8e Linux LVM Partition table entries are not in disk order 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: 設備或資源忙. 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@king-liu tmp]# partx -a /dev/sda #內核重新讀取一下硬盤 [root@king-liu tmp]# partx -a /dev/sda #內核重新讀取一下硬盤 [root@king-liu tmp]# pvcreate /dev/sda{11,12} #將分區創建為物理卷 pv Physical volume "/dev/sda11" successfully created Physical volume "/dev/sda12" successfully created [root@king-liu tmp]# pvs #查看創建的物理卷 pvdisplay查看詳細的物理卷 PV VG Fmt Attr PSize PFree /dev/sda11 lvm2 --- 10.00g 10.00g /dev/sda12 lvm2 --- 10.00g 10.00g [root@king-liu tmp]# vgcreate -s 16M testvg /dev/sda{11,12} #將物理卷創建為卷組vg pe大小為16M 卷組名為testvg Volume group "testvg" successfully created [root@king-liu tmp]# vgdisplay 查看詳細的卷組 --- Volume group --- VG Name testvg 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 20.00 GiB PE Size 16.00 MiB Total PE 1280 Alloc PE / Size 0 / 0 Free PE / Size 1280 / 20.00 GiB VG UUID SgtewP-UMwa-RLY4-a8oq-XrF0-dzx7-F2Reyd [root@king-liu tmp]# lvcreate -L 5G -n testlv testvg #將卷組testvg創建為邏輯卷lv 大小為5G 名字為testlv Logical volume "testlv" created. [root@king-liu tmp]# mke2fs -j /dev/testvg/testlv #將testlv格式化 mke2fs 1.41.12 (17-May-2010) 文件系統標簽= 操作系統:Linux 塊大小=4096 (log=2) 分塊大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 327680 inodes, 1310720 blocks 65536 blocks (5.00%) reserved for the super user 第一個數據塊=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 正在寫入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 27 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@king-liu ~]# mkdir /users #創建users文件夾 [root@king-liu ~]# mount /dev/testvg/testlv /users/ #將/dev/testvg/testlv掛載到 /users/ [root@king-liu ~]# mount #查看是否已掛載 /dev/sda2 on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0") /dev/sda1 on /boot type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) /dev/mapper/testvg-testlv on /users type ext3 (rw) [root@king-liu ~]# ls /users/ #查看 lost+found [root@king-liu ~]# df -hT #查看硬盤 Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 ext4 20G 964M 18G 6% / tmpfs tmpfs 306M 0 306M 0% /dev/shm /dev/sda1 ext4 477M 26M 426M 6% /boot /dev/mapper/testvg-testlv ext3 5.0G 139M 4.6G 3% /users
2: 新建用戶archlinux,要求其家目錄為/users/archlinux,而后su切換至archlinux用戶,復制/etc/pam.d目錄至自己的家目錄;
[root@king-liu ~]# useradd -d /users/archlinux archlinux #創建用戶archlinux 指定家目錄為/users/archlinux [root@king-liu ~]# echo "kingliu" | passwd --stdin archlinux #將kingliu作為archlinux的密碼 更改用戶 archlinux 的密碼 。 passwd: 所有的身份驗證令牌已經成功更新。 [root@king-liu ~]# su archlinux # 切換到archlinux [archlinux@king-liu ~]$ cp -r /etc/pam.d /users/archlinux/ # 復制/etc/pam.d 到 archlinux用戶的家目錄中
3:擴展testlv至7G,要求archlinux用戶的文件不能丟失;
[archlinux@king-liu ~]$ su root #切換到root管理員 密碼: [root@king-liu archlinux]# pvs #查看物理卷是否能擴展 PV VG Fmt Attr PSize PFree /dev/sda11 testvg lvm2 a-- 10.00g 5.00g /dev/sda12 testvg lvm2 a-- 10.00g 10.00g [root@king-liu archlinux]# vgs #查看卷組是否能擴展 VG #PV #LV #SN Attr VSize VFree testvg 2 1 0 wz--n- 20.00g 15.00g [root@king-liu archlinux]# lvextend -L +2G /dev/testvg/testlv #之前已經添加了5g為lv,現在只需再加上2G Size of logical volume testvg/testlv changed from 5.00 GiB (320 extents) to 7.00 GiB (448 extents). Logical volume testlv successfully resized [root@king-liu archlinux]# df -h #發現硬盤并沒有增大 Filesystem Size Used Avail Use% Mounted on /dev/sda2 20G 964M 18G 6% / tmpfs 306M 0 306M 0% /dev/shm /dev/sda1 477M 26M 426M 6% /boot /dev/mapper/testvg-testlv 5.0G 139M 4.6G 3% /users [root@king-liu pam.d]# resize2fs -p /dev/testvg/testlv #通過resize2fs工具來把文件系統確實添加 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/testvg/testlv is mounted on /users; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/testvg/testlv to 1835008 (4k) blocks. The filesystem on /dev/testvg/testlv is now 1835008 blocks long. [root@king-liu pam.d]# df -h #查看硬盤 Filesystem Size Used Avail Use% Mounted on /dev/sda2 20G 964M 18G 6% / tmpfs 306M 0 306M 0% /dev/shm /dev/sda1 477M 26M 426M 6% /boot /dev/mapper/testvg-testlv 6.9G 140M 6.5G 3% /users #加了2G 不一定會真正達到7G的%10以內是可以接受的 [root@king-liu pam.d]# cd /users/archlinux/pam.d/ #查看數據是否丟失 [root@king-liu pam.d]# ls chfn login polkit-1 smartcard-auth-ac sudo chsh newrole remote smtp sudo-i config-util other run_init smtp.postfix su-l crond passwd runuser sshd system-auth fingerprint-auth password-auth runuser-l ssh-keycat system-auth-ac fingerprint-auth-ac password-auth-ac smartcard-auth su
4:收縮testlv至3G,要求archlinux用戶的文件不能丟失;
[root@king-liu /]# umount /dev/testvg/testlv #卸載分區 [root@king-liu /]# resize2fs /dev/testvg/testlv 3G #縮減邏輯大小 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/testvg/testlv is mounted on /users; on-line resizing required On-line shrinking from 1835008 to 786432 not supported. [root@king-liu /]# lvreduce -L 3G /dev/testvg/testlv #縮減物理邊界大小 WARNING: Reducing active and open logical volume to 3.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce testlv? [y/n]: y Size of logical volume testvg/testlv changed from 7.00 GiB (448 extents) to 3.00 GiB (192 extents). Logical volume testlv successfully resized [root@king-liu /]# lvs #查看邏輯卷 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert testlv testvg -wi-ao---- 3.00g [root@king-liu /]# mount /dev/testvg/testlv /users/archlinux/ #掛載 [root@king-liu archlinux]#cd /users/archlinux [root@king-liu archlinux]# ls pam.d [root@king-liu archlinux]# cd pam.d/ [root@king-liu pam.d]# ls chfn login polkit-1 smartcard-auth-ac sudo chsh newrole remote smtp sudo-i config-util other run_init smtp.postfix su-l crond passwd runuser sshd system-auth fingerprint-auth password-auth runuser-l ssh-keycat system-auth-ac fingerprint-auth-ac password-auth-ac smartcard-auth su
5:對testlv創建快照,并嘗試基于快照備份數據,驗正快照的功能;
[root@king-liu pam.d]# lvcreate -L 30M -n backup -s -p r /dev/testvg/testlv #創建快照 -L 指定快照大小 -n 指定名字 -s 創建快照 -p 只讀 Rounding up size to full physical extent 32.00 MiB Logical volume "backup" created. mount: block device /dev/mapper/testvg-backup is write-protected, mounting read-only # 可以通過快照來訪問數據 [root@king-liu tmp]# ls /tmp/backup/ archlinux lost+found [root@king-liu tmp]# ls /tmp/backup/archlinux/ pam.d
總結
文章那里出錯了 請留言QQ1505222150 我會及時改正的。或者還需要增加什么也請留言。 堅信開源才能會越來越好。 請大家多多評論
原創文章,作者:N19_king,如若轉載,請注明出處:http://www.www58058.com/16476