Disk Management-Central

Partition related theoretical knowledge

What is mbr

About mbr
mbr boot: master boot record

Boot process

Power-on self-test (BIOS) (hardware check)
MBR boot 
GRUB menu (select a different kernel)
load the kernel (from the hard disk to the memory)
run the init process (the first process in the Linux system)
read the /etc/inittab configuration file (read Take the run level)
execute the /etc/rc.d/rc.sysinit script (the initialization script of the system, set the IP address)
execute the /etc/rc.d/rc script (based on the run level of the system, start different software at boot time)
Start the mingetty process (login interface)

where is mbr

The boot sector of the disk 0 head 0 track 1 sector
MBR boot 0 head 0 track 1 sector The first 446 bytes of
a sector is 512 bytes, the
first 446 bytes MBR Master boot record The
middle of the boot system The partition table of a 64-byte disk records where the disk partition starts and ends. The
last 2 bytes. The end of the partition means that the 55AA
partition table is
divided into 4 grids in the 64 bytes of the partition table. 16*4 is
stored in each grid Is the partition information (primary partition extended partition)
image.png

View memory usage

free -h
Linux caches the commands or files you have used (buffer cache) for you to improve the speed of next use

[root@nfsnobody ~]# free -h
             total       used       free     shared    buffers     cached
Mem:          980M       721M       259M       224K        59M       529M
 -buffer    +buffer 
 -cache     +cache
-/+ buffers/cache:       131M       848M   这个位置才是真正的剩余内存
Swap:         767M       196K       767M

disk partition

Three partitions

The primary partition has
up to 4 primary partitions and the
extended partitions can
not be used directly. They need to be divided into logical partitions before they can be used.
Logical partitions
must be divided under extended partitions before they can be used.
Primary partition primary Each partition occupies a 16-byte partition table. Generally, there must be an
extended partition for storing data. Extend cannot be used directly. Up to 1   
logical partition. logical Create a logical partition under the extended partition to store data

Disk partition naming rules

Primary partition 1-4
Extended partition 1-4 Generally 4
logical partitions, starting from 5
, the first primary partition
of the first SAS hard disk /dev/sda The second primary partition of the second SATA hard disk /devsdb2
The third sata The first logical partition of the hard disk /dev/sdc5
disk name example
first hard disk sda
second hard disk sdb
third hard disk sdc the
first primary partition of the
first hard disk : sda1 the first logic of the first hard disk Partition: sda5 The
second logical partition of the second hard disk: sdb6

The named
disk devices of the disk partition in the system are placed in the /dev/ directory
sas/sata/scsi/interface sd beginning with
ide interface hd

Partition method

No important data

/boot Boot partition 200M to store the boot information of the system. Kernel
swap swap partition to prevent memory from running out.
If your memory is less than 8G, swap is 1.5 times the memory. If your memory is greater than 8G, swap to 8G
/ how much is left in the root partition How many

A lot of important data

/boot Boot partition 200M stores the boot information of the system. Kernel
swap swap partition to prevent memory from running out. A temporary memory
if your memory is less than 8G, swap is 1.5 times the memory. If your memory is greater than 8G, swap to 8G
/ root partition 20G-200G
/data Store important data as much as left

Don't know if the data is important

/boot Boot partition 200M stores the boot information of the system. Kernel
swap swap partition to prevent memory from running out. A temporary memory
if your memory is less than 8G, swap is 1.5 times the memory. If your memory is greater than 8G, swap to 8G
/ root partition 20G-200G
The remaining space is not divided into who uses this server and who to partition

disk partition

The partition tool
   fdisk in linux is mainly for disks smaller than 2T (only the partition table is mbr)
   parted is mainly for disks larger than 2T (gpt) to
create the environment
and add two hard disks to the virtual machine to simulate the experimental environment

Detailed explanation of /etc/fatab

/etc/fatab 开机自动挂载
#
# /etc/fstab
# Created by anaconda on Tue Jul 10 18:21:44 2018
#
# 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
#
UUID=c4f4be67-f408-4f14-b01e-a0c0777932cb  /               ext4          defaults        1        1
UUID=8dc40910-35ed-4d1b-acd7-5dad0ad47746  /boot           ext4          defaults        1        2
UUID=09e2ce6c-7249-4239-8843-5ef9cc3a9a99  swap            swap          defaults        0        0
tmpfs                   /dev/shm        tmpfs         defaults            0                0
devpts                  /dev/pts        devpts        gid=5,mode=620      0                0
sysfs                   /sys            sysfs         defaults            0                0
proc                    /proc           proc          defaults            0                0
设备分区名称       第二列挂载点(目录) 文件系统类型      挂载参数     是否备份    是否开机磁盘检查

fdisk partition

Two hard drives have been added to the simulation environment just now

[root@nfsnobody ~]# fdisk -l|grep sd[a-c]:
Disk /dev/sda: 21.5 GB, 21474836480 bytes
Disk /dev/sdb: 213 MB, 213909504 bytes     这两块就是刚才添加的  213MB
Disk /dev/sdc: 213 MB, 213909504 bytes

fdisk related commands

The fdisk command and parameters
fdisk -l
      -u When the disk is partitioned, the sector is the unit by default. The default is according to the cylinder
  -c Turn off DOS compatibility mode.
fdisk internal command
m Display help
n new Create partition
p Display all partition information
d Delete partition
w Save and Quit
q Quit without saving

Create a primary partition

If you want to use all the space of the hard disk, just press Enter in the column of selecting the size without entering the content. The default is to
create a 10MB primary partition based on the maximum space.

fdisk -cu关闭dos兼容模式  磁盘分区的时候以扇区为单位默认是按照柱面
[root@nfsnobody ~]# fdisk -cu /dev/sdb 进入fdisk 编辑分区
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x303d0abc.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n     创建分区
Command action              选择分区类型
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1   分区号码
First sector (2048-417791, default 2048): 
从哪里开始 (直接回车 按默认)
Using default value 2048  默认选择第一个扇区
Last sector, +sectors or +size{K,M,G} (2048-417791, default 417791): +10M 
创建10M的分区  选择+10M
Command (m for help): p          p显示所有分区信息

Disk /dev/sdb: 213 MB, 213909504 bytes
64 heads, 32 sectors/track, 204 cylinders, total 417792 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: 0x303d0abc

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       22527       10240   83  Linux

Create extended and logical partitions

At work, the partitioning principle of extended partitions is:
create a 10MB primary partition as much as you have left, create an extended partition that uses all the capacity, create two 50MB logical partitions

[root@nfsnobody ~]# fdisk -cu /dev/sdb   进入fdisk 编辑分区
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x303d0abc.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): p  从上一步继续 创建两个逻辑分区  查看一下有的分区

Disk /dev/sdb: 213 MB, 213909504 bytes
64 heads, 32 sectors/track, 204 cylinders, total 417792 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: 0x303d0abc

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       22527       10240   83  Linux

Command (m for help): n                    创建新的分区
Command action
   e   extended   扩展分区
   p   primary partition (1-4)
e                                          创建扩展分区分区
Partition number (1-4): 2                  选择分区号码
First sector (22528-417791, default 22528): 
Using default value 22528
Last sector, +sectors or +size{K,M,G} (22528-417791, default 417791): 
Using default value 417791

Command (m for help): n              创建新的分区
Command action
   l   logical (5 or over)   逻辑分区,创建完扩展分区后只能创建逻辑分区
   p   primary partition (1-4)
l                                  选择l  创建逻辑分区 有扩展分区后才能创建逻辑分区

First sector (24576-417791, default 24576): 
Using default value 24576
Last sector, +sectors or +size{K,M,G} (24576-417791, default 417791): +50M   选择50M

Command (m for help): P              查看下所有的分区信息 

Disk /dev/sdb: 213 MB, 213909504 bytes
64 heads, 32 sectors/track, 204 cylinders, total 417792 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: 0x303d0abc
创建成功  一个10M的主分区  两个逻辑分区  
如果要删除的话  d删除 删除扩展分区 其下的所有逻辑分区都会被删除
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       22527       10240   83  Linux
/dev/sdb2           22528      417791      197632    5  Extended
/dev/sdb5           24576      126975       51200   83  Linux
/dev/sdb6          129024      231423       51200   83  Linux

fdisk partition and mount

Create a primary partition

Enter fdisk

[root@nfsnobody ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x0a515057.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
警告 : 可以关闭dos的兼容模式,使用扇区作为分区的默认单位  -cu ↓
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p            查看分区信息

Disk /dev/sdb: 213 MB, 213909504 bytes
64 heads, 32 sectors/track, 204 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0a515057

   Device Boot      Start         End      Blocks   Id  System
现在没有创建分区  所以为空

Create primary partition

Command (m for help): n      创建新分区
Command action
   e   extended
   p   primary partition (1-4)
p                              创建主分区
Partition number (1-4): 1      分区号码设置为一
First cylinder (1-204, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-204, default 204): 
Using default value 204

Save and exit after creation

Remember to save before exiting after editing fdisk

Command (m for help): w           w保存退出   fdisk编辑完 退出前记得保存 
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@nfsnobody ~]# fdisk -l 创建完成后 可以查看下

