Posted On February 23, 2020

Linux: Disks & Memory Management

kimconnect 0 comments
blog.KimConnect.com >> Linux >> Linux: Disks & Memory Management

Step 1: Discovery of Disks on a System

# Using List hardware to gather general information about disks
[root@server01 kim]# lshw -class disk -class storage -short
H/W path    Device    Class     Description
=====================================================
/0/100/17   scsi1    storage    Cannon Point-LP SATA Controller [AHCI Mode]
/0/100/17/0  /dev/sda   disk      16GB KINGSTON SNS4151
/0/100/17/1  /dev/sdb   disk      512GB Samsung SSD 850

# Listing blocks of storage
[root@server01 kim]# lsblk
NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0  0 14.9G 0 disk 
├─sda1    8:1  0   4M 0 part 
├─sda2    8:2  0   4G 0 part 
├─sda3    8:3  0  7.5G 0 part 
├─sda5    8:5  0  250M 0 part 
├─sda6    8:6  0  250M 0 part 
├─sda7    8:7  0  110M 0 part 
├─sda8    8:8  0  286M 0 part 
└─sda9    8:9  0  2.5G 0 part 
sdb      8:16  0  477G 0 disk 
├─sdb1    8:17  0  600M 0 part /boot/efi
├─sdb2    8:18  0   1G 0 part /boot
└─sdb3    8:19  0 475.4G 0 part 
 ├─cl-root 253:0  0  50G 0 lvm /
 ├─cl-swap 253:1  0 15.7G 0 lvm [SWAP]
 └─cl-home 253:2  0 409.6G 0 lvm /home

# Showing disk free (common cli command to display utilization)
[root@server01 kim]# df
Filesystem     1K-blocks  Used Available Use% Mounted on
devtmpfs       16323804    0 16323804  0% /dev
tmpfs        16342180    0 16342180  0% /dev/shm
tmpfs        16342180  17248 16324932  1% /run
tmpfs        16342180    0 16342180  0% /sys/fs/cgroup
/dev/mapper/cl-root 52403200 3879908 48523292  8% /
/dev/mapper/cl-home 429313216 3027764 426285452  1% /home
/dev/sdb2       999320 176928  753580 20% /boot
/dev/sdb1       613184  6908  606276  2% /boot/efi
tmpfs         3268436    0  3268436  0% /run/user/0
overlay       52403200 3879908 48523292  8% /var/lib/docker/overlay2/b65c47ea3c25076b8addfaa1ec534911a74acffc0d9ad9a42a911672f02998d9/merged
tmpfs         3268436    0  3268436  0% /run/user/1000
overlay       52403200 3879908 48523292  8% /var/lib/docker/overlay2/87a24ea86dfc204e2c21e751dbe45405db38c429952f9b0188dd9b57d2918e61/merged

[root@server01 kim]# fdisk -l
Disk /dev/sda: 14.9 GiB, 16013942784 bytes, 31277232 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
Disklabel type: gpt
Disk identifier: 943BDACC-FCD6-5F43-8F21-04414CD0906D
--- Long output truncated ---

# Filtering result to show only disks information
[root@server01 kim]# fdisk -l | grep '^Disk'
Disk /dev/sda: 14.9 GiB, 16013942784 bytes, 31277232 sectors
Disklabel type: gpt
Disk identifier: 943BDACC-FCD6-5F43-8F21-04414CD0906D
Disk /dev/sdb: 477 GiB, 512110190592 bytes, 1000215216 sectors
Disklabel type: gpt
Disk identifier: 4167C3C8-E16D-4585-8E5C-510D8B55CBA1
Disk /dev/mapper/cl-root: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk /dev/mapper/cl-swap: 15.7 GiB, 16886267904 bytes, 32980992 sectors
Disk /dev/mapper/cl-home: 409.6 GiB, 439831494656 bytes, 859045888 sectors

