Expanding Partitions: Difference between revisions

From Alteeve Wiki
Jump to navigation Jump to search
mNo edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
Expanding an EXT3\4 logical partition on an ANVIL!
The goal of this document is to walk the user through expanding a partition on an Anvil! device in the field, including expanding the filesystem within the VM once space has been allocated.
 


'''NOTE: Whenever you modify partitions, always always ALWAYS make sure you have current backups of the data. Mistakes happen, the power goes out, coffees are spilled. Back that data up.'''
'''NOTE: Whenever you modify partitions, always always ALWAYS make sure you have current backups of the data. Mistakes happen, the power goes out, coffees are spilled. Back that data up.'''
Line 7: Line 8:
Expanding a VM's storage allocation from the commandline is a simple process. It <i>will</i> require the VM to be restarted, please ensure the service user understands that there will be a short stop in service.
Expanding a VM's storage allocation from the commandline is a simple process. It <i>will</i> require the VM to be restarted, please ensure the service user understands that there will be a short stop in service.


The first step is to ensure there is enough space available in the physical volume group
The first step is to ensure there is enough space available in the physical volume group on the Anvil itself. This is critical as, of course, if there is no space available to be allocated to a VM, we can't expand it!
 
<span class="code">
<span class="code">


Line 145: Line 147:
</span>
</span>


The VM should now see that there are 10GB of free space at the end of the volume. To partition that space and make it usable, the client may request additional intervention, depending on the existence and access to a Striker! dashboard.  
The VM should now see that there are 10GB of free space at the end of the volume. To partition that space and make it usable, the client may request additional intervention, depending on the existence and access to a Striker! dashboard.


= For Anvil! VM Users =
= For Anvil! VM Users =
Line 153: Line 155:
== Linux-based VMs ==
== Linux-based VMs ==


What method of expanding your Linux partition you use depends on the set up of your machine. Generally speaking, either the space available to your VM will be partitioned using Logical Volume Management (LVM), or it will be devided into a series of Primary and Extended partitions. The benefits and drawbacks of each method are beyond the scope of this document.
What method of expanding your Linux partition you use depends on the set up of your machine. Generally speaking, either the space available to your VM will be partitioned using Logical Volume Management (LVM), or it will be devided into a series of Primary and Extended partitions. The benefits and drawbacks of each method are beyond the scope of this document. In either case, we'll be stuck rebuilding a Primary partition at least once, as LVM resides atop a partition.  
 
== Modifying LVM partitions ==
 
The method used to extend an LVM partition in your VM is the same as the method used on the Anvil! itself, 'lvextend'.
 
 
lvdisplay
lvextend -i amount (100%PVG for example) /path/to/dev.
 
 
Once the logical volume has been extended, the filesystem needs to be told about the extra space


== Modifying Primary and Extended Partitions ==
== Modifying Primary and Extended Partitions ==
Line 170: Line 161:
Linux has access to a fantastic utility for managing storage volumes, parted. Open up a terminal and check if it's installed.
Linux has access to a fantastic utility for managing storage volumes, parted. Open up a terminal and check if it's installed.


NOTE: For some Red-Hat based distros, parted is availble from the EPEL repository. Be careful when enabling new repositories. Some (like EPEL and RPMforge) are mutually exclusive. Do your research before enabling one.
NOTE: For some Red-Hat based distros, parted is available from the EPEL repository. Be careful when enabling new repositories. Some (like EPEL and RPMforge) are mutually exclusive. Do your research before enabling one.


Non-LVM based partitions require contiguous disk space to function. For example, they do not allow for data at the end of a drive to be added to the first partition if there is a second partition existing between it. To allocate our new space, we will need to shuffle any existing partitions around to make space for the desired size of each partition.  
Non-LVM based partitions require contiguous disk space to function. For example, they do not allow for data at the end of a drive to be added to the first partition if there is a second partition existing between it. To allocate our new space, we will need to shuffle any existing partitions around to make space for the desired size of each partition.  
Line 188: Line 179:
* One 80 GB drive divided into:
* One 80 GB drive divided into:
** A 512 MB /boot partition
** A 512 MB /boot partition
** a 30 MB / partition
** a 30 GB / partition
** and a 2048mb /swap partition
** and a 2048mb /swap partition


And you wished to make / 50GB, you would need to delete the /swap partition, then delete the / partition, and recreate it with the start block as "512M" and the end block as "51712M" (as 50 gigabytes + 512 megabyes = 51712M). Then you would need to recreate your /swap partition.
And you wished to make / 50GB, you would need to delete the /swap partition, then delete the / partition, and recreate it with the start block as "512M" and the end block as "51712M" (as 50 gigabytes + 512 megabytes = 51712M). Then you would need to recreate your /swap partition.


