(Part 2)
Part 1 的方式雖然有效,不過操作上有點麻煩
之前也想過,乾脆 vdi 就設小一點就好,不過設太小的話
如果臨時要用到大空間又會有另外的問題.....
最後決定用 小空間 vdi + lvm 來處理
因為這個 vm 已用了一段時間,不想再灌一個重新做設定
那只好將現有系統整個轉換到 lvm
新的 vdi 我用兩個,分別是固定 10GB,動態 16GB
先確定一下是否有安裝 lvm2
(guest)
$ pvdisplay
/dev/mapper/control: open failed: Permission denied
Failure to communicate with kernel device-mapper driver.
WARNING: Running as a non-root user. Functionality may be unavailable.
有裝,那就重開機到單人模式來操作,操作前照例先將 vdi clone 備份起來...
$ sudo poweroff
在 host 建立兩個新的 vdi, 10GB(fixed), 16GB(dyna)
$ ls -l (host)
-rw------- 1 xxx xxx 10739515392 Jan 24 17:43 LinuxMint-16-Mate-11.vdi
-rw------- 1 xxx xxx 7340032 Jan 24 17:45 LinuxMint-16-Mate-12.vdi
先掛上 10GB 這個,然後啟動 vm 到單人模式
root Drop to root shell prompt
(輸入密碼後進入 root shell)
(啟動 sshd)
wkSpace ~ # service ssh start
wkSpace ~ # ps ax|grep ssh
898 ? Ss 0:00 /usr/sbin/sshd -D
確認 sshd 已啟動便從 host 登入操作
$ sudo fdisk -l
Disk /dev/sda: 171.8 GB, 171798691840 bytes
255 heads, 63 sectors/track, 20886 cylinders, total 335544320 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 identifier: 0x00082810
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 16001023 7999488 83 Linux
/dev/sda2 16001024 32002047 8000512 82 Linux swap / Solaris
/dev/sda3 32004094 335542271 151769089 5 Extended
/dev/sda5 32004096 134373375 51184640 83 Linux
/dev/sda6 134375424 335542271 100583424 83 Linux
Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 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 identifier: 0x00000000
Disk /dev/sdb doesn't contain a valid partition table
/dev/sdb 是目標硬碟,要用這顆建立 lvm
並把原來 /dev/sda 的整個系統搬到 lvm
/dev/sdb 整顆切成一個主分割區
$ sudo fdisk /dev/sdb
n -> p -> 1 -> [enter] -> [enter] -> t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 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 identifier: 0xa2792bae
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20971519 10484736 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
下面開始建立系統用的 lvm
$ sudo pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created
$ sudo vgcreate vgpool /dev/sdb1
Command failed with status code 5.
/ 預設是唯讀模式掛載,無法對 /etc/lvm 寫入
所以會有這個訊息,解決方式是在 tmpfs 掛一個 /etc/lvm
$ sudo mount -t tmpfs tmpfs /etc/lvm
$ sudo vgcreate vgpool /dev/sdb1
Volume group "vgpool" successfully created
$ sudo pvdisplay
--- Physical volume ---
PV Name /dev/sdb1
VG Name vgpool
PV Size 10.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 2559
Free PE 2559
Allocated PE 0
PV UUID yeKfZB-sn03-Rioi-3hsg-Pnrn-JXfG-Ij6yw1
$ sudo vgdisplay
--- Volume group ---
VG Name vgpool
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size 10.00 GiB
PE Size 4.00 MiB
Total PE 2559
Alloc PE / Size 0 / 0
Free PE / Size 2559 / 10.00 GiB
VG UUID r2keE6-1xBF-2LDR-JXI3-Mlhm-o81n-pq0euk
這樣就能順利建立了 ^_^
再來建立邏輯區, 1個 root,1個 swap
$ sudo lvcreate -L 9G -n root vgpool
Logical volume "root" created
$ sudo lvcreate -l 100%FREE -n swap vgpool
Logical volume "swap" created
$ sudo mkswap /dev/vgpool/swap
mkswap: /dev/vgpool/swap: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 1044476 KiB
no label, UUID=eda54cd3-3295-48a9-9837-de04a7f30650
$ sudo lvdisplay
--- Logical volume ---
LV Path /dev/vgpool/root
LV Name root
VG Name vgpool
LV UUID ZVGlHd-oskG-Yeab-yVJV-CQC2-wIjp-Og5MGM
LV Write Access read/write
LV Creation host, time wkSpace, 2014-01-24 23:54:10 +0800
LV Status available
# open 0
LV Size 9.00 GiB
Current LE 2304
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0
--- Logical volume ---
LV Path /dev/vgpool/swap
LV Name swap
VG Name vgpool
LV UUID KGMwHz-H93e-yKQs-G3FK-dVzD-2WKE-H4tiGc
LV Write Access read/write
LV Creation host, time wkSpace, 2014-01-24 23:54:37 +0800
LV Status available
# open 0
LV Size 1020.00 MiB
Current LE 255
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:1
$ sudo mkfs.ext4 /dev/vgpool/root
$ ls /boot
$ ls /home
把 /dev/sda1(/boot),/dev/sda6(/home)還有新建立的 /dev/vgpool/root 掛上
然後開始搬資料...
$ sudo mount -o ro /dev/sda1 /boot
$ sudo mount -o ro /dev/sda6 /home
$ sudo mount /dev/vgpool/root /mnt
$ sudo rsync -ravzx / /mnt/
............
sent 2478881993 bytes received 2900072 bytes 3668561.81 bytes/sec
total size is 5356209094 speedup is 2.16
$
$ sudo rsync -ravzx /home /mnt
.........
sent 398151983 bytes received 189990 bytes 4260341.96 bytes/sec
total size is 913145324 speedup is 2.29
$
$ sudo rsync -ravzx /boot /mnt
.....
sent 2478881993 bytes received 2900072 bytes 3668561.81 bytes/sec
total size is 5356209094 speedup is 2.16
複製過程大概花不到20分鐘,再來要修改 fstab 與安裝 grub
$ sudo vgchange -a y vgpool
$ sudo mount --bind /dev /mnt/dev
$ sudo mount --bind /proc /mnt/proc
$ sudo mount --bind /sys /mnt/sys
這幾個指令一定要,不然 chroot 到新環境後會找不到很多設定資料
$ sudo chroot /mnt
#
# nano /etc/fstab
(原來的設定資料都註解起來或是直接刪除也可以,然後加下面這兩行)
/dev/vgpool/root / ext4 errors=remount-ro 0 1
/dev/vgpool/swap none swap sw 0 0
再來要在新硬碟安裝 grub,新硬碟在現在的環境是 /dev/sdb
如果搞不清楚就再 fdisk -l 查看
# grub-install /dev/sdb
# update-grub
# poweroff
這樣就完成了,開機先進入單人模式看看
如果開機OK就直接進入一般模式
進入桌面後開 terminal
@wkSpace ~ $ df
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vgpool-root ext4 8.8G 6.4G 2.0G 77% /
@wkSpace ~ $ free -mt
total used free shared buffers cached
Mem: 7985 1194 6791 0 36 600
-/+ buffers/cache: 556 7429
Swap: 1019 0 1019
Total: 9005 1194 7811
如果 /dev/vgpool/root 滿了就直接掛上新的 VDI 加進 lvm
加空間到現有的 lvm 不需要進入單人模式
直接在桌面的 terminal 操作
$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 17.2 GB, 17179869184 bytes
54 heads, 48 sectors/track, 12945 cylinders, total 33554432 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 identifier: 0x00000000
Disk /dev/sdb doesn't contain a valid partition table
$
$ sudo fdisk /dev/sdb
切一塊大小 2GB(/dev/sdb1) type = 8e,然後
@wkSpace ~ $ sudo pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created
用 vgextend 指令將新的空間加到現有的 vgpool
@wkSpace ~ $ sudo vgextend vgpool /dev/sdb1
Volume group "vgpool" successfully extended
@wkSpace ~ $ sudo vgdisplay
--- Volume group ---
VG Name vgpool
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 11.99 GiB <<------ 總容量增加了
PE Size 4.00 MiB
Total PE 3070
Alloc PE / Size 2559 / 10.00 GiB
Free PE / Size 511 / 2.00 GiB <<------ 有 2G free
VG UUID gqg0JC-F09f-y21Y-znz8-yAAf-srrK-X0PXKT
$ sudo lvextend -l +100%FREE /dev/vgpool/root
Extending logical volume root to 11.00 GiB
Logical volume root successfully resized
$ sudo resize2fs /dev/vgpool/root
resize2fs 1.42.8 (20-Jun-2013)
Filesystem at /dev/vgpool/root is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vgpool/root is now 2882560 blocks long.
$ sudo lvdisplay
--- Logical volume ---
LV Path /dev/vgpool/root
LV Name root
VG Name vgpool
LV UUID ZVGlHd-oskG-Yeab-yVJV-CQC2-wIjp-Og5MGM
LV Write Access read/write
LV Creation host, time wkSpace, 2014-01-24 23:54:10 +0800
LV Status available
# open 1
LV Size 11.00 GiB <<------ 邏輯區總容量也增加了
Current LE 2815
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0
$ df
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vgpool-root ext4 11G 6.4G 3.9G 63% /
/ Avail 由原來的 2.0G 增加到 3.9G
這樣 vgpool-root 就有多了將近 2GB 容量可用
系統開機使用了一天,沒有出現異常,vdi 大小也沒有增加
16GB 這個 vdi 雖然切了 2GB 掛上去
不過到目前也都還沒有被使用到
$ ls -l
-rw------- 1 xxx xxx 10739515392 Jan 25 15:47 LinuxMint-16-Mate-11.vdi
-rw------- 1 xxx xxx 7340032 Jan 25 15:47 LinuxMint-16-Mate-12.vdi
這樣以後就不用再煩惱 vdi 無限成長的問題了

這幾天有下載一個大概 2G 的 檔案
$ ls -l
-rw------- 1 xxx xxx 10739515392 Feb 8 10:57 LinuxMint-16-Mate-11.vdi
-rw------- 1 xxx xxx 884998144 Feb 8 10:57 LinuxMint-16-Mate-12.vdi