LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Virtualization and Cloud
User Name
Password
Linux - Virtualization and Cloud This forum is for the discussion of all topics relating to Linux Virtualization and Linux Cloud platforms. Xen, KVM, OpenVZ, VirtualBox, VMware, Linux-VServer and all other Linux Virtualization platforms are welcome. OpenStack, CloudStack, ownCloud, Cloud Foundry, Eucalyptus, Nimbus, OpenNebula and all other Linux Cloud platforms are welcome. Note that questions relating solely to non-Linux OS's should be asked in the General forum.

Notices


Reply
  Search this Thread
Old 05-17-2012, 08:18 AM   #1
dpesios
LQ Newbie
 
Registered: May 2012
Location: Greece
Posts: 6

Rep: Reputation: Disabled
Resizing partition in VHD file - root partition first, swap second (*Advanced*)


Hello,

Well, let me state the situation hoping that some of you, more experienced than me, will help me.

I have a virtual machine (the so-called guest) that in fact is a GNU/Linux and doesn't have an X server. The host machine is a Windows Server 2003 that runs Microsoft Virtual PC 2007.

Everything was going smoothly until one day when we decided (in my lab) to upload a large file on the guest machine (using ssh, anyway ...). The file didn't fit because the VHD file had reached its space limit, so the only solution was to resize it.

To carry this through, by doing a little search, I decided to read the instructions here. Okay, these instructions are for Windows guest machines but we can still use the link to VHD resizer that will, as its name suggests, resize the .vhd file.

Now what ? The swap partition (/dev/hda6) is second and and the root one (/dev/hda5) is first . Can I somehow toggle them without loosing data ? I also want to take advantage of the free unpartitioned space VHD Resize tool will create. And then how can I extend the partition in Linux ?

Thank you in advance.
(If there are disambiguations please let me know.)
 
Old 05-17-2012, 09:27 AM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,334

Rep: Reputation: Disabled
You've increased the size of /dev/hda, which means there's free space at the end of the device. Assuming you're using the MBR partitioning scheme, each partition can only occupy a contiguous area of the disk.

To extend /dev/hda5, you must:

- Delete or move any partitions occupying the space immediately following /dev/hda5
- Modify the partition entry for /dev/hda5