Once that is complete, <span="code">quit</span> parted, and reboot into your original OS. If you've created your root partition properly, it should boot as normal.
Once that is complete, <span="code">quit</span> parted, and reboot into your original OS. If you've created your root partition properly, it should boot as normal.


== Modifying LVM partitions ==


=== Extending the filesystem ===
The method used to extend an LVM partition in your VM is the same as the method used on the Anvil! itself, 'lvextend'.


Once your OS has booted, enter a shell and use the command appropriate to the filesystem to extend the filesystem over the new space. For EXT2/3/4, <span="code">extend2fs /dev/yourpartition</span> is the way to go. For XFS filesystems, <span="code">xfs_growfs</span> is correct. Your filesystem should now be the correct, new size. Job's done!


lvdisplay
lvextend -i amount (100%PVG for example) /path/to/dev.


Once the logical volume has been extended, the filesystem needs to be told about the extra space. Instructions to do so can be found [https://alteeve.ca/w/Expanding_Partitions#Extending_the_filesystem here].


=== Administrator Intervention to insert an ISO image ===
In lieu of a physical DVD to insert into a drive, we can edit a Virtual Machine's definition file to tell it to boot off it's "cdrom" drive, and list a location of the iso that it will find there. To ensure their accessibility, we put definition files for VMs on Anvil! machines in /shared/definitions. Likewise, the boot media we'll be using will be at /shared/files. Any LiveCD with access to gparted will suffice, but the gparted project itself offers a LiveCD specifically for this use at http://gparted.org/download.php. Download it, and make it accessible in /shared/files before continuing.


Once we have access to the iso, we have to edit the definition XML file found at /shared/definitions/path-to-your-vm-definition.xml. We will have to add a boot clause to tell the VM to choose the cdrom drive to boot from first, and then add a source to the cdrom itself to point it towards our ISO.
== Extending the filesystem ==


<span class="code">
Once your OS has booted, enter a shell and use the command appropriate to the filesystem to extend the filesystem over the new space. For EXT2/3/4, <span="code">extend2fs /dev/yourpartition</span> is the way to go. For XFS filesystems, <span="code">xfs_growfs</span> is correct. Your filesystem should now be the correct, new size. Job's done!
vim /shared/definitions/your-vm-definition.xml
<domain type='kvm' id='4'>
  <name>vm01-centos6_6</name>
  <uuid>be60c0d9-eb9d-31b3-db5c-e90f00335f33</uuid>
  <memory unit='KiB'>2097152</memory>
  <currentMemory unit='KiB'>2097152</currentMemory>
  <vcpu placement='static'>2</vcpu>
  <os>
    <type arch='x86_64' machine='rhel6.6.0'>hvm</type>
    <boot dev='cdrom'/> ### Let's add the 'cdrom' device here, above our hard drive entry so it is looked at first
    <boot dev='hd'/>
 
-SNIPPED-
 
<disk type='block' device='cdrom'>
      <driver nayme='qemu' type='raw'/>
      <target dev='hdc' bus='ide'/>
      <<nowiki />source dev='/shared/files/path-to-iso.iso'/> ### And add our source iso.
      <readonly/>
      <alias name='ide0-1-0'/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    </disk>
</span>
 
Done. Shut the VM down, and bring it up again. When you log into the VM, it should have booted from the LiveCD.
 
NOTE: Remember to remove these two lines after you are done with the LiveCD, else the VM will continue to boot into the ISO and not it's own drive!
 
Resizing non-LVM partitions using gparted:
 
Hard drive partitions, even those on virtual hardware, operate similar to a book. There is a first page, a second, and so on until the end. Each page, or 'block', holds a number of words (bytes, in this case). Primary partitions on a hard drive can be thought of as chapters of a book. If you wanted to add pages to chapter 1 of your book, you couldn't just tack the extra pages to the end of the book, they would need to be inserted with the chapter they were meant for. Given that we have essentially just tacked on extra pages with our lvextend command, we'll now have to shuffle the partition around so it makes sense to the VM.
 
To do this, we'll have to unmount any partitions that we will be manipulating, and then shuffle the free space next to the partition we want to incorporate it. We will do this with an application called gparted.
 
== Expanding an encrypted, non-LVM volume ==
 
If you wish to resize an /encrypted/ volume, the process becomes a little more convoluted, but follows the same principles. As before, if the partition you'd like to resize is your root partition, you'll need to reboot into a livecd. If the target partition isn't root, ensure it's unmounted. Once your partition is unmounted, or your VM rebooted into a LiveCD environment, we'll need to make sure the linux kernel has the appropriate module loaded to handle encrypted disks.
<span class="code">
modprobe dm-crypt
</span>
 
Next we'll need to unlock the drive, and create an object in /dev/mapper/ which we can then manipulate like a filesystem. Afterwards, we tell Linux to rescan volume groups, and apply any changes to ensure that /dev/mapper/crypt1 is accessible.
<span class="code">
sudo cryptsetup luksOpen /dev/yourdevice crypt1
Enter encryption password
sudo vgscan --mknodes
sudo vgchange -ay
</span>
 
Unfortunately, gparted won't help us deal with encrypted volumes. We're going to have to use fdisk, a more base-level utility to do so. The method of growing a partition in fdisk is less elegant than in parted. We need to delete the partition, and then recreate it beginning at the same block as before, and including the newly added space.
 
First, launch fdisk.
 
<code>$ fdisk </code>
 
fdisk presents itself as a shell in which you can input commands. 'm' prints a list of possible actions to choose from. For our purpose,
let's print the current partition table, with 'p'. It should print something similar to this:
 
<span style="code">  
Disk /dev/vda: 63.2 GB, 63166218240 bytes
16 heads, 63 sectors/track, 122392 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000980e3
Device Boot        Start        End      Blocks  Id  System
/dev/vda1  *          3        1043      524288  83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1043        9365    4194304  82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/vda3            9365      122393    56966144  83  Linux
Partition 3 does not end on cylinder boundary.
 
</span>
 
We can see that we have three partitions. We know that our encrypted partition is on /dev/vda3, which is the third entry. Let's keep that in mind, as fdisk requests entry numbers for it's operations, not the device name or path.
 
It is also important to note that /dev/vda3 is at the end of the partition table. This makes things easy for us. We simply have to delete the partition, then recreate it using all the available size, and don't have to worry about shuffling space between partitions.
 
Next, we'll delete partition 3, with 'd', and '3' when requested. 'n' and '3' will begin a new partition. Fdisk will ask for input on beginning and end blocks. Hit enter when requested for a beginning block - you want it to start the partition exactly where it did last time (or else data will be lost!). If you don't wish to add all the new volume space to /dev/sda3, input the end block you'd like (ensure it's larger than it's initial size, or again, you'll lose data), or else just hit enter again to select the default - all the remaining blocks. After you've created the new partition, write the changes to disk with 'w', which will also exit fdisk, and reboot your LiveCD.
 
