David Kennedy’s Tech Ramblings

Just another WordPress.com weblog

My Dual-boot nightmare (and a procedure to shrink partition/LVM2/ext4) November 7, 2009

Filed under: Uncategorized — dotnetdave @ 10:10 pm

Yesterday I decided to install Windows 7 natively, aiming for a dual-boot between Fedora 11 & Win 7. Obviously I was aware that windows installers always wipe the MBR with its own boot loader, destroying any loader which plays well with others (just about any OS besides Windows), so I took backups of the MBR (both with and without the partition table, or so I thought):

dd if=/dev/hda of=/home/David/mbr_backup_nopart bs=446 count=1  (just the MBR without the partition table)
I copied this file to a USB memory stick so I would be able to access it following the Win7 install

I then performed the following procedure (worked perfectly) to shrink file systems, logical volumes, and finally the physical partitions:
  1. Boot to any linux Live CD (I used Ubuntu, if your Live CD doesn’t automatically give you sudoer, authenticate as root and drop the ‘sudo’ prefixes)
  2. sudo lvm vgchange -a y (enable logical volume management, since my Fedora install uses LVM2)
  3. sudo fsck -fC /dev/vg_davesdualcore/lv_root (check the file system which I am about to resize)
  4. sudo resize2fs -p /dev/vg_davesdualcore/lv_root  250G (shrink the ext4 file system to use half my hard drive)
  5. sudo lvresize /dev/vg_davesdualcore/lv_root –size 250G (shrink the logical volume accordingly)
  6. sudo lvs (to report the size of your logical volumes, we are after the size of the /swap volume)
  7. sudo lvremove /dev/vg_davesdualcore/lv_swap (remove the swap volume so it can be recreated at the start of the newly available space)
  8. sudo lvcreate –size 3.8G –name lv_swap vg_davesdualcore (recreate the swap partition)
  9. sudo mkswap /dev/vg_davesdualcore/lv_swap (set up a linux swap area on the re-created volume)
  10. sudo pvs (report use and calculate the size the partition can be reduced to, ie. PSize – PFree)
  11. sudo pvresize /dev/sda2 –setphysicalvolumesize 254G (resize the physical volume to free up space at the end of the partition)
  12. sudo pvs –units s  (list physical volumes with size specified in sectors, note PSize)
  13. sudo parted /dev/sda unit s print (print the physical disk partitions with Start, End, & Size in sectors)
  14. Calculate the new End sector (Start from step 13 + PSize from step 12, add at least 32MB/65536 sectors as an error margin)
  15. sudo parted /dev/sda rm 2 (Remove the entry from the partition table. The number is the partition number as listed from step 13)
  16. sudo parted /dev/sda mkpart primary 409663s 533487742s (recreate the partition with the original Start & newly calculate End from step 14)
  17. sudo parted /dev/sda set 2 lvm on (enable logical volume management, again the number is the partition number)
  18. Done! You can view your new structure with ‘parted /dev/sda print’ and/or ‘fdisk -l /dev/sda’
Following this, I inserted a Windows 7 Pro DVD & rebooted. The windows installation completed successfully and I ended up with a working windows 7.

Now, as mentioned at the start of this blog post, the windows installation wiped the MBR. And this is where my nightmare begins..
I attempted to restore the MBR using the following shell command from an Ubuntu Live CD:
(STORE N GO is the volume name of the memory stick I used)

dd if=/media/STORE\'N\'GO/mbr_backup_nopart of=/dev/sda bs=446 count=1
And rebooted..
Much to my dismay I was greeted with the 'Invalid System Disk' message. Fedora 11 still boots following this message however (guess its some sort of BIOS fail-over mechanism), but I am so far unable to get Windows 7 booting from GRUB
When I restored the MBR, it apparently reverted the size of my PV to use most of the disk space again. I re-ran pvresize to set this back, but no luck on the Windows 7 front yet. Looks like I might be up for a complete re-install after all 😦


One Response to “My Dual-boot nightmare (and a procedure to shrink partition/LVM2/ext4)”

  1. sys admin Says:

    Generally speaking, when you are trying to build a dual-boot, the best way is to start with a completely blank hard drive and install Windows first.

    So you can boot from the windows install cd.

    use its partitioner to completely destroy all partitions.

    create a partition large enough for windows (ie all that you need aside from what you want for linux. Leave the rest of the drive unallocated.

    install windows.

    then, install linux. It will use the remaining space on the drive.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s