Skip to content

06 磁盘

1. 查看

1.1 lsblk

lsblk(list block device)可以查看磁盘。

lsblk [options] [<device> ...]
-i 只使用ascii字符输出
-p 打印完整设备名称
-o --output 定制输出列
[root@dev tmp]# lsblk -p
NAME                                 MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
/dev/sda                               8:0    0   50G  0 disk
├─/dev/sda1                            8:1    0    1G  0 part /boot
└─/dev/sda2                            8:2    0   49G  0 part
  ├─/dev/mapper/cl_vdsm--dev-root    253:0    0   44G  0 lvm  /
  └─/dev/mapper/cl_vdsm--dev-swap    253:1    0    5G  0 lvm  [SWAP]
/dev/sdb                               8:16   0    1G  0 disk
└─/dev/sdb1                            8:17   0 1023M  0 part
  └─/dev/mapper/cl_vdsm--dev-test_lv 253:2    0  700M  0 lvm
/dev/sr0                              11:0    1    6G  0 rom
  • RM:是否为可移除设备
  • RO:是否为只读设备
  • MAJ:MIN:主要、次要设备号
  • rom:只读存储设备。镜像

1.2 blkid

blkid -L <label> | -U <uuid>

blkid [-c <file>] [-ghlLv] [-o <format>] [-s <tag>] [-t <token>] [<dev> ...]

blkid -p [-s <tag>] [-O <offset>] [-S <size>] [-o <format>] <dev> ...

blkid -i [-s <tag>] [-o <format>] <dev> ...
[root@dev tmp]# blkid
/dev/sda1: UUID="decc4fd4-a5a2-487d-bcdc-9125218509c4" TYPE="xfs"
/dev/sda2: UUID="rYAGTT-BICS-GotJ-tQR0-dx3q-hcR0-ZTwDgv" TYPE="LVM2_member"
/dev/sdb1: UUID="fC445p-tGHZ-ULDg-oQOJ-jSQa-mVtR-Q0KQfN" TYPE="LVM2_member"
/dev/mapper/cl_vdsm--dev-root: UUID="77579961-708a-449b-ac26-3f30f16fbfb5" TYPE="xfs"
/dev/mapper/cl_vdsm--dev-swap: UUID="66ef502a-63c0-4d37-b69a-c0ced8436c8f" TYPE="swap"
/dev/mapper/cl_vdsm--dev-test_lv: UUID="8192f79a-5fed-451c-bac8-5fadecb467da" TYPE="ext2"
/dev/sr0: PTTYPE="PMBR"

linux会给所有的设备分配一个唯一的UUID。

1.3 parted

列出磁盘的分区信息。

parted [OPTION]... [DEVICE [COMMAND [PARAMETERS]...]...]
[root@dev tmp]# parted -l
Model: VMware, VMware Virtual S (scsi)  # 磁盘厂商
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos                  # 分区表格式,还有MBR、GPT等格式
Disk Flags:
# 分区数据
Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1075MB  1074MB  primary  xfs          boot
 2      1075MB  53.7GB  52.6GB  primary               lvm

Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/cl_vdsm--dev-test_lv: 734MB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags:

Number  Start  End    Size   File system  Flags
 1      0.00B  734MB  734MB  ext2

......

2. 分区

MBR 分区表的第一个扇区很重要,包含了:

  1. 开机区(Master boot record):占用 446 bytes
  2. 分区表(partition table):占用 64 bytes

使用fdisk命令划分MBR分区表的磁盘或新建MBR分区;使用gdisk(GPT fdisk)命令划分GPT分区表的磁盘或新建GPT分区。

使用上面的两命令创建分区之后,通过cat /proc/partitions会发现分区表还未更新,此时可通过重启或执行:

[root@dev tmp]# partprobe -s
/dev/sda: msdos partitions 1 2
/dev/sdb: msdos partitions 1
/dev/sdc: gpt partitions 1 2 3

Warning

不要删除正在使用的分区,要先卸载再删除。

2.1 使用 parted 进行分区

该指令同时支持MBR分区和GPT分区。

parted [OPTION]... [DEVICE [COMMAND [PARAMETERS]...]...]

COMMANDs:
  mklabel,mktable LABEL-TYPE               创建新的Partition Table
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
    PART-TYPE:"primary", "logical", "extended"
    FS-TYPE:"btrfs", "ext2", "ext3", "ext4", "fat16", "fat32", "hfs", "hfs+", "linux-swap", "ntfs", "reiserfs", "xfs"
  print [devices|free|list,all|NUMBER]     
  rm NUMBER                                delete partition NUMBER