Now that we've created a larger partition, we'll need to grow the encrypted layer surrounded your partition. Again booting into the LiveCD, we'll need to repeat some of the steps to make the encrypted partition accessible.
<span class="code">
sudo cryptsetup luksOpen /dev/yourdevice crypt1
Enter encryption password
sudo vgscan --mknodes
sudo vgchange -ay
</span>
 
We'll use the regular tools available to expand the partition. Assuming an EXT3/4 partition, we'll use the following. First, we must check the filesystem, then we will resize the fs to the size of the volume.
 
<span class="code">
sudo e2fsck /dev/mapper/crypt1
sudo resize2fs -p /dev/mapper/crypt1
</span>
 
The filesystem should now be ready to go. If you had modified your VM's xml file in /shared/definitions/ to allow it to boot from a LiveCD, remove the two lines added now, and restart the VM service.
 
== Expanding an encrypted, LVM volume ==
 
If the encrypted volume contained an LVM with further partitions, we would have to instead enlarge the physical volume within the VM, then the logical volume, and then resize the filesystem within.
 
--TESTING--
 
<span class="code">
sudo cryptsetup luksOpen /dev/yourdevice crypt1
Enter encryption password
sudo vgscan --mknodes
sudo vgchange -ay
sudo pvresize /dev/mapper/crypt1
sudo pvchange -x y /dev/mapper/crypt1
lvresize -L100%PVG /dev/path?????
sudo pvchange -x n /dev/mapper/crypt1
sudo e2fsck -f /dev/mapper/???
sudo resize2fs -p /dev/mapper/sameasabove???
 
</span>
 
 
== Resizing Partitions in a Windows environment ==
 
TODO

Latest revision as of 19:06, 6 June 2016

The goal of this document is to walk the user through expanding a partition on an Anvil! device in the field, including expanding the filesystem within the VM once space has been allocated.


