Partition editing

From Noah.org
Jump to: navigation, search

sfdisk

On Ubuntu you may have to install the util-linux package:

aptitude install util-linux

Backup extended partition table

sfdisk -d /dev/hda > /tmp/hda

Restore extended partition table

sfdisk /dev/hda < /tmp/hda

Note that you can edit /tmp/hda between Backup and Restore. You can use this to extend the last partition if you imaged from a smaller drive to a larger drive. You cannot use this to manipulate partitions in the middle without destroying their contents.

parted

The fdisk command is slowly dying. It can't handle large disks or GPT formatted drives. The parted command takes over where fdisk leaves off.

resize a partition (shrink)

You can shrink unmounted ext4 partitions. The technique is a bit sketchy, but it seems to work.

# parted -l /dev/sda
Model: LSI 9750-8i DISK (scsi)
Disk /dev/sda: 5000GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system     Name     Flags
 1      1049kB  2097kB  1049kB                           bios_grub
 2      2097kB  4794GB  4794GB  ext4
 3      4794GB  5000GB  206GB   linux-swap(v1)

Boot with a LiveCD and resize the partition. Note that this does not resize the partition.

# e2fsck -f /dev/sda2
 ...
# resize2fs /dev/sda2 1000G
resize2fs 1.42.5 (29-Jul-2012)
Resizing the filesystem on /dev/sda2 to 262144000 (4k) blocks.
The filesystem on /dev/sda2 is now 262144000 blocks long.

Now for the tricky part -- parted. Note that the partition size I request is bigger than the filesystem. This is because I wasn't sure how to get the partition end to exactly line up with the end of the filesystem. It's bad if the partition ends before the filesystem, but it's OK if the filesystems is smaller than the partition; although, it does waste space. I went overboard by requesting the partition before 200GB bigger than it needs to be. 1GB would have been fine. Also note the message Error: File system has an incompatible feature enabled. Compatible features are has_journal, dir_index, filetype, sparse_super and large_file. Use tune2fs or debugfs to remove features.. As near as I can figure, you basically can't do anything with ext4 partitions in parted except deleted them. I show how to get around this further on.

# parted /dev/sda
GNU Parted 2.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: LSI 9750-8i DISK (scsi)
Disk /dev/sda: 5000GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system     Name     Flags
 1      1049kB  2097kB  1049kB                           bios_grub
 2      2097kB  4794GB  4794GB  ext4
 3      4794GB  5000GB  206GB   linux-swap(v1)

(parted) resize 2
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.
Start?  [2097kB]?
End?  [4794GB]? 1200GB
Error: File system has an incompatible feature enabled.  Compatible features are has_journal, dir_index, filetype, sparse_super and large_file.  Use tune2fs or debugfs to remove features.
(parted) print 2
Error: File system has an incompatible feature enabled.  Compatible features are has_journal, dir_index, filetype, sparse_super and large_file.  Use tune2fs or debugfs to remove features.
(parted)

Even print doesn't work on ext4 filesystems. I didn't feel like digging to see which features were being used on my ext4 filesystem. It seems that one can still at least delete partitions with ext4 filesystems on them. So in the following I delete the partition I want to resize and then I recreate it with the new size that I want. Important detail: I am hoping that when I delete the partition and create a new one that it will not harm the filesystem that was living on the original partition. I also have to make sure the beginning of the new partition exactly lines up with the old one. The end of the partition doesn't matter so long as the total partition size is at least bigger than the filesystem.

(parted) rm 2
(parted) mkpart primary ext4 2097kB 1200GB
(parted) quit

Now reboot and the system should come back up... may come up. Hopefully.

Add LVM partition

Note that the partition numbers are not sequential on disk. They are in the order they were added.

# parted
(parted) mkpart primary ext4 1200GB 4794GB
(parted) print
Model: LSI 9750-8i DISK (scsi)
Disk /dev/sda: 5000GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system     Name     Flags
 1      1049kB  2097kB  1049kB                           bios_grub
 2      2097kB  1200GB  1200GB  ext4            primary  boot
 4      1200GB  4794GB  3594GB                  primary
 3      4794GB  5000GB  206GB   linux-swap(v1)

(parted) set 4 lvm on
(parted) print
Model: LSI 9750-8i DISK (scsi)
Disk /dev/sda: 5000GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system     Name     Flags
 1      1049kB  2097kB  1049kB                           bios_grub
 2      2097kB  1200GB  1200GB  ext4            primary  boot
 4      1200GB  4794GB  3594GB                  primary  lvm
 3      4794GB  5000GB  206GB   linux-swap(v1)

(parted) quit
# vgcreate vg-guests /dev/sda4
  No physical volume label read from /dev/sda4
  Physical volume "/dev/sda4" successfully created
  Volume group "vg-guests" successfully created
# vgdisplay vg-guests
  --- Volume group ---
  VG Name              vg-guests
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               3.27 TiB
  PE Size               4.00 MiB
  Total PE              856833
  Alloc PE / Size       0 / 0
  Free  PE / Size       856833 / 3.27 TiB
  VG UUID               Qd7qpe-YOio-MbVt-9DmY-tzzD-MKQR-R22ti7