linux磁盘管理(MBR和GPT、接口类型)

存储硬件

在linux系统中底层硬件和系统存在映射关系,不同硬件在linux系统中存在不同的文件名称

硬盘设备和接口协议

  • 目前主流的存储接口
    • IDE: 已经在系统层面抽象成为SATA接口,电子集成驱动器。也成为并行ATA接口
    • SATA: 串口,serial vdvanced technology attachmeng
      • SATA硬盘驱动器的SATA端口和电源是分离的,中间是分开的
    • SAS: 串行SCSI, serial attached SCSI
      • 电源接口和端口在一体,同时电源和端口之间还有7个引脚为从端口,SAS兼容SATA接口

在这里插入图片描述

机械硬盘

  • 组成
    • 盘片: 数据实际写入的地方,分为扇区和磁道
      • 扇区(sector):512Bytes和4K
      • 磁道(track):
    • 机械手臂
    • 磁头
    • 主轴马达

分区

  • 主分区(primary partition): 至少要有一个或者4个,因为一个主分区表单独占用MBR
  • 扩展分区(Extend partition): 最多有一个
  • 逻辑分区: 由扩展分n个,编号从5开始,1-4为主分区或扩展分区编号。

分区表

MBR(MSDOS)

磁盘的第一个扇区为MBR分区,一块硬盘有且只有一个MBR分区。

  • 主要开机记录表(MBR): 安装开机管理程序,有446Bytes
  • 分区表(partition table): 记录硬盘分区的状态,有64Bytes

因为一个分区信息要用16Byte表示,一个硬盘最多分为4个主分区

在这里插入图片描述

一个硬盘最多分为3个主分区+1个扩展分区

在这里插入图片描述

三主+1扩展+三个逻辑分区

在这里插入图片描述

  • MBR存在的问题
    • 由于每个分区表用16Byte表示,所以无法抓取2.2T以上的磁盘容量
    • MBR在一块磁盘上唯一存在,破坏后很难恢复
    • MBR开机管理职能存放446Byte,无法容纳更多的开机程序。
GPT(GUID partition table)

GUID partition table,目前已有使用4K的扇区设计出现。

  • LBA: logical block address,默认512字节,逻辑区块位址。
    在这里插入图片描述
    GPT与 MBR 仅使用第一个 512Bytes 区块来纪录不同, GPT 使用了 34 个 LBA 区块来纪录分区
    信息!同时与过去 MBR 仅有一的区块,被干掉就死光光的情况不同, GPT 除了前面 34 个
    LBA 之外,整个磁盘的最后 33 个 LBA 也拿来作为另一个备份。
参数 作用
LBA0 与 MBR 模式相似的,这个相容区块也分为两个部份,一个就是跟之前 446 Bytes 相似的区块,储存了第一阶段的开机管理程序! 而在原本的分区表的纪录区内,这个相容模式仅放入一个特殊标志的分区,用来表示此磁盘为 GPT 格式之意。而不懂 GPT 分区表的磁盘管理程序, 就不会认识这颗磁盘,除非用户有特别要求要处理这颗磁盘,否则该管理理软件不能修改此分区信息,进一步保护了此磁盘喔
LBA1 这个部份纪录了分区表本身的位置与大小,同时纪录了备份用的 GPT 分区 (就是前面谈到的在最后 34 个 LBA 区块) 放置的位置, 同时放置了分区表的检验机制码(CRC32) ,操作系统可以根据这个检验码来判断 GPT 是否正确。若有错误,还可以通过这个纪录区来取得备份的 GPT(磁盘最后的那个备份区块) 来恢复 GPT 的正常运行
LBA2-33 从 LBA2 区块开始,每个 LBA 都可以纪录 4 笔分区纪录,所以在默认的情况下,总共以有 432 = 128 笔分区纪录喔!因为每个 LBA 有 512Bytes,因此每笔纪录用到 128Bytes 的间,除了每笔纪录所需要的识别码与相关的纪录之外,GPT 在每笔纪录中分别提供了 64bits 来记载开始结束的扇区号码,因此,GPT 分区表对於单一分区来说,他的最大容量限制就会在“ 264 512Bytes = 263 1KBytes = 233TB = 8 ZB ”,要注意 1ZB= 230TB 啦! 你说有没有够大了

