Friday, October 24, 2014

Increase disk space for VM running Linux

When I create virtual machines on my notebook I always create too small disk for root partition or partition where I put Oracle binaries. After a while when I want to perform upgrade, or install another Oracle software, there is not enough space. This time I want to note steps about how to increase disk free space.

I can easily extend or shrink my logical volumes because I am using LVM in my virtual machines. Consider using LVM in production also because it gives you more flexibility then using normal hard drive partitions.

In this demo I'm using Oracle Linux 6.4.


Check disk free space after OS installation.

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_linuxtest-lv_root
                      4.9G  2.8G  2.0G  59% /
tmpfs                 770M  100K  770M   1% /dev/shm
/dev/sda1             485M   55M  405M  12% /boot


Add "/u01" mount and assign some disk space for Oracle installation files.


Shutdown VM and add disk.


Partition new disk "/dev/sdb" using fdisk command.

# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xa07249dd.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-391, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-391, default 391):
Using default value 391

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT
1e  Hidden W95 FAT1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

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

Calling ioctl() to re-read partition table.
Syncing disks.


Notice that I have identified partition as "Linux LVM" choosing "8e" hex code.


Using pvcreate command create a physical volume for later use by the LVM.

# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created

Create new volume group "vg_orabin". Later I can add or remove disks from this volume group.

# vgcreate vg_orabin /dev/sdb1
  Volume group "vg_orabin" successfully created


Information about volume group.

# vgdisplay vg_orabin
  --- Volume group ---
  VG Name               vg_orabin
  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               2.99 GiB
  PE Size               4.00 MiB
  Total PE              766
  Alloc PE / Size       0 / 0
  Free  PE / Size       766 / 2.99 GiB
  VG UUID               h3N1o5-AlYF-9nkL-PXiB-P8HK-tGAa-GlXPa5


Create logical volume using disk space from volume group.

# lvcreate --extents 766 -n lv_orabin vg_orabin
  Logical volume "lv_orabin" created


Create and mount filesystem.

# mkfs.ext4 /dev/mapper/vg_orabin-lv_orabin
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
196224< inodes, 784384 blocks
39219 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=805306368
24 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.




# mkdir /u01
# mount /dev/mapper/vg_orabin-lv_orabin /u01

Check disk space available.


# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_linuxtest-lv_root
                      4.9G  2.8G  2.0G  59% /
tmpfs                 770M   88K  770M   1% /dev/shm
/dev/sda1             485M   55M  405M  12% /boot
/dev/mapper/vg_orabin-lv_orabin
                      3.0G   69M  2.8G   3% /u01


Hm, 2.8G is not enough free space for me. Let’s extend this mount adding another disk.



Shutdown VM and add disk.


Partition new disk and create physical volume for LVM.

# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x16953397.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-652, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652):
Using default value 652

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

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

Calling ioctl() to re-read partition table.
Syncing disks.




# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created

Check current status of volume group “vg_orabin”.

# vgdisplay vg_orabin
  --- Volume group ---
  VG Name               vg_orabin
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               2.99 GiB
  PE Size               4.00 MiB
  Total PE              766
  Alloc PE / Size       766 / 2.99 GiB
  Free  PE / Size       0 / 0
  VG UUID               h3N1o5-AlYF-9nkL-PXiB-P8HK-tGAa-GlXPa5

Extend volume group by adding physical volume "/dev/sdc1" using vgextend command.

# vgextend vg_orabin /dev/sdc1
  Volume group "vg_orabin" successfully extended


Check volume group size - it is extended from 2.99G to 7.98G.

# vgdisplay vg_orabin
  --- Volume group ---
  VG Name               vg_orabin
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               7.98 GiB
  PE Size               4.00 MiB
  Total PE              2044
  Alloc PE / Size       766 / 2.99 GiB
  Free  PE / Size       1278 / 4.99 GiB
  VG UUID               h3N1o5-AlYF-9nkL-PXiB-P8HK-tGAa-GlXPa5


Using pvscan command scan all disks and notice physical volumes with free space.

# pvscan
  PV /dev/sdb1   VG vg_orabin      lvm2 [2.99 GiB / 0    free]
  PV /dev/sdc1   VG vg_orabin      lvm2 [4.99 GiB / 4.99 GiB free]
  PV /dev/sda2   VG vg_linuxtest   lvm2 [6.51 GiB / 0    free]
  Total: 3 [14.49 GiB] / in use: 3 [14.49 GiB] / in no VG: 0 [0   ]


With lvdisplay command display logical volume properties
Notice LV size = 2.99G.

# lvdisplay /dev/vg_orabin/lv_orabin
  --- Logical volume ---
  LV Path                /dev/vg_orabin/lv_orabin
  LV Name                lv_orabin
  VG Name                vg_orabin
  LV UUID                ypw9X1-vIsM-4rVF-NtVB-ACrf-f5nh-25p2sn
  LV Write Access        read/write
  LV Creation host, time linuxtest.localdomain, 2014-10-23 13:19:56 +0200
  LV Status              available
  # open                 0
  LV Size                2.99 GiB
  Current LE             766
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:2


