8/16/2016 - 7:19 AM

Migrate existing disk to raid-1, LVM,

Migrate existing disk to raid-1, LVM,

# Migrate esisting disk to raid-1 with lvm (Ubuntu)

# since grub2 aka ubuntu ~8.x there is no need to have /boot on separate partion
# if you want /boot on another partition rung most of the comand twice with proper name/partition change

# transfer partition table (use sgdisk for > 2TB disks with GPT)
sgdisk/sfdisk -d /dev/sda > partition_table
sgdisk/sfdisk /dev/sdb < partition_table

apt-get install mdadm lvm2

mdadm --create /dev/md0 --level=1 --raid-devices=2 missing /dev/sdb1

mdadm --detail --scan >> /etc/mdadm/mdadm.conf

# create LVM on md1 
pvcreate /dev/md0
vgcreate vg0 /dev/md0
lvcreate -n system -l100%FREE vg0

mkfs.ext4 /dev/mapper/vg0-system
# if no LVM:
# mkfs.ext4 /dev/md0

# copy/edit latest entry from /boot/grub/grub.cfg
blkid /dev/mapper/vg0-system
vim /boot/grub/grub.cfg /etc/grub.d/40_custom
# add/update: 
    insmod raid
    insmod mdraid1x
    insmod lvm
    root=/dev/vg0/system bootdegraded=true
# update all UUID to match LV /dev/vg0/system UUID

# increase GRUB_TIMEOUT to >= 10s
vim /etc/default/grub

# for ubuntu 14.04 (bug related)
echo "sleep 60" > /etc/initramfs-tools/scripts/init-premount/delay_for_raid_array_to_build_before_mounting
chmod +x /etc/initramfs-tools/scripts/init-premount/delay_for_raid_array_to_build_before_mounting
echo mpt2sas >>/etc/initramfs-tools/modules

update-initramfs -u -k all

mkdir /mnt/raid_system
mount /dev/mapper/vg0-system /mnt/raid_system

cd / ; find . -depth -xdev | grep -v '^\./tmp/' | cpio -pumd /mnt/raid_system

# fix UUID/PATH for /,swap, etc..
blkid /dev/md0; blkid /dev/vg0/system
vim /mnt/raid_system/etc/fstab

sync; umount /mnt/raid_*

grub-install /dev/sda
grub-install /dev/sdb


# if lvm was used on /dev/sda
# lvremove/vgremote (if LVM was used)
# lvremove /dev/vg0/system
# vgremove /dev/vg0
# pvremove /dev/sda1

mdadm /dev/md0 -a /dev/sda1
watch cat /proc/mdstat

# once synced
# remove bootdegraded=true from /etc/grub.d/40_custom
# add bootdegraded=true to the GRUB_CMDLINE_LINUX_DEFAULT configuration variable
vim /etc/grub.d/40_custom /etc/default/grub 

grub-install /dev/sda
grub-install /dev/sdb