Showing posts with label Partition. Show all posts
Showing posts with label Partition. Show all posts

Monday, April 23, 2018

Assign Unused Space / Reduce space from the LVM Partition in Linux/Unix

Here, we are going to provide all remaining free space of hard disk to /motadata partition.

root@ubuntu:~# df -kh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-root 14G 1.9G 12G 14% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 2.0G 4.0K 2.0G 1% /dev
tmpfs 395M 400K 395M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 2.0G 0 2.0G 0% /run/shm
none 100M 0 100M 0% /run/user
/dev/xvda1 361M 36M 303M 11% /boot
/dev/mapper/vg0-motadata 71G 52M 67G 1% /motadata
root@ubuntu:~# pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/xvda2 vg0  lvm2 a--  129.62g 36.17g
root@ubuntu:~# lvextend /dev/mapper/vg0-motadata /dev/xvda2
Extending logical volume motadata to 107.69 GiB
Logical volume motadata successfully resized
root@ubuntu:~# resize2fs /dev/mapper/vg0-motadata
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/mapper/vg0-motadata is mounted on /motadata; on-line resizing required
old_desc_blocks = 5, new_desc_blocks = 7
The filesystem on /dev/mapper/vg0-motadata is now 28230656 blocks long.
root@ubuntu:~# df -kh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-root 14G 1.9G 12G 14% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 2.0G 4.0K 2.0G 1% /dev
tmpfs 395M 400K 395M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 2.0G 0 2.0G 0% /run/shm
none 100M 0 100M 0% /run/user
/dev/xvda1 361M 36M 303M 11% /boot
/dev/mapper/vg0-motadata 106G 60M 101G 1% /motadata

Reduce space from /motadata partition.

root@ubuntu:~# lvs
LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert
motadata vg0 -wi-ao--- 107.69g
root vg0 -wi-ao--- 14.30g
swap vg0 -wi-ao--- 7.63g
root@ubuntu:~# df -kh
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/vg0-root       14G  1.9G   12G  14% /
none                      4.0K     0  4.0K   0% /sys/fs/cgroup
udev                      2.0G  4.0K  2.0G   1% /dev
tmpfs                     395M  400K  395M   1% /run
none                      5.0M     0  5.0M   0% /run/lock
none                      2.0G     0  2.0G   0% /run/shm
none                      100M     0  100M   0% /run/user
/dev/xvda1                361M   36M  303M  11% /boot
/dev/mapper/vg0-motadata  106G   60M  101G   1% /motadata
root@ubuntu:~# umount -v /motadata
/dev/mapper/vg0-motadata has been unmounted
root@ubuntu:~# e2fsck -f /dev/mapper/vg0-motadata
e2fsck 1.42.9 (4-Feb-2014)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/vg0-motadata: 27/7061504 files (0.0% non-contiguous), 491233/28230656 blocks
root@ubuntu:~# resize2fs /dev/mapper/vg0-motadata 36G
resize2fs 1.42.9 (4-Feb-2014)
Resizing the filesystem on /dev/mapper/vg0-motadata to 9437184 (4k) blocks.
The filesystem on /dev/mapper/vg0-motadata is now 9437184 blocks long.
root@ubuntu:~# lvreduce -L -36G /dev/mapper/vg0-motadata
  WARNING: Reducing active logical volume to 71.69 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce motadata? [y/n]: y
  Reducing logical volume motadata to 71.69 GiB
  Logical volume motadata successfully resized
root@ubuntu:~# resize2fs /dev/vg0/motadata
resize2fs 1.42.9 (4-Feb-2014)
Resizing the filesystem on /dev/vg0/motadata to 18793472 (4k) blocks.
The filesystem on /dev/vg0/motadata is now 18793472 blocks long.
root@ubuntu:~# mount /dev/vg0/motadata /motadata/
root@ubuntu:~# lvs
LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert
motadata vg0 -wi-ao--- 71.69g
root vg0 -wi-ao--- 14.30g
swap vg0 -wi-ao--- 7.63g

Create a New partition using attached hard disk.

