Linux Disks/Partitions:

1. Disk Partitioning

 Here is the disk layout example for partitions  in linux being used for file system mount points. The following show the output of  available disks in the system.  Where /dev/sda  and /dev/sdb are the disks available.

/dev/sda – has three partitions sda1, sda2,sda3. 
/dev/sdb – No partitions

susevm:~ # fdisk –l

Disk /dev/sda: 21.6 GB, 21613379584 bytes, 42213632 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 label type: dos
Disk identifier: 0x00095ee4

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048     4208639     2103296   82  Linux swap / Solaris
/dev/sda2   *     4208640    20209663     8000512   83  Linux         <-- * mark means boot information is on this partition.
/dev/sda3        20209664    42213375    11001856   83  Linux

Disk /dev/sdb: 10.8 GB, 10836967424 bytes, 21165952 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

  Look at the below image where it has file systems  on partitions created from  /dev/sda.

                    image

Now lets see how to create partitions on existing disk, /dev/sdb.

Create Partitions:

#fdisk /dev/sdb

m – for help
n – new partition
p – partition type ( You can make primary or extented partitions ) 
assign the partition  number (1-4) .. as you can create only 4 primary partitions on disk with msdos label.
w – write changes to the disk ( which will create or delete the partition request you made with fdisk)

susevm:~ # fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xd6eafaa0.

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

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): 1
First sector (2048-21165951, default 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-21165951, default 21165951): +5G
Partition 1 of type Linux and of size 5 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.
susevm:~ #

check the partition you created.

susevm:~ # fdisk -l /dev/sdb

Disk /dev/sdb: 10.8 GB, 10836967424 bytes, 21165952 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 label type: dos
Disk identifier: 0xd6eafaa0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux

Create file system:

Create a file system on the linux disk partition.

