linux - How can I write a Windows (NTFS) .img file to a partition and add it to grub?

16
2013-10
  • Darlan Alves

    I'm trying some crazy stuff here. First, I had a VirtualBox machine with Windows XP installed, using .vdi format. Then I exported the image to a OVA, extracted the .vmdk and converted it to a raw .img file. Then I've created a new VM into KVM via virt-manager, loaded this image as a hard disk and got it running.

    Now, I'm trying to copy this entire image to a disk partition, via dd, and boot the first NTFS partition inside this image on boot, via grub. So far, i've already dd'ed the first partition inside the image (the actual NTFS partition) and it was recognized both by gParted as NTFS and grub as a Windows install, but I can't boot it. So I've tried to copy the entire image, including the partition table, boot sector and stuff, to this /dev/sda4 physical partition. Now the image is booting via KVM, but I can't find a way to add it to Grub. I'm trying to find a way to do it via loop mounting, I just have no idea on how config grub to mount/map/boot this image and access the first (NTFS) partition inside it.

    What I want to do:

    - Turn my XP image into a valid physical installation to dual boot
    - Keep the VM running from this disk partition
    

    This way, if possible, I'll use the VM on Ubuntu, or boot it on grub.

    For clarification, the commands I used:

    $ sudo su
    
    # mounts the entire image
    $ losetup /dev/loop0 /path/to/WindowsXP.img
    $ fdisk -l /dev/loop0
    
    > Device        Boot    Start   End         Blocks      Id  System
    > /dev/loop0p1  *       63      31439204    15719571    7   HPFS/NTFS/exFAT
    
    # mounts the first NTFS partition inside image (offset 63*512)
    $ losetup /dev/loop1 -o 32256 /dev/loop0
    

    At this point, I have both NTFS partition on /dev/loop1 and the full disk (with MBR) on /dev/loop0. Then:

    # copied the entire image to /dev/sda4. This is not recognized by GRUB, 
    # therefore it must be loopmounted before booted via grub config somehow
    $ dd if=/dev/loop0 of=/dev/sda4 bs=10m
    
    # copied the NTFS virtual partition to physical one
    # GRUB should recognize and boot it, since is a valid XP install, however it misses the partition table,
    # so the virtual machine won't be able to boot it.
    $ dd if /dev/loop1 of=/dev/sda4 bs=10m
    

    As an alternative, I thought about a second disk image attached to VM, only with GRUB and minimal info to boot this partition. This way, I could keep the NTFS partition on /dev/sda4 to grub as a dual boot, and boot from a second tiny image on KVM.

    Ideas?

  • Answers
  • Aaron Okano

    Disclaimer: this is more of a suggestion, not a well-informed answer, but I don't have enough reputation to comment.

    Since you seem to be able to copy the Windows partition to a legitimate physical partition (/dev/sda4) why don't you just boot /dev/sda in a virtual machine? You could then boot into the Windows partition by choosing the Windows option in GRUB. It appears this sort of setup has been done before: https://bbs.archlinux.org/viewtopic.php?id=68216


  • Related Question

    multi boot - How can I make Grub recognise my old Linux Partition
  • Robbie

    Problem:
    I have two partitions. On one I have installed Fedora 9 and on my other partition I just installed Fedora 12. Now that I've installed Fedora 12 I can't get Grub to boot the old partition. How can I configure Grub to boot to my old fedora partition?

    Some Notes:

    • My Fedora 9 seems to show up as Linux LVM instead of just Linux in fdisk. I had virtual box installed on that Linux, but it isn't a virtual machine itself.
    • I'm pretty sure Fedora 12 installs grub instead of grub2
    • I can see and mount both partitions from the Fedora 12 live CD if that helps
    • My box is 64 bit.

    Info:
    This is what I get from fdisk -l:

    Disk /dev/sda: 750.2 GB, 750156374016 bytes
    255 heads, 63 sectors/track, 91201 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Disk identifier: 0x0005745c
    
    Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1       64685   519578624   83  Linux
    /dev/sda2           65706       65730      200812+  83  Linux
    /dev/sda3           65731       91201   204595807+  8e  Linux LVM
    /dev/sda4           64686       65705     8193150    5  Extended
    /dev/sda5           64686       65705     8192000   82  Linux swap / Solaris
    

    And this is what is currently in my menu.lst:

    # grub.conf generated by anaconda
    #
    # Note that you do not have to rerun grub after making changes to this file
    # NOTICE:  You do not have a /boot partition.  This means that
    #          all kernel and initrd paths are relative to /, eg.
    #          root (hd0,0)
    #          kernel /boot/vmlinuz-version ro root=/dev/sda1
    #          initrd /boot/initrd-[generic-]version.img
    #boot=/dev/sda
    default=0
    timeout=5
    splashimage=(hd0,0)/boot/grub/splash.xpm.gz
    hiddenmenu
    
    title Fedora (2.6.31.12-174.2.3.fc12.x86_64)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.31.12-174.2.3.fc12.x86_64 ro root=UUID=876dd1b5-104a-46de-b5fb-08f96d2ce420  LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us rhgb quiet mem=8192M
        initrd /boot/initramfs-2.6.31.12-174.2.3.fc12.x86_64.img
    title Fedora 9
        rootnoverify (hd0,3)
        chainloader +1
    

    The first fedora in that list is Fedora 12 which boots correctly, the second was just a guess as to where my Fedora 9 install was. When I select that in Grub on boot it goes to a blank screen with a blinking cursor that doesn't respond to the keyboard.

    Update:
    I was able to mount my old FC9 system and in the /boot section I found this under menu.lst if that helps:

    title Fedora (2.6.27.25-78.2.56.fc9.x86_64)
        root (hd0,1)
        kernel /vmlinuz-2.6.27.25-78.2.56.fc9.x86_64 ro root=/dev/VolGroup00/LogVol00 rhgb quiet
        initrd /initrd-2.6.27.25-78.2.56.fc9.x86_64.img
    

    Question:
    What do I need to do to boot into my old Fedora?


  • Related Answers
  • quack quixote

    LVM isn't "Linux Virtual Machine", it's "Logical Volume Manager". If I understand LVM correctly, to boot to a root partition on LVM you need a separate /boot partition outside of the LVM (was that the small partition on /dev/sda2?). Your system may still have this, but if your FC12 installation overwrote it recovery will be much more difficult.

    Here's a couple of links to Grub vs LVM problems: link 1, link 2

    Also see "Mounting a Linux LVM volume" -- you should be able to verify that your old FC9 system is still OK by mounting and examining its root partition while booted into FC12. If so, you can probably recover fairly easily, even if you've overwritten the FC9 /boot partition.

    Here's an example Grub boot entry for an LVM system (source). In this case, (hd0,0) (meaning /dev/sda1) is a small /boot partition, and the rest of the system resides on an LVM on /dev/sda2. Note the root= and lvm2root= entries on the kernel line:

    title Debian GNU/Linux, kernel 2.6.26 Using LVM with SATA Disk
    root (hd0,0)
    kernel /vmlinuz-2.6.26 root=/dev/ram0 lvm2root=/dev/mapper/sda_vol-root
    initrd /initrd-lvm2-2.6.26.gz
    

    Postmortem: This user ended up with the best-case scenario. The FC12 installation didn't overwrite or reformat FC9's /boot partition, /dev/sda2, and all that was needed was to access FC9's old /boot/grub/menu.lst and copy the boot entries into FC12's Grub menu.

  • Trevoke
    title Fedora (2.6.31.12-174.2.3.fc12.x86_64)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.31.12-174.2.3.fc12.x86_64 ro root=UUID=876dd1b5-104a-46de-b5fb-08f96d2ce420  LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us rhgb quiet mem=8192M
        initrd /boot/initramfs-2.6.31.12-174.2.3.fc12.x86_64.img
    title Fedora 9
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.31.12-174.2.3.fc12.x86_64 ro root=*****  LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us rhgb quiet mem=8192M
        initrd /boot/initramfs-2.6.31.12-174.2.3.fc12.x86_64.img
    

    See where I wrote root=* ? Either get the UUID for hd0,3 or just write root=/dev/hda3 or /dev/sda3 there. And .. It should be fine, if that's the correct partition.