2

First off, I'm an absolute newbie in Linux, but I've been tasked to automate the process of resizing a partition on RHEL 6.5. The server resides as a virtual machine in VMWare, and we are trying to orchestrate an activity whereby an existing VMDK file is expanded and the partitions on that VMDK resize automatically.

As a test case, the VMDK is seen on the system as /dev/sdb. The size of this is say 5GB. Now, I have 2 partitions on it, /dev/sdb1, /dev/sdb2, sdb1 taking up 1GB and sdb2 taking up 5GB

If someone now increases the size of the VMDK from 5GB to 10GB and specifies that the extra space should go to /dev/sdb1, how can I do that? So far I've managed to create a process whereby the last partition can be resized without a problem (i.e. /dev/sdb2) but any previous ones can't because of Starting and Ending block numbers.

My process is as follows:

1) Increase the VMDK size 2) Run echo "1" > /sys/class/scsi_device/0:0:0:0/device/rescan to pick up the resized drive 3) Using fdisk, delete the partition and recreate it

This works fine if the partition being modified is the last partition, but if its the first or second, then the partition just gets recreated the way it was. Is there some tool which can do this intelligently?

This is the fdisk output in my environment:

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
67 heads, 62 sectors/track, 5048 cylinders
Units = cylinders of 4154 * 512 = 2126848 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xf3405e93

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         510     1059239   83  Linux
/dev/sdb2             511        2524     4183078   83  Linux

As you can see, sdb1 takes up 1GB and SDB2 takes up 4GB, but there is about 5GB "free" on the disk. Now, how do I change this so that sdb1 takes up that remaining space? If I delete sdb1 and recreate it, it makes the same partition. I can easily create a new partition to occupy that space, or even change sdb2 to take up the remainder space (just delete sdb2 and recreate it using fdisk), but the question is how do I modify sdb1 to take up that space?

Hope my question is clear, and thanks in advance

MadHatter
  • 79,770
  • 20
  • 184
  • 232
NullPointer
  • 121
  • 1
  • 7
  • 4
    The short answer is that you can't directly. You'd need to move sdb2 to the end of the disk to be able to extend sdb1. Have you considered using LVM? – Mat Jun 24 '14 at 15:24
  • 2
    This isn't a direct answer to your question, but perhaps using LVM might be a simpler solution. Lets say instead of just having sdb1 and sdb2, you have lvm1 and lvm2. If you grow the disk (or add a new disk), you can take that new space and divide it however you want amongst the LVMs. (Edit: I realized that if you aren't familiar with LVMs, that example probably makes no sense. LVMs are logical volumes. You could have many disks make up one large logical volume, or you could have one disk split into many logical volumes. It's easy to add space to any volume) – Safado Jun 24 '14 at 15:24
  • Thanks guys, I don't know about LVM's so I'll read up on that, sounds like a more elegant solution – NullPointer Jun 24 '14 at 21:11
  • I don't recommend LVM for virtual environments, especially given the ease with which virtual disks and mountpoints can be added. – ewwhite Jun 25 '14 at 07:45
  • I read up a bit more on LVM and looks like that is the correct approach. The question now is, if I have created an LVM comprising of sdb1 and sdb2, then increase the size of sdb2, how do I get the LVM to recognise that increased size? I tried resize2fs but that didn't work :(. Also, I'm trying to do all this without a reboot, not sure if that will be necessary – NullPointer Jun 25 '14 at 10:45
  • All sorted now. Used LVM's and created a new partition if it needs to be extended, attach the partition to the VG, resize the LV then resize2fs on the LV. Thanks for all your help – NullPointer Jun 26 '14 at 09:32

2 Answers2

2

You could move the 2nd partition sdb2 to the end of the "disk" and then recreate the first, by then you will have the ability to recreate the first one with "full" size.

However depending on your filesystem you should not need to delete it but rather "only" unmount it, and then do resize2fs (at least for ext2/3/4) , after doing the above move.

The move itself could be done with parted.

For how to do this: refer to following links:

Parted user manual gnu.org and linuxquestions.org

Something like this might work (you need to change your numbers:

parted move 2 -125 -0
parted resize 1 0.063 -125

However after version 2.4. parted does not contain move anymore so you might need another tool (or it is renamed to another function which i did not find)

Qoute Manual from gnu.org

Note that after version 2.4, the following commands were removed: check, cp, mkfs, mkpartfs, move, resize.

Dennis Nolte
  • 2,881
  • 4
  • 27
  • 37
  • Sounds logical, I haven't seen those 2 commands you mentioned so I'll look into that. How would I be able to move sdb2 to the end of the disk if the "free" space is less than the size of sdb2 though? – NullPointer Jun 24 '14 at 21:13
  • @NullPointer i can't tell you exactly how it done, but if you ever used some GUI partition manager programm you can move the partition around when you have free space before or after the partition itself. Size itself, if you have 10MB or 100GB free before or after doesnot matter. For how exactly you do that with parted : i change my answer to add this – Dennis Nolte Jun 25 '14 at 07:23
  • Thanks Dennis, valid method however I went down the LVM route (as per my comments above). – NullPointer Jun 26 '14 at 09:41
1

I can feel your pain, I am relatively new at Linux as well, so I understand your pain. LVMs is the way to go, they can be easily expanded. If you are registered to Red Hat, then you can install the GUI (yum -y install system-config-lvm). Once installed, on the CMD line run the 'system-config-lvm' this will launch the GUI and from there you can view the various disks/partitions and make the changes or on the command line you would have to go through a three step process. So I just use the GUI which is fast and simply. Hope this helps