📜 ⬆️ ⬇️

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


I got an instruction here how to enable LVM for the root file system on two popular dedicated hosting systems : Hetzner and ServerLoft. I publish it here.

LVM in Linux is used, in particular, to create “frozen” file system snapshots all at once, 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 make an incremental backup of the database, for example, using file rdiff-backup or duplicity (including to a remote FTP server, free and fast place which many hosters provide). 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 over RAID-1.

But the trouble is that neither Hetzner nor ServerLoft allow LVM to be enabled during the initial creation of the machine “using only one mouse”. Instead, they provide an opportunity to install one of the fixed OS images, the maximum is with RAID-1 enabled. Below are two instructions on how to switch such a "poured-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 operating systems, the configuration may differ).
')
Attention: if you decide to try, perform all these actions only where it is not a pity to lose data. In no case do not experiment with a "live" server, if you do not thoroughly understand the question.

Turn on LVM by car in Hetzner

Below it is assumed that you only need 220G on the disk (in total, the LVM volume contains 300G, but we will need the remaining space to create snapshots). If the disk is larger, but you absolutely do not need extra space, it is better to leave a smaller value - this speeds up the rebuilding of RAID-1 in the event of a crash.

  1. Select OS type “recover” when you install a new machine.
  2. Login via SSH and run "installimage" command (just type "installimage" and press Enter).
  3. Select Ubuntu-1204-64-minimal.
  4. Enter the following configuration in the browser:
     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, 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
    
  5. Press F2, then F10 and wait until the installation is finished.

Enable LVM on the machine in ServerLoft

ServerLoft does not have such a rich configuration utility, like Hetzner installimage, and lvm2 is not included by default in the list of packages, so the instruction is more complicated. A lot of blood was spilled on its preparation. Below we create an LVM volume of 80% of free disk space, leaving 20% ​​for snapshot support. The script works correctly for both software RAID and hardware RAID (yes, some configurations in ServerLoft have hardware RAID - I would not recommend taking these because of the deterioration of transparency, of course, but they are encountered).

  1. Select OS type "Ubuntu 12.04 Minimal, software RAID1" at my.serverloft.com/en/Dedicated/Restore
  2. Enter the recovery mode after the installation is finished at my.serverloft.com/en/Dedicated/Recovery/Index
  3. 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
    
  4. 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 customized LVM, you can do this kind of magic:

# In case last time you forgot to unmount, unmount 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 snapshot is active). Then mount it for use.
lvcreate -L40G -s -n snap / dev / vg0 / root
mount / dev / vg0 / snap / mnt -o ro
# Then quietly make a backup / mnt / * - there will be a “frozen” state of the root file system.
duplicity ... / mnt ...
# Unmount and remove snapshot.
umount / mnt
lvremove -f / dev / vg0 / snap

Memo to anyone who uses OpenVZ

By default, LVM support is not enabled in the OpenVZ kernels - more precisely, the Device Mapper (there is no dm-mod in conf / modules in / boot / initrd *). If you want to use OpenVZ over LVM later, you can run such a script to add the dm-mod module to the existing initrd (unpacked-added-packed), and also to turn OpenVZ into the default kernel. Probably, there is a simpler way, but I will give the one that once used:

 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

Source: https://habr.com/ru/post/176369/


All Articles