# Using another disk partitioning tool
[root@server01 kim]# parted -l
Model: ATA KINGSTON SNS4151 (scsi)
Disk /dev/sda: 16.0GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 
Number Start  End   Size  File system Name Flags
 1   32.8kB 4194kB 4162kB fat16       boot, esp
 5   4211kB 266MB  262MB  fat16       msftdata
 6   266MB  528MB  262MB  fat16       msftdata
 7   528MB  644MB  115MB
 8   644MB  944MB  300MB  fat16       msftdata
 9   944MB  3628MB 2684MB
 2   3628MB 7922MB 4294MB fat16       msftdata
 3   7922MB 16.0GB 8092MB
Model: ATA Samsung SSD 850 (scsi)
Disk /dev/sdb: 512GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 
Number Start  End   Size  File system Name         Flags
 1   1049kB 630MB  629MB  fat32    EFI System Partition boot, esp
 2   630MB  1704MB 1074MB ext4
 3   1704MB 512GB  510GB                   lvm

Step 2: Make Changes to a Disk

# Partitioning a Disk
targetDisk='/dev/sda'
fdisk $targetDisk

# Showing partitions
Command (m for help): F
Unpartitioned space /dev/sda: 14.9 GiB, 16012877312 bytes, 31275151 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
Start   End Sectors Size
 2048 31277198 31275151 14.9G

# Deleting partition
Command (m for help): d
Partition number (3,5-9, default 9): 1
Partition 1 has been deleted.

# Creating a partition
Command (m for help): n
Partition number (1-128, default 1): 
First sector (34-31277198, default 2048): 34
Last sector, +sectors or +size{K,M,G,T,P} (34-31277198, default 31277198):  
Created a new partition 1 of type 'Linux filesystem' and of size 14.9 GiB.

# Saving partition table changes
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

# Formatting a Disk
targetPartition='/dev/sda1'
mkfs.ext4 $targetPartition

[root@server01 kim]# mkfs.ext4 $targetPartition
mke2fs 1.44.6 (5-Mar-2019)
Discarding device blocks: done               
Creating filesystem with 3909645 4k blocks and 979200 inodes
Filesystem UUID: 3cf16eda-8ded-4422-80e9-1e9f323c64fe
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208
Allocating group tables: done               
Writing inode tables: done               
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done 

Step 3: modifying system memory using swap

# Creating a swap directory
diskName='/swap'
mkdir $diskName
mount $targetPartition $diskName

# Updating /etc/fstab file to create automount
echo "$targetPartition              $diskName         ext3    defaults        1 2" >> /etc/fstab

# Labeling the disk
e2label $targetPartition  $diskName

# Allocating swapfile, setting appropriate permissions, creating swapfile
blockCount=$(df -P $targetPartition | tail -1 | awk '{print int($4/1024)}') # Obtain available disk space of 
blockSize=1MiB
swapfile='/swap/swapfile'
sudo dd if=/dev/zero of=$swapfile bs=$blockSize count=$blockCount   #allocate 
chmod 600 $swapfile       #secure the directory
mkswap $swapfile       #make swapfile in the /swapfile directory
swapon $swapfile        #configure system to use /swapfile

# Setting a swapfile to load on boot
echo "$swapfile none swap defaults 0 0" >> /etc/fstab

# Removing a swapfile
swapfileToRemove='/dev/mapper/cl-swap'
swapoff -v $swapfileToRemove
rm $swapfileToRemove -f
sed -i.bak '/\/dev\/mapper\/cl-swap/d' /etc/fstab # Assuming swapfile to remove is /dev/mapper/cl-swap

# Validation
[root@server01 kim]# swapon -s
Filename                                Type            Size    Used    Priority
/swap/swapfile                          file            14487548        0       -2

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Ubuntu 19.04: How to Install Adobe Acrobat Reader

Back  in the days when Adobe Acrobat Reader for Linux was supported and available on…

How to Install Chrome on Ubuntu 20.04

Ubuntu and its variants are pre-packaged with Mozilla's Firefox Web Browser. Although that is a…

CentOS 8: How to Only Apply Security Updates

[cuilo@test-box ~]# sudo yum --security update CentOS-8 - AppStream 12 kB/s | 4.3 kB 00:00…