GPT没有主分区和扩展分区等概念,可以收每个都是主分区,linux操作系统中fdisk不支持GPT,要使用parted和gdisk等工具,grub不识别GPT,grub2识别GPT,

EBR

磁盘MBR表中有且只有一个条目可以指定为扩展分区,和MBR不同的是,EBR记录形成一个链表,下个逻辑分区的EBR指向上一个逻辑分区的EBR,所以逻辑分区的数量受限于扩展磁盘空间的限制。

linux磁盘分区

lsblk 查看分区信息

  • lsblk不添加任何参数
[root@zhanghao-server ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 506.6G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 500M 0 part /boot
└─sda3 8:3 0 506.1G 0 part
  ├─rhel-root 253:0 0 504.1G 0 lvm /
  └─rhel-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 333.4G 0 disk
sr0 11:0 1 1024M 0 rom
sr1 11:1 1 1024M 0 rom
参数 意思
NAME 就是设备的文件名啰!会省略 /dev 等前导目录!
MAJ:MIN 其实核心认识的设备都是通过这两个代码来熟悉的!分别是主要:次要设备代码!
RM 是否为可卸载设备 (removable device) ,如光盘、USB 磁盘等等
SIZE 当然就是容量啰!
RO 是否为只读设备的意思
TYPE 是磁盘 (disk) 、分区 (partition) 还是只读存储器 (rom) 等输出
MOUTPOINT 就是前一章谈到的挂载点
  • lsblk -d不显示分区信息
[root@zhanghao-server ~]# lsblk -d
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 506.6G 0 disk
sdb 8:16 0 333.4G 0 disk
sr0 11:0 1 1024M 0 rom
sr1 11:1 1 1024M 0 rom
  • lsblk -f 列出文件系统的名称
[root@zhanghao-server ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1
├─sda2 xfs 57df6b0e-72e3-4180-8184-a5f52a01d1ff /boot
└─sda3 LVM2_member cWUujW-68xu-o1VZ-1aAB-PEk5-TYeJ-M6V8Ca
  ├─rhel-root xfs 713d1c18-f8f0-400e-b17f-c639855814cd /
  └─rhel-swap swap d241200a-44e7-4a61-85eb-81d9e0db9945 [SWAP]
sdb
sr0
sr1
  • lsblk -m查看磁盘权限信息
[root@zhanghao-server ~]# lsblk -m
NAME SIZE OWNER GROUP MODE
sda 506.6G root disk brw-rw----
├─sda1 1M root disk brw-rw----
├─sda2 500M root disk brw-rw----
└─sda3 506.1G root disk brw-rw----
  ├─rhel-root 504.1G root disk brw-rw----
  └─rhel-swap 2G root disk brw-rw----
sdb 333.4G root disk brw-rw----
sr0 1024M root cdrom brw-rw----
sr1 1024M root cdrom brw-rw----
  • lsblk -p列出完整的目录名称
[root@zhanghao-server ~]# lsblk -p
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
/dev/sda 8:0 0 506.6G 0 disk
├─/dev/sda1 8:1 0 1M 0 part
├─/dev/sda2 8:2 0 500M 0 part /boot
└─/dev/sda3 8:3 0 506.1G 0 part
  ├─/dev/mapper/rhel-root 253:0 0 504.1G 0 lvm /
  └─/dev/mapper/rhel-swap 253:1 0 2G 0 lvm [SWAP]
/dev/sdb 8:16 0 333.4G 0 disk
/dev/sr0 11:0 1 1024M 0 rom
/dev/sr1 11:1 1 1024M 0 rom
  • lsblk -t列出磁盘完整的信息
[root@zhanghao-server ~]# lsblk -t
NAME ALIGNMENT MIN-IO OPT-IO PHY-SEC LOG-SEC ROTA SCHED RQ-SIZE RA WSAME
sda 0 512 0 512 512 1 cfq 128 4096 0B
├─sda1 0 512 0 512 512 1 cfq 128 4096 0B
├─sda2 0 512 0 512 512 1 cfq 128 4096 0B
└─sda3 0 512 0 512 512 1 cfq 128 4096 0B
  ├─rhel-root 0 512 0 512 512 1 128 4096 0B
  └─rhel-swap 0 512 0 512 512 1 128 4096 0B
sdb 0 512 0 512 512 1 cfq 128 4096 0B
sr0 0 512 0 512 512 1 cfq 128 128 0B
sr1 0 512 0 512 512 1 cfq 128 128 0B

blkid 设备UUID

[root@zhanghao-server ~]# blkid
/dev/sda2: UUID="57df6b0e-72e3-4180-8184-a5f52a01d1ff" TYPE="xfs"
/dev/sda3: UUID="cWUujW-68xu-o1VZ-1aAB-PEk5-TYeJ-M6V8Ca" TYPE="LVM2_member"
/dev/mapper/rhel-root: UUID="713d1c18-f8f0-400e-b17f-c639855814cd" TYPE="xfs"
/dev/mapper/rhel-swap: UUID="d241200a-44e7-4a61-85eb-81d9e0db9945" TYPE="swap"

查看分区格式

  • parted device_name print看到partition table,msdos为MBR,gpt为GPT
[root@zhanghao-server ~]# parted /dev/sda print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 544GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
 1 1049kB 2097kB 1049kB primary
 2 2097kB 526MB 524MB primary xfs boot
 3 526MB 544GB 543GB primary lvm

磁盘分区工具gdisk和fdisk

gdisk

给GPT格式的磁盘分区

[root@zhanghao-server ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.6

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

Creating new GPT entries.

Command (? for help): ?
b back up GPT data to a file
c change a partition's name
d delete a partition
i show detailed information on a partition
l list known partition types
n add a new partition
o create a new empty GUID partition table (GPT)
p print the partition table
q quit without saving changes
r recovery and transformation options (experts only)
s sort partitions
t change a partition's type code
v verify disk
w write table to disk and exit
x extra functionality (experts only)
? print this menu
  • 分区步骤
root@zhanghao-server ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.6

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

Creating new GPT entries.

Command (? for help): ?
b back up GPT data to a file
c change a partition's name
d delete a partition
i show detailed information on a partition
l list known partition types
n add a new partition
o create a new empty GUID partition table (GPT)
p print the partition table
q quit without saving changes
r recovery and transformation options (experts only)
s sort partitions
t change a partition's type code
v verify disk
w write table to disk and exit
x extra functionality (experts only)
? print this menu

Command (? for help): n
Partition number (1-128, default 1):
First sector (34-699074014, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-699074014, default = 699074014) or {+-}size{KMGTP}: +20G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): L
0700 Microsoft basic data 0c01 Microsoft reserved 2700 Windows RE
4200 Windows LDM data 4201 Windows LDM metadata 7501 IBM GPFS
7f00 ChromeOS kernel 7f01 ChromeOS root 7f02 ChromeOS reserved
8200 Linux swap 8300 Linux filesystem 8301 Linux reserved
8e00 Linux LVM a500 FreeBSD disklabel a501 FreeBSD boot
a502 FreeBSD swap a503 FreeBSD UFS a504 FreeBSD ZFS
a505 FreeBSD Vinum/RAID a580 Midnight BSD data a581 Midnight BSD boot
a582 Midnight BSD swap a583 Midnight BSD UFS a584 Midnight BSD ZFS
a585 Midnight BSD Vinum a800 Apple UFS a901 NetBSD swap
a902 NetBSD FFS a903 NetBSD LFS a904 NetBSD concatenated
a905 NetBSD encrypted a906 NetBSD RAID ab00 Apple boot
af00 Apple HFS/HFS+ af01 Apple RAID af02 Apple RAID offline
af03 Apple label af04 AppleTV recovery af05 Apple Core Storage
be00 Solaris boot bf00 Solaris root bf01 Solaris /usr & Mac Z
bf02 Solaris swap bf03 Solaris backup bf04 Solaris /var
bf05 Solaris /home bf06 Solaris alternate se bf07 Solaris Reserved 1
bf08 Solaris Reserved 2 bf09 Solaris Reserved 3 bf0a Solaris Reserved 4
bf0b Solaris Reserved 5 c001 HP-UX data c002 HP-UX service
ed00 Sony system partitio ef00 EFI System ef01 MBR partition scheme
ef02 BIOS boot partition fb00 VMWare VMFS fb01 VMWare reserved
fc00 VMWare kcore crash p fd00 Linux RAID
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdb: 699074048 sectors, 333.3 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 15FF43EB-0AD7-4537-90C2-A61E79C4933F
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 699074014
Partitions will be aligned on 2048-sector boundaries
Total free space is 657130941 sectors (313.3 GiB)

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

  • 查看分区表信息
[root@zhanghao-server ~]# partprobe -s
/dev/sda: msdos partitions 1 2 3
/dev/sdb: gpt partitions 1 2
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@zhanghao-server ~]# cat /proc/partitions
major minor #blocks name

   8 0 531159296 sda
   8 1 1024 sda1
   8 2 512000 sda2
   8 3 530644992 sda3
   8 16 349537024 sdb
   8 17 20971520 sdb1
   8 18 20971520 sdb2
  11 0 3963904 sr0
  11 1 1048575 sr1
 253 0 528543744 dm-0
 253 1 2097152 dm-1
  • 删除分区
[root@zhanghao-server ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.6

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

Found valid GPT with protective MBR; using GPT.

Command (? for help): p
Disk /dev/sdb: 699074048 sectors, 333.3 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 15FF43EB-0AD7-4537-90C2-A61E79C4933F
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 699074014
Partitions will be aligned on 2048-sector boundaries
Total free space is 615187901 sectors (293.3 GiB)

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

Command (? for help): d
Partition number (1-2): 1

Command (? for help): d
Using 2

Command (? for help): p
Disk /dev/sdb: 699074048 sectors, 333.3 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 15FF43EB-0AD7-4537-90C2-A61E79C4933F
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 699074014
Partitions will be aligned on 2048-sector boundaries
Total free space is 699073981 sectors (333.3 GiB)

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

Command (? for help): w

fdisk用法和gdisk相同

parted分区命令

显示设备分区信息

parted device_name unit mb print

  • unit mb: 单位统一使用MB
[root@zhanghao-server ~]# parted /dev/sdb unit mb print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 357926MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
 1 1.05MB 21476MB 21475MB xfs Linux filesystem
 2 23623MB 25771MB 2147MB linux-swap(v1) Linux swap

修改磁盘为gpt
[root@zhanghao-server ~]# parted /dev/sdb mklabel gpt
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? yes
Information: You may need to update /etc/fstab.
创建主分区
[root@zhanghao-server ~]# parted /dev/sdb print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 358GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
 1 17.4kB 50.0GB 50.0GB primary

格式化mkfs

[root@study ~]# mkfs.xfs [-b bsize] [-d parms] [-i parms] [-l parms] [-L label] [-f] \
[-r parms] 设备名称
选项与参数:
关於单位:下面只要谈到“数值”时,没有加单位则为 Bytes 值,可以用 k,m,g,t,p (小写) 等来解释
比较特殊的是 s 这个单位,它指的是 sector 的“个数”喔!
-b :后面接的是 block 容量,可由 512 到 64k,不过最大容量限制为 Linux 的 4k 喔!
-d :后面接的是重要的 data section 的相关参数值,主要的值有:
        agcount=数值 :设置需要几个储存群组的意思(AG) ,通常与 CPU 有关
        agsize=数值 :每个 AG 设置为多少容量的意思,通常 agcount/agsize 只选一个设置即可
        file :指的是“格式化的设备是个文件而不是个设备”的意思!(例如虚拟磁盘)
        size=数值 :data section 的容量,亦即你可以不将全部的设备容量用完的意思
        su=数值 :当有 RAID 时,那个 stripe 数值的意思,与下面的 sw 搭配使用
        sw=数值 :当有 RAID 时,用于储存数据的磁盘数量(须扣除备份碟与备用碟)
        sunit=数值 :与 su 相当,不过单位使用的是“几个 sector(512Bytes大小) ”的意思
        swidth=数值 :就是 su*sw 的数值,但是以“几个 sector(512Bytes大小) ”来设置
-f :如果设备内已经有文件系统,则需要使用这个 -f 来强制格式化才行!
-i :与 inode 有较相关的设置,主要的设置值有:
       size=数值 :最小是 256Bytes 最大是 2k,一般保留 256 就足够使用了!
       internal=[0|1]:log 设备是否为内置?默认为 1 内置,如果要用外部设备,使用下面设置
       logdev=device :log 设备为后面接的那个设备上头的意思,需设置 internal=0 才可!
       size=数值 :指定这块登录区的容量,通常最小得要有 512 个 block,大约 2M 以上才行!
-L :后面接这个文件系统的标头名称 Label name 的意思!
-r :指定 realtime section 的相关设置值,常见的有:
       extsize=数值 :就是那个重要的 extent 数值,一般不须设置,但有 RAID 时,
       最好设置与 swidth 的数值相同较佳!最小为 4K 最大为 1G 。

[root@study ~]# mkfs.xfs /dev/vda4
meta-data=/dev/vda4 isize=256 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=262144, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
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
根据cpu格式化

提高文件系统并发能力,命令是通过-d vgcount来进行指定的,默认为4

  • 查询cpu核数
[root@zhanghao-server ~]# grep 'processor' /proc/cpuinfo | wc -l
1

  • 设置agcount
[root@zhanghao-server ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=1310720 blks
         = sectsz=512 attr=2, projid32bit=1
         = crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5242880, 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@zhanghao-server ~]# mkfs.xfs -d agcount=1 /dev/sdb1
mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
[root@zhanghao-server ~]# mkfs.xfs -f -d agcount=1 /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=1, agsize=5242880 blks
         = sectsz=512 attr=2, projid32bit=1
         = crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5242880, 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

  • 使用-t添加格式化文件系统类型
$ mkfs -t xfs/vfat/ext4

文件系统检验xfs_repair

文件系统因为突然断电等问题导致系统无法启动

xfs文件系统

使用xfs_repair命令可以进行修复,由于修复工作很是复杂,所以要求设备要在卸载的模式下进行,如果要对根目录进行修复,那么进入单用户模式下面使用-d进行修复。

### 检查流程

 1. Inode and inode blockmap (addressing) checks: bad magic number in inode, bad magic numbers in inode blockmap blocks, extents out of order, incor‐
              rect number of records in inode blockmap blocks, blocks claimed that are not in a legal data area of the filesystem, blocks that are claimed by
              more than one inode.

       2. Inode allocation map checks: bad magic number in inode map blocks, inode state as indicated by map (free or in-use) inconsistent with state indi‐
              cated by the inode, inodes referenced by the filesystem that do not appear in the inode allocation map, inode allocation map referencing blocks
              that do not appear to contain inodes.

       3. Size checks: number of blocks claimed by inode inconsistent with inode size, directory size not block aligned, inode size not consistent with
              inode format.

       4. Directory checks: bad magic numbers in directory blocks, incorrect number of entries in a directory block, bad freespace information in a direc‐
              tory leaf block, entry pointing to an unallocated (free) or out of range inode, overlapping entries, missing or incorrect dot and dotdot entries,
              entries out of hashvalue order, incorrect internal directory pointers, directory type not consistent with inode format and size.

       5. Pathname checks: files or directories not referenced by a pathname starting from the filesystem root, illegal pathname components.

       6. Link count checks: link counts that do not agree with the number of directory references to the inode.

       7. Freemap checks: blocks claimed free by the freemap but also claimed by an inode, blocks unclaimed by any inode but not appearing in the freemap.

       8. Super Block checks: total free block and/or free i-node count incorrect, filesystem geometry inconsistent, secondary and primary superblocks con‐
              tradictory.

  • 指令用法
$ xfs_repair 设备名称
  • -f : 后面的设备是文件,不是设备
  • -L:强制将log清空,这个存在丢数据的风险
  • -n: 单纯检查,并不进行修复
  • -d: 通常在单用户模式下面,对根目录进行检查和修复,存在风险
ext4文件系统
$ fsck.ext4 -fpD
-p: 修复存在交互回复y的时候,自动回复
-f: 强制检查
-D: 针对文件系统进行强制检查

挂载和卸载

我们在linux上面使用的都是目录,这个目录底层是硬件设备格式化成文件系统进行挂载完成的。

  • 一个文件系统不能重复挂载。
  • 挂载目录为空文件加

month命令

[root@study ~]# mount [-t 文件系统] LABEL='' 挂载点
[root@study ~]# mount [-t 文件系统] UUID='' 挂载点 # 鸟哥近期建议用这种方式喔!
[root@study ~]# mount [-t 文件系统] 设备文件名 挂载点
选项与参数:
-a :依照配置文件 [/etc/fstab](../Text/index.html#fstab) 的数据将所有未挂载的磁盘都挂载上来
-l :单纯的输入 mount 会显示目前挂载的信息。加上 -l 可增列 Label 名称!
-t :可以加上文件系统种类来指定欲挂载的类型。常见的 Linux 支持类型有:xfs, ext3, ext4,reiserfs, vfat, iso9660(光盘格式) , nfs, cifs, smbfs (后三种为网络文件系统类型)
-n :在默认的情况下,系统会将实际挂载的情况实时写入 /etc/mtab 中,以利其他程序的运行。但在某些情况下(例如单人维护模式) 为了避免问题会刻意不写入。此时就得要使用 -n 选项。
-o :后面可以接一些挂载时额外加上的参数!比方说帐号、密码、读写权限等:
    async, sync: 此文件系统是否使用同步写入 (sync) 或非同步 (async) 的内存机制,请参考[文件系统运行方式(../Text/index.html#harddisk-filerun)。默认为 async。
    atime,noatime: 是否修订文件的读取时间(atime) 。为了性能,某些时刻可使用 noatime
    ro, rw: 挂载文件系统成为只读(ro) 或可读写(rw)
    auto, noauto: 允许此 filesystem 被以 mount -a 自动挂载(auto)
    dev, nodev: 是否允许此 filesystem 上,可创建设备文件? dev 为可允许
    suid, nosuid: 是否允许此 filesystem 含有 suid/sgid 的文件格式?
    exec, noexec: 是否允许此 filesystem 上拥有可执行 binary 文件?
    user, nouser: 是否允许此 filesystem 让任何使用者执行 mount ?一般来说,mount 仅有 root 可以进行,但下达 user 参数,则可让一般 user 也能够对此 partition 进行 mount 。
    defaults: 默认值为:rw, suid, dev, exec, auto, nouser, and async
    remount: 重新挂载,这在系统出错,或重新更新参数时,很有用! 
  • 根据/etc/fstab中挂载信息进行挂载
$ mount -a
挂载U盘(vfat)
### uuid通过blkid查询出来
$ mount -o codepage=950,iocharset=utf8 UUID="35BC-6D6B" /data/usb 

### codepage=950可以显示中文
### iocharset=utf8指定中文为万国码
mount目录

比如我有个比较深的目录/zhanghao/test1/test2/test3挂载到/zhanghao/test/

[root@zhanghao-server ~]# ls -lid /zhanghao/test1/test2/test3 /zhanghao/test
      204 drwxr-xr-x. 2 root root 6 Oct 4 00:54 /zhanghao/test
806243582 drwxr-xr-x. 2 root root 6 Oct 4 00:54 /zhanghao/test1/test2/test3
  • mount --bind

可以看到两个链接的是同一个inode,说明两个指向设备是相同的。

[root@zhanghao-server ~]# ls -lid /zhanghao/test1/test2/test3 /zhanghao/test
806243582 drwxr-xr-x. 2 root root 22 Oct 4 00:55 /zhanghao/test
806243582 drwxr-xr-x. 2 root root 22 Oct 4 00:55 /zhanghao/test1/test2/test3

修改磁盘信息

  • mknod修改major和minor两个值
  • xfs_admin: 修改磁盘的UUID和Label name
  • uuidgen:生成新的uuid
  • tune2fs: 修改ext4文件系统的UUID和Label name

设置开机自动挂载

/etc/fstab文件格式

[设备/UUID] [挂载点] [文件系统] [文件系统参数] [dump] [fsck]

  • 设备/UUID: 设备名称,可以是UUID,设备文件名称,LABEL

  • 挂载点: 一定是一个目录

  • 文件系统: 通用xfs,ext4,swap等

  • 文件系统参数: 一般为default

  • dump和fsck:开机过程中是否做备份和扇区检查,我们都是关闭的 0

swap分区机制

swap呢村交换空间,服务器内存在使用到一定权限的时候,会使用swap分区作为备用内存。一般情况是不会用到的。

创建swap分区
[root@zhanghao-server ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs 504G 1.6G 503G 1% /
devtmpfs devtmpfs 2.1G 0 2.1G 0% /dev
tmpfs tmpfs 2.1G 0 2.1G 0% /dev/shm
tmpfs tmpfs 2.1G 8.5M 2.1G 1% /run
tmpfs tmpfs 2.1G 0 2.1G 0% /sys/fs/cgroup
/dev/sda2 xfs 497M 135M 363M 28% /boot
tmpfs tmpfs 430M 0 430M 0% /run/user/0
/dev/sr0 iso9660 3.8G 3.8G 0 100% /mnt
[root@zhanghao-server ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.6

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

Found valid GPT with protective MBR; using GPT.

Command (? for help): n
Partition number (2-128, default 2):
First sector (34-699074014, default = 41945088) or {+-}size{KMGTP}: +2G
Last sector (46139392-699074014, default = 699074014) or {+-}size{KMGTP}: +2G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): L
0700 Microsoft basic data 0c01 Microsoft reserved 2700 Windows RE
4200 Windows LDM data 4201 Windows LDM metadata 7501 IBM GPFS
7f00 ChromeOS kernel 7f01 ChromeOS root 7f02 ChromeOS reserved
8200 Linux swap 8300 Linux filesystem 8301 Linux reserved
8e00 Linux LVM a500 FreeBSD disklabel a501 FreeBSD boot
a502 FreeBSD swap a503 FreeBSD UFS a504 FreeBSD ZFS
a505 FreeBSD Vinum/RAID a580 Midnight BSD data a581 Midnight BSD boot
a582 Midnight BSD swap a583 Midnight BSD UFS a584 Midnight BSD ZFS
a585 Midnight BSD Vinum a800 Apple UFS a901 NetBSD swap
a902 NetBSD FFS a903 NetBSD LFS a904 NetBSD concatenated
a905 NetBSD encrypted a906 NetBSD RAID ab00 Apple boot
af00 Apple HFS/HFS+ af01 Apple RAID af02 Apple RAID offline
af03 Apple label af04 AppleTV recovery af05 Apple Core Storage
be00 Solaris boot bf00 Solaris root bf01 Solaris /usr & Mac Z
bf02 Solaris swap bf03 Solaris backup bf04 Solaris /var
bf05 Solaris /home bf06 Solaris alternate se bf07 Solaris Reserved 1
bf08 Solaris Reserved 2 bf09 Solaris Reserved 3 bf0a Solaris Reserved 4
bf0b Solaris Reserved 5 c001 HP-UX data c002 HP-UX service
ed00 Sony system partitio ef00 EFI System ef01 MBR partition scheme
ef02 BIOS boot partition fb00 VMWare VMFS fb01 VMWare reserved
fc00 VMWare kcore crash p fd00 Linux RAID
Hex code or GUID (L to show codes, Enter = 8300): 8200
Changed type of partition to 'Linux swap'

Command (? for help): p
Disk /dev/sdb: 699074048 sectors, 333.3 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 15FF43EB-0AD7-4537-90C2-A61E79C4933F
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 699074014
Partitions will be aligned on 2048-sector boundaries
Total free space is 652936637 sectors (311.3 GiB)

Number Start (sector) End (sector) Size Code Name
   1 2048 41945087 20.0 GiB 8300 Linux filesystem
   2 46139392 50333695 2.0 GiB 8200 Linux swap

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@zhanghao-server ~]# free -h
              total used free shared buff/cache available
Mem: 4.2G 159M 3.7G 8.4M 378M 3.8G
Swap: 2.0G 0B 2.0G

[root@zhanghao-server ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 506.6G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 500M 0 part /boot
└─sda3 8:3 0 506.1G 0 part
  ├─rhel-root 253:0 0 504.1G 0 lvm /
  └─rhel-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 333.4G 0 disk
├─sdb1 8:17 0 20G 0 part
└─sdb2 8:18 0 2G 0 part
sr0 11:0 1 3.8G 0 rom /mnt
sr1 11:1 1 1024M 0 rom
[root@zhanghao-server ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=7a709f30-d27d-4263-a168-5402844945cd

[root@zhanghao-server ~]# free -h
              total used free shared buff/cache available
Mem: 4.2G 161M 3.7G 8.4M 377M 3.8G
Swap: 2.0G 0B 2.0G

[root@zhanghao-server ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 2097148 0 -1

[root@zhanghao-server ~]# swapon /dev/sdb2

[root@zhanghao-server ~]# free -h
              total used free shared buff/cache available
Mem: 4.2G 161M 3.7G 8.4M 377M 3.8G
Swap: 4.0G 0B 4.0G

######关闭swap

[root@zhanghao-server ~]# swapoff /dev/sdb2
[root@zhanghao-server ~]# free -h
              total used free shared buff/cache available
Mem: 4.2G 161M 3.7G 8.4M 378M 3.8G
Swap: 2.0G 0B 2.0G
swap自动挂载
### /etc/fstab
/dev/mapper/rhel-swap swap swap defaults 0 0

猜你喜欢

转载自blog.csdn.net/hao_zhang_shrek/article/details/106317752