2

I have a VM with one HD of size 3GB and I'd like to enlarge its HD to 7GB.

So I execute this command on the host (while guest is shutdown):

VBoxManage modifyhd debian.vdi --resize 7168

Then I run the guest, Debian 6, and then:

smith@debian6:~$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             2.8G  2.6G   60M  98% /
tmpfs                  61M     0   61M   0% /lib/init/rw
udev                   57M  160K   57M   1% /dev
tmpfs                  61M     0   61M   0% /dev/shm
smith@debian6:~$ sudo parted /dev/sda print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 3221MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system     Flags
 1      1049kB  3035MB  3034MB  primary   ext3            boot
 2      3036MB  3220MB  185MB   extended
 5      3036MB  3220MB  185MB   logical   linux-swap(v1)
smith@debian6:~$ cat /proc/partitions
major minor  #blocks  name

   8        0    3145728 sda
   8        1    2962432 sda1
   8        2          1 sda2
   8        5     180224 sda5

So, no automatic resizing (detection) of the HD/partition (while VirtualBox, in the host, shows it's 7GB now). Ok...

Then I do:

smith@debian6:~$ sudo resize2fs /dev/sda1 
resize2fs 1.41.12 (17-May-2010)
The filesystem is already 740608 blocks long.  Nothing to do!
smith@debian6:~$ sudo parted
GNU Parted 2.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) select /dev/sda1                                                 
Using /dev/sda1
(parted) resize                                                           
WARNING: you are attempting to use parted to operate on (resize) a file system.
parted's file system manipulation code is not as robust as what you'll find in
dedicated, file-system-specific packages like e2fsprogs.  We recommend
you use parted only to manipulate partition tables, whenever possible.
Support for performing most operations on most types of file systems
will be removed in an upcoming release.
Partition number? 1                                                       
Start? 0                                                                  
End?  [3034MB]?

Here I'm stuck. At the above parted it asks me to resize to 3GB. No point in that, right..

What should I do in order to enlarge this partition?

Poni
  • 315
  • 3
  • 14
  • Did the `VBoxManage modifyhd` command run successfully? It seems it didn't change anything to your disk -- sda is still 3145728 blocks. – gertvdijk Nov 27 '12 at 22:38
  • @gertvdijk it did run successfully. – Poni Nov 28 '12 at 06:27
  • It simply does not make sense to me then. Clearly, the disk wasn't resized stating the `Disk /dev/sda: 3221MB` output. – gertvdijk Nov 28 '12 at 10:17
  • Seems like duplicate from http://serverfault.com/questions/452345/having-problems-expanding-the-space-of-one-partition-on-a-disk – mdpc Nov 28 '12 at 17:49

2 Answers2

3

If you haven't rebooted your VM yet then the OS may not know that the disk has been expanded. So I would start with that first (or do a full VM shutdown, then startup). A second problem you'll run into is that any edits to the partition table on a drive that is in use won't be seen until the system is rebooted (or the drive otherwise made not in use). So assuming that the drive is showing up as 7GB after the reboot, you will have to enlarge the partition, then reboot again and finally issue your resize2fs command. BTW, I personally don't normally use parted, but it looks like the re-size command tries to re-size both the partition and the filesystem -- in this case, you would probably be better off just re-sizing the partition first (via fdisk), then do the filesystem after the OS is able to re-read the partition table (via a reboot). Oh, and make sure you have a good backup first.

Edit: I set up a 3GB Debian VM in VirtualBox, same as yours, and recorded a log of the steps I used to expand it to 7GB. Summary: Shut down the VM, backup the VDI file, extend the VDI, restart VM, use fdisk -- set units to sectors (u command) and display partition table (p), delete existing partitions, recreate root partition (being careful to use same starting sector as original one), recreate extended partition and swap, mark primary as bootable (a), write, reboot, then resize filesystem.

root@debian:~# cat /proc/partitions
major minor #blocks name

8 0 3145728 sda
8 1 2962432 sda1
8 2 1 sda2
8 5 180224 sda5

root@debian:~# exit

$ VBoxManage list runningvms
"Debian" {b32e56f1-a6b9-4753-a67d-19f03503f884}

$ VBoxManage controlvm Debian acpipowerbutton

$ cp Debian.vdi Debian.vdi.backup

$ VBoxManage modifyhd Debian.vdi 7168

$ VBoxManage startvm Debian

$ ssh root@192.168.56.101

root@debian:~# cat /proc/partitions
major minor #blocks name

8 0 7340032 sda
8 1 2962432 sda1
8 2 1 sda2
8 5 180224 sda5

root@debian:~# swapoff /dev/sda5
root@debian:~# fdisk /dev/sda

Command (m for help): u
Changing display/entry units to sectors

Command (m for help): p

Disk /dev/sda: 7516 MB, 7516192768 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 5926911 2962432 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 5928958 6289407 180225 5 Extended
Partition 2 does not end on cylinder boundary.
/dev/sda5 5928960 6289407 180224 82 Linux swap / Solaris

Command (m for help): d
Partition number (1-5): 5

Command (m for help): d
Partition number (1-5): 2

Command (m for help): d
Selected partition 1

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First sector (63-14680063, default 63): 2048
Last sector, +sectors or +size{K,M,G} (2048-14680063, default 14680063): +6G

#Impportant! Fdisk in this case defaults to a starting sector of 63, but the original OS install started at sector 2048. You must specify the same starting sector (manually) as what was originally there.

Command (m for help): p

