Installing grub2 manually under CentOS 7
This is a long posting, generally for those with expert level grub/os install experience. I am moving my project from CentOS 6 to CentOS 7 and need to deal with the changes in the boot requirements introduced by CentOS 7, particularly the upgrade from grub to grub2. The gist of the problem is this:
In my CentOS 6 based work, I had my hard drive partitioned into various slices that each hosted a VM, installed with virt-install. I used a pre-created CentOS image that I could untar directly into the partition that was hosting the VM. Each VM's slice was partitioned into boot, swap, and rootfs partitions. I ran the required grub install commands to make the VM bootable.
In CentOS 7, things have changed a bit. When grub2 is installed on a GPT drive it requires an additional "bios_grub" partition. Let's assume I am installing a VM into /dev/sda4 of my hard drive. I've distilled my process down to the following set of commands:
-- Prepare the disk partition to host a VM. The VM has four partitions, one for grub, one for /boot, one for swap, and one for the main root file system.
# parted -s /dev/sda4 mklabel gpt
# parted -s /dev/sda4 mkpart primary ext3 1049kB 2097kB
# parted -s /dev/sda4 mkpart primary ext3 2MiB 130MiB
# parted -s /dev/sda4 mkpart primary linux-swap 130MiB 146MiB
# parted -s /dev/sda4 mkpart primary ext4 146MiB 100%
# parted -s /dev/sda4 set 1 bios_grub on
# parted -s /dev/sda4 set 2 boot on
# parted -s /dev/sda4 print
Model: Unknown (unknown)
Disk /dev/sda4: 75.2GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 2097kB 1049kB primary bios_grub
2 2097kB 136MB 134MB ext3 primary boot
3 136MB 153MB 16.8MB linux-swap(v1) primary
4 153MB 75.2GB 75.0GB ext4 primary
-- Now install the vm image
# kpartx -av /dev/sda4
add map sda4p1 (253:0): 0 2048 linear /dev/sda4 2048
add map sda4p2 (253:1): 0 262144 linear /dev/sda4 4096
add map sda4p3 (253:2): 0 32768 linear /dev/sda4 266240
add map sda4p4 (253:4): 0 146499584 linear /dev/sda4 299008
# mkfs.ext3 /dev/mapper/sda4p1
# mkfs.ext3 /dev/mapper/sda4p2
# mkswap /dev/mapper/sda4p3
# mkfs.ext4 /dev/mapper/sda4p4
# mkdir /mnt/rootfs
# mount /dev/mapper/sda4p4 /mnt/rootfs
# mkdir /mnt/rootfs/boot
# mount /dev/mapper/sda4p2 /mnt/rootfs/boot
# tar zxf vm-template.tgz -C /mnt/rootfs
# mount -t devtmpfs /dev /mnt/rootfs/dev
# mount -t proc /proc /mnt/rootfs/proc
# mount -t sysfs /sys /mnt/rootfs/sys
-- Next, install grub2
# chroot /mnt/rootfs grub2-install --force /dev/sda4
Installing for i386-pc platform.
Installation finished. No error reported.
# chroot /mnt/rootfs grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-229.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-229.el7.x86_64.img
done
-- Umount everything
# umount /mnt/rootfs/sys
# umount /mnt/rootfs/proc
# umount /mnt/rootfs/dev
# umount /mnt/rootfs/boot
# umount /mnt/rootfs
# kpartx -dv /dev/sda4
-- And finally create my VM...
# virt-install --connect=qemu:///system --network bridge=br0,mac=RANDOM,model=virtio --name=vm-00 --disk path=/dev/sda4,bus=virtio --force --vcpus=1 --ram=1024 --check-cpu --accelerate --hvm --nographics --noreboot --import
Starting install...
Creating domain...
Domain creation completed. You can restart your domain by running:
virsh --connect qemu:///system start vm-00
Alas, at this point, the VM does start, but it immediately hangs. The BIOS does not make the transition to grub to boot CentOS. The same basic approach works for CentOS 6 so I assume I am missing some key step here, most likely related to how I am installing grub2. If anyone is experienced in this area and has any advice, I'd appreciate some input.
Peter
|