Sharing

2011年8月30日 星期二

大玩特玩 LVM

參考文件:

主要是

http://linux.vbird.org/linux_basic/0420quota.php#lvm
http://linux.vbird.org/somepaper/20050321-LVM-1.pdf

http://ericfoto.com/618
http://blog.csdn.net/xjtuse_mal/article/details/5504768
http://tonychou.posterous.com/lvm



查看一下現在整個系統內有幾個 partition.
pjack@ubuntu1104-64-5:/dev$ sudo fdisk -l

Disk /dev/sda: 500.1 GB, 500107862016 bytes 
255 heads, 63 sectors/track, 60801 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: 0x000d41c2

# 從上面可以看到有一個 Disk, 大小為 500GB

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          32      248832   83  Linux       # 主要分割區 
/dev/sda2              32       60802   488134657    5  Extended    # 延申分割區
/dev/sda5              32       60802   488134656   8e  Linux LVM   # 邏輯分割, 對應到 LVM 內的一塊 lv


看一下 Physical Volume 的狀況
pjack@ubuntu1104-64-5:/dev$ sudo pvscan
  PV /dev/sda5   VG ubuntu1104-64-5   lvm2 [465.52 GiB / 48.00 MiB free]
  Total: 1 [465.52 GiB] / in use: 1 [465.52 GiB] / in no VG: 0 [0   ]

pjack@ubuntu1104-64-5:/dev$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               ubuntu1104-64-5
  PV Size               465.52 GiB / not usable 2.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              119173
  Free PE               12
  Allocated PE          119161
  PV UUID               FnI4qo-0ilE-FKlj-caS2-LWyZ-ySXC-8KE3Yl

# 有一塊 PV, 對應到 /dev/sda5, 大小為 465 GB, PE size 為 4MB, 和查看 partition 是符合的


查看一下 Volumn Group

pjack@ubuntu1104-64-5:/dev$ sudo vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "ubuntu1104-64-5" using metadata type lvm2
pjack@ubuntu1104-64-5:/dev$ sudo vgdisplay
  --- Volume group ---
  VG Name               ubuntu1104-64-5
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2   # 分割成 2 個 Logical Volume
  Open LV               2
  Max PV                0
  Cur PV                1   # 由 1 個 Physical Volume 組成, 也就是我們查到的 /dev/sda5
  Act PV                1
  VG Size               465.52 GiB
  PE Size               4.00 MiB
  Total PE              119173
  Alloc PE / Size       119161 / 465.47 GiB
  Free  PE / Size       12 / 48.00 MiB
  VG UUID               cxPj4c-xOvg-8f13-HGdl-m2Qj-goI0-eSjXak


查看一下 Logical Volumne
pjack@ubuntu1104-64-5:/dev$ sudo lvdisplay
[sudo] password for pjack:
  --- Logical volume ---
  LV Name                /dev/ubuntu1104-64-5/root
  VG Name                ubuntu1104-64-5
  LV UUID                w6DSBS-nejc-aY6a-Uw0T-7eVd-WA0I-jUukBL
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                441.48 GiB
  Current LE             113020
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           251:0

# 第一塊的大小是 441 GB, 對應到 /dev/ubuntu1104-64-5/root
# 然後 VG 是 ubuntu1104-64-5

  --- Logical volume ---
  LV Name                /dev/ubuntu1104-64-5/swap_1
  VG Name                ubuntu1104-64-5
  LV UUID                5tP2Cf-3zWd-YjEl-7sKx-Kac0-PiOo-zFU0Z2
  LV Write Access        read/write
  LV Status              available
  # open                 2
  LV Size                23.99 GiB
  Current LE             6141
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           251:1

# 第一塊的大小是 24 GB, 對應到 /dev/ubuntu1104-64-5/swap_1
# 然後 VG 是 ubuntu1104-64-5
# 和我們剛剛查到 ubuntu1104-64-5 內有 2 塊 LV 是一致的

回頭看一下目前有 mount 起來的東西

pjack@ubuntu1104-64-5:/dev$ df
Filesystem                             1K-blocks      Used Available Use% Mounted on
/dev/mapper/ubuntu1104--64--5-root     455666040   3301844 429217700   1% /

# 其中一塊 LM 掛在 / 底下 
# 至於 swap 就不需要掛在地方

none                                    12358272       212  12358060   1% /dev
none                                    12366300         0  12366300   0% /dev/shm
none                                    12366300        72  12366228   1% /var/run
none                                    12366300         0  12366300   0% /var/lock
/dev/sda1                                 233191     45214    175536  21% /boot

