Monday, January 14, 2019

Flexible Server Part I: Bare metal installation


In Part I we'll go through the bare metal installation process, focusing on the post OS installation steps. The build used Ubuntu Server 18.04.1 as the base and we pick it up after the installer has worked its magic. Ensure you install on LVM2 which will let you snapshot your root volume and revert any unwanted changes, and to create consistent backups which you can store separately.

It's assumed that you can now remotely log in via ssh and elevate your privileges via sudo.

Let's first take a look at the storage configuration on the physical host:
  
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
gsw@goat-lin:~$ sudo pvs
  PV         VG        Fmt  Attr PSize   PFree
  /dev/sda3  ubuntu-vg lvm2 a--  110.79g 106.79g
gsw@goat-lin:~$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               ubuntu-vg
  PV Size               110.79 GiB / not usable 0
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              28363
  Free PE               27339
  Allocated PE          1024
  PV UUID               zA096A-SzzE-y732-54Pl-hYRu-TaDB-ANVGD2
gsw@goat-lin:~$ sudo lvs
  LV        VG        Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  ubuntu-lv ubuntu-vg -wi-ao---- 4.00g

  
gsw@goat-lin:~$ sudo pvs
  PV         VG        Fmt  Attr PSize   PFree
  /dev/sda3  ubuntu-vg lvm2 a--  110.79g 106.79g

gsw@goat-lin:~$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               ubuntu-vg
  PV Size               110.79 GiB / not usable 0
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              28363
  Free PE               27339
  Allocated PE          1024
  PV UUID               zA096A-SzzE-y732-54Pl-hYRu-TaDB-ANVGD2

gsw@goat-lin:~$ sudo lvs
  LV        VG        Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  ubuntu-lv ubuntu-vg -wi-ao---- 4.00g

We can see that we have a 110GB physical disk and an initial volume of 4GB. Whilst this will be enough for the initial setup I'd recommend you use 10GB to avoid resizing later. The small size helped to speed up some of the backup process which is explored in a later post.

Now let's try out a simple volume snapshot. We can see the snapshot has been created and can mount it to inspect the filesystem contents.

?

  
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
gsw@goat-lin:~$ sudo lvcreate -s -L 4GB -n snap_test ubuntu-vg/ubuntu-lv
  Using default stripesize 64.00 KiB.
  Logical volume "snap_test" created.
gsw@goat-lin:~$ sudo lvs
  LV        VG        Attr       LSize Pool Origin    Data%  Meta%  Move Log Cpy%Sync Convert
  snap_test ubuntu-vg swi-a-s--- 4.00g      ubuntu-lv 0.02
  ubuntu-lv ubuntu-vg owi-aos--- 4.00g
sudo mount /dev/mapper/ubuntu--vg-snap_test /mnt/test/
gsw@goat-lin:~$ cd /mnt/test/
gsw@goat-lin:/mnt/test$ ls
bin   dev  home        initrd.img.old  lib64       media  opt   root  sbin  srv  tmp  var      vmlinuz.old
boot  etc  initrd.img  lib             lost+found  mnt    proc  run   snap  sys  usr  vmlinuz

  
gsw@goat-lin:~$ sudo lvcreate -s -L 4GB -n snap_test ubuntu-vg/ubuntu-lv
  Using default stripesize 64.00 KiB.
  Logical volume "snap_test" created.


gsw@goat-lin:~$ sudo lvs
  LV        VG        Attr       LSize Pool Origin    Data%  Meta%  Move Log Cpy%Sync Convert
  snap_test ubuntu-vg swi-a-s--- 4.00g      ubuntu-lv 0.02
  ubuntu-lv ubuntu-vg owi-aos--- 4.00g

sudo mount /dev/mapper/ubuntu--vg-snap_test /mnt/test/

gsw@goat-lin:~$ cd /mnt/test/
gsw@goat-lin:/mnt/test$ ls
bin   dev  home        initrd.img.old  lib64       media  opt   root  sbin  srv  tmp  var      vmlinuz.old
boot  etc  initrd.img  lib             lost+found  mnt    proc  run   snap  sys  usr  vmlinuz
Now that we have a mechanism to roll back unwanted changes, we can forge ahead with adding some new packages. Don't forget that you can also revert your snapshots with lvconvert --merge ubuntu-vg/snap_test. However, you may find that keeping your snapshots on the server's local disk can consume space over time, or can be lost if there's a failure or rebuild so we look at an alternative approach next. I create an NFS export/share on my NAS and make it available to the server. I can use this to store disk images on more durable storage and can retrieve them whenever I need them down the track. Here we install the nfs client package and update /etc/fstab to mount the share on boot.
?

  
1
2
3
4
5
6
sudo apt-get install nfs-common
gsw@goat-lin:/mnt$ cat /etc/fstab
UUID=af8bea23-be3c-11e8-bc12-1c6f65319988 / ext4 defaults 0 0
UUID=af8bea22-be3c-11e8-bc12-1c6f65319988 /boot ext4 defaults 0 0
192.168.1.2:/volume2/Scratch /mnt/scratch nfs auto,noatime,nolock,bg,nfsvers=4,intr,tcp,actimeo=1800 0 0

  
sudo apt-get install nfs-common

gsw@goat-lin:/mnt$ cat /etc/fstab
UUID=af8bea23-be3c-11e8-bc12-1c6f65319988 / ext4 defaults 0 0
UUID=af8bea22-be3c-11e8-bc12-1c6f65319988 /boot ext4 defaults 0 0
192.168.1.2:/volume2/Scratch /mnt/scratch nfs auto,noatime,nolock,bg,nfsvers=4,intr,tcp,actimeo=1800 0 0
Next, we identify the device containing our snapshot and back it up using dd to the NFS mount.
?

  
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
gsw@goat-lin:/mnt$ lsblk
NAME                           MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0                            7:0    0  87.9M  1 loop /snap/core/5328
loop1                            7:1    0  86.9M  1 loop /snap/core/4917
sda                              8:0    0 111.8G  0 disk
├─sda1                           8:1    0     1M  0 part
├─sda2                           8:2    0     1G  0 part /boot
└─sda3                           8:3    0 110.8G  0 part
  ├─ubuntu--vg-ubuntu--lv-real 253:0    0     4G  0 lvm
  │ ├─ubuntu--vg-ubuntu--lv    253:2    0     4G  0 lvm  /
  │ └─ubuntu--vg-snap_test     253:3    0     4G  0 lvm
  └─ubuntu--vg-snap_test-cow   253:1    0     4G  0 lvm
    └─ubuntu--vg-snap_test     253:3    0     4G  0 lvm
gsw@goat-lin:/mnt/scratch$ sudo dd if=/dev/mapper/ubuntu--vg-snap_test bs=64K conv=noerror,sync status=progress | gzip -c  > /mnt/scratch/goat-lin/base_install.20180923.img.gz
4250075136 bytes (4.3 GB, 4.0 GiB) copied, 95 s, 44.7 MB/s
65536+0 records in
65536+0 records out
4294967296 bytes (4.3 GB, 4.0 GiB) copied, 95.6187 s, 44.9 MB/s

  
gsw@goat-lin:/mnt$ lsblk
NAME                           MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0                            7:0    0  87.9M  1 loop /snap/core/5328
loop1                            7:1    0  86.9M  1 loop /snap/core/4917
sda                              8:0    0 111.8G  0 disk
├─sda1                           8:1    0     1M  0 part
├─sda2                           8:2    0     1G  0 part /boot
└─sda3                           8:3    0 110.8G  0 part
  ├─ubuntu--vg-ubuntu--lv-real 253:0    0     4G  0 lvm
  │ ├─ubuntu--vg-ubuntu--lv    253:2    0     4G  0 lvm  /
  │ └─ubuntu--vg-snap_test     253:3    0     4G  0 lvm
  └─ubuntu--vg-snap_test-cow   253:1    0     4G  0 lvm
    └─ubuntu--vg-snap_test     253:3    0     4G  0 lvm

gsw@goat-lin:/mnt/scratch$ sudo dd if=/dev/mapper/ubuntu--vg-snap_test bs=64K conv=noerror,sync status=progress | gzip -c  > /mnt/scratch/goat-lin/base_install.20180923.img.gz
4250075136 bytes (4.3 GB, 4.0 GiB) copied, 95 s, 44.7 MB/s
65536+0 records in
65536+0 records out
4294967296 bytes (4.3 GB, 4.0 GiB) copied, 95.6187 s, 44.9 MB/s
With the backup created, we can now create a new volume to restore the data.
?

  
1
2
3
4
5
6
7
gsw@goat-lin:/mnt/scratch$ sudo lvcreate -L 4GB -n restore_test ubuntu-vg
  Logical volume "restore_test" created.
gsw@goat-lin:/mnt/scratch$ sudo lvs
  LV           VG        Attr       LSize Pool Origin    Data%  Meta%  Move Log Cpy%Sync Convert
  restore_test ubuntu-vg -wi-a----- 4.00g
  snap_test    ubuntu-vg swi-a-s--- 4.00g      ubuntu-lv 0.64
  ubuntu-lv    ubuntu-vg owi-aos--- 4.00g

  
gsw@goat-lin:/mnt/scratch$ sudo lvcreate -L 4GB -n restore_test ubuntu-vg
  Logical volume "restore_test" created.
gsw@goat-lin:/mnt/scratch$ sudo lvs
  LV           VG        Attr       LSize Pool Origin    Data%  Meta%  Move Log Cpy%Sync Convert
  restore_test ubuntu-vg -wi-a----- 4.00g
  snap_test    ubuntu-vg swi-a-s--- 4.00g      ubuntu-lv 0.64
  ubuntu-lv    ubuntu-vg owi-aos--- 4.00g
The volume is created, so time to invoke the restore, again using dd.
?

  
01
02
03
04
05
06
07
08
09
10
11
gsw@goat-lin:/mnt/scratch$ sudo gunzip -c /mnt/scratch/goat-lin/base_install.20180923.img.gz | sudo dd of=/dev/mapper/ubuntu--vg-restore_test status=progress
4289700352 bytes (4.3 GB, 4.0 GiB) copied, 199 s, 21.5 MB/s
8388608+0 records in
8388608+0 records out
4294967296 bytes (4.3 GB, 4.0 GiB) copied, 200.069 s, 21.5 MB/s
gsw@goat-lin:/mnt/scratch$ sudo mount /dev/mapper/ubuntu--vg-restore_test /mnt/test/
gsw@goat-lin:/mnt/scratch$ ls /mnt/test/
bin   dev  home        initrd.img.old  lib64       media  opt   root  sbin  srv  tmp  var      vmlinuz.old
boot  etc  initrd.img  lib             lost+found  mnt    proc  run   snap  sys  usr  vmlinuz

  
gsw@goat-lin:/mnt/scratch$ sudo gunzip -c /mnt/scratch/goat-lin/base_install.20180923.img.gz | sudo dd of=/dev/mapper/ubuntu--vg-restore_test status=progress
4289700352 bytes (4.3 GB, 4.0 GiB) copied, 199 s, 21.5 MB/s
8388608+0 records in
8388608+0 records out
4294967296 bytes (4.3 GB, 4.0 GiB) copied, 200.069 s, 21.5 MB/s


gsw@goat-lin:/mnt/scratch$ sudo mount /dev/mapper/ubuntu--vg-restore_test /mnt/test/
gsw@goat-lin:/mnt/scratch$ ls /mnt/test/
bin   dev  home        initrd.img.old  lib64       media  opt   root  sbin  srv  tmp  var      vmlinuz.old
boot  etc  initrd.img  lib             lost+found  mnt    proc  run   snap  sys  usr  vmlinuz

The backup and restore process appears to work. It's worth noting sometimes you will need to update your package management config (main, universe, restricted, multiverse) in order to see some packages that may be required. Here's how you can accomplish this:
?

  
1
2
3
4
gsw@goat-lin:~$ sudo add-apt-repository main
'main' distribution component is already enabled for all sources.
gsw@goat-lin:~$ sudo add-apt-repository universe
'universe' distribution component enabled for all sources.

  
gsw@goat-lin:~$ sudo add-apt-repository main
'main' distribution component is already enabled for all sources.
gsw@goat-lin:~$ sudo add-apt-repository universe
'universe' distribution component enabled for all sources.
Finally, double check your installation is using the right timezone. There are a few ways of doing this via the installer, but if you skipped that step you can simply:
?

  
1
gsw@goat-lin:/etc/xen-tools$ sudo ln -sf /usr/share/zoneinfo/Australia/Melbourne /etc/localtime

  
gsw@goat-lin:/etc/xen-tools$ sudo ln -sf /usr/share/zoneinfo/Australia/Melbourne /etc/localtime

No comments:

Post a Comment