I will add only 2G (of 5G) using lvextend command.

# lvextend -L +2G /dev/mapper/vg_orabin-lv_orabin /dev/sdc1
  Extending logical volume lv_orabin to 4.99 GiB
  Logical volume lv_orabin successfully resized


Mount volume and check for free space.

# mount /dev/mapper/vg_orabin-lv_orabin /u01

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_linuxtest-lv_root
                      4.9G  2.8G  2.0G  59% /
tmpfs                 770M   88K  770M   1% /dev/shm
/dev/sda1             485M   55M  405M  12% /boot
/dev/mapper/vg_orabin-lv_orabin
                      3.0G   69M  2.8G   3% /u01


Resize filesystem using resize2fs command:
# resize2fs /dev/mapper/vg_orabin-lv_orabin
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_orabin-lv_orabin is mounted on /u01; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/mapper/vg_orabin-lv_orabin to 1308672 (4k) blocks.
The filesystem on /dev/mapper/vg_orabin-lv_orabin is now 1308672 blocks long.


Now I have 4.6G free space for "/u01" mount.

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_linuxtest-lv_root
                      4.9G  2.8G  2.0G  59% /
tmpfs                 770M   88K  770M   1% /dev/shm
/dev/sda1             485M   55M  405M  12% /boot
/dev/mapper/vg_orabin-lv_orabin
                      5.0G   70M  4.6G   2% /u01



===========================================

Now I will try to extend root partition.

Newer Oracle Linux releases are using LVM by default during install.
Let’s see can I increase my root partition using commands above.


Display information about logical volumes using lvs command.

# lvs
  LV        VG           Attr      LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  lv_root   vg_linuxtest -wi-ao--- 4.97g
  lv_swap   vg_linuxtest -wi-ao--- 1.54g
  lv_orabin vg_orabin    -wi-a---- 4.99g

Check free space.
# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_linuxtest-lv_root
                      4.9G  2.8G  2.0G  59% /
tmpfs                 770M   88K  770M   1% /dev/shm
/dev/sda1             485M   55M  405M  12% /boot


Shutdown VM and add disk for extending root partition.


Partition new disk and create physical volume for LVM.

# fdisk /dev/sdd
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xf0608435.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-652, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652):
Using default value 652

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

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

Calling ioctl() to re-read partition table.
Syncing disks.



# pvcreate /dev/sdd1
  Physical volume "/dev/sdd1" successfully created


Check information about volume group.

# vgdisplay vg_linuxtest

  --- Volume group ---
  VG Name               vg_linuxtest
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               6.51 GiB
  PE Size               4.00 MiB
  Total PE              1666
  Alloc PE / Size       1666 / 6.51 GiB
  Free  PE / Size       0 / 0
  VG UUID               TXkKYl-PIxu-s2xk-LsEB-sgTZ-TdcO-8wapCV

Extend volume group using new physical volume.

# vgextend vg_linuxtest /dev/sdd1
  Volume group "vg_linuxtest" successfully extended

Logical volume status.
# lvdisplay  /dev/vg_linuxtest/lv_root
  --- Logical volume ---
  LV Path                /dev/vg_linuxtest/lv_root
  LV Name                lv_root
  VG Name                vg_linuxtest
  LV UUID                VNgeT7-4yhd-XqRi-2da1-XTqT-qTvm-oVK2pz
  LV Write Access        read/write
  LV Creation host, time linuxtest.localdomain, 2014-10-23 10:30:21 +0200
  LV Status              available
  # open                 1
  LV Size                4.97 GiB
  Current LE             1272
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0


Extend logical volume.
# lvextend /dev/mapper/vg_linuxtest-lv_root /dev/sdd1
  Extending logical volume lv_root to 9.96 GiB
  Logical volume lv_root successfully resized

Resize filesystem.

# resize2fs /dev/mapper/vg_linuxtest-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_linuxtest-lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/mapper/vg_linuxtest-lv_root to 2611200 (4k) blocks.
The filesystem on /dev/mapper/vg_linuxtest-lv_root is now 2611200 blocks long.


Check disk free space. Notice that I have 6.6G of free space for my root partition.

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_linuxtest-lv_root
                      9.9G  2.8G  6.6G  30% /
tmpfs                 770M   88K  770M   1% /dev/shm
/dev/sda1             485M   55M  405M  12% /boot



WARNING! Be very careful when using commands from blog post on your production system. These are dangerous commands which can cause loss of data or many other problems. I’ve used this commands in my test environment for educational purpose and it is possible that I have made mistakes in this demo. After all I am only simple Oracle DBA not Linux SA :-)


REFERENCES
http://www.linuxuser.co.uk/features/resize-your-disks-on-the-fly-with-lvm
http://www.rootusers.com/how-to-increase-the-size-of-a-linux-lvm-by-adding-a-new-disk/
https://wiki.archlinux.org/index.php/LVM

1 comment:

  1. Thank you Marco, for a thorough and nice post.

    ReplyDelete