Boot in recovery mode an ubuntu server KVM virtual machine from a remote pc
Why you should need to restart a VM ubuntu 12.04 server on your KVM remote server?
Simple: just miss an apparently innocuous command (in my case I needed to add a user to a group).
Then I typed the command:
1 |
sudo usermod -G mygroup myuser |
Unfortunately I forgot the “-a” option thus erasing all user groups including the group “sudo” that makes my user “sudoer”.
What to do when the only user having the login permission (unfortunately for root the login was not enabled…) cannot execute sudo commands???
the only chance that I found was to start the server in recovery mode…
So below a short guide to do the trick connecting through virt-viewer from a Desktop ubuntu pc to the remote KVM server.
1) @client: install virt-viewer if not already installed.
1 |
sudo apt-get install virt-viewer |
2) @client: generate ssh key in order to log in to the KVM server without typing the password (!!!)
1 |
ssh-keygen -t rsa |
3) @client: s-copy the generate key on the KVM server
1 |
scp .ssh/id_rsa.pub USER@KVM_SERVER_IP:KVM_SERVER_PORT:my_id_rsa.pub |
4) @server: authorize the rsa key
1 |
cat my_id_rsa.pub >> .ssh/authorized_keys |
5) @server: shutdown the guest VM to restore (virsh list for getting the correct VM_DOMAIN)
1 |
virsh shudown VM_DOMAIN |
6) @client: connect virt-viewer with the “–wait” option to the KVM server asking for VM_DOMAIN
1 |
virt-viewer --wait -c qemu+ssh://USER@KVM_SERVER_IP:KVM_SERVER_PORT/system VM_DOMAIN |
7) @server: start the VM to restore
1 |
virsh start VM_DOMAIN |
At this point, the client will launch automatically virt-viewer showing the bootloader menu of the remote VM through which start the server in recovery mode
In my case it was essential in order to log in as root and restore the groups (basically the group “sudo”) of the user.
In addition, entering as root, I encountered problems launching the command “usermod” beacuse the main disk was not correctly mounted.
Then:
1 2 3 |
mount -o remount /dev/mapper/ubuntu1204-root usermod -a -G sudo myuser reboot |