Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm writing a script that takes a master KVM image, converts it to VDI, makes a few changes to it, and offers it up for download.

I need to update the kernel boot parameters for some of the newly created images. Normally one would edit the GRUB_CMDLINE_LINUX_DEFAULT setting in /etc/default/grub, and then run update-grub. Of course, I can only do this in a chroot, which fails: '/usr/sbin/grub-probe: error: cannot find a device for / (is /dev mounted?).'

Bind mounting /dev from the running system into the chroot allows update-grub to run, but results in a broken grub.cfg (wrong root device from the image's perspective).

If I just use sed to add the kernel parameters I need, it will get the image booting, but subsequent runs of update-grub (from inside the now-running image) will cause trouble.

So my current solution is:

  1. edit parameters in /etc/default/grub
  2. bind mount /dev into the chroot
  3. run update-grub in the chroot
  4. use sed to fix erroneous device nodes in /boot/grub/grub.cfg

Please tell me there is a better way.

share|improve this question

1 Answer 1

The correct answer is to mount /dev into the chroot, and use a $CHROOT/boot/grub/device.map file to tell grub that (hd0) (from the image's perspective) is actually the loopback file that you've "partitioned" (and then mounted into your chroot with kpartx et c).

# generate grub configs and install it to the generated blockdev
chroot $MR update-grub 2> /dev/null
chroot $MR grub-mkconfig -o /boot/grub/grub.cfg 2> /dev/null
cat > $MR/boot/grub/device.map <<EOF
(hd0)   ${LOOPDEV}
EOF
chroot $MR grub-install ${LOOPDEV} 2> /dev/null

After you're done installing grub to the "mbr" of the loopdev, you should rm the device.map file because it won't be accurate from the image's perspective once it actually boots.

I have a script that builds complete qcow2 images for ubuntu 13.10 saucy x64 here:

https://github.com/sneak/kvm-ubuntu-imagebuilder/blob/master/buildimage.sh#L211

Good luck.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.