Thursday, January 22, 2015

Virsh

We all know how fun brain dumps are to read, so move along, nothing to see here.

Today was my second day arguing with learning virsh. Here's what I've learned so far (with some bruises).

Basic virsh

reference

You'll need to install various tools, but that should be readily search-able. I had the tools and some existing VMs as my starting point, but I had a lot to learn.

Create (build) a new VM

  • define (export) NEWVM, OLDVM, HOST, DOMAIN, and ME (assuming your VG, LV, and VM naming scheme matches mine; otherwise, watch out!)
  • sudo lvcreate -n vm_${NEWVM} -L 8192M /dev/vg_${HOST} # from http://virt-tools.org/learning/install-with-command-line/
  • The rest of this is mostly http://manpages.ubuntu.com/manpages/trusty/man1/virt-builder.1.html
  • virt-builder --list
  • virt-builder --notes ubuntu-14.04
  • From that, I learned that I want to add --firstboot-command "dpkg-reconfigure openssh-server" to virt-builder.
  • Create a file with your desired initial root password. Mine was /tmp/password.
  • virt-builder ubuntu-14.04 -o /dev/vg_${HOST}/lv_${NEWVM} --firstboot-command "dpkg-reconfigure openssh-server" --root-password file:/tmp/passwd --hostname ${NEWVM}.${DOMAIN}
  • Now I leverage that I already had some working VMs of the same OS type and version:
  • virsh dumpxml ${OLDVM} | sed 's/${OLDVM}/${NEWVM}/g' | grep -v "mac address\|uuid" > ${NEWVM}.xml
  • virsh define ${NEWVM}.xml # persistent VM
  • virsh start ${NEWVM}
  • Alternatively, 'virsh create ${NEWVM}.xml' is a transient VM that goes away when shutdown
  • Very awesomely, console access was available by default! If it weren't, you'll want to configure it.
  • virsh console ${NEWVM} # log in as root with the password set in the file
  • useradd -G sudo -s /bin/bash -d /home/${ME} -m -c "Your Name" ${ME}
  • passwd ${ME} # set your password
  • If console doesn't work, you'll need to use some brute force.
    • virsh domiflist ${NEWVM}
    • ipv6calc --action prefixmac2ipv6 --in prefix+mac --out ipv6addr fe80:: [that MAC]
    • ssh -l root [that IP]
  • Add DNS records for the new VM.
  • When you're done, virsh shutdown ${NEWVM}

Clone an Ubuntu VM

  • Read https://help.ubuntu.com/community/KVM/Virsh.
  • virsh list --all
  • virsh shutdown ${OLDVM}
  • virsh list --all # verify that shutdown completed
  • sudo virt-clone --original ${OLDVM} --name ${NEWVM} --prompt # first time, or
  • sudo virt-clone --original ${OLDVM} --name ${NEWVM} -m [previous MAC] --prompt # subsequently, if you're learning by breaking as I did
    • answer /dev/vg_${HOST}/vm_${NEWVM} to prompt if you named yours like mine
  • virsh list --all
  • virsh start ${OLDVM}
  • sudo virt-sysprep --hostname ${NEWVM}.${DOMAIN} --enable cron-spool,dhcp-client-state,dhcp-server-state,logfiles,mail-spool,random-seed,ssh-hostkeys,yum-uuid -d ${NEWVM}
  • virsh start ${NEWVM}
  • The clone has my user account and other niceties, but I don't know its address. Luckily I can use available information and link-local IPv6.
  • virsh domiflist ${NEWVM}
  • ipv6calc --action prefixmac2ipv6 --in prefix+mac --out ipv6addr fe80:: [that MAC]
  • ssh [replace with that IPv6 address]%br0 # replace with your local network interface after '%'
    • sudo dpkg-reconfigure openssh-server # if you get ssh errors and have to use console
    • grep "${OLDVM}\|${NEWVM}" /etc/hostname # always verify!!!
    • sudo sed -i 's/${OLDVM}/${NEWVM}/g' /etc/hostname
    • grep "${OLDVM}\|${NEWVM}" /etc/hostname # and check my work!
  • virsh reboot ${NEWVM}
  • Add DNS records for the new VM.
  • virsh shutdown ${NEWVM} # when you're done