NOTE: Whenever you modify partitions, always always ALWAYS make sure you have current backups of the data. Mistakes happen, the power goes out, coffees are spilled. Back that data up.

For Anvil! Administrators

Expanding a VM's storage allocation from the commandline is a simple process. It will require the VM to be restarted, please ensure the service user understands that there will be a short stop in service.

The first step is to ensure there is enough space available in the physical volume group on the Anvil itself. This is critical as, of course, if there is no space available to be allocated to a VM, we can't expand it!

$pvdisplay

 --- Physical volume ---
 PV Name               /dev/drbd1
 VG Name               an-a05n02_vg0
 PV Size               287.13 GiB / not usable 1.99 MiB
 Allocatable           yes 
 PE Size               4.00 MiB
 Total PE              73506
 Free PE               73506
 Allocated PE          0
 PV UUID               x4bkWj-UQup-W3Xb-egha-VB9X-bG93-e4wzGU
  
 --- Physical volume ---
 PV Name               /dev/drbd0
 VG Name               an-a05n01_vg0
 PV Size               504.96 GiB / not usable 2.18 MiB
 Allocatable           yes 
 PE Size               4.00 MiB
 Tolocation             inherit
 Read ahead sectors     auto
 - currently set to     256
 Block device           253:0
  
 --- Logical volume ---
 LV Path                /dev/an-a05n01_vg0/vm01-centos6_6
 LV Name                vm01-centos6_6
 VG Name                an-a05n01_vg0
 LV UUID                1olier-t7TM-afvr-hOJU-7Nbo-MYzd-19g5mK
 LV Write Access        read/write
 LV Creation host, time an-a05n01.alteeve.ca, 2015-08-06 15:04:53 -0400
 LV Status              available
 # open                 1
 LV Size                48.83 GiB
 Current LE             12500
 Segments               1
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     256
 Block device           253:1
  
 --- Logical volume ---
 LV Path                /dev/an-a05n01_vg0/vm02-win8_0
 LV Name                vm02-win8_0
 VG Name                an-a05n02_vg0
 LV UUID                tDYtcd-NAdJ-Q5Mk-PSCj-qL4q-ZJRT-3b6hWD
 LV Write Access        read/write
 LV Creation host, time an-a05n01.alteeve.ca, 2015-08-10 11:36:03 -0400
 LV Status              available
 # open                 0
 LV Size                48.83 GiB
 Current LE             12500
 Segments               1
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     256
 Block device           253:2

We can see that we have a /shared/ device and a centos 6.6 LV on one physical volume, and a Windows 8 VM on the other.

Let's extend our Centos partition, /dev/an-a05n01_vg0/vm01-centos6_6. DBRD offers us a very simple tool to do so, 'lvextend'. Using lvextend, we can either expand a partition by an amount, "-L +10G" to add ten gigabytes to a volume, or to a total size "-L 60G" to make the total size of the volume sixty gigabytes. Let's just add 10 gigs.

 lvextend -L+1G /dev/an-a05n01_vg0/vm01-centos6_6
 Size of logical volume an-a05n01_vg0/vm01-centos6_6 changed from 48.83 GiB (12500 extents) to 58.83 GiB (15060 extents).
 Logical volume vm01-centos6_6 successfully resized

And let's check the new size of the volume

 $ lvdisplay
 --- Logical volume ---
 LV Path                /dev/an-a05n01_vg0/shared
 LV Name                shared
 VG Name                an-a05n01_vg0
 LV UUID                66OdHC-7YDH-96yt-7CWk-wO2L-PHMy-4p6CZv
 LV Write Access        read/write
 LV Creation host, time an-a05n01.alteeve.ca, 2015-08-06 12:34:12 -0400
 LV Status              available
 # open                 1
 LV Size                40.00 GiB
 Current LE             10240
 Segments               1
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     256
 Block device           253:0
  
 --- Logical volume ---
 LV Path                /dev/an-a05n01_vg0/vm01-centos6_6
 LV Name                vm01-centos6_6
 VG Name                an-a05n01_vg0
 LV UUID                1olier-t7TM-afvr-hOJU-7Nbo-MYzd-19g5mK
 LV Write Access        read/write
 LV Creation host, time an-a05n01.alteeve.ca, 2015-08-06 15:04:53 -0400
 LV Status              available
 # open                 0
 LV Size                58.83 GiB
 Current LE             15060
 Segments               2
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     256
 Block device           253:1
  
 --- Logical volume ---
 LV Path                /dev/an-a05n01_vg0/vm02-win8_0
 LV Name                vm02-win8_0
 VG Name                an-a05n01_vg0
 LV UUID                tDYtcd-NAdJ-Q5Mk-PSCj-qL4q-ZJRT-3b6hWD
 LV Write Access        read/write
 LV Creation host, time an-a05n01.alteeve.ca, 2015-08-10 11:36:03 -0400
 LV Status              available
 # open                 1
 LV Size                48.83 GiB
 Current LE             12500
 Segments               1
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     256
 Block device           253:2

