Repartition

From PrgmrWiki

Repartitioning the disk can be useful if the original partition sizes are inappropriate, or the disk has been upgraded. Of course, this usually means destroying all data on the existing partition or whole disk, so be sure to backup what you want to keep!

You can keep using the existing disk label (partition table/map) and just delete or change existing partitions, or make a new label. If it is very corrupted, you may want to zero out the first portion of the disk or even the entire disk. Then, make new partitions for swap and filesystems or lvm in the proportions you want. Gnu parted and fdisk are available in the rescue image for repartitioning. A reboot may be needed for the kernel to recognize the new partitions. After the new partitions are ready, you can initialize them with filesystems or other data. You can reinstall following the instructions on Untarring a fresh OS image.

To repartition or reinstall, boot into the CentOS rescue image so none of your filesystems are mounted. The rescue image can be booted from the first bootloader menu when the vps is restarting. If the disk has just been resized or other vps parameters changed, you will need to shutdown completely and create the vps again from the menu on the host server (dom0) for it to reread the vps (domU) configuration file.

This is the first bootloader menu where you can choose the rescue image:


    GNU GRUB  version 0.97  (65536K lower / 0K upper memory)

 +-------------------------------------------------------------------------+
 | user bootloader configuration                                           |  
 | CentOS 5.5 rescue (2.6.18-194.3.1.el5xen)                               |
 | CentOS 5.5 installer                                                    |
 | Ubuntu 10.04 LTS installer                                              |
 |                                                                         |
 |                                                                         |
 |                                                                         |
 |                                                                         |
 |                                                                         |
 |                                                                         |
 |                                                                         |
 |                                                                         |  
 +-------------------------------------------------------------------------+
    Use the ^ and v keys to select which entry is highlighted.
    Press enter to boot the selected OS, 'e' to edit the
    commands before booting, or 'c' for a command-line.

Login as root with no password and you can run fdisk or parted on your disk (/dev/xvda):


CentOS release 5.5 (Final)
Kernel 2.6.18-194.3.1.el5xen on an i686

build.xen.prgmr.com login: root
Last login: Wed Jun  2 01:03:26 on xvc0
[root@build ~]# fdisk /dev/xvda

The number of cylinders for this disk is set to 57985.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/xvda: 25.7 GB, 25769803776 bytes
124 heads, 7 sectors/track, 57985 cylinders
Units = cylinders of 868 * 512 = 444416 bytes

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1               1        6913     3000000   83  Linux

Command (m for help): 

Adding a new partition for a new filesystem different than root can also be done without the rescue image and after the new filesystem is made, you can just mount it manually or with mount -a after adding it to /etc/fstab. Sometimes a reboot is still needed to recognize a new partition though. Adding a new partition is shown here in the rescue image:

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (6913-57985, default 6913): 
Using default value 6913
Last cylinder or +size or +sizeM or +sizeK (6913-57985, default 57985): 
Using default value 57985

Command (m for help): p

Disk /dev/xvda: 25.7 GB, 25769803776 bytes
124 heads, 7 sectors/track, 57985 cylinders
Units = cylinders of 868 * 512 = 444416 bytes

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1               1        6913     3000000   83  Linux
/dev/xvda2            6913       57985    22165489+  83  Linux

Command (m for help):

Then save the new partition layout, mkfs, and add it to /etc/fstab:

Command (m for help): w
The partition table has been altered!
                
Calling ioctl() to re-read partition table.
Syncing disks.  
[root@build ~]# mkfs.ext3 /dev/xvda2
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux  
Block size=4096 (log=2)
Fragment size=4096 (log=2)
2774400 inodes, 5541372 blocks
277068 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
170 block groups
32768 blocks per group, 32768 fragments per group
16320 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000 
                
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
                
This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@build ~]# mount /dev/xvda1 /mnt
[root@build ~]# vi /mnt/etc/fstab
                
# UNCONFIGURED FSTAB FOR BASE SYSTEM
LABEL=PRGMRDISK1   /    ext3    errors=remount-ro       1       1
/dev/xvda2      /home   ext3    defaults        1       2
/var/swap      none     swap    sw    0 0

~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"/mnt/etc/fstab" 5L, 181C written
[root@build ~]# umount /mnt
[root@build ~]# 

To erase the existing filesystem and make a new one taking the whole disk do this:

[root@build ~]# fdisk /dev/xvda

The number of cylinders for this disk is set to 57985.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/xvda: 25.7 GB, 25769803776 bytes
124 heads, 7 sectors/track, 57985 cylinders
Units = cylinders of 868 * 512 = 444416 bytes

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1               1           3        1298+  83  Linux

Command (m for help): o
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.


The number of cylinders for this disk is set to 57985.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-57985, default 1): 
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-57985, default 57985): 
Using default value 57985

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@build ~]# 

After erasing the old partition table and making a new partition, you can reinstall an image or run one of the distro installers from the first bootloader menu to install interactively. When you are done repartitioning, boot back into the normal filesystem.