Skip to content

Linux - Handling Storage and Disks

List block storage

Here we have two disks, /dev/sda and /dev/sdb.

/dev/sda is in-use and has two lvm partitions, one for / and another for swap

/dev/sdb has no defined partition and we'll play with it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[root@CentOS7 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   12G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   11G  0 part
  ├─centos-root 253:0    0  9.8G  0 lvm  /
  └─centos-swap 253:1    0  1.2G  0 lvm  [SWAP]
sdb               8:16   0   40G  0 disk
sr0              11:0    1 1024M  0 rom
[root@CentOS7 ~]#

Use gdisk

gdisk allows creation of gpt partition.

fdisk allows creation of dos partition (mbr).

Let's create 4 partitions of 20G, 10G, 5G and 5G with gdisk:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
[root@CentOS7 ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.

Command (? for help): n
Partition number (1-128, default 1):
First sector (34-83886046, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-83886046, default = 83886046) or {+-}size{KMGTP}: _20G
Last sector (2048-83886046, default = 83886046) or {+-}size{KMGTP}: ^C
[root@CentOS7 ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.

Command (? for help): p
Disk /dev/sdb: 83886080 sectors, 40.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 782396E4-DBD4-4C6C-B8C3-40B51B5CCA56
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 83886046
Partitions will be aligned on 2048-sector boundaries
Total free space is 83886013 sectors (40.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name

Command (? for help): n
Partition number (1-128, default 1):
First sector (34-83886046, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-83886046, default = 83886046) or {+-}size{KMGTP}: +20G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdb: 83886080 sectors, 40.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 782396E4-DBD4-4C6C-B8C3-40B51B5CCA56
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 83886046
Partitions will be aligned on 2048-sector boundaries
Total free space is 41942973 sectors (20.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        41945087   20.0 GiB    8300  Linux filesystem

Command (? for help): n
Partition number (2-128, default 2):
First sector (34-83886046, default = 41945088) or {+-}size{KMGTP}:
Last sector (41945088-83886046, default = 83886046) or {+-}size{KMGTP}: +10G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help):

Command (? for help): p
Disk /dev/sdb: 83886080 sectors, 40.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 782396E4-DBD4-4C6C-B8C3-40B51B5CCA56
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 83886046
Partitions will be aligned on 2048-sector boundaries
Total free space is 20971453 sectors (10.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        41945087   20.0 GiB    8300  Linux filesystem
   2        41945088        62916607   10.0 GiB    8300  Linux filesystem

Command (? for help): n
Partition number (3-128, default 3):
First sector (34-83886046, default = 62916608) or {+-}size{KMGTP}:
Last sector (62916608-83886046, default = 83886046) or {+-}size{KMGTP}: +5G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdb: 83886080 sectors, 40.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 782396E4-DBD4-4C6C-B8C3-40B51B5CCA56
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 83886046
Partitions will be aligned on 2048-sector boundaries
Total free space is 10485693 sectors (5.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        41945087   20.0 GiB    8300  Linux filesystem
   2        41945088        62916607   10.0 GiB    8300  Linux filesystem
   3        62916608        73402367   5.0 GiB     8300  Linux filesystem

Command (? for help): n
Partition number (4-128, default 4):
First sector (34-83886046, default = 73402368) or {+-}size{KMGTP}:
Last sector (73402368-83886046, default = 83886046) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdb: 83886080 sectors, 40.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 782396E4-DBD4-4C6C-B8C3-40B51B5CCA56
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 83886046
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        41945087   20.0 GiB    8300  Linux filesystem
   2        41945088        62916607   10.0 GiB    8300  Linux filesystem
   3        62916608        73402367   5.0 GiB     8300  Linux filesystem
   4        73402368        83886046   5.0 GiB     8300  Linux filesystem

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
[root@CentOS7 ~]#

Check the new partitions are available:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[root@CentOS7 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   12G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   11G  0 part
  ├─centos-root 253:0    0  9.8G  0 lvm  /
  └─centos-swap 253:1    0  1.2G  0 lvm  [SWAP]
sdb               8:16   0   40G  0 disk
├─sdb1            8:17   0   20G  0 part
├─sdb2            8:18   0   10G  0 part
├─sdb3            8:19   0    5G  0 part
└─sdb4            8:20   0    5G  0 part
sr0              11:0    1 1024M  0 rom
[root@CentOS7 ~]#

With GPT, the partition information is stored as follows:

  • First 512Bytes (Protective MBR),
  • Second 512Bytes (GPT Headers),
  • Next 16KiBytes (Primary GPT)
  • Last 16KiBytes (Backup GPT)

We can delete the first 32KiBytes of the GPT Table:

1
2
3
4
5
[root@CentOS7 ~]# dd if=/dev/zero of=/dev/sdb count=1 bs=32K
1+0 records in
1+0 records out
32768 bytes (33 kB) copied, 0.00109791 s, 29.8 MB/s
[root@CentOS7 ~]#

Which results in loss of information from the partition table:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[root@CentOS7 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   12G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   11G  0 part
  ├─centos-root 253:0    0  9.8G  0 lvm  /
  └─centos-swap 253:1    0  1.2G  0 lvm  [SWAP]
sdb               8:16   0   40G  0 disk
sr0              11:0    1 1024M  0 rom
[root@CentOS7 ~]#

But if we start GPT again, we can recover the partition table as it still has a back-up:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
root@CentOS7 ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10

Caution: invalid main GPT header, but valid backup; regenerating main header
from backup!

Caution! After loading partitions, the CRC doesn't check out!
Warning! Main partition table CRC mismatch! Loaded backup partition table
instead of main partition table!

Warning! One or more CRCs don't match. You should repair the disk!

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: damaged

Found invalid MBR and corrupt GPT. What do you want to do? (Using the
GPT MAY permit recovery of GPT data.)
 1 - Use current GPT
 2 - Create blank GPT

Your answer: 1

Command (? for help): p
Disk /dev/sdb: 83886080 sectors, 40.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 782396E4-DBD4-4C6C-B8C3-40B51B5CCA56
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 83886046
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        41945087   20.0 GiB    8300  Linux filesystem
   2        41945088        62916607   10.0 GiB    8300  Linux filesystem
   3        62916608        73402367   5.0 GiB     8300  Linux filesystem
   4        73402368        83886046   5.0 GiB     8300  Linux filesystem

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
[root@CentOS7 ~]#

We then recover the partition table:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[root@CentOS7 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   12G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   11G  0 part
  ├─centos-root 253:0    0  9.8G  0 lvm  /
  └─centos-swap 253:1    0  1.2G  0 lvm  [SWAP]
sdb               8:16   0   40G  0 disk
├─sdb1            8:17   0   20G  0 part
├─sdb2            8:18   0   10G  0 part
├─sdb3            8:19   0    5G  0 part
└─sdb4            8:20   0    5G  0 part
sr0              11:0    1 1024M  0 rom
[root@CentOS7 ~]#

To completely erase the GPT, start gdisk, go to expert mode with x and choose z to destroy GPT data structures:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
[root@CentOS7 ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): x

Expert command (? for help): z
About to wipe out GPT on /dev/sdb. Proceed? (Y/N): Y
GPT data structures destroyed! You may now partition the disk using fdisk or
other utilities.
Blank out MBR? (Y/N): Y
[root@CentOS7 ~]#

GPT table information is lost:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
[root@CentOS7 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   12G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   11G  0 part
  ├─centos-root 253:0    0  9.8G  0 lvm  /
  └─centos-swap 253:1    0  1.2G  0 lvm  [SWAP]
sdb               8:16   0   40G  0 disk
sr0              11:0    1 1024M  0 rom
[root@CentOS7 ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.

Command (? for help):

Now you are screwd up.

Formatting EXT4 File System

[root@CentOS7 ~]# gdisk -l /dev/sdb GPT fdisk (gdisk) version 0.8.10

Partition table scan: MBR: protective BSD: not present APM: not present GPT: present

Found valid GPT with protective MBR; using GPT. Disk /dev/sdb: 83886080 sectors, 40.0 GiB Logical sector size: 512 bytes Disk identifier (GUID): 8D92E802-639C-48E9-9773-59B6C417646C Partition table holds up to 128 entries First usable sector is 34, last usable sector is 83886046 Partitions will be aligned on 2048-sector boundaries Total free space is 2014 sectors (1007.0 KiB)

Number Start (sector) End (sector) Size Code Name 1 2048 83886046 40.0 GiB 8300 Linux filesystem [root@CentOS7 ~]#

We format using mkfs.ext4:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@CentOS7 ~]# mkfs
mkfs         mkfs.cramfs  mkfs.ext3    mkfs.minix
mkfs.btrfs   mkfs.ext2    mkfs.ext4    mkfs.xfs
[root@CentOS7 ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
2621440 inodes, 10485499 blocks
524274 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2157969408
320 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

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

[root@CentOS7 ~]#

We can add a label with -L :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@CentOS7 ~]# mkfs.ext4 -L DATA /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=DATA
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
2621440 inodes, 10485499 blocks
524274 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2157969408
320 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

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

[root@CentOS7 ~]#

We can change paramters after creation with tune2fs:

[root@CentOS7 ~]# tune2fs -L MYDATA /dev/sdb1 tune2fs 1.42.9 (28-Dec-2013) [root@CentOS7 ~]#

And verify with dumpe2fs /dev/sdb1 | less

Formatting XFS File System

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[root@CentOS7 ~]# mkfs.xfs --force /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=2621375 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=10485499, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=5119, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@CentOS7 ~]#

Check metadata with xfs_db -x (expert mode to allow writing the label):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[root@CentOS7 ~]# xfs_db -x /dev/sdb1
    xfs_db> uuid
UUID = dfc93e08-8618-42cf-bf7f-e045bb23e406
xfs_db> label
label = ""
xfs_db> label DATA2
writing all SBs
new label = "DATA2"
xfs_db> quit
[root@CentOS7 ~]#

Mount the partition

We use mount to mount the partition and start writing data to the disk:

1
2
3
4
5
6
7
8
9
[root@CentOS7 ~]# mount /dev/sdb1 /mnt/
[root@CentOS7 ~]# ls /mnt/
[root@CentOS7 ~]# touch /mnt/toto
[root@CentOS7 ~]# ls -lah /mnt/
total 0
drwxr-xr-x.  2 root root  18 Mar 22 06:23 .
dr-xr-xr-x. 18 root root 236 Mar 22 06:24 ..
-rw-r--r--.  1 root root   0 Mar 22 06:29 toto
[root@CentOS7 ~]#

Unmount the partition with umount:

1
2
3
4
5
[root@CentOS7 ~]# umount /mnt/
[root@CentOS7 ~]# ls -lah /mnt/
total 0
drwxr-xr-x.  2 root root   6 Apr 11  2018 .
dr-xr-xr-x. 18 root root 236 Mar 22 06:24 ..

Again, mount the partition to another place (mountpoint):

1
2
3
4
5
6
7
8
[root@CentOS7 ~]# mkdir -p /data/mydata
[root@CentOS7 ~]# mount /dev/sdb1 /data/mydata/
[root@CentOS7 ~]# ls -lah /data/mydata/
total 0
drwxr-xr-x. 2 root root 18 Mar 22 06:23 .
drwxr-xr-x. 3 root root 20 Mar 22 06:24 ..
-rw-r--r--. 1 root root  0 Mar 22 06:29 toto
[root@CentOS7 ~]#

We can retrieve some information using mount:

1
2
3
[root@CentOS7 ~]# mount | grep mydata
/dev/sdb1 on /data/mydata type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@CentOS7 ~]#

Get the disk UUID with:

[root@CentOS7 ~]# blkid /dev/sdb1
/dev/sdb1: LABEL="DATA2" UUID="dfc93e08-8618-42cf-bf7f-e045bb23e406" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="3f1df8be-1bfa-4141-b17c-622b02878104"

Mount the partition at boot with fstab by editing /etc/fstab and using the UUID from the previous command:

   #
   # /etc/fstab
   # Created by anaconda on Fri Mar 22 04:31:48 2019
   #
   # Accessible filesystems, by reference, are maintained under '/dev/disk'
   # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
   #
   /dev/mapper/centos-root /                       xfs     defaults        0 0
   UUID=804e7784-abb4-4a97-b542-6c2ead762524 /boot                   xfs     defaults        0 0
   /dev/mapper/centos-swap swap                    swap    defaults        0 0
   UUID="dfc93e08-8618-42cf-bf7f-e045bb23e406" /data/mydata/ xfs defaults 0 0

Now, check the partition is not mounted:

1
2
[root@CentOS7 ~]# mount | grep mydata
[root@CentOS7 ~]#

Use mount -a to mount all filesystems mentioned in fstab:

1
2
3
4
[root@CentOS7 ~]# mount -a
[root@CentOS7 ~]# mount | grep mydata
/dev/sdb1 on /data/mydata type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@CentOS7 ~]#

Mount options

Use man mount:

FILESYSTEM INDEPENDENT MOUNT OPTIONS:

noatime: Do not update inode access times on this filesystem (e.g., for faster access on the news spool to speed up news servers).

auto: Can be mounted with the -a option.

defaults: Use default options: rw, suid, dev, exec, auto, nouser, and async.

noexec: Do not allow direct execution of any binaries on the mounted filesystem.

group: Allow an ordinary (i.e., non-root) user to mount the filesystem if one of his groups matches the group of the device. This option implies the options nosuid and nodev (unless overridden by subsequent options, as in the option line group,dev,suid).

suid: Allow set-user-identifier or set-group-identifier bits to take effect.

See also mount options for xfs and mount options for ext4.

LVM Volumes

Cleanup

Let's clean our previous set-up by unmounting /dev/sdb:

1
[root@CentOS7 ~]# umount /dev/sdb1

Remove the entry in fstab:

1
[root@CentOS7 ~]# vi /etc/fstab

Then Delete the partition:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
[root@CentOS7 ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): x

Expert command (? for help): z
About to wipe out GPT on /dev/sdb. Proceed? (Y/N): Y
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
GPT data structures destroyed! You may now partition the disk using fdisk or
other utilities.
Blank out MBR? (Y/N): Y

Now create 4 new partitions with LVM type:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
[root@CentOS7 benoit]# gdisk  /dev/sdb
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.

Command (? for help): n
Partition number (1-128, default 1):
First sector (34-83886046, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-83886046, default = 83886046) or {+-}size{KMGTP}: +10G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8E00
Changed type of partition to 'Linux LVM'

Command (? for help): n
Partition number (2-128, default 2):
First sector (34-83886046, default = 20973568) or {+-}size{KMGTP}:
Last sector (20973568-83886046, default = 83886046) or {+-}size{KMGTP}: +10G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8E00
Changed type of partition to 'Linux LVM'

Command (? for help): n
Partition number (3-128, default 3):
First sector (34-83886046, default = 41945088) or {+-}size{KMGTP}:
Last sector (41945088-83886046, default = 83886046) or {+-}size{KMGTP}: +10G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8E00
Changed type of partition to 'Linux LVM'

Command (? for help): n
Partition number (4-128, default 4):
First sector (34-83886046, default = 62916608) or {+-}size{KMGTP}:
Last sector (62916608-83886046, default = 83886046) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8E00
Changed type of partition to 'Linux LVM'

Command (? for help): p
Disk /dev/sdb: 83886080 sectors, 40.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 2F259FCF-434B-45A5-A9FA-FAD8F6339B45
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 83886046
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        20973567   10.0 GiB    8E00  Linux LVM
   2        20973568        41945087   10.0 GiB    8E00  Linux LVM
   3        41945088        62916607   10.0 GiB    8E00  Linux LVM
   4        62916608        83886046   10.0 GiB    8E00  Linux LVM

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
[root@CentOS7 benoit]#

Create Logical Volumes

By default in CentOS, LVM is used:

Physical Volume

Scan for all physical volumes:

1
2
3
4
[root@CentOS7 ~]# pvscan
  PV /dev/sda2   VG centos          lvm2 [<11.00 GiB / 4.00 MiB free]
  Total: 1 [<11.00 GiB] / in use: 1 [<11.00 GiB] / in no VG: 0 [0   ]
[root@CentOS7 ~]#

Volume Group

Scan for all volume groups:

1
2
3
4
[root@CentOS7 ~]# vgscan
  Reading volume groups from cache.
  Found volume group "centos" using metadata type lvm2
[root@CentOS7 ~]#

Logical Volumes

Scan for all logical volume:

1
2
3
4
[root@CentOS7 ~]# lvscan
  ACTIVE            '/dev/centos/swap' [<1.20 GiB] inherit
  ACTIVE            '/dev/centos/root' [9.79 GiB] inherit
[root@CentOS7 ~]#

Create new partitions with LVM Type (8E00)

Create Physical Volume using the two first partitions in /dev/sdb1 and /dev/sdb2:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[root@CentOS7 ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
[root@CentOS7 ~]# pvcreate /dev/sdb2
  Physical volume "/dev/sdb2" successfully created.
[root@CentOS7 ~]# pvscan
  PV /dev/sda2   VG centos          lvm2 [<11.00 GiB / 4.00 MiB free]
  PV /dev/sdb1   VG vg1             lvm2 [<10.00 GiB / <10.00 GiB free]
  PV /dev/sdb2   VG vg1             lvm2 [<10.00 GiB / <10.00 GiB free]
  Total: 3 [<30.99 GiB] / in use: 3 [<30.99 GiB] / in no VG: 0 [0   ]
[root@CentOS7 ~]#

Create Volume Group:

1
2
[root@CentOS7 ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2
  Volume group "vg1" successfully created

Check the new Volume Group:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[root@CentOS7 ~]# vgscan
  Reading volume groups from cache.
  Found volume group "centos" using metadata type lvm2
  Found volume group "vg1" using metadata type lvm2
[root@CentOS7 ~]#

[root@CentOS7 ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   2   0 wz--n- <11.00g  4.00m
  vg1      2   0   0 wz--n-  19.99g 19.99g
[root@CentOS7 ~]#

Create logical volume:

1
2
3
4
5
6
7
[root@CentOS7 ~]# lvcreate -n lv1 -l 100%FREE vg1
  Logical volume "lv1" created.
[root@CentOS7 ~]# lvscan
  ACTIVE            '/dev/centos/swap' [<1.20 GiB] inherit
  ACTIVE            '/dev/centos/root' [9.79 GiB] inherit
  ACTIVE            '/dev/vg1/lv1' [19.99 GiB] inherit
[root@CentOS7 ~]#

Format the logical volume:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[root@CentOS7 ~]# mkfs.xfs /dev/vg1/lv1
meta-data=/dev/vg1/lv1           isize=512    agcount=4, agsize=1310208 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5240832, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@CentOS7 ~]#