LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices


Reply
  Search this Thread
Old 07-11-2014, 05:39 PM   #1
PeterSteele
Member
 
Registered: Jun 2012
Posts: 264

Rep: Reputation: Disabled
Installing grub manually for a VM image


I am preparing a bootable image manually (via a script) for use with virt-install and the --import option. The bootable image has three main partitions, the first which will be mounted as /boot on the VM, the second as swap, and the third as / (root).

I've got the image populated with the files I need and all I have left is to install grub so that the image will be bootable. I've done some searches and I'm not entirely sure what steps are needed. Let's say my bootable image is test.img. I'm running kpartx to give me access to the partitions in my image:

# kpartx -av test.img
add map loop0p1 (253:0): 0 249967 linear /dev/loop0 34
add map loop0p2 (253:1): 0 8388608 linear /dev/loop0 251904
add map loop0p3 (253:2): 0 91357184 linear /dev/loop0 8640512

I need to get grub installed correctly into the /boot partition. I can access it after doing the kpartx command via the /dev/mapper/loop0p1 device (the real block device is actually /dev/dm-0; loop0p1 is a symbolic link). I'm just not sure which grub commands I need to make this a proper bootable image. Can anyone enlighten me as to the procedure I need to follow?

Last edited by PeterSteele; 07-11-2014 at 05:46 PM.
 
Old 07-11-2014, 08:08 PM   #2
PeterSteele
Member
 
Registered: Jun 2012
Posts: 264

Original Poster
Rep: Reputation: Disabled
I should add that I'm using CentOS 6.4/6.5, and the end result needs to be a process that can be automated.
 
Old 07-12-2014, 08:59 PM   #3
ReaperX7
LQ Guru
 
Registered: Jul 2011
Location: California
Distribution: Slackware64-15.0 Multilib
Posts: 6,558
Blog Entries: 15

Rep: Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097Reputation: 2097
Are you using Grub-1.x or Grub-2.x?
 
Old 07-12-2014, 10:46 PM   #4
PeterSteele
Member
 
Registered: Jun 2012
Posts: 264

Original Poster
Rep: Reputation: Disabled
Neither. The reports itself as being 0.97:

# grub --version
grub (GNU GRUB 0.97)

These are the basic steps I'm following:

- Step 1: Create image file that will host the VM
# dd if=/dev/zero of=test.img count=0 bs=1 seek=50GB

- Step 2: Partition the image as needed (boot, swap, root)
# parted -s test.img mklabel gpt
# parted -s test.img mkpart ext4 0 128M
# parted -s test.img mkpart swap 129M 4400MB
# parted -s test.img mkpart ext4 4400MB 100%

- Step 3: Format partitions
# kpartx -av test.img
# mkfs.ext4 /dev/dm-0
# mkfs.ext4 /dev/dm-2

- Step 4: Populate image with custom CentOS template
# mkdir -p /mnt/sysimage
# mount -o loop /dev/dm-2 /mnt/sysimage
# mkdir -o /mnt/sysimage/boot
# mount -o loop /dev/dm-0 /mnt/sysimage/boot
# tar -zxpf custom-img.tar.gz -C /mnt/sysimage

At this point I have everything completed except for the final step of creating grub.conf and preparing the image so that it will boot when launched as a VM. Assuming I get grub setup properly, I'll need to run virt-install as the final step:

# virt-install --blah --blah --import --disk path=test.img

I just can't quite figure out the grub magic to put a bow on this procedure. I've read lots of examples but nothing that works for my specific case.

Note: I have a separate procedure that I use to create my custom CentOS VM.
 
Old 07-12-2014, 11:07 PM   #5
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: Slackware, Ubuntu, PCLinux,
Posts: 10,492

Rep: Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488
Quote:
grub --version
grub (GNU GRUB 0.97)
That means it still uses Grub Legacy. According to the Archlinux site below, neither Grub Legacy nor Lilo support GPT which you are using.

https://wiki.archlinux.org/index.php...artition_Table

Quote:
GRUB Legacy and LILO do not support GPT.
 
Old 07-12-2014, 11:32 PM   #6
PeterSteele
Member
 
Registered: Jun 2012
Posts: 264

Original Poster
Rep: Reputation: Disabled
The CentOS grub support gpt just fine. If I use the CentOS kickstart facility to duplicate the steps I'm attempting to do manually, the end result is a bootable image that is configured with gpt partitions:

