Sunday, December 23, 2012

Up to date Arch Linux Kernel with GPT/LVM on OVH dedicated hosting


My Needs

As I am starting an e-commerce website, I needed a hosting provider. I could have chose something like GoDaddy or BlueHost, but I had too many difficulties with them, they are very slow and performances highly depends on the current server load.
Additionally, I needed to be able to run cron task and random scripts so I went for a dedicated hosting.
This way, if tomorrow I start an other website or service, I'll already have everything ready :)
As they offered a very interesting discount, I chose OVH.

OVH

French Hosting provider now present in north america, OVH is the leader of hosting provider in Europe.
They provide very cheap services and as I have been very satisfied by their services in France, I decided to use them in the US too.

Pros

  • Very cheap: 140€/yr! With unlimited traffic! (special offer here as of december 2012)
  • Ability to have localized IP in US
  • Ability to have multiple 'failover' IPs
  • Rescue mode + vKVM, a KVM over VNC, very useful for debugging the system (or not)
  • Full IPv6 support
  • Extensive choice of operating system

Cons

  • Very poort customer service
  • Custom and often outdated kernel impossible to upgrade
  • Manager slow and difficult to use

Arch Linux

For a couple of years now, I use Arch Linux, it is a very lightweight distribution which is always up to date.
Some will say that it makes the distribution buggy, but I prefer to have this than to have to use experimental/test repositories from other solutions. And after a few years, even if I had a couple of but, it never took me more than 10 min to solve and I have been greatly satisfied.

Until very recently, OVH proposed only Arch Linux with a 2.6 Kernel, which started to get kind of old... But now they offer Arch 2012 with a 3.3 Kernel, much better indeed, but still outdated.
The partition tool from their manager is really awful and you can't do anything with it so you are stuck with MBR and their /, /home and swap partition scheme.
That's why I decided to get rid of their custom kernel and start from a fresh install, as I would do locally.

Rescue Mode

The rescue mode is basically a Netboot. It allows you to boot on an other system so you are able to recover your data from your harddrive in case you screwed with the network configuration or anything that would make your system unreachable.
Thanks to this mode, we can completely format the hard-drive.
For this time, I wanted to do something 'clean', so I decided to setup a GPT + LVM2 partition scheme.
The idea is to have:
/
/boot
/srv
/var
/tmp
/home
swap

Pre-Requisite

Before doing anything, as always, we need backups ;). I had a lot of bad experiences and had to re-format everything more than once.
Think to write down you IP, your default route and your DNS servers. IPv4 and IPv6.

Hard-drive wipe

The first step is to wipe out the hard-drive and to write the basic partition scheme. As I went for GPT/LVM2, the 'real' partitions are going to be simply the bios partition (required by GPT) and the main partition that will be used by LVM.
In order to do that, we are going to use gdisk, the fdisk equivalent for GPT.
$rescue> gdisk /dev/sda
It works basically like fdisk. We can display the partition table with 'p', create new partition with 'n' and remove partition with 'd'.

So remove all present partition (let's make sure they are all unmounted before), and create two partitions.
As it is better (IMHO) to deal with /dev/sda1 than /dev/sdaX, let's create the main partiton first :). We are using LVM in order to avoid to deal with /dev/sdaX ;).
In order to do so, simply create the partition with the first sector at 4096 until the end of the disk and then create a partition from the beginning of the disk to 4095.
Our main partition is going to be used by LVM so we set the type 'Linux LVM' to it (code 8e00) and the small one is going to be used by GPT and needs to be of type 'BIOS boot partition' (code ef02)

When displaying the partition table, we should have something like
Number  Start (sector)  End (sector)  Size        Code  Name
1       4096            3907029134    1.8 TiB     8E00  Linux LVM
2       2048            4095          1024.0 KiB  EF02  BIOS boot partition
Then, type 'w' within gdisk to write the partition table.

Partition creation

Now we are going to create our virtual partition table.
Create the LVM device
$rescue> pvcreate /dev/sda1
Create a virtual group lv1 using this device
$rescue> vgcreate lv1 /dev/sda1
From now on, we will never deal with /dev/sdX again, yes, I created the main partition first just for this ;)
Create the wanted partitions, for exemple, for a disk of 2TB
$rescue> lvcreate -L100M -n boot lv1
$rescue> lvcreate -C y -L8G -n swap lv1
$rescue> lvcreate -L50G -n root lv1
$rescue> lvcreate -L50G -n tmp lv1
$rescue> lvcreate -L300G -n var lv1
$rescue> lvcreate -L500G -n srv lv1
$rescue> lvcreate -L800G -n home lv1
As you can see, I left a little of free space, I'll use it later on for snapshots. But this will be an other article ;)

