Mounting KVM images manually


I built an ubuntu Server image this week because I needed to test an application we built at work would work without X.org and gtk+ installed.  Turns out it does.  That's good.

But in testing I got annoyed with the 80 character width of the console.  See, Ubuntu Server doesn't have a desktop.  You can remotely login to it of course and that eliminates the console from the equation.  But using the console via virt-manager leaves me at 80 characters.  So I started to dig around to see if I could bump this to 128 characters.

A long time back I remember one distro that would let you do that.  Can't remember now which one or how they did it.  But all the info for Ubuntu focuses on telling grub to use a different display resolution.  Of course, this doesn't work because the display resolution getting higher doesn't mean the text is displayed any smaller in relationship to the display size.  There were lots of other options related to changing the font for the console, all of which changed the font size but not the number of characters that could be displayed.  Eventually I gave up and just use ssh access to skip the console for that VM.

However, along the way, I tried change the console resolution using Grub in a couple different ways.  The thing is, if you use in Grub configs and the emulator (qemu) doesn't support it you get a black screen.  You can fix this by hitting ESC when the VM reboots to get the grub menu list but it took me awhile to realize it was ESC I needed to hit.  Once you do this there is a rescue mode that will let you rebuild the grub configuration.

Before I knew this I thought the only way to fix the console mode was to mount the kvm image and edit the file (you still have to rebuild the grub config but at least now you can edit the file) and run update-grub from a chroot.  Turns out you can edit the file, quite easily.  Since it's so easy I'll just copy the commands from Dustin Kirkland's post on the same thing:

# losetup /dev/loop0 foo.img
# kpartx -av /dev/loop0
# mount /dev/mapper/loop0p1 /mnt

# unmount /mnt
# kpartx -dv /dev/loop0
# losetup -d /dev/loop0

And that's it.  So if you trash your boot on your KVM image you can still fix it manually.  Well, mostly.  I suppose you could do some clever tricks using chroot with this too but my only attempt at running update-grub in a chroot of this image failed, so I'm not sure that really works.  In the end, I needed to use the rescue mode from the Ubuntu Server grub menu to rerun update-grub and allow the system to work again.

Anyway, just being able to access the files to fix the config was enough to save me.  Thanks Dustin!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.