2022. november 30., szerda


physical to lvm

Írta: | Kulcsszavak: cenOS . redhat . linux . lvm



1. Collect information
2. Add a disk
3. Create structure
4. Copy data
5. Setup grub and fstab

1. Info collection

# mount
/dev/sda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda2 on /var type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x00064d71

Device Boot Start End Blocks Id System
/dev/sda1 * 1 1275 10240000 83 Linux
/dev/sda2 1275 2295 8192000 83 Linux
/dev/sda3 2295 2611 2538496 82 Linux swap / Solaris

3 patitions:
/ -> 10GB
/var -> 8GB
swap -> 2.5GB

2. Add a disk

Buy one or in case of virtual machine just create a new disk.

3. Create structure

New drive partitioning plan:

1: boot
2: LVM -> lvol-root, lvol-var, lvol-swap


fdisk /dev/sdb

Create partition by pressing n, set boot to 100MB and add the rest to LVM. Set partition type with t to 8e for partition 2. result:

Device Boot Start End Blocks Id System
/dev/sdb1 1 100 803218+ 83 Linux
/dev/sdb2 101 2610 20161575 8e Linux LVM

When everything is set properly write table to disk with w. Maybe you have to reread partiton table with partprobe.

Setup lvm:

# pvcreate /dev/sdb2
# vgcreate vg01 /dev/sdb2
# lvcreate -L 10G -n root vg01
# lvcreate -L 8G -n var vg01
# lvcreate -l 100%FREE -n swap vg01

Warning: Disk size is same as original, so swap partition is reduced by the size of sda1.

Format partitions:

mkfs.ext4 /dev/sdb1
# mkfs.ext4 /dev/vg01/root
# mkfs.ext4 /dev/vg01/var
# mkswap /dev/vg01/swap

4. Copy data

All the partitions should be mounted and data synced with rsync. I choose a 3 step sync to reduce mount point creation and downtime.

Mount root partition:

# mkdir /mnt/root
# mount /dev/vg01/root /mnt/root

Sync only the root partition (x):

# rsync -avXxPHWA --numeric-ids / /mnt/root/

Mount remaining partitions and sync again (exclude destination and other dirs. If you use NFS or other network share exclude it as well.):

# rm -rf /mnt/root/boot/*
# mount /dev/sdb1 /mnt/root/boot
# mount /dev/vg01/var /mnt/root/var/
# rsync -avXPHWA --numeric-ids --exclude='/tmp' --exclude='/proc' --exclude='/sys' --exclude='/mnt/root' / /mnt/root/


Switch to single mode (console) and sync again

# init 1
# rsync -avXPHWA --numeric-ids --exclude='/tmp' --exclude='/proc' --exclude='/sys' --exclude='/mnt/root' / /mnt/root/

5. Setup grub and fstab

Change grub.cfg (/mnt/root/boot/grub/grub.cfg). Important to change kernel and initrd path as we boot from a boot partition, so /boot/vmlinuz is changed to /vmlinuz. Also check for no LVM and change it to rd_LVM_LV. Swap is checked same as root lvol. Last, but not least setup root partition. I didn't want deal with UUID, so I used /dev/mapper here.

title Red Hat Enterprise Linux (2.6.32-358.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-358.el6.x86_64 ro root=/dev/mapper/vg01-root rd_NO_LUKS rd_LVM_LV=vg01/root rd_LVM_LV=vg01/swap LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-358.el6.x86_64.img

Reinstall grub. Do not forget to change /dev/sdb accordingly to the new disc.

# grub-install --recheck --root-directory=/mnt/root /dev/sdb

Change fstab. Here I have lvols, not UUIDs, but no real difference. It is up to you, just use blkid to query UUID. I used devices instead. Please mind new entry for boot partition.

/dev/vg01/root / ext4 defaults 1 1
/dev/vg01/var /var ext4 defaults 1 2
UUID=629a2743-4973-435f-82f8-982ad855e54e /boot ext4 defaults 1 2
/dev/vg01/swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0

Reboot and have fun.

Copyright © 2000-2022 PROHARDVER Informatikai Kft.