User Tools

Site Tools


computing:vmserver

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
computing:vmserver [2021/11/08 06:54] oemb1905computing:vmserver [2024/02/17 20:45] oemb1905
Line 7: Line 7:
 ------------------------------------------- -------------------------------------------
  
-[Spoiler Alert: I changed the initial setup!  Read below to skip the pain!]+//vmserver//      
  
-I have a dual 8-core Xeon SuperMicro server, with 8 HD bays in use, 96GBRAM, SAS to SATA SCU for the hard drives.  I calculated that for a 500W PSU, that the RAM would be around 360W at capacity but rarely hit that or even close, that the HDs would often (especially on boot) hit up to 21.3W per drive, or around 150W excluding the boot SSD volume.  The motherboard would be 100W, putting me at 610W.  This is over, however, I don't expect all the RAM and all the HDs to reach peak, even on boot.  After testing and confirming it worked, I went on to OS and setup.  I installed Debian on the 120GB SSD, and as for the other 7 drives, my initial idea was to do LUKS first, then zfs, meaning 6 could be mirrors in zfs and I would keep 1 as a spare LUKS crypt for keys, other crap, etc.  To create the LUKS crypts, I did the following 6 times, each time appending the last 4 digits of the block ID to the LUKS crypt name:+-------------------------------------------
  
-  cryptsetup luksFormat /dev/sda +I am currently running a Supermicro 6028U-TRTP+ wDual 12-core Xeon E5-2650 at 2.2Ghz, 384GB RAM, with four two-way mirrors of Samsung enterprise SSDs for the primary vdev, and two two-way mirrors of 16TB platters for the backup vdev. All drives using SAS. I am using a 500W PSU. I determine the RAM would be about 5-10W a stick, the mobo about 100Wand the drives would consume most of the rest at roughly 18-22W per driveThe next step was to install Debian on the bare metal to control and manage the virtualization environment. The virtualization stack is virsh and kvm/qemu. As for the file system and drive formatting, used luks and pam_mount to open an encrypted home partition and mapped home directory. I use this encrypted home directory to store keys for the zfs pool and/or other sensitive data, thus protecting them behind FDE. Additionally, I create file-level encrypted zfs data sets within each of the vdevs that are unlocked by the keys on the LUKS home partition. Instead of tracking each UUID down on your initial build, do the following:
-  cryptsetup luksOpen /dev/sda sdafc11 +
-   +
-You then make sure to use the LUKS label names when making the zpoolnot the short nameswhich can change at times during reboots did this as follows:+
  
-  sudo apt install zfs-utils +  zpool create -m /mnt/pool pool -f mirror sda sdb mirror sdc sdh mirror sde sdf mirror sdg sdh 
-  zpool create -m /mnt/vms vms -f mirror sdafc11 sdb9322 mirror sdc8a33 sdh6444 mirror sde5b55 sdf8066 +  zpool export pool 
-   +  zpool import -/dev/disk/by-id pool
-ZFS by default mount at boot.  This is a problem if you don't use auto-unlocking and key files with LUKS to also unlock on boot (and/or a custom script that unlocks).  The problem, in this use cases, is ZFS will try to mount the volumes before they are unlocked.  The two other options are none/legacy modes, both of which rely on you mounting the volume using traditional methods.  But, the whole point of using zfs finally was to not use traditional methods lol, so for that reason I investigated if there was a fix.  The closest to a fix is setting cachefile=none boot, but this a) hosed the pool once b) requires resetting, rebooting again and/or manually re-mounting the pool - either of which defeat the point.  Using key files, cache file adjustments, etc., and/or none/legacy were all no-gos for me, so in the end, I decided to tolerate that zfs would fail at boot, and that I would ''zpool import'' it afterwards.+
  