First Check current HDD.
root@Ubuntu:/# df -kh
Filesystem Size Used Avail Use% Mounted on
udev 3.9G 12K 3.9G 1% /dev
tmpfs 798M 416K 798M 1% /run
/dev/sda1 29G 866M 27G 4% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
none 5.0M 0 5.0M 0% /run/lock
none 3.9G 0 3.9G 0% /run/shm
none 100M 0 100M 0% /run/user
/dev/sdb1 40G 48M 38G 1% /mnt
root@Ubuntu:/# fdisk -l

Disk /dev/sda: 31.5 GB, 31457280000 bytes
4 heads, 32 sectors/track, 480000 cylinders, total 61440000 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: 0x0004a8a3

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    61439999    30718976   83  Linux

Disk /dev/sdb: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0xb468d93a

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    83884031    41940992    7  HPFS/NTFS/exFAT

Disk /dev/sdc: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders, total 104857600 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: 0x0cb3884b

   Device Boot      Start         End      Blocks   Id  System
root@Ubuntu:/# fdisk /dev/sdc
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-104857599, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599):
Using default value 104857599
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.
root@Ubuntu:/# mkfs -t ext4 /dev/sdc1
mke2fs 1.42.9 (4-Feb-2014)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3276800 inodes, 13106944 blocks
655347 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
400 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
root@Ubuntu:/# tune2fs -m 1 /dev/sdc1
tune2fs 1.42.9 (4-Feb-2014)
Setting reserved blocks percentage to 1% (131069 blocks)
root@Ubuntu:/# mkdir /motadata/
root@Ubuntu:/# mount /dev/sdc1 /motadata/
root@Ubuntu:/# df -kh
Filesystem      Size  Used Avail Use% Mounted on
udev            3.9G   12K  3.9G   1% /dev
tmpfs           798M  416K  798M   1% /run
/dev/sda1        29G  866M   27G   4% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none            5.0M     0  5.0M   0% /run/lock
none            3.9G     0  3.9G   0% /run/shm
none            100M     0  100M   0% /run/user
/dev/sdb1        40G   48M   38G   1% /mnt
/dev/sdc1        50G   52M   49G   1% /motadata
root@Ubuntu:/# blkid
/dev/sr0: LABEL="rd_rdfe_stable.161107-1031" TYPE="udf"
/dev/sda1: LABEL="cloudimg-rootfs" UUID="5651329a-c2a3-4ab1-9a39-09a559160b9a" TYPE="ext4"
/dev/sdb1: UUID="a1838da8-2818-4a42-8881-78fdbaee7f28" TYPE="ext4"
/dev/sdc1: UUID="b37b0bde-65c9-47a2-9e93-cad8fcf23364" TYPE="ext4"
root@Ubuntu:/# vim /etc/fstab
 # CLOUD_IMG: This file was created/modified by the Cloud Image build process
UUID=5651329a-c2a3-4ab1-9a39-09a559160b9a       /        ext4   defaults,discard        0 0
/dev/disk/cloud/azure_resource-part1    /mnt    auto    defaults,nobootwait,comment=cloudconfig 0       2
UUID=b37b0bde-65c9-47a2-9e93-cad8fcf23364       /motadata       ext4    defaults,discard  

Extend / Reduce SWAP Partitions in Linux

Extend Swap Partition.

1) Determine the name of the swap logical volume using the "lvs" command:

root@ubuntu:~# lvs
  LV       VG   Attr      LSize  Pool Origin Data%  Move Log Copy%  Convert
  motadata vg0  -wi-ao--- 71.52g
  root     vg0  -wi-ao--- 14.30g
  swap     vg0  -wi-ao---  7.63g
2) Turn off the swap partition:
root@ubuntu:~# swapoff /dev/mapper/vg0-swap
3) Resize the swap partition, adding 15GB more space to it:

root@ubuntu:~# lvresize -L +5G /dev/mapper/vg0-swap
  Extending logical volume swap to 12.63 GiB
  Logical volume swap successfully resized
4) Format the new swap space to make it usable:
root@ubuntu:~# mkswap /dev/mapper/vg0-swap
mkswap: /dev/mapper/vg0-swap: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 13242364 KiB
no label, UUID=a1ea414e-f650-4d11-8525-43ff01ed48d8
5) Turn the swap volume back on:

root@ubuntu:~# swapon /dev/mapper/vg0-swap

6) Use the free command to show your new swap space:
root@ubuntu:~# free -mh | grep -i swap
Swap: 12G 0B 12G