[root@dev tmp]# parted /dev/mapper/cl_vdsm--dev-test_lv2 print
Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/cl_vdsm--dev-test_lv2: 336MB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags:

Number  Start  End    Size   File system  Flags
 1      0.00B  336MB  336MB  ext4

# 更改磁盘分区类型,所有的数据都会丢失!!!
[root@dev tmp]# parted /dev/mapper/cl_vdsm--dev-test_lv2 mklabel gpt
Warning: The existing disk label on /dev/mapper/cl_vdsm--dev-test_lv2 will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? y
Information: You may need to update /etc/fstab.
[root@dev tmp]# parted /dev/mapper/cl_vdsm--dev-test_lv2 mktable msdos
Warning: The existing disk label on /dev/mapper/cl_vdsm--dev-test_lv2 will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? y
Information: You may need to update /etc/fstab.

# 新建分区
# 要先找出前一个分区的终点
[root@dev tmp]# parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End    Size   File system     Name        Flags
 1      1049kB  538MB  537MB  linux-swap(v1)  Linux swap
#                                       PART-TYPE FS-TYPE 起点 终点
[root@dev tmp]# parted /dev/sdc mkpart primary xfs 538MB 1050MB
[root@dev tmp]# parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size   File system     Name        Flags
 1      1049kB  538MB   537MB  linux-swap(v1)  Linux swap
 2      538MB   1050MB  512MB                  primary
[root@dev tmp]# partprobe
# 这里仍然要格式化,否则无法使用!?
[root@dev tmp]# mkfs.xfs /dev/sdc2

3. 格式化

磁盘阵列情况下的mkfs。TODO

4. 文件系统检验

fs发生错误时的救援。

Warning

正常情况下不要使用这些指令。

检查/修复时需要先卸载设备。但如果是根目录的话是无法卸载的,需要进入单人维护或救援模式,使用xfs_repair -d选项进行处理,检验完毕之后自动重启。

4.1 xfs

xfs_repair [options] device

Options:
  -f           The device is a file
  -l logdev    Specifies the device where the external log resides.
  -n           No modify mode, just checks the filesystem for damage.
  -t interval  Reporting interval in seconds.
  -d           Repair dangerously.

4.2 ext4

fsck.ext4 [-panyrcdfvtDFV] [-b superblock] [-B blocksize]
                [-I inode_buffer_blocks] [-P process_inode_size]
                [-l|-L bad_blocks_file] [-C fd] [-j external_journal]
                [-E extended-options] device

-p                   Automatic repair (no questions)
-n                   Make no changes to the filesystem
-y                   Assume "yes" to all questions
-f                   Force checking even if filesystem is marked clean
-D                  针对fs下的目录优化配置
-b superblock        使用备份的 superblock

5. 挂载

mount [options] <source> <directory>
mount <operation> <mountpoint> [<target>]

Options:
 -n, --no-mtab       Mount without writing in /etc/mtab.

 -o PARAMTERS
 PARAMETERS:
    async,sync      此fs使用async(异步)或sync(同步)的内存机制
    atime,notime    是否修改读取时间
    ro,rw           只读或读写
    auto,noauto     允许被 mount -a 自动挂载
    dev,nodev       允许建立设备文件
    suid,nosuid     允许 SUID/SGID
    exec,noexec     允许可执行的二进制文件
    user,nouser     允许所有人执行mount
    defaults         async,atime,rw,dev,suid,exec,nouser
    remount          重新挂载

Source:
 -L, --label <label>     synonym for LABEL=<label>
 -U, --uuid <uuid>       synonym for UUID=<uuid>
 LABEL=<label>           specifies device by filesystem label
 UUID=<uuid>             specifies device by filesystem UUID
 PARTLABEL=<label>       specifies device by partition label
 PARTUUID=<uuid>         specifies device by partition UUID
 <device>                specifies device by path
 <directory>             mountpoint for bind mounts (see --bind/rbind)
 <file>                  regular file for loopdev setup

Operations:
 -B, --bind              mount a subtree somewhere else (same as -o bind)
 -M, --move              move a subtree to some other place
 -R, --rbind             mount a subtree and all submounts somewhere else

重新挂载,在系统出错或更新参数时使用

