邏輯卷管理器(LVM) 允許對卷進行方便操作的抽象層,包括重新設定文件系統的大小 允許在多個物理設備間重新組織文件系統 將設備指定為物理卷 用一個或者多個物理卷來創建一個卷組 物理卷是用固定大小的物理區域(Physical Extent,PE)來定義的 在物理卷上創建的邏輯卷是由物理區域(PE)組成 可以在邏輯卷上創建文件系統 LVM LVM: Logical Volume Manager, Version: 2 dm: device mapper:將一個或多個底層塊設備組織成一個邏輯設備的模塊 設備名:/dev/dm-# 軟鏈接: /dev/mapper/VG_NAME-LV_NAME /dev/mapper/vol0-root /dev/VG_NAME/LV_NAME /dev/vol0/root LVM更改文件系統的容量 LVM可以彈性的更改LVM的容量通過交換PE來進行資料的轉換,將原來LV內的PE轉移到其他的設備中以降低 LV的容量,或將其他設備中的PE加到LV中以加大容量 pv管理工具 顯示pv信息 pvs:簡要pv信息顯示 pvdisplay 創建pv pvcreate /dev/DEVICE vg管理工具 顯示卷組 vgs vgdisplay 創建卷組 vgcreate [-s #[kKmMgGtTpPeE]] VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...] 管理卷組 vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...] vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...] 刪除卷組 先做pvmove,再做vgremove lv管理工具 顯示邏輯卷 lvs Lvdisplay 創建邏輯卷 lvcreate -L #[mMgGtT] -n NAME VolumeGroup lvcreate -l 60%VG -n mylv testvg lvcreate -l 100%FREE -n yourlv testvg 刪除邏輯卷 lvremove /dev/VG_NAME/LV_NAME 重設文件系統大小 fsadm [options] resize device [new_size[BKMGTEP]] resize2fs [-f] [-F] [-M] [-P] [-p] device [new_size] 擴展和縮減邏輯卷 擴展邏輯卷: lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME resize2fs /dev/VG_NAME/LV_NAME lvresize -r -l +100%FREE /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 跨主機遷移卷組 源計算機上 1 在舊系統中,umount所有卷組上的邏輯卷 2 vgchange –a n vg0 lvdisplay 3 vgexport vg0 pvscan vgdisplay 拆下舊硬盤 在目標計算機上 4 在新系統中安裝舊硬盤,并vgimport vg0. 5 vgchange –ay vg0 6 mount所有卷組上的邏輯卷 創建邏輯卷示例 創建物理卷 pvcreate /dev/sda3 為卷組分配物理卷 vgcreate vg0 /dev/sda3 從卷組創建邏輯卷 lvcreate -L 256M -n data vg0 mke2fs -j /dev/vg0/data mount /dev/vg0/data /mnt/data 邏輯卷管理器快照 快照是特殊的邏輯卷,它是在生成快照時存在的邏輯卷的準確拷貝 對于需要備份或者復制的現有數據臨時拷貝以及其它操作來說,快照是最合適的選擇快照只有在它們和原 來的邏輯卷不同時才會消耗空間在生成快照時會分配給它一定的空間,但只有在原來的邏輯卷或者快照有所 改變才會使用這些空間,當原來的邏輯卷中有所改變時,會將舊的數據復制到快照中。 快照中只含有原來的邏輯卷中更改的數據或者自生成快照后的快照中更改的數據 建立快照的卷大小只需要原始邏輯卷的15%~20%就夠了,也可以使用lvextend放大快照 邏輯卷管理器快照 快照就是將當時的系統信息記錄下來,就好像照相一般,若將來有任何數據改動了,則原始數據會被移動 到快照區,沒有改動的區域則由快照區和文件系統共享由于快照區與原本的LV共用很多PE的區塊,因此快照 與被快照的LV必須在同一個VG中.系統恢復的時候的文件數量不能高于快照區的實際容量 使用LVM快照 為現有邏輯卷創建快照 lvcreate -l 64 -s -n data-snapshot -p r /dev/vg0/data 掛載快照 mkdir -p /mnt/snap mount -o ro /dev/vg0/data-snapshot /mnt/snap 恢復快照 umount /dev/vg0/data-snapshot umount /dev/vg0/data lvconvert --merge /dev/vg0/data-snapshot 刪除快照 umount /mnt/databackup lvremove /dev/vg0/databackup 練習 1、創建一個至少有兩個PV組成的大小為20G的名為testvg的VG;要求PE大小為16MB, 而后在卷組中創建 大小為5G的邏輯卷testlv;掛載至/users目錄 2、新建用戶archlinux,要求其家目錄為/users/archlinux,而后su切換至archlinux用戶,復制 /etc/pam.d目錄至自己的家目錄 3、擴展testlv至7G,要求archlinux用戶的文件不能丟失 4、收縮testlv至3G,要求archlinux用戶的文件不能丟失 5、對testlv創建快照,并嘗試基于快照備份數據,驗證快照的功能 centos6中 ---->首先準備好兩塊或者兩塊以上硬盤或分區,(分區格式調為8e),總大小為20G ┌─[root@centos6]─[~] └──? #lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 3.7G 0 rom /mnt/cdrom sdb 8:16 0 20G 0 disk └─sdb1 8:17 0 10G 0 part sdc 8:32 0 20G 0 disk └─sdc1 8:33 0 10G 0 part sda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 0 part /boot ├─sda2 8:2 0 48.8G 0 part / ├─sda3 8:3 0 29.3G 0 part /data ├─sda4 8:4 0 1K 0 part ├─sda5 8:5 0 2G 0 part [SWAP] └─sda6 8:6 0 3G 0 part ---->創建pv ┌─[root@centos6]─[~] └──? #pvcreate /dev/sd{b1,c1} Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdc1" successfully created ---->創建vg ┌─[root@centos6]─[~] └──? #vgcreate -s 16M testvg /dev/sd{b1,c1} Volume group "testvg" successfully created ---->創建lv ┌─[root@centos6]─[~] └──? #lvcreate -L 5G -n testlv testvg Logical volume "testlv" created. ---->創建文件系統
┌──[root@centos6]─[~] └──? #mkfs -t ext4 -L "users new home" /dev/testvg/testlv mke2fs 1.41.12 (17-May-2010) Filesystem label=users new home OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 327680 inodes, 1310720 blocks 65536 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1342177280 40 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 32 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. ---->創建掛載點目錄 ┌─[root@centos6]─[~] └──? #mkdir /users ---->掛載 ┌─[root@centos6]─[~] └──? #mount /dev/testvg/testlv /users ---->創建新用戶,指定新加目錄 ┌─[root@centos6]─[~] └──? #useradd -m -d /users/archlinux archlinux ---->擴展lv到7G ┌─[root@centos6]─[~] └──? #lvextend -L 7G /dev/testvg/testlv -r Size of logical volume testvg/testlv changed from 5.00 GiB (320 extents) to 7.00 GiB (448 extents). Logical volume testlv successfully resized. resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/mapper/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/mapper/testvg-testlv to 1835008 (4k) blocks. The filesystem on /dev/mapper/testvg-testlv is now 1835008 blocks long. ---->縮小lv到3G ┌─[root@centos6]─[~] └──? #umount /dev/testvg/testlv ┌─[root@centos6]─[~] └──? #fsck -t ext4 -f /dev/testvg/testlv fsck from util-linux-ng 2.17.2 e2fsck 1.41.12 (17-May-2010) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information users_new_home: 19/458752 files (0.0% non-contiguous), 64455/1835008 blocks ┌──[root@centos6]─[~] └──? #resize2fs /dev/testvg/testlv 3G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/testvg/testlv to 786432 (4k) blocks. The filesystem on /dev/testvg/testlv is now 786432 blocks long. ┌─[root@centos6]─[~] └──? #lvreduce -L 3G /dev/testvg/testlv WARNING: Reducing active logical volume to 3.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce testvg/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@centos6]─[~] └──? #lvcreate -L 1G -s -n testlv-snapshot -p r /dev/testvg/testlv Logical volume "testlv-snapshot" created. ---->掛載 ┌─[root@centos6]─[~] └──? #mount /dev/testvg/testlv-snapshot /snap mount: block device /dev/mapper/testvg-testlv--snapshot is write-protected, mounting read-only ---->刪除pam.d下以k開頭的文件 [archlinux@centos6 pam.d]$ rm -f k* [archlinux@centos6 pam.d]$ ls abrt-cli-root gdm reboot sudo-i abrt-gui-root gdm-autologin remote su-l atd gdm-fingerprint run_init system-auth authconfig gdm-password runuser system-auth-ac authconfig-gtk gnome-screensaver runuser-l system-auth.rpmnew authconfig-tui halt setup system-config-authentication chfn login smartcard-auth system-config-date chsh newrole smartcard-auth-ac system-config-kdump config-util other smartcard-auth.rpmnew system-config-keyboard crond passwd smtp system-config-language cups password-auth smtp.postfix system-config-network cvs password-auth-ac smtp.sendmail system-config-network-cmd eject password-auth.rpmnew sshd system-config-users fingerprint-auth polkit-1 ssh-keycat xdm fingerprint-auth-ac poweroff su xserver fingerprint-auth.rpmnew ppp sudo ---->恢復快照 ┌─[root@centos6]─[~] └──? #umount /snap ┌─[root@centos6]─[~] └──? #umount /users ┌─[root@centos6]─[~] └──? #lvconvert --merge /dev/testvg/testlv-snapshot Merging of volume testlv-snapshot started. testlv: Merged: 100.0% testlv: Merged: 100.0% Merge of snapshot into logical volume testlv has finished. Logical volume "testlv-snapshot" successfully removed ---->查看快照效果 ┌──[root@centos6]─[~] └──? #su - archlinux [archlinux@centos6 ~]$ cd pam.d/ [archlinux@centos6 pam.d]$ ls abrt-cli-root gdm-fingerprint poweroff sudo-i abrt-gui-root gdm-password ppp su-l atd gnome-screensaver reboot system-auth authconfig halt remote system-auth-ac authconfig-gtk kcheckpass run_init system-auth.rpmnew authconfig-tui kdm runuser system-config-authentication chfn kdm-np runuser-l system-config-date chsh kppp setup system-config-kdump config-util kscreensaver smartcard-auth system-config-keyboard crond ksu smartcard-auth-ac system-config-language cups login smartcard-auth.rpmnew system-config-network cvs newrole smtp system-config-network-cmd eject other smtp.postfix system-config-users fingerprint-auth passwd smtp.sendmail xdm fingerprint-auth-ac password-auth sshd xserver fingerprint-auth.rpmnew password-auth-ac ssh-keycat gdm password-auth.rpmnew su gdm-autologin polkit-1 sudo
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/97089