-  sudo -i +Once the pool is created, you can create your encrypted datasets. To do so, I made some unlock keys with the dd command and placed the keys in a hidden directory inside that LUKS encrypted home partition I mentioned above: 
-  screen + 
-  su user [pam_mount unlocks /home for physical host primary user and the spare 1TB vault] +  dd if=/dev/random of=/secure/area/example.key bs=1 count=32 
-  ctrl-a-d [detaches from screen]+  zfs create -o encryption=on -o keyformat=raw -o keylocation=file:///mnt/vault/example.key pool/dataset 
 + 
 +When the system reboots, the vdevs will automatically mount but the data sets won't because the LUKS keys won't be available until you mount the home partition by logging in to the user that holds the keys. For security reasons, this must be done manually or it defeats the entire purpose. So, once the administrator has logged in to the user in a screen session (remember, it is using pam_mount), they simple detach from that session and then load the keys and datasets as follows: 
 + 
 +  zfs load-key pool/dataset 
 +  zfs mount pool/dataset
      
-After unlocking my home directory and the spare 1TB vaultthe next step is to unlock each LUKS volumewhich decided simple shell script would suffice which looks like this mount-luks.sh:+If you have a lot of data setsyou can make a simple script to load them all at once, etc. Since we have zfs, it's a good idea to run some snapshots. To do that, I created small shell script with the following commands and then set it to run 4 times a day, or every 6 hours:
  