mount -o remount,rw,auto /

挂载目录到另一个文件夹

[root@dev tmp]# mount --bind t1/ test_lv/
[root@dev tmp]# ll -id t1/ test_lv/
103706375 drwxr-xr-x 2 root root 6 Sep 27 15:02 t1/
103706375 drwxr-xr-x 2 root root 6 Sep 27 15:02 test_lv/

卸载umount

umount -a [options]
umount [options] <source> | <directory>

Options:
 -a, --all               unmount all filesystems
 -f, --force             force unmount (in case of an unreachable NFS system)
 -l, --lazy              detach the filesystem now, and cleanup all later
 -n, --no-mtab           don't write to /etc/mtab

卸载时除了指定挂载点之外,也可以指定设备进行卸载(不可对源目录进行卸载):

[root@dev tmp]# mount /dev/mapper/cl_vdsm--dev-test_lv test_lv/
[root@dev tmp]# umount -l -v test_lv/
umount: /tmp/test_lv (/dev/mapper/cl_vdsm--dev-test_lv) unmounted
[root@dev tmp]# mount /dev/mapper/cl_vdsm--dev-test_lv test_lv/
[root@dev tmp]# umount -v /dev/mapper/cl_vdsm--dev-test_lv
umount: /tmp/t1 (/dev/mapper/cl_vdsm--dev-test_lv) unmounted

/home分区是有用户登录就会使用,故需要将普通用户全部登出直接以root登录方可卸载。

5.1 自动挂载

/etc/fstab(filesystem table)的是自动挂载时所读取的文件。

[root@dev tmp]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Thu Nov  8 00:46:18 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   挂载点 文件系统类型 mount的文件系统参数  dump    fsck
/dev/mapper/cl_vdsm--dev-root /                       xfs     defaults        0 0
UUID=decc4fd4-a5a2-487d-bcdc-9125218509c4 /boot                   xfs     defaults        0 0
/dev/mapper/cl_vdsm--dev-swap swap                    swap    defaults        0 0
  • mount的文件系统参数:mount的-o选项中的内容
  • dump:能否被dump(用做备份的工具)指令作用。0表示不可用。
  • fsck:在早期的开机过程中,会去检验本机的fs,看其是否完整(clean),主要是通过fsck完成;但是xfs会自动检验,无需此动作,填0即可。

实际上fs的挂载是记录到/etc/mtab/proc/mounts文件中的,每次fs的挂载有变更时,也会更新这两个文件。在单人委会模式下,/是只读状态,无法更新/etc/fstab/etc/mtab,此时可以执行mount -n -o remount,rw /来解决。

5.2 挂载镜像

[root@dev tmp]# mount -o loop /dev/sr0 /mnt/
[root@dev tmp]# df -T
Filesystem                       Type      1K-blocks      Used  Available Use% Mounted on
······
/dev/sr0                         iso9660     4227724   4227724          0 100% /run/media/root/CentOS 7 x86_64
/dev/loop0                       iso9660     4227724   4227724          0 100% /mnt

6. 磁盘/文件系统的参数修改

6.1 创建设备

[root@dev tmp]# ll /dev/sd*
brw-rw---- 1 root disk 8,  0 Sep 27 15:32 /dev/sda
brw-rw---- 1 root disk 8,  1 Sep 27 15:32 /dev/sda1
brw-rw---- 1 root disk 8,  2 Sep 27 15:32 /dev/sda2
brw-rw---- 1 root disk 8, 16 Sep 27 15:32 /dev/sdb
brw-rw---- 1 root disk 8, 17 Sep 27 15:32 /dev/sdb1

输出的两个数字分别是主要(Major)设备id、次要(Minor)设备id,linux就是通过这两个数字来识别设备的。两者通过lsblk也能看到。

mknod [OPTION]... NAME TYPE [MAJOR MINOR]

TYPE:
  b      create a block (buffered) special file
  c, u   create a character (unbuffered) special file 输入设备,例如键鼠
  p      create a FIFO

6.2 改变xfs文件系统的参数

xfs_admin [ -eflpu ] [ -c 0|1 ] [ -L label ] [ -U uuid ] device

