Configure RAID1 + LVM (for file system snapshots) in Hetzner and ServerLoft

Here I was overwhelmed with instructions on how to enable LVM for the root file system on two popular dedicated hosting: Hetzner and ServerLoft. I publish it here.
Linux LVM is used, in particular, to instantly create “frozen” file system snapshots, which can then be safely backed up while the server continues to work. This is a very convenient feature, especially for database servers: you can do an incremental backup of the database, for example, using the file rdiff-backup or duplicity (including the remote FTP server, which many hosting providers provide free and fast space on). As for RAID-1 (mirroring on 2 disks), it is absolutely necessary on the server so that if one of the disks fails, it can be replaced without stopping the machine. Of course, you need to install LVM on top of RAID-1.
But the trouble is that neither Hetzner nor ServerLoft allow you to enable LVM during the initial creation of the machine "with just one mouse." Instead, they make it possible to install one of the fixed OS images, at most with RAID-1 enabled. Below are two instructions on how to switch such a “crowded-in” machine to use LVM for the root file system, and then a short example of how to work with snapshots. We will work with Ubuntu Server 12.04 minimal (for other OSs, the configuration may vary).
Attention: if you decide to try, perform all these steps only where it is not a pity to lose data. In no case do not experiment with a "live" server, unless you understand thoroughly the issue.
Turn on LVM by car in Hetzner
Below it is assumed that you only need 220G of disk space (in total, the LVM volume contains 300G, but we will need the remaining space to create snapshots). If the disk is larger, but you do not need extra space at all, it is better to leave a lower value - this speeds up the rebuilding of RAID-1 in case of accidents.
- Select OS type "recover" when you install a new machine.
- Login via SSH and run "installimage" command (just type "installimage" and press Enter).
- Select Ubuntu-1204-precise-64-minimal.
- Enter the following configuration in the opened editor:
DRIVE1 / dev / sda DRIVE2 / dev / sdb SWRAID 1 SWRAIDLEVEL 1 BOOTLOADER grub HOSTNAME newhost PART / boot ext2 512M PART lvm vg0 300G # We use only 300G of our large HDD to speedup RAID1 rebuild if it happens. # Anyway, we do not need so much space typically, so 300G is enough. LV vg0 swap swap swap 4G LV vg0 tmp / tmp reiserfs 10G LV vg0 root / ext3 220G IMAGE /root/.oldroot/nfs/install/../images/Ubuntu-1204-precise-64-minimal.tar.gz
- Press F2, then F10 and wait until the installation is finished.
Turn on LVM on the machine in ServerLoft
ServerLoft does not have such a rich configuration utility as the Hetzner installimage, and lvm2 is not included in the list of packages by default, so the instructions are more complicated. A lot of blood was spilled on its compilation. Below we create an LVM volume of 80% of the free disk space, leaving 20% to support snapshot. The script works correctly for both software RAID and hardware RAID (yes, some configurations in ServerLoft have hardware RAID - I would not recommend taking them because of poor transparency, of course, but they do).
- Select OS type "Ubuntu 12.04 Minimal, software RAID1" at my.serverloft.com/en/Dedicated/Restore
- Enter the recovery mode after the installation is finished at my.serverloft.com/en/Dedicated/Recovery/Index
- Run the following commands to prepare partitions:
if mdadm -A --scan; then export DEV_BOOT = / dev / md0 export DEV_LARGE = / dev / md2 else export DEV_BOOT = / dev / sda2 export DEV_LARGE = / dev / sda4 fi mount $ DEV_LARGE / mnt mkdir / rootcopy if [-d / mnt / rootcopy]; then echo "Please enter the recovery mode before running this script!" rmdir / rootcopy; sleep 5; exit fi cp -a / mnt / * / rootcopy umount $ DEV_LARGE pvcreate $ DEV_LARGE vgcreate vg0 $ DEV_LARGE lvcreate --name tmp --size 10G vg0 lvcreate --name root -l 80% FREE vg0 mkfs.ext3 / dev / mapper / vg0-tmp mkfs.ext3 / dev / mapper / vg0-root mount / dev / mapper / vg0-root / mnt cp -a / rootcopy / * / mnt rm -rf / rootcopy mount $ DEV_BOOT / mnt / boot mount -o bind / dev / mnt / dev mount -t proc none / mnt / proc mount -t sysfs none / mnt / sys perl -p -i -e 's {^ [^ \ s #] + \ s + / \ s + \ S +} {/ dev / mapper / vg0-root / ext3} s' / mnt / etc / fstab echo "/ dev / mapper / vg0-tmp / tmp ext3 defaults, noatime 0 4" >> / mnt / etc / fstab chroot / mnt apt-get install lvm2 --yes chroot / mnt update-grub chroot / mnt grub-install / dev / sda chroot / mnt grub-install / dev / sdb umount / mnt / sys umount / mnt / proc umount / mnt / dev umount $ DEV_BOOT umount / dev / mapper / vg0-root - Reboot the server, then in 30 seconds - stop the recovery mode at my.serverloft.com/en/Dedicated/Recovery/Index (maybe reboot twice if the first reboot will not succeed).
An example of how to use snapshots
Actually, having a configured LVM, you can do this kind of magic:
# In case you forgot to unmount the last time, unmount it now.
lvremove -f / dev / vg0 / snap 2> / dev / null
# Create a new snapshot (40G is the maximum volume that can be written to the root
# file system while the snapshot is active). Then mount it for use.
lvcreate -L40G -s -n snap / dev / vg0 / root
mount / dev / vg0 / snap / mnt -o ro
# Next, calmly backup / mnt / * - there will be a “frozen” state of the root file system.
duplicity ... / mnt ...
# Unmount and remove the snapshot.
umount / mnt
lvremove -f / dev / vg0 / snap
Memo to someone who uses OpenVZ
By default, support for LVM is not enabled in OpenVZ kernels - more precisely, Device Mapper (in / boot / initrd * there is no dm-mod in conf / modules). If you later want to use OpenVZ on top of LVM, you can run this script to add the dm-mod module (unpacked-added-packed) to the existing initrd module, as well as turning OpenVZ into the default kernel. There is probably an easier way, but I will give the one that I used to use:
FILE = `ls /boot/initrd.img-*stab* 2> / dev / null | head -n 1` if ["$ FILE" == ""]; then echo Cannot find an OpenVZ initrd in / boot. Aborting exit 1 fi rm -rf / tmp / initrd * 2> / dev / null mkdir / tmp / initrd cd / tmp / initrd zcat $ FILE | cpio -i if! grep "dm-mod" conf / modules> / dev / null; then echo dm-mod >> conf / modules find ./ | cpio -H newc -o | gzip> / tmp / initrd-new cat / tmp / initrd-new> $ FILE fi cd / rm -rf / tmp / initrd * sed -i ' s / GRUB_DEFAULT =. * / GRUB_DEFAULT = 1 /; s / # GRUB_DISABLE_RECOVERY =. * / GRUB_DISABLE_RECOVERY = true /; s / GRUB_CMDLINE_LINUX =. * / GRUB_CMDLINE_LINUX = "selinux = no" /; '/ etc / default / grub update-grub