Now that we have our partitions, we need to create filesystems.

I did very basic filesystem choices: /boot in ext2, swap in swap (what a surprise!) and all the rest in ext4. If someone has a better suggestion, I'd be happy to hear it :)
$rescue> mkswap /dev/lv1/swap
$rescue> mkfs.ext2 /dev/lv1/boot
$rescue> mkfs.ext4 /dev/lv1/root
$rescue> mkfs.ext4 /dev/lv1/tmp
$rescue> mkfs.ext4 /dev/lv1/var
$rescue> mkfs.ext4 /dev/lv1/srv
$rescue> mkfs.ext4 /dev/lv1/home 
And of course, once we are done, we mount our brand new partition table :).
$rescue> mount /dev/lv1/root /mnt
$rescue> cd /mnt
$rescue> mkdir boot srv var tmp home
$rescue> mount /dev/lv1/boot /mnt/boot
$rescue> mount /dev/lv1/srv /mnt/srv
$rescue> mount /dev/lv1/var /mnt/var
$rescue> mount /dev/lv1/tmp /mnt/tmp
$rescue> mount /dev/lv1/home /mnt/home
$rescue> swapon /dev/lv1/swap

Installation

Ok, the hard drive is ready with the proper partition table and filesystems, we can install Arch Linux on it.
I simply follow the first method from the https://wiki.archlinux.org/index.php/Install_from_Existing_Linux article:
$rescue> cd /tmp
$rescue> wget http://tokland.googlecode.com/svn/trunk/archlinux/arch-bootstrap.sh
$rescue> sh arch-bootstrap.sh -a x86_64 /mnt

This will install a very basic Arch into /mnt, but you still do not have a system.

As long as you have wget, download havegen, you will need it later:
$rescue> wget -O /mnt/tmp/havegen.tar.xz https://www.archlinux.org/packages/community/x86_64/haveged/download/ 

Mount the system partitions and chroot on /mnt
$rescue> mount -o bind /dev /mnt/dev
$rescue> mount -o bind /dev/pts /mnt/dev/pts
$rescue> mount -t proc /prorc /mnt/proc
$rescue> mount -t sysfs /sys /mnt/sys
$rescue> chroot /mnt
$chroot>

At his point, you cannot use pacman, you need to generate keys.

To do so, the first step is to get entropy, otherwise, it will take hours. The easiest way is through havegen. As you downloaded it earlier, you just need to extract it and lunch it
$chroot> cd /
$chroot> tar xJf /tmp/havegen.tar.xz
$chroot> havegen -w 1024

This will generate enough entropy for you to generate your key:
$chroot> pacman-key --init
$chroot> pacman-key --populate

You can now use pacman. The first thing you want to do is install the 'complete' system:
$chroot> pacman -S base base-devel

At this point it is just like if you had done a local install using pacstrap. We can now install the needed packages like the bootloader and ssh.
If you'd like, you can kill and remove havegen unless you need it.

$chroot> pacman -S grub2-bios gdisk htop zsh emacs vim openssh most tmux arch-install-scripts

Use the genfstab from the arch-install-scripts package to generate /etc/fstab:
$chroot> gensftab / > /etc/fstab

Setup your locale and timezone
$chroot> emacs /etc/locale.gen
* uncomment the wanted locales *
$chroot> locale-gen
$chroot> ln -s /usr/share/timeinfo/America/New_York /etc/localtime

As we use LVM, we need to build the device-mapper module within the Kernel and load the lvm2 hook. To do so, add dm_mod within the module list in /etc/mkinitcpio.conf and add lvm2 in the Hook list (as suggested by the ArchLinux website, I placed it just before the filesystem hook). Once this done, rebuild the kernel.
$chroot> mkinitcpio -p linux

Now, we need to setup the boatloader:
$chroot> grub-install --recheck --no-floppy --boot-directory=/ /dev/sda
$chroot> grub-mkconfig -o /boot/grub/grub.cfg

The default config works pretty well, no need to dig into /etc/grub.d. It loads the ext2 and lvm mods, setup the proper boot and root partitions.

Finalization

It might seem finish, but as we did a fresh install, we lost all network configuration

$chroot> systemctl enable netcfg@net_eth0.service

And as it might be usefull to access the server using SSH so don't forget to enable sshd at boot.
$chroot> systemctl enable sshd.service
Don't forget to set a password to the root account and to create at least one user account.

Reboot and it should work fine :)

1 comment:

  1. Great article! Just the one I was searching for. Maybe you also have the same written on vdr data room pros and cons and pricing? This might be very interesting for some followers to read. Thank you in advance for your reply
    Regards,
    Rick

    ReplyDelete