Yep, we're golden.

To get the VM to recognize the additional size, we'll need to shut the service down entirely, and then bring it back up. Take whatever steps needed to make sure no data is lost on the client VM before shutting it down.

$ clusvcadm -d vm:vm01-centos6_6
<OUTPUT>
$ clusvcadm -e vm:vm01-centos6_6

The VM should now see that there are 10GB of free space at the end of the volume. To partition that space and make it usable, the client may request additional intervention, depending on the existence and access to a Striker! dashboard.

For Anvil! VM Users

Once additional space has been added to your VM, it must be partitioned to be usable. To do so, log in to your vm using virt-manager, or another suitable application. The specific method of partitioning space depends on your VM's operating system.

Linux-based VMs

What method of expanding your Linux partition you use depends on the set up of your machine. Generally speaking, either the space available to your VM will be partitioned using Logical Volume Management (LVM), or it will be devided into a series of Primary and Extended partitions. The benefits and drawbacks of each method are beyond the scope of this document. In either case, we'll be stuck rebuilding a Primary partition at least once, as LVM resides atop a partition.

Modifying Primary and Extended Partitions

Linux has access to a fantastic utility for managing storage volumes, parted. Open up a terminal and check if it's installed.

NOTE: For some Red-Hat based distros, parted is available from the EPEL repository. Be careful when enabling new repositories. Some (like EPEL and RPMforge) are mutually exclusive. Do your research before enabling one.

Non-LVM based partitions require contiguous disk space to function. For example, they do not allow for data at the end of a drive to be added to the first partition if there is a second partition existing between it. To allocate our new space, we will need to shuffle any existing partitions around to make space for the desired size of each partition.


Most default linux installs have a root, "/" partition, and space allocated to a swap file. In cases such as this, your job is fairly straightforward.

Before any modification of the root filesystem can be done, it must be available in an unmounted state. To achieve that, we must boot the system from CD, USB key, etc. In an Anvil! environment, that can be achieved by accessing the Striker dashboard, selecting your Anvil, then VM, and changing it's boot device to a virtual CD-ROM. The image used as the 'disk' can also be selected from the VM management screen. Any LiveCD will work, though we recommend a Fedora LiveCD, which is available at https://getfedora.org/en_GB/workstation/download/.

Once booted into the LiveCD environment, open a terminal and and enter <span="code">parted. Remove the swap partition (located using the command <span="code"> print all ) via <span="code"> rm and the partition's ID.

As there is no way to "expand" a partition once created, we will have to delete the partition we wish to expand, then recreate it. The new partition MUST start at the same 'block' of the drive, or your data will be lost. This sounds frightening, but in practice it is relatively straightforward.

Begin by noting the 'Start' block of the device, as seen from the parted command <span="code"> print all , then delete the partition by using <span="code> rm $yourpartition . Once that is done, figure out what the end block will be by adding the Start block to your desired size, and create a new partition using the <span="code">mkpart $startblock $endblock command.

For example, if your drive layout was:

  • One 80 GB drive divided into:
    • A 512 MB /boot partition
    • a 30 GB / partition
    • and a 2048mb /swap partition

And you wished to make / 50GB, you would need to delete the /swap partition, then delete the / partition, and recreate it with the start block as "512M" and the end block as "51712M" (as 50 gigabytes + 512 megabytes = 51712M). Then you would need to recreate your /swap partition.

Once that is complete, <span="code">quit parted, and reboot into your original OS. If you've created your root partition properly, it should boot as normal.

Modifying LVM partitions

The method used to extend an LVM partition in your VM is the same as the method used on the Anvil! itself, 'lvextend'.


lvdisplay lvextend -i amount (100%PVG for example) /path/to/dev.

Once the logical volume has been extended, the filesystem needs to be told about the extra space. Instructions to do so can be found here.


Extending the filesystem

Once your OS has booted, enter a shell and use the command appropriate to the filesystem to extend the filesystem over the new space. For EXT2/3/4, <span="code">extend2fs /dev/yourpartition is the way to go. For XFS filesystems, <span="code">xfs_growfs is correct. Your filesystem should now be the correct, new size. Job's done!