Code:
# parted -s test.img print
Model: Unknown (unknown)
Disk test.img: 51.2GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt

Number  Start   End     Size    File system     Name     Flags
 1      17.4kB  128MB   128MB   ext4            primary  boot
 2      129MB   4424MB  4295MB  linux-swap(v1)
 3      4424MB  51.2GB  46.8GB  ext4
So I know I can accomplish what I need to do since CentOS itself does it during an OS install. I can duplicate all of the steps except for what CentOS does to make the image bootable and the specific grub commands it runs. Pages like this describe grub well but I cannot workout what's needed in my more specialized case.
 
Old 07-13-2014, 12:55 AM   #7
PeterSteele
Member
 
Registered: Jun 2012
Posts: 264

Original Poster
Rep: Reputation: Disabled
For what its worth, the kickstart script I use to accomplish the layout I need uses a %pre section with these commands:

Code:
parted -s /dev/vda mklabel gpt
parted -s /dev/vda mkpart primary ext4 0 128
mkfs.ext4 /dev/vda1
and the main kickstart header section specifies these options:

Code:
bootloader --location=mbr --append="console=tty0 console=ttyS0,115200 selinux=0"
part /boot --onpart=vda1 
part swap  --ondrive=vda --asprimary --fstype swap --size=4096
part /     --ondrive=vda --asprimary --fstype=ext4 --size=500 --grow
The bootloader option ends up performing the exact grub install I am trying to reverse engineer.

Last edited by PeterSteele; 07-13-2014 at 12:56 AM.
 
Old 07-13-2014, 07:47 AM   #8
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: Slackware, Ubuntu, PCLinux,
Posts: 10,492

Rep: Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488Reputation: 2488
I just found a link to the CentOS forums confirming what you already knew, that CentOS 6 and later supports GPT. Just noticed that my ArchLinux site had no date on it so don't know if it was written last week or ten years ago. I'm afraid I can't help you because I've not used Red Hat or derivatives much a little with Fedora and never had much success even trying to do a remaster.
 
Old 07-13-2014, 10:11 AM   #9
PeterSteele
Member
 
Registered: Jun 2012
Posts: 264

Original Poster
Rep: Reputation: Disabled
That's quite alright. This specialized stuff and will need advice from the CentOS experts...
 
Old 07-13-2014, 10:27 AM   #10
PeterSteele
Member
 
Registered: Jun 2012
Posts: 264

Original Poster
Rep: Reputation: Disabled
In looking at the logs that are creating by anaconda during a kickstart controlled VM install, the commands it appears to perform are

- Switch to perspective of VM image just created
# chroot /mnt/sysimage

- Do first step of grub install
# grub-install --just-copy

- Then complete grub install using
# grub --batch --no-floppy --device-map=/boot/grub/device.map
grub> root (hd0,0)
grub> install --stage2=/boot/grub/stage2 /grub/stage1 d (hd0) /grub/stage2 p (hd0,0)/grub/grub.conf

When I try the same sequence of commands after chrooting to my own prepared image, it fails on the "root (hd0,0)" command saying that the selected disk does not exist. There's obviously another step that's not obvious from the logs that makes hd0 visible to Anaconda's chroot environment that I'm missing.
 
Old 07-13-2014, 12:45 PM   #11
PeterSteele
Member
 
Registered: Jun 2012
Posts: 264

Original Poster
Rep: Reputation: Disabled
I've got the solution. Before launching my chroot session, I need to setup hard links representing the devices in my image:

ln /dev/loop0 /dev/vda
ln /dev/dm-0 /dev/vda1
ln /dev/dm-2 /dev/vda3

and create device.map to map (hd0) to /dev/vda. After doing this the grub commands all worked and I was able to create and boot a VM using the manually prepared image.

Last edited by PeterSteele; 07-14-2014 at 06:53 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Installing Grub on Disk Image Wafic01 Linux - Virtualization and Cloud 2 03-09-2011 02:54 AM
Manually installing Grub dunraven23 Linux - Newbie 8 01-28-2010 11:48 AM
Manually Installing Grub jan1024188 Fedora 3 08-16-2006 03:44 PM
Installing GRUB manually, is this going to be a challenge? rollo Linux - Newbie 3 03-01-2006 04:29 PM
installing grub manually abhinav_raj Linux - Software 3 03-22-2005 09:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat

All times are GMT -5. The time now is 11:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration