Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Last time I shrank a partition, it was just as easy as deleting it and recreating it from the same start sector. But this time, fdisk won't allow to recreate the partition from sector 63, it forces 2048 as a minimum.

Note that I'm working on a headless server and gparted is not an option because it's a GUI.

  • Why this behavior from fdisk?
  • Would using 2048 cause data loss?
  • Any workaround or solution to shrink the partition?

root@ubuntulive:~# fdisk -u=sectors /dev/sda

Command (m for help): p

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders, total 312581808 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0ab2afe6

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *          63   104872319    52436128+  83  Linux
/dev/sda2       104872320   113274314     4200997+  83  Linux
/dev/sda3       113274315   312576704    99651195   83  Linux

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

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-312581807, default 2048): 63
Value out of range.
First sector (2048-312581807, default 2048):  
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104872319, default 104872319): 52436159

Command (m for help): p

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders, total 312581808 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0ab2afe6

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    52436159    26217056   83  Linux
/dev/sda2       104872320   113274314     4200997+  83  Linux
/dev/sda3       113274315   312576704    99651195   83  Linux
share|improve this question
    
Related to superuser.com/questions/352572/… –  ernie Dec 12 '13 at 0:11

2 Answers 2

Why this behaviour from fdisk ?

Performance on some hard drive models

Would using 2048 cause data-loss ?

If it's the root partition it shouldn't be able to boot anyway.

Any work around or solution to shrink the partition ?

Use parted instead of fdisk. Be careful, unlike fdisk, parted write changes on the fly !

(parted) p                                                                
Model: ATA Hitachi HTS72501 (scsi)
Disk /dev/sda: 160041885696B
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start         End            Size           Type     File system     Flags
 1      32256B        53694627839B   53694595584B   primary  ext4            boot
 2      53694627840B  57996449279B   4301821440B    primary  linux-swap(v1)
 3      57996449280B  160039272959B  102042823680B  primary  xfs

(parted) rm 1
(parted) mkpart primary 32256B 28998224639B                               
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore                                                     
(parted) p                                                                
Model: ATA Hitachi HTS72501 (scsi)
Disk /dev/sda: 160041885696B
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start         End            Size           Type     File system     Flags
 1      32256B        28998224895B   28998192640B   primary  ext4
 2      53694627840B  57996449279B   4301821440B    primary  linux-swap(v1)
 3      57996449280B  160039272959B  102042823680B  primary  xfs

Warning !!

I thought that ext4 would not fragment so much (indeed it had only 0.1% of non-contiguous blocks) so I did not run resize2fs.

At first I did that and couldn't mount /dev/sda1 again. (After a good laugh) I recreated the partition at its initial size, ran e2fsck and decided to run resize2fs before trying parted again and it worked.

Remember, we're just editing the partition table with parted/fdisk, not altering any physical data.

This is a headless server, after reboot it would not ping. I decided not to reboot into rescue mode again, as this would potentially interrupt boot-time fsck, so I went to sleep. The next morning I found the server back up.

share|improve this answer

You can use GParted to resize the filesystem and the partition at the same time. IMHO, that's generally the best solution, because it leaves less room for human error to creep in. Note, however, that if you use MiB alignment in GParted (and maybe even if you don't), it's likely to try to shift the start of the partition to sector 2048. This will result in a lot of extra data movements, which are time-consuming and risky. OTOH, this may be desirable if it's a boot disk and you want to install a newer version of GRUB -- some versions now require more space after the MBR than the 62 sectors you've got now. (You can continue to use an older GRUB or LILO with your current disk, though.)

If you must do it using fdisk, try using an older distribution (or at least an older version of fdisk). Newer versions of fdisk don't seem to provide a way to specify a start sector as small as you want to use, although that is perfectly legal and was the standard until recently (2-3 years ago for Linux; up to Windows XP for Windows). Be careful about units, though -- older versions of fdisk used the fictitious "cylinder" values by default. Those values are imprecise, and the piecemeal changes in fdisk mean that you might not get the behavior you expect with certain in-between versions.

share|improve this answer
    
Thanks for your feedback ! I can't use gparted on a headless server. –  jpic Dec 13 '13 at 12:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.