susevm:~ # mkfs.ext3 /dev/sdb1
mke2fs 1.42.8 (20-Jun-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 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

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Mount the file system . Now “/opt/” is allocated with a dedicated file system space of  “5GB”

susevm:~ # mount /dev/sdb1 /opt/
susevm:~ # df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       7.4G  2.9G  4.2G  42% /
devtmpfs        494M   32K  494M   1% /dev
tmpfs           499M     0  499M   0% /dev/shm
tmpfs           499M  2.2M  497M   1% /run
tmpfs           499M     0  499M   0% /sys/fs/cgroup
tmpfs           499M  2.2M  497M   1% /var/lock
tmpfs           499M  2.2M  497M   1% /var/run
/dev/sda3        11G  8.3G  1.5G  86% /home
/dev/sdb1       4.8G   11M  4.6G   1% /opt
susevm:~ #

Now lets create a filesystem, following with  “ logical volume management” .

2. Logical Volume Manager

The hierarchy follows:

.Physical Volumes  ( Created on the disks or disks partitions. eg: /dev/sdb, /dev/sdb1 )
.Volume Groups    ( Created using the available physical volumes, you can use single or multiple PV’s to create a VG. But you cannot use a single PV for multiple volume groups).
.Logical Volumes    ( Logical volumes are created on Volume Groups. You can create one or more logical volumes on the volume group depending on the available space on the VG.)


Now lets start with two disks in our system, /dev/sdb and /dev/sdc.  Using logical volume manager(lvm), you will have better leverage in managing available file system space, over to the file systems you create over disks partitions directly.

Example file system layout with logical volumes from multiple disks.

image

List the available disks with lsscsi  or you can use the following.
fdisk -l or lsblk  (shows the available disks and partitions on those. Try checking with all for better understanding.)

susevm:~ # lsscsi
[0:0:0:0]    disk    ATA      VBOX HARDDISK    1.0   /dev/sda
[1:0:0:0]    disk    ATA      VBOX HARDDISK    1.0   /dev/sdb
[2:0:0:0]    disk    ATA      VBOX HARDDISK    1.0   /dev/sdc
[4:0:0:0]    cd/dvd  VBOX     CD-ROM           1.0   /dev/sr0

Here we use /dev/sdb and /dev/sdc

Create Physical volumes :

susevm:~ # pvcreate /dev/sdb /dev/sdc
susevm:~ # pvs
  PV         VG   Fmt  Attr PSize  PFree
  /dev/sdb        lvm2 a--  10.09g 10.09g
  /dev/sdc        lvm2 a--  10.09g 10.09g
susevm:~ #

Create Volume Group:
Create Volume Group using  PV’s /dev/sdb and /dev/sdc.

susevm:~ # vgcreate vg01 /dev/sdb /dev/sdc
susevm:~ # vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  vg01   2   0   0 wz--n- 20.18g 20.18g

Now you have Volume Group “vg01” with 20G free space. Where the above output shows the number of PV’s used, number of LV’s available on VG as well.

Create Logical Volumes:

Creating the logical volumes with specific size .
Here -L is the logical volume size ( can be in K, M, or G etc.. )
-n -- Name of the logical volume.
So the following indicates that, you are requesting your system to create a logical volume of size 5G, with name “lvol01”, on the Volume Group “vg01”(/dev/vg01).

susevm:~ # lvcreate -L 5G -n lvol01 /dev/vg01
susevm:~ # lvs
  LV     VG   Attr      LSize Pool Origin Data%  Move Log Copy%  Convert
  lvol01 vg01 -wi-a---- 5.00g
susevm:~ #

Also you can create logical volumes with -l option where you specify the percentage of space to be used from the Volume Group. Here we are issuing a  command to create an LV  with name “lvol02” using 50% of the available space from  the Volume Group “vg01”.

susevm:~ # lvcreate -l 50%FREE -n lvol02 /dev/vg01
susevm:~ # lvs
  LV     VG   Attr      LSize Pool Origin Data%  Move Log Copy%  Convert
  lvol01 vg01 -wi-a---- 5.00g
  lvol02 vg01 -wi-a---- 7.59g

lvol02 logical volume created. Now you have two logical volumes in active state. Check available free space on the VG.

susevm:~ # vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  vg01   2   2   0 wz--n- 20.18g 7.59g
susevm:~ #

Create file system type “ext3” on “lvol02”

Create a File System: ( eg: ext3)

susevm:~ # mkfs.ext3 /dev/vg01/lvol02
mke2fs 1.42.8 (20-Jun-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
497760 inodes, 1989632 blocks
99481 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2038431744
61 block groups
32768 blocks per group, 32768 fragments per group
8160 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

       With this, you have 7.5 G of available ext3 file system space.

Mounting file system:

mount this volume to /opt.

susevm:~ # mount /dev/vg01/lvol02 /opt
susevm:~ # df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/sda2                7.4G  2.9G  4.2G  42% /
devtmpfs                 494M   32K  494M   1% /dev
tmpfs                    499M     0  499M   0% /dev/shm
tmpfs                    499M  2.2M  497M   1% /run
tmpfs                    499M     0  499M   0% /sys/fs/cgroup
tmpfs                    499M  2.2M  497M   1% /var/run
tmpfs                    499M  2.2M  497M   1% /var/lock
/dev/sda3                 11G  8.3G  1.5G  86% /home
/dev/mapper/vg01-lvol02  7.4G   18M  7.0G   1% /opt

Now lets say we might be needing 10GB of space for /opt, we can extend the logical volume to 10GB and then resize the filesystem to 10GB. For that you need to have free space on the Volume Group to extend the logical volume.

Using “lvextend”, we can extend the logical volume.

susevm:~ # vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  vg01   2   2   0 wz--n- 20.18g 7.59g

susevm:~ # lvextend -L 10G /dev/vg01/lvol02
susevm:~ # lvs
  LV     VG   Attr      LSize  Pool Origin Data%  Move Log Copy%  Convert
  lvol01 vg01 -wi-a----  5.00g
  lvol02 vg01 -wi-a---- 10.00g
susevm:~ #

  LV is extended but still your filesystem size will show 7.5G, until you resize the filesystem space.

susevm:~ # df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/sda2                7.4G  2.9G  4.2G  42% /
devtmpfs                 494M   56K  494M   1% /dev
tmpfs                    499M     0  499M   0% /dev/shm
tmpfs                    499M  3.7M  495M   1% /run
tmpfs                    499M     0  499M   0% /sys/fs/cgroup
tmpfs                    499M  3.7M  495M   1% /var/lock
tmpfs                    499M  3.7M  495M   1% /var/run
/dev/sda3                 11G  8.3G  1.5G  86% /home
/dev/mapper/vg01-lvol02  7.4G   18M  7.0G   1% /opt

Resize file system with “resize2fs” command. 
Note: resize2fs can be run on a mounted filesystem from kernel versions 2.6 or later. ( check kernel version with uname -r).

susevm:~ # resize2fs /dev/vg01/lvol02
resize2fs 1.42.8 (20-Jun-2013)
Filesystem at /dev/vg01/lvol02 is mounted on /opt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg01/lvol02 is now 2621440 blocks long.

susevm:~ # df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/sda2                7.4G  2.9G  4.2G  42% /
devtmpfs                 494M   56K  494M   1% /dev
tmpfs                    499M     0  499M   0% /dev/shm
tmpfs                    499M  3.7M  495M   1% /run
tmpfs                    499M     0  499M   0% /sys/fs/cgroup
tmpfs                    499M  3.7M  495M   1% /var/lock
tmpfs                    499M  3.7M  495M   1% /var/run
/dev/sda3                 11G  8.3G  1.5G  86% /home
/dev/mapper/vg01-lvol02  9.8G   18M  9.3G   1% /opt
susevm:~ #

File system Size extended to 10GB now.

To be continued .. with Various types of file systems:

 

 

 

 

 

 

 

 

 

 

No comments:

Post a Comment

Tux Tutorial 1: