VMWare ESX Уменьшение раздела гостевой машины Linux с LVM

Выполняем c LiveCD

su
service iptables stop
service sshd start
passwd (меняем пароль, чтобы зайти по ssh)

Активируем LVM

lvm vgchange -ay

Смотри что есть

[[email protected] ~]# lvm lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv_root vg_v12 -wi-a----- 55.53g
lv_swap vg_v12 -wi-a----- 3.97g

[[email protected] ~]# pvdisplay -m
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_v12
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4994
Free PE 0
Allocated PE 4994
PV UUID 93cTkO-pU2H-ARpf-0Ya9-4gBs-vkDK-AUkybo
--- Physical Segments ---
Physical extent 0 to 3977:
Logical volume /dev/vg_v12/lv_root
Logical extents 0 to 3977
Physical extent 3978 to 4993:
Logical volume /dev/vg_v12/lv_swap
Logical extents 0 to 1015
--- Physical volume ---
PV Name /dev/sda3
VG Name vg_v12
PV Size 40.00 GiB / not usable 4.07 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 10238
Free PE 0
Allocated PE 10238
PV UUID njw34K-C0rg-xIqw-mMDV-d2h1-wITg-qO3kIa
--- Physical Segments ---
Physical extent 0 to 10237:
Logical volume /dev/vg_v12/lv_root
Logical extents 3978 to 14215

Проверка файловой системы

e2fsck -f /dev/vg_V12/lv_root

Ответ:

logical volumes ... now active

Уменьшаем файловую систему. Выполняется долго.

resize2fs /dev/vg_v12/lv_root 14G

Ответ:

resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg_v12/lv_root to 3670016 (4k) blocks.
The filesystem on /dev/vg_v12/lv_root is now 3670016 blocks long.

Уменьшаем логический том. Выполняется быстро.

[[email protected] ~]# lvm lvreduce -L 15G /dev/vg_v12/lv_root
WARNING: Reducing active logical volume to 15.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_root? [y/n]: y
Reducing logical volume lv_root to 15.00 GiB
Logical volume lv_root successfully resized

Смотрим, что получилось

[[email protected] ~]# lvm lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv_root vg_v12 -wi-a----- 15.00g
lv_swap vg_v12 -wi-a----- 3.97g

Проверяем диск перед увеличением

[[email protected] ~]# e2fsck -f /dev/vg_v12/lv_root
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
/dev/vg_v12/lv_root: 236193/913920 files (0.2% non-contiguous), 3112486/3670016 blocks

Расширяем файловую систему до размера тома

[[email protected] ~]# resize2fs /dev/vg_v12/lv_root
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg_v12/lv_root to 3932160 (4k) blocks.
The filesystem on /dev/vg_v12/lv_root is now 3932160 blocks long.

Смотрим, где расположены занятые Экстенты

[[email protected] ~]# pvdisplay -m
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_v12
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 4994
Free PE 138
Allocated PE 4856
PV UUID 93cTkO-pU2H-ARpf-0Ya9-4gBs-vkDK-AUkybo
--- Physical Segments ---
Physical extent 0 to 3839:
Logical volume /dev/vg_v12/lv_root
Logical extents 0 to 3839
Physical extent 3840 to 3977:
FREE
Physical extent 3978 to 4993:
Logical volume /dev/vg_v12/lv_swap
Logical extents 0 to 1015
--- Physical volume ---
PV Name /dev/sda3
VG Name vg_v12
PV Size 40.00 GiB / not usable 4.07 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 10238
Free PE 10238
Allocated PE 0
PV UUID njw34K-C0rg-xIqw-mMDV-d2h1-wITg-qO3kIa
--- Physical Segments ---
Physical extent 0 to 10237:
FREE

Выводим раздел из группы

[[email protected] ~]# vgreduce vg_v12 /dev/sda3
Removed "/dev/sda3" from volume group "vg_v12"

Выводи физический раздел

[[email protected] ~]# зм
Labels on physical volume "/dev/sda3" successfully wiped

Проверяем. Все отключено.

[[email protected] ~]# pvdisplay -m
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_v12
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 4994
Free PE 138
Allocated PE 4856
PV UUID 93cTkO-pU2H-ARpf-0Ya9-4gBs-vkDK-AUkybo
--- Physical Segments ---
Physical extent 0 to 3839:
Logical volume /dev/vg_v12/lv_root
Logical extents 0 to 3839
Physical extent 3840 to 3977:
FREE
Physical extent 3978 to 4993:
Logical volume /dev/vg_v12/lv_swap
Logical extents 0 to 1015

Перегружаемся, находим с диска

Смотрим. Все красиво:

[[email protected] ~]# lvm lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv_root vg_v12 -wi-ao--- 15.00g
lv_swap vg_v12 -wi-ao--- 3.97g
[[email protected] ~]# pvdisplay -m
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_v12
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 4994
Free PE 138
Allocated PE 4856
PV UUID 93cTkO-pU2H-ARpf-0Ya9-4gBs-vkDK-AUkybo
--- Physical Segments ---
Physical extent 0 to 3839:
Logical volume /dev/vg_v12/lv_root
Logical extents 0 to 3839
Physical extent 3840 to 3977:
FREE
Physical extent 3978 to 4993:
Logical volume /dev/vg_v12/lv_swap
Logical extents 0 to 1015

Смотрим на диск на VMWARE

/vmfs/volumes/52b935ee-ffdedee8-43b8-00199990ce08/oblik # ls -lh
total 62914560
-rw------- 1 root root 60.0G Jan 21 07:21 V12-flat.vmdk
-rw------- 1 root root 488 Jan 21 07:10 V12.vmdk

Возвращаемся на Хост

[[email protected] ~]# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0x0007d1bf
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVM
/dev/sda3 2611 7832 41939020 8e Linux LVM

Удаляем sda3

cfdisk

Смотрим

[[email protected] ~]# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 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: 0x0007d1bf
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVM

Перезагрузился – Успешно.
Выключаем машину и сжимаем файл диска на VMWare
Смотрим VMDK

cat V12.vmdk

Extent description
БЫЛО: RW 125829120 VMFS “V12-flat.vmdk”
СТАЛО: RW 44040192 VMFS “V12-flat.vmdk”
Пересчитали и измелили значение в данной строке по формуле
= 1024*1024*1024/512 * (размер диска в ГигаБайтах)
На 21 Гб получилось 44040192

Процесс не мгновенный

/vmfs/volumes/52b935ee-ffdedee8-43b8-00199990ce08/oblik # vmkfstools -i V12.vmdk small.vmdk
Destination disk format: VMFS zeroedthick
Cloning disk 'V12.vmdk'...
Clone: 34% done.

Получили:

/vmfs/volumes/52b935ee-ffdedee8-43b8-00199990ce08/oblik # ls -lh
total 84934656
-rw------- 1 root root 60.0G Jan 21 07:39 V12-flat.vmdk
-rw------- 1 root root 487 Jan 21 07:34 V12.vmdk
-rw------- 1 root root 21.0G Jan 21 07:39 small-flat.vmdk
-rw------- 1 root root 489 Jan 21 07:49 small.vmdk

Отключаем старый диск в машине, подключаем новый

УРА. Все получилось

[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_v12-lv_root
15G 12G 2.4G 84% /
tmpfs 504M 0 504M 0% /dev/shm
/dev/sda1 485M 36M 425M 8% /boot
[[email protected] ~]# lvm lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv_root vg_v12 -wi-ao--- 15.00g
lv_swap vg_v12 -wi-ao--- 3.97g

Общее время выполнение всего: 33 минуты