Linux對LVM操作過程 公司的FTP伺服器經過幾年的運行,現在出現了空間不足的情況,決定用LVM方式實現擴容工作。思路:先拷貝資料,再購置一塊大容量硬碟添加到伺服器上,然後將己有的普通分區轉換為物理卷,最後通過物理卷建立邏輯卷組來實現容量的擴充。實現過程:A、檢查系統中是否安裝了LVM工具,如果沒有安裝就下載安裝。[root@linuxas ~]# rpm -qa | grep lvmsystem-config-lvm-1.0.22-1.0.el5lvm2-2.02.16-3.el5B、 建立和管理LVM1、建立或轉換分區[root@linuxas ~]# fdisk /dev/sdaThe number of cylinders for this disk is set to 1044.There is nothing wrong with that, but this is larger than 1024,and could in certain setups cause problems with:1) software that runs at boot time (e.g., old versions of LILO)2) booting and partitioning software from other OSs(e.g., DOS FDISK, OS/2 FDISK)Command (m for help): p(查看分區情況)Disk /dev/sda: 8589 MB, 8589934592 bytes255 heads, 63 sectors/track, 1044 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesDevice Boot Start End Blocks Id System/dev/sda1 * 1 25 200781 83 Linux/dev/sda2 26 535 4096575 83 Linux/dev/sda3 536 854 2562367+ 83 Linux/dev/sda4 855 1044 1526175 5 Extended/dev/sda5 855 905 409626 82 Linux swap / Solaris/dev/sda6 906 1044 1116486 83 LinuxCommand (m for help): t (更改分區類型)Partition number (1-6): 6 (將第6個分區轉換成LVM)Hex code (type L to list codes): 8e(將分區類型指定為“8e”就是LVM)Changed system type of partition 6 to 8e (Linux LVM)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.Syncing disks.2、建立物理卷[root@linuxas /]# pvcreate /dev/sda6Can't open /dev/sda6 exclusively. Mounted filesystem?出現以上錯誤的原因是分區檔案正在使用中,必須先umount才行。[root@linuxas /]# pvcreate /dev/sda6Physical volume "/dev/sda6" successfully created建好物理卷後可以用“pvdisplay”命令查看物理卷情況[root@linuxas /]# pvdisplay--- NEW Physical volume ---PV Name /dev/sda6VG NamePV Size 1.06 GBAllocatable NOPE Size (KByte) 0Total PE 0Free PE 0Allocated PE 0PV UUID N2LgeT-RB4Y-8YEP-lO2J-tDWu-UeCT-4Obl8p3、建立邏輯卷組[root@linuxas /]# vgcreate tgt /dev/sda6Volume group "tgt" successfully createdYou have new mail in /var/spool/mail/root查看邏輯卷組情況[root@linuxas /]# vgdisplay--- Volume group ---VG Name tgtSystem IDFormat lvm2Metadata Areas 1Metadata Sequence No 1VG Access read/writeVG Status resizableMAX LV 0Cur LV 0Open LV 0Max PV 0Cur PV 1Act PV 1VG Size 1.06 GB (VG為邏輯卷組的大小)PE Size 4.00 MB (PE為邏輯卷組最小儲存單位)Total PE 272Alloc PE / Size 0 / 0Free PE / Size 272 / 1.06 GBVG UUID g33wzW-yCvX-gc64-YQuD-LYeP-zgra-TGAa8G4、 啟用邏輯卷組[root@linuxas /]# vgchange -a y tgt0 logical volume(s) in volume group "tgt" now active5、建立邏輯卷[root@linuxas /]# lvcreate -L 1000M -n lvm tgt (-L指定大小,-n指定該邏輯卷的名稱)Logical volume "lvm" created6、建立檔案系統[root@linuxas /]# mkfs -j /dev/tgt/lvmmke2fs 1.39 (29-May-2006)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)128000 inodes, 256000 blocks12800 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=2642411528 block groups32768 blocks per group, 32768 fragments per group16000 inodes per groupSuperblock backups stored on blocks:32768, 98304, 163840, 229376Writing inode tables: doneCreating journal (4096 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 31 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.7、掛載檔案系統[root@linuxas /]# mount -t ext3 /dev/tgt/lvm /homeC、 為LVM擴容1、新添一塊硬碟並進行分區操作,並指定為LVM[root@linuxas /]# fdisk -lDisk /dev/sda: 8589 MB, 8589934592 bytes255 heads, 63 sectors/track, 1044 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesDevice Boot Start End Blocks Id System/dev/sda1 * 1 25 200781 83 Linux/dev/sda2 26 535 4096575 83 Linux/dev/sda3 536 854 2562367+ 83 Linux/dev/sda4 855 1044 1526175 5 Extended/dev/sda5 855 905 409626 82 Linux swap / Solaris/dev/sda6 906 1044 1116486 8e Linux LVMDisk /dev/sdb: 5368 MB, 5368709120 bytes255 heads, 63 sectors/track, 652 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesDevice Boot Start End Blocks Id System/dev/sdb1 1 652 5237158+ 8e Linux LVM2、建立物理卷[root@linuxas /]# pvcreate /dev/sdb1Physical volume "/dev/sdb1" successfully created=====================================最後,w儲存退出。再使用sfsik -l即可看到新增的磁碟。 此時,使用lvm的初始化磁碟工具或者直接使用pvcreate /dev/sda3命令會提示出錯,提示 Device “/dev/sda3” not found (or ignored by filtering) 出錯的原因在於使用fdisk修改配置時沒有在runlevel 1下進行,重啟系統後可解決此問題。====================================3、將新添的物理卷加入到已有的邏輯卷組中[root@linuxas /]# vgextend tgt /dev/sdb1Volume group "tgt" successfully extended4、為LVM擴容[root@linuxas /]# lvextend -L 5368M /dev/tgt/lvm (此命令將此卷的總容量定為5368M)Extending logical volume lvm to 5.24 GBLogical volume lvm successfully resized或者用[root@linuxas /]# lvextend -L +1000M /dev/tgt/lvm(此命令將此卷的總容量增加1000M)[root@linuxas /]# e2fsck -f /dev/tgt/lvm ( 檢查LVM裝置對應inode和block之間的關係)[root@linuxas /]# resize2fs /dev/tgt/lvm (使增加的容量生效)