How to force files together on a Linux partition?

1

1

I have a dynamically expanding VDI file which is 14 GB big while the partition itself is only 2 GB.

The files are too spread out over the disk so zero-ing the free space and compacting the VDI didn't help, if I can get the files too be together on the disk like MyDefrag can do on Windows I can spare out 12 GB.

Steps taken so far:

  • Made root partition as small as possible. (NCurses Disk Usage shows only 2 GB left)
  • Used zerofree on the root partition.
  • Used the VirtualBox tools to compact the VDI file. (It went from 16 GB -> 14 GB)

The file should be half the size of what it is now...

Info about the disk:

Disk /dev/sda: 536.9 GB, 536870912000 bytes  
255 heads, 63 sectors/track, 65270 cylinders  
Units = cylinders of 16065 * 512 = 8225280 bytes  
Sector size (logical/physical): 512 bytes / 512 bytes  
I/O size (minimum/optimal): 512 bytes / 512 bytes  
Disk identifier: 0x837d7521  

   Device Boot      Start         End      Blocks   Id  System  
/dev/sda1               1           5       40131   83  Linux  
/dev/sda2               6          71      530145   82  Linux swap / Solaris  
/dev/sda3              72       65270   523710967+  83  Linux


Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 537GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system     Flags
 1      32.3kB  41.1MB  41.1MB  primary  ext2
 2      41.1MB  584MB   543MB   primary  linux-swap(v1)
 3      584MB   537GB   536GB   primary  ext3


Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda3            515492812   2443136 486864128   1% /

I'm currently trying to defragment it, then I'll do the zerofree and compact steps again.

This might work...

Total Files:                    178174
Fragmented Files:               25881
File Fragmentation Rate:        14.5256883720408%
Avg File Fragments(1 is best):  1.16055653462346

But does it?

Done!
Now lets see the results...
Statistics for /
Total Files:                    0
Fragmented Files:               0
Illegal division by zero at /usr/sbin/defragfs line 193.

Ouch, fail... It broke my install (kernel panic), I think I still have a back-up of a month ago.

Don't worry about it, I only have to emerge --sync; emerge -Du world; svn up on the back-up...
I currently use it for native Linux developing only, that's why I need it to be 3 GB and not 14 GB.

Tamara Wijsman

Posted 2010-08-02T13:21:39.130

Reputation: 54 163

Updated question with the steps taken so far, next time I boot the system I'll check the size of the boot and swap partition and then I will try the defragmenter that harrymc mentions. – Tamara Wijsman – 2010-08-07T11:27:37.273

Answers

0

If your problem is the layout of the partitions, you can use any partitioning product.
Just download the image of the boot CD of any partitioning product and boot with it as you would with a real physical computer. GParted is one possible choice.

If the partitions won't resize, defragmentation might be required. Although the file system on Linux is organized more efficiently than on Windows, it is not immune to this need. Linux File System Defragmenter is one possible solution.

EDIT

Sorry about losing the kernel.

Weird: Having a file-system with 1% used that can't resize is really weird.
Did you have on it some large files that you deleted?

In any case, defragmenters for ext3 are rare, unlike ext4 which is planned to eventually include an online defragmentation utility. However, here are some utilities that may play nicer with your disk. Be sure to read well the README files.

Shake is a defragmenter that runs in userspace, without the need of patching the kernel and while the system is used (for now, on GNU/Linux only).

Defrag the first is a shell-level script.

Defrag the second is another shell-level script.

harrymc

Posted 2010-08-02T13:21:39.130

Reputation: 306 093

The lay-out of partition is the default one, first the boot partition, then the swap partition and then the root partition. My root partition contains 2 GB and I'm sure the other two together are not 12 GB. So I think that the reason the VDI doesn't compact more is that there are a lot of blocks that contain used and free together, so I want to force the used together so that the free ones are apart and thus can be compacted I will try to see if your second link helps me to do this... – Tamara Wijsman – 2010-08-07T11:24:05.407

@TomWij: Try first to get an over-all view of the disk to understand the problem better. See http://www.cyberciti.biz/faq/linux-viewing-drive-partitions-with-fdisk-parted/ . You might also post this info.

– harrymc – 2010-08-07T11:59:11.770

Updated, trying the defragmenter now. – Tamara Wijsman – 2010-08-10T19:06:21.430

The defragmenter makes me have a kernel panic during the boot, why does that happen? See the answer for details about the failed defragmentation process. – Tamara Wijsman – 2010-08-10T20:41:23.700

Is it possible that my file system got corrupt? Can I troubleshoot or remedy that? I'll try Shake tomorrow if I still have a back-up, will make a back-up copy too so I don't have to go trough the restore feature of my back-up software. Maybe my back-up doesn't show up corruption... – Tamara Wijsman – 2010-08-11T22:42:14.290

I don't know about those large files, I deleted quite a lot things that weren't needed on my minimal Gentoo install. I actually only use it in headless mode and use Xming and connect through X11 over SSH to it with Putty and show Eclipse on my Windows, this leaves me with the sole base I need to test natively on Linux without all the extra stuff I don't need... Well, maybe I should reinstall it in a period when I have more time and think out the size in a better way. I've awarded the bounty to you because of the time you spent in this answer... – Tamara Wijsman – 2010-08-11T22:46:53.390

The back-up works and I didn't lose anything... I'll try this again later and hope for the corruption to not appear this time, maybe something was corrupted before or between these operations due to a bad shutdown... I'll accept this one. – Tamara Wijsman – 2010-08-28T13:05:05.493

0

How to effectively compact Linux Virtual Machine: http://kb.parallels.com/en/5683

In Parallels Desktop version 3:

  • Click Hard Disk 1
  • Advanced
  • click Compact

In Parallels Desktop version 4:

  • Start Parallels Image Tools from Applications/Parallels
  • Select Disk to compact pointing to Virtual Machine pvm
  • Click "Choose" button
  • Click "Continue"
  • Select "Compact Disk"
  • Click "Start"

Tom

Posted 2010-08-02T13:21:39.130

Reputation: 1 321

I've already used the zerofree utility... I'm using VirtualBox so these instructions do not help me, I've already chosen to compact using VirtualBox and that only made it go from 16 GB to 14 GB. Hmm, I just realized I didn't check the space the boot and swap partition uses, but that doesn't count up to 12 GB so I still need to force the partition files together to get rid of the free space in between. (Force together as MyDefrag calls it, but that's a Windows utility) – Tamara Wijsman – 2010-08-07T08:49:47.657

0

May not be the best solution, but an easy way would be to copy the filesystem on the partition to a new partition, using copy -a to preserve the file attributes.

A quick edit of fstab to mount the new partition in place of the old one and away you go...

Neal

Posted 2010-08-02T13:21:39.130

Reputation: 8 447

But then I would need to resize, which makes me think: Can I resize the current partition to 3 GB somehow? – Tamara Wijsman – 2010-08-10T20:50:22.957