As /dev/hda6 is a swap partition, it doesn't actually contain any useful data and can safely be deleted and recreated. The following procedure should work:
  1. Boot the VM from a live CD (I recommend System Rescue CD)
  2. Delete /dev/hda6
  3. Modify /dev/hda5 with parted or a similar tool (or you could just use fdisk to delete and recreate the partition; fdisk just modifies the partition table, it doesn't touch the data)
  4. Recreate /dev/hda6
  5. Depending on the application used to perform step 3, you may now have to manually extend the file system on /dev/hda5 with a resizing tool for the file system in question (you didn't say which file system you're using)
 
1 members found this post helpful.
Old 05-17-2012, 09:53 AM   #3
dpesios
LQ Newbie
 
Registered: May 2012
Location: Greece
Posts: 6

Original Poster
Rep: Reputation: Disabled
Okay,

machine_name:~# df -T -h --all
Filesystem Type Size Used Avail Use% Mounted on
/dev/hda5 ext3 16G 13G 2.2G 86% /
proc proc 0 0 0 - /proc
sysfs sysfs 0 0 0 - /sys
devpts devpts 0 0 0 - /dev/pts
tmpfs tmpfs 189M 0 189M 0% /dev/shm

machine_name:~# fdisk -l

Disk /dev/hda: 17.1 GB, 17179803648 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 1 2088 16771828+ 5 Extended
/dev/hda5 * 1 2018 16209522 83 Linux
/dev/hda6 2019 2088 562243+ 82 Linux swap / Solaris

It seems that I do use the MBR partitioning scheme, right ? (where the one partition ends, the next begins ...)
I have never used System Rescue CD (I have used UBCD many times however) and my "dummy" question is as follows.
Should I boot the host machine with a bootable CD (System Rescue CD) and load the VHD file with a program included in the environment of the CD and then perform the steps you suggested ?
As I said, the guest machine doesn't have a graphical interface environment (startx not included ...).

Please clear these things up before I do anything irreversible (although i kept a back-up image).

Thanks again.
 
Old 05-17-2012, 11:53 AM   #4
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,334

Rep: Reputation: Disabled
Quote:
Originally Posted by dpesios View Post
machine_name:~# fdisk -l

Disk /dev/hda: 17.1 GB, 17179803648 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 1 2088 16771828+ 5 Extended
/dev/hda5 * 1 2018 16209522 83 Linux
/dev/hda6 2019 2088 562243+ 82 Linux swap / Solaris

It seems that I do use the MBR partitioning scheme, right ? (where the one partition ends, the next begins ...)
A single, extended partition containing a bootable root partition and a swap partition? I've actually never seen a partition scheme like that before, and had someone asked me yesterday if I thought it would work, my answer would have been "probably not". Well, you learn something new every day.

Once you've resized the vhd file, you'll notice that the output from fdisk -l /dev/hda still shows the partitions as having the same size as before, but the second line should show a higher number of cylinders dor the disk itself.

The procedure will have to be modified slightly to account for the extended partition (I should have seen that from the partition numbers): You'll actually have to resize two partitions; the extended partition (hda1) and the bootable root partition within it (hda5).

Quote:
Originally Posted by dpesios View Post
I have never used System Rescue CD (I have used UBCD many times however) and my "dummy" question is as follows.
Should I boot the host machine with a bootable CD (System Rescue CD) and load the VHD file with a program included in the environment of the CD and then perform the steps you suggested ?
No, you just change the boot order for the virtual machine, so that it boots from a live CD (either a physical disc or an iso file) instead of the virtual hard drive (vhd file).

I would strongly recommend backing up the VM before doing any partition resizing, regardless of the tool and procedure being used. Does Virtual PC 2007 support taking snapshots of vhd files? If so, that is without a doubt the best way to make sure nothing gets broken no matter what you do to the partition table.

The second best solution is to back up the partition table to a file. You can do that from within the VM with dd if=/dev/hda of=backup_of_partition_table.bin bs=512 count=1 (which means "read 1 512-byte block from the start of /dev/hda and write it to a file"). Copy the file to another system for safekeeping, and you're all set.

System Rescue CD boots to the command line by default, although X may be started manually. The command line is all you need here:
  • Start fdisk (fdisk /dev/hda) and delete all three partitions. Don't worry, nothing is written to disk before you type "w", and no data are deleted in any case, just the partition table entries.
  • (Re)create an extended partition that covers the entire disk (new, extended, partition number 1, default values for start and end).
  • Create a new logical partition. This will become the new /dev/hda5, and as long as it is at least as big as the old /dev/hda5, it will contain the entire, undamaged file system from that partition. Remember to change the partition type to 83 with t and set the bootable flag with a.
  • Create a new swap partition and set the type to 82. It will most likely be non-functional until you run mkswap /dev/hda6.
  • View the results with p, and make sure the block count for /dev/hda5 is higher than the previous value (16209522). Abort with q and start from scratch if something went wrong. Otherwise, write the changes to the disk with w.
  • At this point you may choose to boot the VM from the vhd, just to see that everything is indeed working before you resize the file system. Errors relating to the swap partition should be expected. If for some reason the system fails to boot, you can roll back the snapshot or restore the partition table from backup. Reboot into the live CD afterwards.
  • Finally, extend the file system with resize2fs /dev/hda5 <size>. It you don't know exactly what the maximum is, resize2fs -P /dev/hda5 will tell you.
  • Run mkswap /dev/hda6
  • Reboot.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Resizing partition in VHD file - root partition first, swap second (*Advanced*) dpesios Linux - Virtualization and Cloud 2 05-18-2012 05:50 AM
Hard Drive Partition Management - Mandriva Double Partition with Swap File partition moshebagelfresser Linux - Hardware 2 05-23-2008 10:46 AM
Resizing the swap partition R N Ghosh Fedora 5 04-07-2006 02:39 AM
Resizing swap partition shujja Linux - Newbie 3 09-30-2005 02:20 AM
Resizing swap partition... PapaLewA Linux - Newbie 4 11-17-2001 01:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Virtualization and Cloud

All times are GMT -5. The time now is 01:05 AM.

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