SWAP 使用 LV

  1. 若要將 swap 的分割區也使用 LVM 管理,則可將整個硬碟的可用空間都分割為 /dev/vda4,並將格式設定為 8e (lvm)。
    [root@kvm7 ~]# fdisk /dev/vda 
    Welcome to fdisk (util-linux 2.23.2).
    
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    
    Command (m for help): p
    
    Disk /dev/vda: 4294 MB, 4294967296 bytes, 8388608 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x00094fcc
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048      411647      204800   83  Linux
    /dev/vda2          411648     7579647     3584000   8e  Linux LVM
    /dev/vda3         7579648     7845887      133120   8e  Linux LVM
    
    Command (m for help): n 
    Partition type:
       p   primary (3 primary, 0 extended, 1 free)
       e   extended
    Select (default e): p
    Selected partition 4
    First sector (7845888-8388607, default 7845888): 
    Using default value 7845888
    Last sector, +sectors or +size{K,M,G} (7845888-8388607, default 8388607): 
    Using default value 8388607
    Partition 4 of type Linux and of size 265 MiB is set
    
    Command (m for help): p
    
    Disk /dev/vda: 4294 MB, 4294967296 bytes, 8388608 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x00094fcc
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048      411647      204800   83  Linux
    /dev/vda2          411648     7579647     3584000   8e  Linux LVM
    /dev/vda3         7579648     7845887      133120   8e  Linux LVM
    /dev/vda4         7845888     8388607      271360   83  Linux
    
    Command (m for help): t
    Partition number (1-4, default 4): 
    Hex code (type L to list all codes): 8e
    Changed type of partition 'Linux' to 'Linux LVM'
    
    Command (m for help): p
    
    Disk /dev/vda: 4294 MB, 4294967296 bytes, 8388608 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x00094fcc
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048      411647      204800   83  Linux
    /dev/vda2          411648     7579647     3584000   8e  Linux LVM
    /dev/vda3         7579648     7845887      133120   8e  Linux LVM
    /dev/vda4         7845888     8388607      271360   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: 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.
    
  2. 偵測新的分割表。
    [root@kvm7 ~]# partprobe 
    [root@kvm7 ~]# kpartx /dev/vda
    vda1 : 0 409600 /dev/vda 2048
    vda2 : 0 7168000 /dev/vda 411648
    vda3 : 0 266240 /dev/vda 7579648
    vda4 : 0 542720 /dev/vda 7845888
    
  3. pvcreate 將 /dev/vda4 初始化為 LVM 的實體空間。
    [root@kvm7 ~]# pvcreate /dev/vda4
      Physical volume "/dev/vda4" successfully created.
    
  4. 在 LVM 的實體空間 /dev/vda4 產生名為 vg_mntx,PE 大小為 4M 的 VG。
    [root@kvm7 ~]# vgcreate -s 4M vg_mntx /dev/vda4
      Volume group "vg_mntx" successfully created
    
  5. 在 VG vg_mntx 中建立名為 pub,大小為 10 個 PE 的 LV。
    [root@kvm7 ~]# lvcreate --extents 10 -n pub vg_mntx
      Logical volume "pub" created.
    
  6. 在 VG vg_mntx 中建立名為 swap,大小為 30M 的 LV,但因 VG 建立時指定的 PE 大小為 4M,LV 的大小必須是 PE 大小的倍數,因此自動加大到 32M。
    [root@kvm7 ~]# lvcreate --size 30M -n swap vg_mntx
      Rounding up size to full physical extent 32.00 MiB
      Logical volume "swap" created.
    
  7. 將 LV /dev/vg_mntx-pub 依要求格式化,此例為 ext3。
    [root@kvm7 ~]# mkfs.ext3 /dev/vg_mntx/pub 
    mke2fs 1.42.9 (28-Dec-2013)
    Filesystem label=
    OS type: Linux
    Block size=1024 (log=0)
    Fragment size=1024 (log=0)
    Stride=0 blocks, Stripe width=0 blocks
    10240 inodes, 40960 blocks
    2048 blocks (5.00%) reserved for the super user
    First data block=1
    Maximum filesystem blocks=41943040
    5 block groups
    8192 blocks per group, 8192 fragments per group
    2048 inodes per group
    Superblock backups stored on blocks: 
    	8193, 24577
    
    Allocating group tables: done                            
    Writing inode tables: done                            
    Creating journal (4096 blocks): done
    Writing superblocks and filesystem accounting information: done
    
  8. 將 LV /dev/vg_mntx-swap 格式化 swap。
    [root@kvm7 ~]# mkswap /dev/vg_mntx/swap 
    Setting up swapspace version 1, size = 32764 KiB
    no label, UUID=03be6480-7674-4fee-80fd-ebe573737927
    
  9. 在開機掛載表中,加入以下兩行,設定開機時將 LV vg_mnt-pub 掛載在 /mnt/pubx,及啟動 LV vg_mntx-swap swap。
    [root@kvm7 ~]# vim /etc/fstab 
    [root@kvm7 ~]# grep vg_mntx /etc/fstab
    /dev/mapper/vg_mntx-pub    /mnt/pubx        ext3    defaults        1 2
    /dev/mapper/vg_mntx-swap    swap            swap    defaults        0 0
    
  10. 依掛載表 /etc/fstab 指定的格式及目錄掛載所有檔案系統。
    [root@kvm7 ~]# mount -a
    
  11. 查看掛載狀況,LV vg_mntx-pub 以 ext3 格式掛載在 /mnt/pubx。
    [root@kvm7 ~]# df -TH
    Filesystem                  Type      Size  Used Avail Use% Mounted on
    devtmpfs                    devtmpfs  505M     0  505M   0% /dev
    tmpfs                       tmpfs     520M     0  520M   0% /dev/shm
    tmpfs                       tmpfs     520M  7.1M  513M   2% /run
    tmpfs                       tmpfs     520M     0  520M   0% /sys/fs/cgroup
    /dev/mapper/vg_kvm7usb-root ext4      3.3G  2.9G  198M  94% /
    /dev/vda1                   xfs       207M  188M   19M  91% /boot
    /dev/mapper/vg_kvm7home-vo  ext4       78M  1.7M   70M   3% /home
    tmpfs                       tmpfs     104M     0  104M   0% /run/user/0
    /dev/mapper/vg_mntx-pub     ext3       37M  521k   34M   2% /mnt/pubx
    
  12. 啟動掛載表 /etc/fstab 中設定的所有 swap。
    [root@kvm7 ~]# swapon -a
    
  13. 查詢啟動的 swap,其中 /dev/dm-4 就是 LV vg_mntx-swap。
    [root@kvm7 ~]# swapon -s
    Filename          Type        Size    Used    Priority
    /dev/dm-0         partition   126972  0       -1
    /dev/dm-4         partition   32764   0       -2
    
    [root@kvm7 ~]# ll /dev/mapper/
    total 0
    crw-------. 1 root root 10, 236 Nov  2 07:26 control
    lrwxrwxrwx. 1 root root       7 Nov  2 07:26 vg_kvm7home-vo -> ../dm-2
    lrwxrwxrwx. 1 root root       7 Nov  2 07:26 vg_kvm7usb-root -> ../dm-1
    lrwxrwxrwx. 1 root root       7 Nov  2 07:26 vg_kvm7usb-swap -> ../dm-0
    lrwxrwxrwx. 1 root root       7 Nov  2 07:33 vg_mntx-pub -> ../dm-3
    lrwxrwxrwx. 1 root root       7 Nov  2 07:33 vg_mntx-swap -> ../dm-4