Disk /dev/sda: 7516 MB, 7516192768 bytes

Device Boot Start End Blocks Id System
/dev/sda1 2048 12584960 6291456+ 83 Linux

Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 2
First sector (63-14680063, default 63): 12584961
Last sector, +sectors or +size{K,M,G} (12584961-14680063, default 14680063):
Using default value 14680063

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First sector (12585024-14680063, default 12585024):
Using default value 12585024
Last sector, +sectors or +size{K,M,G} (12585024-14680063, default 14680063):
Using default value 14680063

Command (m for help): p

Device Boot Start End Blocks Id System
/dev/sda1 2048 12584960 6291456+ 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 12584961 14680063 1047551+ 5 Extended
Partition 2 does not end on cylinder boundary.
/dev/sda5 12585024 14680063 1047520 83 Linux

Command (m for help): a
Partition number (1-5): 1

Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 82
Changed system type of partition 5 to 82 (Linux swap / Solaris)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
root@debian:~# reboot

$ ssh root@192.168.56.101

root@debian:~# cat /proc/partitions
major minor #blocks name

8 0 7340032 sda
8 1 6291456 sda1
8 2 1 sda2
8 5 1047520 sda5
root@debian:~# df /
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 2915832 809244 1958468 30% /

root@debian:~# resize2fs /dev/sda1
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/sda1 to 1572864 (4k) blocks.
The filesystem on /dev/sda1 is now 1572864 blocks long.

root@debian:~# df /
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 6194240 810684 5071636 14% /

root@debian:~# mkswap /dev/sda5

Derek Pressnall
  • 643
  • 4
  • 8
  • Did a full reboot. What exactly would you do with `fdisk`? – Poni Nov 28 '12 at 06:30
  • Does `cat /proc/partitions` show the extra space? If so then at least we've conquered the first goal. In that case, you can try using parted, but I don't know if it will let you with an in-use partition table. For you current layout, if I was doing it manually I would delete /dev/sda5 and sda2 (sda5 is swap, sda2 is the extended partition), delete sda1, recreate sda1 as a larger size, recreate sda2 and sda5, reboot, then use resize2fs on sda1 and mkswap on sda5. If you aren't used to `fdisk`you can also boot from a rescue ISO image and use parted from there. – Derek Pressnall Nov 28 '12 at 08:05
  • Wouldn't it remove the whole OS if I'd delete sda1? And no, `cat /proc/partitions` does not show the extra space. Very weird.. – Poni Nov 28 '12 at 15:51
  • Deleting and recreating a partition using fdisk doesn't wipe the partition contents, as long as the starting sector is the same as the original partition (and of course the length is the same or larger). This is why you would change the units to sectors instead of cylinders, so you can be as precise as possible. But since your disk size didn't change after the reboot, that means there is an issue a bit further down the stack. You mentioned you were using Virtual Box? If you want I'll try to replicate your setup tonight and put together a procedure for you. – Derek Pressnall Nov 28 '12 at 21:14
  • I just ran through a quick test, installed RedHat on a 3GB VM, and VirtualBox didn't let me resize the vdi file while it was in use (it actually tried, but the tool crashed). But it did let me resize it with the VM shut off. And after booting the VM, `cat /proc/partitions` showed the larger space. I deleted the swap partition and extended partition (/dev/sda5, /dev/sda2), deleted /dev/sda1 (root), recreated /dev/sda1 covering the extra space, put swap back in place, rebooted, ran resize2fs, and the extra space was then available. I'll record a screen session, and update the answer tonight. – Derek Pressnall Nov 28 '12 at 22:45
  • I did EXACTLY as @Derek says, but it didn't work for me. What I got, after a reboot, is a non-bootable drive message on boot (Debian's error). – Poni Nov 28 '12 at 22:55
  • When you recreated the sda1 partition, did you happen to notice if it was bootable? If not, you can recover your system by booting from a rescue cd image, use fdisk and the `a` parameter to mark it as bootable. – Derek Pressnall Nov 29 '12 at 02:00
  • Hi Derek, I've screwed up the image but going to try it later, with a fresh installation. I do however **APPRICIATE** your effort!! THANKS! – Poni Dec 01 '12 at 19:46
  • No problem, glad to help if I can. I think I figured out what went wrong. When I tried it with Debian (instead of Fedora), I found that the Debian installer puts the first sector of partition 1 on block 2048, whereas if you take the default with fdisk, it goes on block 63. If you wrote it out with the wrong starting sector, it is still fixable -- just delete and recreate partition 1 starting on block 2048. I'll add a note to the answer so it doesn't catch anyone else by suprise. – Derek Pressnall Dec 01 '12 at 20:31
  • Great job, worked like a charm - thanks for the effort! – Achim Mar 31 '20 at 14:28
2

You will be unable to increase the size of the /dev/sda1 partition in this particular instance because adjacent to it lies the extended partition (ultimately swap partition).

You can only expand space if there is contiguous space around the target partition in this case there is none. This is of course true if you do not want to reload the partition.

In this case what you'd have to do is delete the swap partition, delete the extended partition, expand partition 1 leaving room for swap partition, recreate the extended partition, and then recreate the swap partition. Then you can do the resize2fs on the enlarged partition. You'll probably have to do that mkswap on the modified swap partition too to make it properly function.

mdpc
  • 11,856
  • 28
  • 53
  • 67
  • Believe I answered this same problem in http://serverfault.com/questions/452345/having-problems-expanding-the-space-of-one-partition-on-a-disk – mdpc Nov 28 '12 at 17:52