-  cryptsetup luksOpen /dev/disk/by-uuid/2702e690-...-0c4267a6fc11 sdafc11 +  DATE=date +"%Y%m%d-%H:%M:%S" 
-  cryptsetup luksOpen /dev/disk/by-uuid/e3b568ad-...-cdc5dedb9322 sdb9322 +  /usr/sbin/zfs snapshot -r pool/vm1dataset@backup_$DATE 
-  cryptsetup luksOpen /dev/disk/by-uuid/d353e727-...-e4d66a9b8a33 sdc8a33 +  /usr/sbin/zfs snapshot -r pool/vm2dataset@backup_$DATE 
-  cryptsetup luksOpen /dev/disk/by-uuid/352660ca-...-5a8beae15b44 sde5b44 +  /usr/sbin/zfs snapshot -r pool/@backup_$DATE 
-  cryptsetup luksOpen /dev/disk/by-uuid/fa1a6109-...-f46ce1cf8055 sdf8055 +  /usr/sbin/zfs snapshot pool@backup_$DATE 
-  cryptsetup luksOpen /dev/disk/by-uuid/86da0b9f-...-13bc38656466 sdh6466+ 
 +Make sure to manage your snapshots and only retain as many as you can etc., as they will impact performanceIf you need to zap all of them and start over, you can use this command: 
 + 
 +  zfs list -H -o name -t snapshot | xargs -n1 zfs destroy 
 + 
 +Off-site //full// backups are essential but they take a long time to download. For that reason, it's best to have the images as small as possible. When using ''cp'' in your workflow, make sure to specify ''--sparse=always''Before powering the virtual hard disk back up, you should run ''virt-sparsify'' on the image to free up the unused blocks on the host and that are not actually used in the VMIn order for the VM to designate those blocks as empty, ensure that you are running fstrim within the VMIf you want the ls command to show the size of the virtual disk that remains after the zeroing, you will need to run ''qemu-img create'' on it, which will create a new copy of the image without listing the ballooned size. the new purged virtual hard disk image can then be copied to a backup directory where one can compress and tarball it to further reduce its size. I use BSD tar and the pbzip2 compression which makes ridiculously small images. GNU tar glitches with the script for some reason. BSD tar can be downloaded with ''sudo apt install libarchive-tools''. I made a script to automate all of those steps for a qcow2 image. I also adapted that to work for raw images. 
 + 
 +[[https://repo.haacksnetworking.org/haacknet/haackingclub/-/blob/main/scripts/virtualmachines/vm-bu-production-QCOW-loop.sh|vm-bu-production-QCOW-loop.sh]] \\ 
 +[[https://repo.haacksnetworking.org/haacknet/haackingclub/-/blob/main/scripts/virtualmachines/vm-bu-production-RAW-loop.sh|vm-bu-production-RAW-loop.sh]]
  
-This script simply opens each LUKS crypt so long as you enter or copy/paste your HD password 6 times.  After that, one has to re-mount the pool / rebuild the quasi RAID1 mirror/logical volumes with the import command as follows once the volumes are opened:+On the off-site backup machineI originally would pull the tarballs down using a one line rsync script. I would adjust the cron timing of the rsync script to work well with when the tarballs are created. 
  
-  zpool import vms+  sudo rsync -av --log-file=/home/logs/backup-of-vm-tarballs.log --ignore-existing -e 'ssh -i /home/user/.ssh/id_rsa' root@domain.com:/backups/tarballs/ /media/user/Backups/
      
-Rebooting in this manner takes about 3-5 minutes for the hostand 2 minutes to screen into my user name, detach, and run the mount LUKS script (which also ends the script by importing the pool) The above was the original setup.  I changed that below.+Since thenI've switched to using rsnapshot to pull down the tarballs in some cases. The rsnapshot configurations can be found here:
  
--- Alternate Setup --+[[https://repo.haacksnetworking.org/haacknet/haackingclub/-/tree/main/scripts/rsnapshot|Rsnapshot Scripts]]
  
-Eventually, I ended up agreeing with a friend that it made no sense to do LUKS first because that would preclude me from rebuilding degraded pools using zfs tools.  This is because, if a drive failed, then the pool could never be imported, and thus never used without very complicated tinkering.  So, I destroyed the LUKS pools above, made a zfs pool with the same command structure but used the regular short-names only.  Then, after that, I created two datasets (zfs' name for encrypted folders inside pools/mountpoints for LVM mirrors).  The datasets each unlock by pulling a dd-generated key from the encrypted home partition on the SSD boot volume.  I set up the keys/datasets as follows: 
  
-  dd if=/dev/random of=/mnt/vault/example.key bs=1 count=32 +**** 
-  zfs create -o encryption=on -o keyformat=raw -o keylocation=file:///mnt/vault/example.key pool/dataset+ 
 +-- Network Bridge Setup VMs -- 
 + 
 +Up until now, I've covered how to provision the machines with virt-manager, how to backup the machines on the physical host, and how to pull those backups to an off-site workstation. Now I will discuss how to assign each VM an external IP. The first step is to provision the physical host with a virtual switch (wrongly called a bridge) to which VMs can connect. To do this, I kept it simple and used ''ifup'' and ''bridge-utils'' package and some manual editing in ''/etc/network/interfaces''.
      
-When you create this on the current running instance, it will also mount it for you as a courtesy, but upon reboot, you need to load the key, then mount the dataset using zfs commands.  In my case, I created three datasets (one for raw isos, one for disk images, and a last one for backup sparse tarballs).  Each one was created as follows:+  sudo apt install bridge-utils 
 +  sudo brctl addbr br0 
 +  sudo nano /etc/network/interfaces
  
-  zfs load-key pool/dataset +Now that you have added created the virtual switch, you need to reconfigure your physical host's ''/etc/network/interfaces'' file to use the switch. In my case, I used 1 IP for the host itself, and another for the switch, meaning that two ethernet cables are plugged into my physical host. I did this so that if I hose my virtual switch settings, I still have a separate connection to the box. Here's the configuration in ''interfaces'': 
-  zfs mount pool/dataset+ 
 +  #eth0  [1st physical port] 
 +  auto ent8s0g0 
 +    iface ent8s0f0 inet static 
 +    address 8.25.76.160 
 +    netmask 255.255.255.0 
 +    gateway 8.25.76.1 
 +    nameserver 8.8.8.8 
 + 
 +  #eth1 [2nd physical port] 
 +  auto enp8s0g1 
 +  iface enp8s0g1 inet manual 
 + 
 +  auto br0 
 +  iface br0 inet static 
 +    address 8.25.76.159 
 +    netmask 255.255.255.0 
 +    gateway 8.25.76.1 
 +    bridge_ports enp8s0g1 
 +    nameserver 8.8.8.8 
 +     
 +After that, either reboot or ''systemctl restart networking.service'' to make the changes current. Execute ''ip a'' and you should see both external IPs on two separate interfaces, and you should see ''br0 state UP'' in the output of the second interface ''enp8s0g1''. You should also run some ''ping 8.8.8.8'' and ''ping google.com'' tests to confirm you can route. If anyone wants to do this in a home, small business, or other non-public facing environment, you can easily use dhcp and provision the home/small business server's ''interface'' file as follows: 
 + 
 +  auto eth1 
 +  iface eth1 inet manual 
 + 
 +  auto br0 
 +  iface br0 inet dhcp 
 +        bridge_ports eth1 
 + 
 +The above home-version allows, for example, users to have a virtual machine that gets an ip address on your LAN and makes ssh/xrdp access far easier. If you have any trouble routing on the physical host, it could be that you do not have nameservers setup. If that's the case, do the following: 
 + 
 +    echo nameserver 8.8.8.8 > /etc/resolv.conf 
 +    systemctl restart networking.service 
 + 
 +Now that the virtual switch is setup, I can now provision VMs and connect them to the virtual switch ''br0'' in virt-manager. You can provision the VMs within the GUI using X passthrough, or use the command line. First, create a virtual disk to your desired size by excuting ''sudo qemu-img create -f raw new 1000G'' and then run something like this: 
 + 
 +  sudo virt-install --name=new.img \ 
 +  --os-type=Linux \ 
 +  --os-variant=debian10 \ 
 +  --vcpu=1 \ 
 +  --ram=2048 \ 
 +  --disk path=/mnt/vms/students/new.img \ 
 +  --graphics spice \ 
 +  --location=/mnt/vms/isos/debian-11.4.0-amd64-netinst.iso \ 
 +  --network bridge:br0 
 + 
 +The machine will open in virt-viewer, but if you lose the connection you can reconnect easily with: 
 + 
 +  virt-viewer --connect qemu:///system --wait new.img 
      
-Once I created all the datasetsI made a script that would load the keys and unlock all of them, then rebooted and tested it for functionality.  Upon verifying that the datasets worked, I could now feel comfortable creating VMs again, since the hard drive images for those VMs would be stored in encrypted datasets with zfs.  My next task was to create both snapshots within zfs, which would handle routine rollbacks and smaller errors/mistakes I did that by creating a small script that runs via cron 4 times a day, or every 6 hours:+Once you finish installationconfigure the guestOS interfaces file ''sudo nano /etc/network/interfaces'' with the IP you intend to assign itYou should have something like this:
  
-  DATE=`date +"%Y%m%d-%H:%M:%S" +  auto epr1 
-  /usr/sbin/zfs snapshot -r pool/vm1dataset@backup_$DATE +  iface epr1 inet static 
-  /usr/sbin/zfs snapshot -r pool/vm2dataset@backup_$DATE +    address 8.25.76.158 
-  /usr/sbin/zfs snapshot -r pool/@backup_$DATE +    netmask 255.255.255.0 
-  /usr/sbin/zfs snapshot pool@backup_$DATE+    gateway 8.25.76.1 
 +    nameservers 8.8.8.8
  
-The snapshots allow me to perform roll backs when end-users make mistakes, e.g., delete an instructional video after class session, etc., or what have you.  However, if the data center is compromised physically or their upstream goes down, I also need remote/failover optionsso my next task was to find a way to easily take advantage of cp's understanding of sparse files and tar so that I could easily use rsync to bring over tarballs of the VM disks that only utilized actual data, instead of the entire 1TB container.  To do this, I used the ''c'' and ''S'' flags in tar, together with bzip2 compression for speed, in order to provide myself remote/failover options.  I did this as follows, and take care when adjusting this script, as most alterations will break the ability of tar to properly treat the .img file as sparse+If you are creating VMs attached to a virtual switch on the smaller home/business environmentthen adjust the guest OS by executing ''sudo nano /etc/network/interfaces'' and then something like this recipe:
  
-  DATE=`date +"%Y%m%d-%H:%M:%S"` +  auto epr1 
-  cd /backups +  iface epr1 inet dhcp
-  cp -ar /vms/vol.img /backups/vol.img_QUICK_.bak +
-  bsdtar --use-compress-program=pbzip2 -Scf vol.img_QUICK_.tar.bz2 vol.img_QUICK_.bak +
-  mv /backups/vol.img_QUICK_.tar.bz2 /backups/tbs/vol.img_QUICK_$DATE.tar.bz2 +
-  rm /backups/vol.img_QUICK_.bak+
  
-In addition to daily live images using the above, script, I also run a 1/3 days version called 'SANE, which runs ''virsh shutdown domain'' before copying/tarballing and then runs ''virsh start domain'' at the end of the tarballing After these runpull the changes to offsite backup / computer using rsync on the offsite host as follows:    +If your guest OS uses Ubuntu, you will need to do extra steps to ensure that the guestOS can route. This is because Ubuntu-based distros have deprecated ''ifupdown'' in favor of ''netplan'' and disabled manual editing of ''/etc/resolv.conf''Soeither you want to learn netplan syntax and make interface changes using its YAML derivative, or you can install the optional ''resolvconf'' package to restore ''ifupdown'' functionality. To do this, adjust the VM provision script above (or use the virt-manager GUI with X passthrough) to temporarily use NAT then override Ubuntu defaults and restore ''ifupdown'' functionality as follows:
  
-  sudo rsync -av --log-file=/home/logs/backup-of-vm.log --ignore-existing -e 'ssh -/home/user/.ssh/id_rsaroot@domain.com:/backups/tarballs/ /media/user/Backups/+  sudo apt install ifupdown 
 +  sudo apt remove --purge netplan.io 
 +  sudo apt install resolvconf 
 +  sudo nano /etc/resolvconf/resolv.conf.d/tail 
 +  <nameserver 8.8.8.8> 
 +  systemctl restart networking.service 
 + 
 +You should once again execute ''ping 8.8.8.8'' and ''ping google.com'' to confirm you can route within the guest OS. Sometimes, I find a reboot is required. At this stage, you now have a physical host configured with a virtual switch, and one VM provisioned to use the switch with its own external IP. Both the physical host and guest OS in this scenario are public facing so take precautions to properly secure each by checking services ''netstat -tulpn'' and/or utilizing a firewall. The main things to configure at this point are ssh access so you no longer need to rely on the virt-viewer console which is slow. To do that, you will need to add packages (if you use the netinst.iso). To make that easy, I keep the sources.list on my primary business server:  
 + 
 +  wget https://haacksnetworking.org/sources.list 
 + 
 +Once you grab the ''sources.list'' file, install ''openssh-server'' and exchange keys, you can now use a shell to ssh into the guestOS henceforwardThis means that at this point you are now in a position to create VMs and various production environments at will or start working on the one you just created. Another thing to consider is to create base VMs that have ''interfaces'' and ''ssh'' access all ready to go, and then leverage those to make new instances using ''cp''. Alternately, you can power down a base VM and then clone it as follows: 
 + 
 +  virt-clone \ 
 +  --original=clean \ 
 +  --name=sequoia \ 
 +  --file=/mnt/vms/students/sequoia.img 
 + 
 +The purpose of this project was to create my own virtualized VPS infrastructure (using KVM and VMs), to run my own production environments and for clients, students, and family. Here's a few to check out: 
 + 
 +  * [[https://nextcloud.haacksnetworking.org|Haack's Networking - Nextcloud Talk Instance]] 
 +  * [[https://mrhaack.org|GNU/Linux Social - Mastodon Instance]] 
 +  * [[http://space.hackingclub.org|My Daughter's Space Website]] 
 +  * [[http://bianca.hackingclub.org|A Student's Pentesting Website]] 
 + 
 +That's all folks! Well ... except for one more thing. When I first did all of this, I was convinced that zfs should be within LUKS as it was difficult for me to let go of LUKS full disk encryption. I've now decided that's insane because of one primary reason. Namely, by putting zfs (or any file system) within LUKS, you lose the hot swapability that you have when zfs (or regular RAID) run directly on the hardware. That would mean that replacing a hard drive would require an entire server rebuild, which is insane. However, it is arguably more secure that way, so if budget and time permits, I've retained how I put zfs inside LUKS in the passage that follows. Proceed at your own risk lol. 
 + 
 +-- LUKS FIRST, ZFS SECOND - (LEGACY SETUP, NOT CURRENT) -- 
 + 
 +My initial idea was to do LUKS first, then zfs, meaning 6 could be mirrors in zfs and I would keep 1 as a spare LUKS crypt for keys, other crap, etc. To create the LUKS crypts, I did the following 6 times, each time appending the last 4 digits of the block ID to the LUKS crypt name: 
 + 
 +  cryptsetup luksFormat /dev/sda 
 +  cryptsetup luksOpen /dev/sda sdafc11 
 + 
 +You then make sure to use the LUKS label names when making the zpool, not the short names, which can change at times during reboots. I did this as follows: 
 + 
 +  sudo apt install zfs-utils bridge-utils 
 +  zpool create -m /mnt/vms vms -f mirror sdafc11 sdb9322 mirror sdc8a33 sdh6444 mirror sde5b55 sdf8066
      
-Since the workstation is on rsnapshotI get redundant dailies on its backup that extend beyond the quantity on the physical host (because of space on my primary workstation)+ZFS by default executes its mount commands at boot. This is a problem if you don't use auto-unlocking and key files with LUKS to also unlock on boot (and/or a custom script that unlocks). The problemin this use cases, is ZFS will try to mount the volumes before they are unlocked. The two other options are none/legacy modes, both of which rely on you mounting the volume using traditional methods. But, the whole point of using zfs finally was to not use traditional methods lol, so for that reason I investigated if there was a fix. The closest to a fix is setting cachefile=none boot, but this a) hosed the pool once b) requires resetting, rebooting again and/or manually re-mounting the pool - either of which defeat the point. Using key files, cache file adjustments, etc., and/or none/legacy were all no-gos for me, so in the end, I decided to tolerate that zfs would fail at boot, and that I would ''zpool import'' it afterwards. 
 +   
 +  sudo -i 
 +  screen 
 +  su - user [pam_mount unlocks /home for physical host primary user and the spare 1TB vault] 
 +  ctrl-a-d [detaches from screen] 
 + 
 +After unlocking my home directory and the spare 1TB vault, the next step is to unlock each LUKS volume, which I decided a simple shell script would suffice which looks like this mount-luks.sh: 
 + 
 +  cryptsetup luksOpen /dev/disk/by-uuid/2702e690-…-0c4267a6fc11 sdafc11 
 +  cryptsetup luksOpen /dev/disk/by-uuid/e3b568ad-…-cdc5dedb9322 sdb9322 
 +  cryptsetup luksOpen /dev/disk/by-uuid/d353e727-…-e4d66a9b8a33 sdc8a33 
 +  cryptsetup luksOpen /dev/disk/by-uuid/352660ca-…-5a8beae15b44 sde5b44 
 +  cryptsetup luksOpen /dev/disk/by-uuid/fa1a6109-…-f46ce1cf8055 sdf8055 
 +  cryptsetup luksOpen /dev/disk/by-uuid/86da0b9f-…-13bc38656466 sdh6466 
 + 
 +This script simply opens each LUKS crypt so long as you enter or copy/paste your HD password 6 times. After that, one has to re-mount the pool / rebuild the quasi RAID1 mirror/logical volumes with the import command as follows once the volumes are opened: 
 + 
 +  zpool import pool 
 +  
 +Rebooting in this manner takes about 3-5 minutes for the host, and 2 minutes to screen into my user name, detach, and run the mount LUKS script to mount the pools/datasets, etc. Again, I ultimately rejected this because you cannot use zfs tools when hard drives fail with this setup.
  
- --- //[[jonathan@haacksnetworking.com|oemb1905]] 2021/11/04 15:15//+ --- //[[jonathan@haacksnetworking.org|oemb1905]] 2022/11/12 12:39//
computing/vmserver.txt · Last modified: 2024/02/17 21:11 by oemb1905