# /dev/sda1 被 mount 到 /boot 區


另外觀察一下 /dev/mapper 及 /dev/ubuntu1104-64-5 之間的關係

pjack@ubuntu1104-64-5:/dev$ ll /dev/mapper
total 0
drwxr-xr-x  2 root root     100 2011-08-22 13:54 ./
drwxr-xr-x 18 root root    4560 2011-08-25 12:09 ../
crw-------  1 root root 10, 236 2011-08-22 13:54 control
lrwxrwxrwx  1 root root       7 2011-08-30 16:09 ubuntu1104--64--5-root -> ../dm-0
lrwxrwxrwx  1 root root       7 2011-08-30 16:09 ubuntu1104--64--5-swap_1 -> ../dm-1

pjack@ubuntu1104-64-5:/dev$ ll /dev/ubuntu1104-64-5/
total 0
drwxr-xr-x  2 root root   80 2011-08-22 13:54 ./
drwxr-xr-x 18 root root 4560 2011-08-25 12:09 ../
lrwxrwxrwx  1 root root    7 2011-08-30 16:09 root -> ../dm-0
lrwxrwxrwx  1 root root    7 2011-08-30 16:09 swap_1 -> ../dm-1

# 可以發現都是對應到 /dev/dm-0 & /dev/dm-1

我的任務是把 /dev/ubuntu1104-64-5/root 先縮小, 然後再 create 另一個 lv 來建立 Clustered File System Pool, 中間會警告你可能會傷害到一些檔案, 因為這台只有我在用, 所以我就完全不管, 刪了再說, 每一個 PE 的大小是 4MB , 900000 * 4MB = 360G, 所以從 450G 減少到 90 G

pjack@ubuntu1104-64-5:/dev$ sudo lvresize -l -90000 /dev/ubuntu1104-64-5/root
  WARNING: Reducing active and open logical volume to 89.92 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce root? [y/n]: y
  Reducing logical volume root to 89.92 GiB
  Backup of volume group ubuntu1104-64-5 metadata failed.
  Logical volume root successfully resized

很好.. 從此就掛掉了 XD 重灌吧~



後來和前輩討教, 發現我少做了一件事, 就是要先把 file system 縮小, 我縮小的是 block level partition, 他並不知道上面有一層 file system, 所以直接把 partition 縮小會破壞到 file system 的結構.

首先試著 resize2fs,結果出現 error

pjack@ubuntu1104-64-6:~$ sudo resize2fs / 50G
[sudo] password for pjack:
resize2fs 1.41.14 (22-Dec-2010)
open: Is a directory while opening /

# 先試著直接 resize, 不過正在被使用, 所以無法被 resize

pjack@ubuntu1104-64-6:~$ sudo umount /
umount: /: device is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))

pjack@ubuntu1104-64-6:~$ fuser -uv /
                     USER        PID ACCESS COMMAND
/:                   root     kernel mount (root)/
                     pjack      1578 .r... (pjack)bash


# 嗯, device is busy, 因為有人登入... 那要怎麼 umount 呀~~ Orz

pjack@ubuntu1104-64-6:~$ sudo umount -l /
[sudo] password for pjack:

# 強迫他 umount 好了.. 看看會怎麼樣

pjack@ubuntu1104-64-6:~$ sudo resize2fs /dev/dm-0 50G
[sudo] password for pjack:
resize2fs 1.41.14 (22-Dec-2010)
open: No such file or directory while opening /dev/dm-0

pjack@ubuntu1104-64-6:~$ ll /dev/dm*
ls: cannot access /dev/dm*: No such file or directory

pjack@ubuntu1104-64-6:~$ df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/ubuntu1104--64--6-root
                     455666040   4702340 427817204   2% /
none                 455666040   4702340 427817204   2% /sys
df: `/sys/fs/fuse/connections': No such file or directory
df: `/sys/kernel/debug': No such file or directory
df: `/sys/kernel/security': No such file or directory
none                 455666040   4702340 427817204   2% /dev
none                 455666040   4702340 427817204   2% /dev/pts
none                 455666040   4702340 427817204   2% /dev/shm
none                 455666040   4702340 427817204   2% /var/run
none                 455666040   4702340 427817204   2% /var/lock
/dev/sda1            455666040   4702340 427817204   2% /boot

# Orz 又全部爛掉了.. 幸好重開機之後就回復了


於是拿出 liveCD.. 天呀.. 網路慢到不行, 我只好放棄.. 再度重灌
雖然沒有成功, 不過也因此對整個操作, 還有對 lvm & filesystem 都有滿清楚的概念
也是一大收獲!

沒有留言: