Machine/template-focal

From Sugar Labs
Jump to navigation Jump to search

VM Creation (host part)

virt-install -v --accelerate --nographics -x console=ttyS0,115200 \
--name template-focal --vcpus=3 --ram $((1 * 1024)) \
--os-type=linux --os-variant=ubuntu18.04 --network bridge:br0 \
--disk path=/var/lib/libvirt/images/boot/template-focal-boot.img,bus=virtio,size=0.25,format=raw \
--disk path=/dev/justice/template-focal-root,bus=virtio,size=10 \
--location http://ubuntu.media.mit.edu/ubuntu/dists/focal/main/installer-amd64/


Obs: format=raw is mandatory, otherwise qcow2 format will be used by default. raw format allows us to easily create device mappings for the image.


The new VM will boot into the installer. Answer all questions with the defaults, except:

  1. Hostname: template-focal
  2. Mirror: enter information manually
  3. Mirror hostname: ubuntu.media.mit.edu
  4. (create your user with a strong password and no encrypted home)
  5. Partitioning: manual (see Partitioning below)
  6. Automatically install security updates
  7. Software selection:
    • Basic Ubuntu Server
    • OpenSSH server
  8. GRUB: let the installer setup grub on /dev/vda (which contains /boot)


Switch the root filesystem to an LV

When the machine is offline, go to the host to recreate the root filesystem directly as an LV (as opposed to a partitioned volume)

First of all, we need to set up the device mapping for the first and only partition where the root filesystem resides.

kpartx -av  /dev/justice/template-focal-root

Mount the root partition:

mkdir /mnt/template-focal-root
mount /dev/mapper/justice-template--focal-root1 /mnt/template-focal-root

Now create and format a new LV:

 lvcreate -L 10G -n template-focal-root2 justice
 mkfs.ext4 -L template-focal -O flex_bg,extent,uninit_bg,sparse_super /dev/justice/template-focal-root2
 tune2fs -c -1 -i 0 /dev/justice/template-focal-root2
 mkdir /mnt/template-focal-root2
 mount /dev/justice/template-focal-root2 /mnt/template-focal-root2

Move the files over:

 rsync -HAXphax --numeric-ids /mnt/template-focal-root/ /mnt/template-focal-root2/

NOTE: By default, Ubuntu 20.04 uses UUID in /etc/fstab in order to mount partitions. Since we have changed the root partition to a new disk, the UUID will change. Aside from that, the grub.cfg also specifies the location of the root filesystem using UUID notation (ex: /vmlinuz-4.4.0-89-generic root=UUID=0ad5d004-e5dd-4b93-abe4-2bb0ba4fd94a).

Before we umount the filesystems, let's create a chroot environment and fix previous issues:

 kpartx -av /var/lib/libvirt/images/boot/template-focal-boot.img
 mount /dev/mapper/loop0 /mnt/template-focal-root2/boot
 mount --bind /dev/ /mnt/template-focal-root2/dev/
 mount -t proc proc /mnt/template-focal-root2/proc/
 mount -t sysfs sys /mnt/template-focal-root2/sys/
 chroot /mnt/template-focal-root2/

Once inside the chroot environment:

  • Fix serial console access by making getty listen on /dev/ttyS0:
 systemctl enable serial-getty@ttyS0.service
  • Replace UUID with device name for root fs location inside /boot/grub/grub.cfg
 sed -i -r "s/root=UUID=[0-9a-f-]+/root=\/dev\/vdb/" /boot/grub/grub.cfg
  • Adjust /etc/fstab to mount the filesystems from "LABEL=boot" and "LABEL=template-focal".


Finally (VERY IMPORTANT), umount all filesystems before starting the VM:

 umount /mnt/template-focal-root2/boot/
 umount /mnt/template-focal-root2/dev/
 umount /mnt/template-focal-root2/proc/
 umount /mnt/template-focal-root2/sys/
 umount /mnt/template-focal-root2/ /mnt/template-focal-root/

Get rid of the old root and rename the new one on top of it

 kpartx -d /var/lib/libvirt/images/boot/template-focal-boot.img
 lvremove /dev/justice/template-focal-root
 lvrename justice template-focal-root2 template-focal-root

Partitioning

The goal is to have a small disk file for the MBR and /boot, and a larger raw filesystem in an LVM Logical Volume. We don't want the LV to be partitioned because this makes it harder to resize, mount, etc.

Now create a partition table in the smallest disk (256MB) and create a single partition in it. Format this partition as ext4, labeled "boot" and mounted as /boot.

The installer won't let you format the entire disk as a filesystem, so go ahead and partition the 10GB disk too, then create a primary partition in it and format it as ext4, mounted as / and labeled "template-focal" ("template-focal-root" would exceed the ext4 limit).

Preferably, when asked for size, remove the contents and replace it with 'max'

And yes.. just in case you're wondering. We don't use swap partitions.

We'll have to fix the disk later.