-l          Print the current filesystem label.
-u          Print the current filesystem UUID (Universally Unique IDentifier).
-L label    Set the filesystem label to label.
-U uuid     Set  the  UUID of the filesystem to uuid.
[root@dev tmp]# xfs_admin -l -u /dev/mapper/cl_vdsm--dev-test_lv
label = ""
UUID = f78f6d49-9fa6-41f1-af9c-6a845510addc
[root@dev tmp]# xfs_admin -L study-lv /dev/mapper/cl_vdsm--dev-test_lv
writing all SBs
new label = "study-lv"
[root@dev tmp]# xfs_admin -l /dev/mapper/cl_vdsm--dev-test_lv
label = "study-lv"

可以使用uuidgen生成UUID:

[root@dev tmp]# uuidgen
288e54b6-d55d-4ab4-90c2-b0c2879e2205

6.3 改变ext文件系统的参数

tune2fs  [  -l  ]  [  -c  max-mount-counts  ]  [  -e  errors-behavior  ] [ -f ] [ -i interval-between-checks ] [ -j ] [ -J journal-options ] [ -m reserved-blocks-percentage ] [ -o [^]mount-options[,...]  ] [ -r reserved-blocks-count ] [ -s sparse-super-flag ] [ -u user ] [ -g group ] [ -C mount-count ] [ -E extended-options ] [ -L volume-name ] [ -M last-mounted-directory ] [ -O [^]feature[,...]  ] [ -Q quota-options ] [ -T time-last-checked ] [ -U UUID ] device

-l          类似于 dumpe2fs -h, 列出superblock中的数据
-L          修改 Filesystem volume name
-U          修改 UUID
[root@dev tmp]# tune2fs -L ext4-lv /dev/mapper/cl_vdsm--dev-test_lv2
tune2fs 1.42.9 (28-Dec-2013)
[root@dev tmp]# tune2fs -l /dev/cl_vdsm-dev/test_lv2
tune2fs 1.42.9 (28-Dec-2013)
Filesystem volume name:   ext4-lv
......

7. 创建大文件制作loop设备

情景一:在磁盘分区时规划不好,导致一个分区已满而其它分区还有很多空间,此时可以制作一个loop设备来挂载使用。

[root@dev tmp]# dd if=/dev/zero of=loop-dev bs=1M count=512

[root@dev tmp]# mkfs.xfs -f loop-dev
# 为了向后兼容,最好加上 -o loop 选项
[root@dev tmp]# mount -o loop loop-dev /mnt
[root@dev tmp]# blkid
/tmp/loop-dev: UUID="c655a24b-46fd-4a8a-a823-a9f6ae088d3f" TYPE="xfs"
/dev/loop0: UUID="c655a24b-46fd-4a8a-a823-a9f6ae088d3f" TYPE="xfs"
[root@dev tmp]# df -Th
/dev/loop0                        xfs       509M   26M  483M   6% /tmp/mnt

8. 创建内存交换区 swap

个人pc是不需要该设备的,但是服务器一定要有。

有两种创建方式:

  1. 创建一个linux swap分区
  2. 创建一个虚拟内存文件

8.1 使用分区创建

# 创建分区
[root@dev tmp]# gdisk /dev/sdc
# 格式化swap
[root@dev tmp]# mkswap /dev/sdc1
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=c6533ffa-aca3-4cff-a2e9-bf30e258ee42
# 打开swap
[root@dev tmp]# swapon /dev/sdc1
# 列出打开的swap
[root@dev tmp]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sdc1                               partition       524284  0       -2
/dev/dm-1                               partition       5242876 0       -3
# 在 /etc/fstab 文件中添加(因为不是fs,故没有挂载点,直接填写swap):
/dev/sdc1 swap                    swap    defaults        0 0

8.2 使用文件创建

与创建loop文件一样,也可以创建swap文件。在不能使用磁盘之类的设备创建swap时可以使用这种方式。

[root@dev tmp]# dd if=/dev/zero of=swap-dev bs=1M count=128
[root@dev tmp]# mkswap swap-dev
Setting up swapspace version 1, size = 131068 KiB
no label, UUID=9edadcd9-0815-4173-a230-ce5f8327e611
[root@dev tmp]# chmod 0600 swap-dev
[root@dev tmp]# swapon swap-dev
[root@dev tmp]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sdc1                               partition       524284  0       -2
/dev/dm-1                               partition       5242876 0       -3
/tmp/swap-dev                           file    131068  0       -4
[root@dev tmp]# cat /etc/fstab
······
# 这里不能使用UUID来识别,因为系统只会查询块设备,不会查询文件
/tmp/swap-dev swap                    swap    defaults        0 0