Disk management - expand swap space + disk quota + hard and soft links

Posted by barryflood22 on Fri, 03 Sep 2021 00:27:45 +0200

Disk management - supplemental expansion

Expand swap space

Add disk expansion

Process: new partition – > refresh partition table – > format – > activate

see information
[root@server1 ~]# free -h
            total      used      free    shared  buff/cache   available
Mem:         1.8G      147M      1.2G      9.6M        410M        1.5G
Swap:        2.0G        0B      2.0G
[root@server1 ~]# swapon -s
 file name				type		size	 Used	jurisdiction
/dev/dm-1      	partition 2097148  0  -2

New partition (use) sdb2 (as swap space)
[root@server1 ~]# fdisk /dev/sdb
#Welcome to fdisk (util Linux 2.23.2).

#The changes remain in memory until you decide to write the changes to disk.
#Think twice before using the write command.


#Command (enter m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
#Partition number (2-4, default 2):
#Start sector (4196352-10485759, the default is 4196352):
#The default value 4196352 will be used
#Last sector, + sector or +size{K,M,G} (4196352-10485759, the default is 10485759): + 2G
#Partition 2 is set to Linux type and size is set to 2 GiB

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

Calling ioctl() to re-read partition table.
#Synchronizing disks.
[root@server1 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0    5G  0 disk 
├─sdb1            8:17   0    2G  0 part 
└─sdb2            8:18   0    2G  0 part 
sr0              11:0    1  973M  0 rom  

Refresh partition table
[root@server1 ~]# partprobe /dev/sdb

format
[root@server1 ~]# mkswap /dev/sdb2
#Setting swap space version 1, size = 2097148 KiB
#No label, UUID=ce1f363b-a131-41d0-9123-345978b1e95e
[root@server1 ~]# blkid /dev/sdb2
/dev/sdb2: UUID="ce1f363b-a131-41d0-9123-345978b1e95e" TYPE="swap" 

Temporary activation swap Partition( swapoff (temporarily closed)
[root@server1 ~]# swapon /dev/sdb2 
[root@server1 ~]# swapon -s
 file name				type		size	Used	jurisdiction
/dev/dm-1        partition	2097148	0	-2
/dev/sdb2        partition	2097148	0	-3

Permanent intensification
[root@server1 ~]# vim /etc/fstab
#Add
/dev/sdb2   swap    swap    defaults    0   0

dd command simulates large files to expand capacity

dd usage

dd if=source file of=Target file bs=Size of replicated data count=Number of copies

Common examples
1.Backup device files
[root@server1 ~]# dd if=/dev/sr0 of=/centos7.6.iso
 1992704 was recorded+0 Read in
 1992704 was recorded+0 Writing
1020264448 byte(1.0 GB)Copied, 31.5341 Seconds, 32.4 MB/second

2.Simulate large files
#/dev/zero special equipment is generally used to simulate a large file and an endless stream of binary bit streams;
[root@server1 ~]# dd if=/dev/zero of=/tmp/swap_file bs=1M count=1024
 1024 recorded+0 Read in
 1024 recorded+0 Writing
1073741824 byte(1.1 GB)Copied, 2.74676 Seconds, 391 MB/second

3.Damage the system disk (do not die)
[root@server1 ~]# dd if=/dev/zero of=/dev/sda bs=1 count=512

Simulate large file expansion

Simulate large files
[root@server1 ~]# dd if=/dev/zero of=/tmp/swap_file bs=1M count=1024
#1024 + 0 reads are recorded
#1024 + 0 writes are recorded
#1073741824 bytes (1.1 GB) copied, 2.74676 seconds, 391 MB / sec

format
[root@server1 ~]# mkswap /tmp/swap_file 
#Setting swap space version 1, size = 1048572 KiB
#No label, UUID=b2d34da8-3388-4094-9a83-af6a6fe40cf3

activation
[root@server1 ~]# swapon -p 1 /tmp/swap_file
swapon: /tmp/swap_file: Unsafe permission 0644, 0600 is recommended.
[root@server1 ~]# chmod 0600 /tmp/swap_file 

see
[root@server1 ~]# free -h
            total      used      free    shared  buff/cache   available
Mem:         1.8G      149M       72M      9.6M        1.6G        1.5
Swap:        5.0G        0B      5.0G

Disk quota case

  • Function: limit the use of disk space by users or groups, such as file server, mail server

  • Case requirements:

    • Create three users user1, user2 and user3 with the same password and user name. The initial group is usergrp group.
    • Three users can get 500M disk space, more than 400M, give a prompt. The number of files cannot be more than 5. If it exceeds 3, you will be prompted
Create partition sdb1
[root@server1 ~]# fdisk /dev/sdb
#Command (enter m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
#Partition number (1-4, default 1):
#Start sector (2048-10485759, 2048 by default):
#The default value 2048 will be used
#Last sector, + sector or +size{K,M,G} (2048-10485759, the default is 10485759): + 2G
#Partition 1 is set to Linux type and size is set to 2 GiB
#Command (enter m for help): w
Calling ioctl() to re-read partition table.
#Synchronizing disks.

Refresh partition table
[root@server1 ~]# partprobe /dev/sdb
[root@server1 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0    5G  0 disk 
└─sdb1            8:17   0    2G  0 part 
sr0              11:0    1  973M  0 rom  

format
[root@server1 ~]# mkfs.ext4 /dev/sdb1

mount 
[root@server1 ~]# mkdir /mnt/mount_sdb1
[root@server1 ~]# mount /dev/sdb1 /mnt/mount_sdb1/
[root@server1 ~]# df -Th
 file system                type      Capacity used available used% Mount point
devtmpfs                devtmpfs  899M     0  899M    0% /dev
tmpfs                   tmpfs     910M     0  910M    0% /dev/shm
tmpfs                   tmpfs     910M  9.6M  901M    2% /run
tmpfs                   tmpfs     910M     0  910M    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        17G  1.9G   16G   11% /
/dev/sda1               xfs      1014M  194M  821M   20% /boot
tmpfs                   tmpfs     182M     0  182M    0% /run/user/0
/dev/sdb1               ext4      2.0G  6.0M  1.8G    1% /mnt/mount_sdb1

Create user
[root@server1 ~]# groupadd usergrp
[root@server1 ~]# useradd -g usergrp -b /mnt/mount_sdb1/ user01
[root@server1 ~]# useradd -g usergrp -b /mnt/mount_sdb1/ user02
[root@server1 ~]# useradd -g usergrp -b /mnt/mount_sdb1/ user03

Ensure that the file system supports quotas
[root@server1 ~]# mount |grep mount_sdb1
/dev/sdb1 on /mnt/mount_sdb1 type ext4 (rw,relatime,data=ordered
[root@server1 ~]# mount -o remount,usrquota,grpquota /mnt/mount_sdb1/
[root@server1 ~]# mount |grep mount_sdb1
/dev/sdb1 on /mnt/mount_sdb1 type ext4 (rw,relatime,quota,usrquota,grpquota,data=ordered)

Permanent support
[root@server1 ~]# vim /etc/fstab
/dev/sdb1   /mnt/mount_sdb1 ext4 defaults,usrquota,grpquota  0   0

start-up quota Disk quota
[root@server1 ~]# yum install -y quota
[root@server1 ~]# quotacheck -acug
 Option Description:
-a: All partitions (quota supported)
-c: establish
-u: user
-g: group
[root@server1 ~]# ll /mnt/mount_sdb1/
Total consumption 44
-rw------- 1 root   root     7168 8 December 21:12 aquota.group
-rw------- 1 root   root     7168 8 December 21:12 aquota.user
drwx------ 2 root   root    16384 8 December 21:06 lost+found
drwx------ 2 user01 usergrp  4096 8 December 21:07 user01
drwx------ 2 user02 usergrp  4096 8 December 21:07 user02
drwx------ 2 user03 usergrp  4096 8 December 21:07 user03
[root@server1 ~]# quotaon -a

Edit quota configuration
[root@server1 ~]# edquota -u user01
Disk quotas for user user01 (uid 1001):
  Filesystem    blocks     soft       hard    inodes   soft     hard
  /dev/sdb1      16       409600     512000     4        8       10

soft:It is also called soft limit. When the user reaches this limit, the system will give a warning, but it can still be written.
hard:Also known as hard limit, when this limit is reached, any writing is completely prohibited

The following three are disk space limit settings:
blocks: Space used, no setting required
soft: User space usage limit is a soft limit and needs to be set
hard: User space usage limit is a hard limit and needs to be set
 The following three are the limits of the total number of files:
inodes: The total number of existing files does not need to be set (the limit on the total number of files is created as described above)-4=4 Only 10 files are allowed to be created-4=6 (files)
soft: The total number of files limit is a soft limit and needs to be set
hard: The total number of files is a hard limit, which needs to be set

Note: space restrictions are based on k In.
[root@server1 ~]# edquota -p user01 -u user02 
[root@server1 ~]# edquota -p user01 -u user03

View restrictions
[root@server1 ~]# repquota -as
*** Report for user quotas on device /dev/sdb1
Block grace time: 7days; Inode grace time: 7days
                        Space limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --     20K      0K      0K              2     0     0       
user01    +-    451M    400M    500M  7days       5     8    10       
user02    --     16K    400M    500M              5     8    10       
user03    --     16K    400M    500M              4     8    10  

test
[user01@server1 ~]$ dd if=/dev/zero of=bigfile bs=1M count=450
sdb1: warning, user block quota exceeded.
450 recorded+0 Read in
 450 recorded+0 Writing
471859200 byte(472 MB)Copied, 7.2745 Seconds, 64.9 MB/second

[user02@server1 ~]$ touch file{1..6}
sdb1: warning, user file quota exceeded.
[user02@server1 ~]$ touch file7
sdb1: write failed, user file limit reached.
touch: could not be built"file7": Disk quota exceeded

Hard and soft links

Hard link

inode introduction

  • If I have a 500G hard disk, it will be divided into three parts after formatting: superblock, inode and data block

    • superblock: it will store the overall information of the file system, including the total amount, usage and remaining amount of inode/block, as well as the format and related information of the file system
    • Inode: records file attributes, such as file permissions, size, modification time, owner, etc. A file occupies an inode and records the block number of the file's data. By default, an inode table is 128 bytes and a block record consumes 4B. When the record is full, a new inode will be created for expansion.
    • Data block: it is really used to store data
  • If I want to view the contents of file1 file, the system does not search by file name, but by inode.

Specific process: open file1 – > View inode – > find the data block corresponding to inode – > read

View the of the file inode
[root@server1 tmp]# ll -i
 Total consumption 0
16784325 -rw-r--r-- 1 root root 0 8 December 21:11 file1
 
View file system inode Space occupied
[root@server1 tmp]# df -ih
 file system                Inode Used(I) available(I) Used(I)% Mount point
devtmpfs                 225K     399    225K       1% /dev
tmpfs                    228K       1    228K       1% /dev/shm
tmpfs                    228K     743    227K       1% /run
tmpfs                    228K      16    228K       1% /sys/fs/cgroup
/dev/mapper/centos-root  8.5M     39K    8.5M       1% /
/dev/sda1                512K     334    512K       1% /boot
tmpfs                    228K       1    228K       1% /run/user/0

Hard link overview

  • Hard link: hard link. Two files with the same inode are hard links to each other

  • Because the data block is found through the inode number, the two files that are hard linked to each other are essentially the same file, and the content is naturally the same. One file changes and the other changes naturally

  • Deleting a file does not affect other hard linked files

  • Every time we add a hard link, the number of inode connections of the file will increase by 1; And only when the number of inode connections of the file is 0 can it be completely deleted. In other words, since the hard link is actually a pointer to the inode of the original file, even if the original file is deleted, it can still be accessed through the hard link file.

  • It should be noted that due to technical limitations, we cannot link directory files across partitions

  • characteristic

    • Whether you modify the source file (test file) or the hard link file (test hard file), the data in the other file will change.
    • Whether you delete the source file or delete the hard link file, as long as there is another file, the file can be accessed.
    • Hard links do not create new inode information or change the total number of inodes.
    • Hard links cannot be established across file systems (partitions) because inode numbers are recalculated in different file systems
    • Hard links cannot link directories

establish

[root@server1 tmp]# ln file1 h_file1
[root@server1 tmp]# ll -i
 Total consumption 0
16784325 -rw-r--r-- 2 root root 0 8 December 21:11 file1
16784325 -rw-r--r-- 2 root root 0 8 December 21:11 h_file1
17184545 lrwxrwxrwx 1 root root 5 8 December 21:52 sl_file1 -> file1

Soft connection

summary

  • Soft link: symbolic link, also known as symbolic link

  • It only contains the pathname of the linked file, so it can link the directory file or link across the file system. However, when the original file is deleted, the linked file will also become invalid, which is the same as the "shortcut" in Windows system

Find file process:

Open the soft connection – > find file1 through the path – > View inode – > find the data block corresponding to inode – > read

  • characteristic

    • Whether you modify the source file (check) or the soft link file (check soft), the data in another file will change

    • Delete the soft link file and the source file will not be affected. If the original file is deleted, the soft link file will not find the actual data, so it shows that the file does not exist

    • The soft link will create its own inode information and block, but the actual file data is not stored in the block, but the file name and inode number of the source file are stored

    • Soft links can link directories

    • Soft links can span partitions

    • Soft connect is not the same file as the source file

Create and delete

establish
[root@server1 tmp]# touch file1
[root@server1 tmp]# ln -s file1 sl_file1
[root@server1 tmp]# ll -i
#Total consumption 0
16784325 -rw-r--r-- 1 root root 0 8 December 21:11 file1
17184545 lrwxrwxrwx 1 root root 5 8 December 21:16 sl_file1 -> file1

delete
[root@server1 tmp]# unlink sl_file1 
[root@server1 tmp]# ls
file1

Topics: Linux Operation & Maintenance CentOS