Format the file system

mkfs == make filesystem is to create a file system.

[root@nfsnobody ~]# partprobe /dev/sdb1   操作这一步 正常是不回任何提示的
[root@nfsnobody ~]# mkfs.ext4 /dev/sdb1
这一步会生成block和inode
使用mkfs.ext4  会报以下警告
This filesystem will be automatically checked every 25 mounts or
这个磁盘分区会被自动检查     每挂载25次或每隔180天 就会进行一次磁盘检查
180 days, whichever comes first.  Use tune2fs -c or -i to override.
关闭磁盘的自动检查                    使用tune2fs -c or -i可以关闭
-c 每挂载多少次进行一次磁盘检查 -c 0  关闭
-i 每隔多少天进行一次磁盘检查   -i 0  关闭

Turn off automatic checking of partitions

-c Check the disk every time it is mounted -c 0 close
-i Check the disk every few days -i 0 close

[root@nfsnobody ~]# tune2fs -c 0 -i 0 /dev/sdb1
tune2fs 1.41.12 (17-May-2010)
Setting maximal mount count to -1
Setting interval between checks to 0 seconds

Mount partition

Mount to /data without /data, remember to create it in advance

[root@nfsnobody ~]# mkdir -p /data           创建挂载目录
[root@nfsnobody ~]# mount /dev/sdb1 /data/   挂载
[root@nfsnobody ~]# df -h     挂载后记得检查
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        19G  1.7G   16G  10% /
tmpfs           491M     0  491M   0% /dev/shm
/dev/sda1       190M   35M  146M  19% /boot
/dev/sdb1       194M  1.8M  182M   1% /data    已挂载

Permanently mount

/etc/rc.local 开机自启动
追加到 /etc/fatab底部    /bin/mount /dev/sdb1 /data/

/etc/fatab 开机自动挂载
[root@nfsnobody ~]# vim /etc/fstab 
/dev/sdb1               /data                   ext4    defaults        0 0

parted create partition

Detailed parted command

parted 的使用  parted 还可以非交互式创建分区  
parted使用需谨慎  parted是实时保存的
parted可以创建gpt和 mbr两种格式的分区表
gpt
msdos(mbr) 
parted /dev/sdc 

print    显示分区信息
mktable  创建磁盘分区表  
         gpt  
         msdos(mbr)
mkpart   创建分区
rm       删除分区
q        退出不保存

parted help information

The parted built-in command h is to view help

[root@nfsnobody ~]# parted /dev/sdc
GNU Parted 2.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)    h

  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
 创建分区表
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
 创建一个分区
  mkpartfs PART-TYPE FS-TYPE START END     make a partition with a file system
 创建一个分区 分区带着文件系统
  print [devices|free|list,all|NUMBER]     display the partition table, available
        devices, free space, all found partitions, or a particular partition
 显示分区信息
  rm NUMBER                                delete partition NUMBER
 删除一个分区

PART-TYPE type that mkpart can use

GPT format can create N primary partitions, so all types are selected as primary partitions.

(parted) help mkpart                                        
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
    PART-TYPE is one of: primary, logical, extended
                         主分区 ,逻辑分区 ,扩展分区

Create partition

Parted creates a partition to take effect in real time, which is more dangerous.

[root@nfsnobody ~]# parted /dev/sdc   使用parted编辑分区
GNU Parted 2.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p                         p查看分区                                        
Error: /dev/sdc: unrecognised disk label   未识别	                                      
(parted) mktable gpt               创建 gpt 磁盘分区表
(parted) p                         p查看分区                                          
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 214MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start  End  Size  File system  Name  Flags

(parted) mkpart LH 0 10            创建 分区 分区名LH  从多少开始到多少结束                                       
Warning: The resulting partition is not properly aligned for best performance.
提示分区没有对齐,这个错误无视即可。
Ignore/ Cancel? i                    忽略信息 i   忽略/取消                                    
(parted) p                          p查看分区                                       
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 214MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name  Flags
 1      17.4kB  10.0MB  9983kB               LH

(parted) mkpart LH  10 20             创建 分区 分区名LH  从多少开始到多少结束                                     
(parted) p                            p查看分区                                      
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 214MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name  Flags
 1      17.4kB  10.0MB  9983kB               LH
 2      10.5MB  19.9MB  9437kB     

Delete partition

(parted) rm 2                      删除第二个分区                                         
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 214MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name  Flags
 1      17.4kB  10.0MB  9983kB               LH

Non-interactive partition creation

parted  后面直接跟上内置的命令 就可以直接使用 实现非交互式创建
parted  /dev/sdc  mktable gpt 
parted  /dev/sdc  mkpart primary 0 10 ignore 
parted  /dev/sdc  mkpart primary 10 20 
parted  /dev/sdc  p
[root@nfsnobody ~]# parted /dev/sdc p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 214MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name  Flags
 1      17.4kB  10.0MB  9983kB               LH
[root@nfsnobody ~]# parted /dev/sdc mkpart LH 50 100
Information: You may need to update /etc/fstab.                          
[root@nfsnobody ~]# parted /dev/sdc print     显示磁盘的格式
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 107MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  50.0MB  50.0MB               primary
 2      50.3MB  99.6MB  49.3MB               primary

Increase swap

Failure case The java program occupies a lot of memory. Start using swap. Swap is insufficient.
Increase swap.
View swap information free -h

[root@nfsnobody ~]# free -h
             total       used       free     shared    buffers     cached
Mem:          980M       275M       704M       236K        28M       132M
-/+ buffers/cache:       114M       866M
Swap:         767M         0B       767M  swap信息

Create a file and become swap

dd creates   
/dev/zero and keeps outputting zero
/dev/null black hole
if input file where to get data (input file)
of output file where to output
bs block size how many files to
copy each time count How many times to copy to
create a 100M file

[root@nfsnobody ~]# dd   if=/dev/zero     of=/tmp/100m     bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.441882 s, 237 MB/s
[root@nfsnobody ~]# ll -h /tmp/100m   检查 
-rw-r--r-- 1 root root 100M Aug 15 11:58 /tmp/100m   创建成功
[root@nfsnobody ~]# file /tmp/100m
/tmp/100m: data          现在的类型为data 数据块。

Make this file a swap

Create swap to make this file a swap (format)

mkswap
[root@nfsnobody ~]# mkswap /tmp/100m   
mkswap: /tmp/100m: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 102396 KiB
no label, UUID=9ee16d10-5fab-495d-a5fd-697b74ca360e
[root@nfsnobody ~]# file /tmp/100m         file命令显示这个文件的类型
/tmp/100m: Linux/i386 swap file (new style) 1 (4K pages) size 25599 pages
mkswap命令将文件类型格式化成swap格式

Activate the swap partition

swapon -s shows the composition of swap

[root@nfsnobody ~]# swapon /tmp/100m   挂载(激活)
[root@nfsnobody ~]# swapon -s
Filename	Type	Size	Used	Priority
/dev/sda2                               partition	786428	0	-1
/tmp/100m                               file	102396	0	-2
永久增加
/etc/rc.local 
/sbin/swapon /tmp/100m

Make the added swap permanent

Put the command into the /etc/rc.local startup file.
       Append the swapon /tmp/100m command to /etc/rc.local and 
write it to the /etc/fstab file. The
format is:
/tmp/100m swap swap defaults 0 0

Show uuid in the system

Use the blkid command to view the uuid of the system disk

[root@nfsnobody ~]# blkid
/dev/sda3: UUID="49bad9e9-cf33-4a15-ba84-4fd28e70bd29" TYPE="ext4"
/dev/sda1: UUID="7426d0f3-56d6-4fa6-a1c3-f2c8632bfbb8" TYPE="ext4"
/dev/sda2: UUID="46bc0a52-b13f-4845-8baa-90207849d5c5" TYPE="swap"
/dev/sdb1: UUID="7101630b-b325-49d1-92b9-0a500c2a07f6" TYPE="ext4"

Unformatted error

When performing some operations on the disk, you may be prompted that the disk is not formatted and needs to be formatted.

[root@nfsnobody ~]# tune2fs -c0 -i0 /dev/sdc
tune2fs 1.41.12 (17-May-2010)
tune2fs: Bad magic number in super-block while trying to open /dev/sdc
Couldn't find valid filesystem superblock.
没有找到可用的文件系统

Guess you like

Origin blog.csdn.net/qq_45533926/article/details/112237277