Clone a CentOS VM

  • Read https://help.ubuntu.com/community/KVM/Virsh. This was a CentOS 7 clone.
  • virsh list --all
  • virsh shutdown ${OLDVM}
  • virsh list --all # verify that shutdown completed
  • sudo virt-clone --original ${OLDVM} --name ${NEWVM} --prompt # first time, or
  • sudo virt-clone --original ${OLDVM} --name ${NEWVM} -m [previous MAC] --prompt # subsequently, if you're learning by breaking as I did
    • answer /dev/vg_${HOST}/vm_${NEWVM} to prompt if you named yours like mine
  • virsh list --all
  • virsh start ${OLDVM}
  • sudo virt-sysprep --hostname ${NEWVM}.${DOMAIN} --enable cron-spool,dhcp-client-state,dhcp-server-state,logfiles,mail-spool,random-seed,ssh-hostkeys,yum-uuid -d ${NEWVM}
  • virsh start ${NEWVM}
  • The clone has my user account and other niceties, but I don't know its address. Luckily I can use available information and link-local IPv6.
  • virsh domiflist ${NEWVM}
  • ipv6calc --action prefixmac2ipv6 --in prefix+mac --out ipv6addr fe80:: [that MAC]
  • ssh [replace with that IPv6 address]%br0 # replace with your local network interface after '%'
    • grep "${OLDVM}\|${NEWVM}" /etc/hostname # always verify!!!
    • sudo sed -i 's/${OLDVM}/${NEWVM}/g' /etc/hostname
    • grep "${OLDVM}\|${NEWVM}" /etc/hostname # and check my work!
    • sudo vgrename centos_${OLDVM} centos_${NEWVM} # or use lvm, see below
    • grep "${OLDVM}\|${NEWVM}" /etc/fstab
    • sudo sed -i 's/${OLDVM}/${NEWVM}/g' /etc/fstab # only needed if vgrename used
    • grep "${OLDVM}\|${NEWVM}" /etc/fstab
    • grep "${OLDVM}\|${NEWVM}" /etc/default/grub
    • sudo sed -i 's/${OLDVM}/${NEWVM}/g' /etc/default/grub # only needed if vgrename used
    • Now is a good time to add the RHEL7 console tips below!
    • grep "${OLDVM}\|${NEWVM}" /etc/default/grub
    • sudo grub2-mkconfig -o /boot/grub2/grub.cfg
    • grep "${OLDVM}\|${NEWVM}" /boot/grub2/grub.cfg
    • exit # log out of ${NEWVM} now
  • virsh reboot ${NEWVM}
  • Add DNS records for the new VM.
  • virsh shutdown ${NEWVM} # when you're done

Enable CentOS Console Access

Typically, you don't care about console access until it's the only way to get out of trouble. So enable it right away if it isn't working yet. Check with 'virsh console ${NEWVM}' and press return at least once to see if you get a login prompt.

The directions for RHEL6 are https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Virtualization_Administration_Guide/sect-Virtualization-Troubleshooting_-Troubleshooting_with_serial_consoles.html. Edit the /boot/grub/grub.conf file to append this to the kernel line: console=tty0 console=ttyS0,115200

The directions for RHEL7 are http://www.daemonzone.net/e/17/, to add the following lines to /etc/default/grub:

GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=ttyS0,115200n8"
GRUB_TERMINAL=serial
GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"
Since mine already had GRUB_CMDLINE_LINUX (to apply it to all the menu entries), I just appended " console=tty0 console=ttyS0,115200n8" to that line inside the quotes, then added the next two lines. Next you need to rebuild the grub.cfg file:
sudo grub2-mkconfig -o /boot/grub2/grub.cfg

LVM

[${ME}@${NEWVM} ~]$ sudo lvm
lvm> pvdisplay
lvm> vgrename centos_${OLDVM} centos_${NEWVM}
  Volume group "centos_${OLDVM}" successfully renamed to "centos_${NEWVM}"
lvm> pvdisplay
lvm> lvdisplay
lvm> exit

Loose Ends

sudo virt-inspector -d ${NEWVM}
sudo virt-filesystems -d ${NEWVM}
sudo virt-df -d ${NEWVM}
sudo virt-df
sudo virt-edit -d ${BrokenGuest} /boot/grub2/grub.cfg
sudo virt-rescue -d ${BrokenGuest}

No comments:

Post a Comment