磁盘命名
IDE驱动器
/dev/hda、/dev/hdb、/dev/hdc...
/dev/hda1、/dev/hda2、/dev/hda3...
SATA驱动器
/dev/sda、/dev/sdb、/dev/sdc...
/dev/sda1、/dev/sda2、/dev/sda3...
创建分区
fdisk
注意:需要超级用户权限
所有操作都在内存中进行,只要不w就不会保存到磁盘
常用参数 | 描述 |
d | 删除分区 |
l | 显示可用分区类型 |
m | 帮助 |
n | 添加一个新分区 |
p | 显示当前分区表 |
q | 退出 |
t | 修改分区的系统ID |
w | 将分区表写入磁盘 |
分区可按主分区或扩展分区创建,主分区文件系统可直接格式化,扩展分区只能容纳其他逻辑分区。
扩展分区出现的原因是每个存储设备上只能有4个主分区,可以通过在扩展分区上创建多个逻辑分区进行扩展。
[root@tzPC ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos Disk identifier: 0x892ef256 Device Boot Start End Blocks Id System Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-41943039, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +2G Partition 1 of type Linux and of size 2 GiB is set Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos Disk identifier: 0x892ef256 Device Boot Start End Blocks Id System /dev/sdb1 2048 4196351 2097152 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
Id列定义了Linux怎样对待该分区,默认是83,可使用l参数查看,对应的是Linux文件系统
某些发行版生成新分区后并不会更新分区,需要使用partprob或hdparm命令,或者重启系统才会更新分区表
创建文件系统
查看本机文件系统几个命令
df -T parted -l blkid lsblk -f
创建ext4文件系统类型,相当于格式化分区
[root@tzPC ~]# mkfs.ext4 /dev/sdb1 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 131072 inodes, 524288 blocks 26214 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=536870912 16 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done
挂载
[root@tzPC ~]# mkdir /mnt/sdb1 [root@tzPC ~]# mount -t ext4 /dev/sdb1 /mnt/sdb1/ [root@tzPC ~]# df -Th /mnt/sdb1 Filesystem Type Size Used Avail Use% Mounted on /dev/sdb1 ext4 2.0G 6.0M 1.8G 1% /mnt/sdb1
文件系统的检查与修复
每个文件系统都有各自恢复命令,为了简便,有一个通用命令检查和修复大部分类型的Linux文件系统。
fsck
使用/etc/fstab文件来自动决定正常挂载到系统上的存储设备的文件系统,如果未挂载,需要使用-t指定文件系统类型。
使用前必须先卸载文件系统,检查完成后重新挂载。
逻辑卷管理
物理卷(PV)
硬盘
卷组(VG)
多个物理卷可以形成一个卷组,逻辑卷管理系统将卷组视为一个物理盘,而卷组可以是由分布在多个物理硬盘上的多个物理分区组成。
逻辑卷(LV)
可格式化挂载
定义物理卷
修改分区系统ID使其转换成Linux LVM格式
[root@tzPC ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): t Selected partition 1 Hex code (type L to list all codes): 8e Changed type of partition 'Linux LVM' to 'Linux LVM' Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos Disk identifier: 0x892ef256 Device Boot Start End Blocks Id System /dev/sdb1 2048 4196351 2097152 8e Linux LVM Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks.
在创建一个Linux LVM的分区
Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos Disk identifier: 0x892ef256 Device Boot Start End Blocks Id System /dev/sdb1 2048 4196351 2097152 8e Linux LVM /dev/sdb2 4196352 8390655 2097152 8e Linux LVM
更新一下分区表
[root@tzPC ~]# partprobe Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
没有此命令则使用yum安装
[root@tzPC ~]# yum install -y parted
将硬盘转换成物理卷
如果挂载了 分区请先卸载
[root@tzPC ~]# pvcreate /dev/sdb1 /dev/sdb2 Physical volume "/dev/sdb1" successfully created. Physical volume "/dev/sdb2" successfully created.
显示已创建的物理卷列表
[root@tzPC ~]# pvdisplay /dev/sdb1 /dev/sdb2 "/dev/sdb2" is a new physical volume of "2.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb2 VG Name PV Size 2.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID q4rZeD-Bjun-XjyZ-IgnH-Tj96-4rad-WRE33J "/dev/sdb1" is a new physical volume of "2.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size 2.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID 8VjENe-yEZp-i0FI-4X8r-0yr2-n8G1-C7Pvza
创建卷组
[root@tzPC ~]# vgcreate vol_sdb /dev/sdb1 /dev/sdb2 Volume group "vol_sdb" successfully created
查看
[root@tzPC ~]# pvdisplay /dev/sdb1 /dev/sdb2 "/dev/sdb2" is a new physical volume of "2.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb2 VG Name PV Size 2.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID q4rZeD-Bjun-XjyZ-IgnH-Tj96-4rad-WRE33J "/dev/sdb1" is a new physical volume of "2.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size 2.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID 8VjENe-yEZp-i0FI-4X8r-0yr2-n8G1-C7Pvza [root@tzPC ~]# vgcreate vol_sdb /dev/sdb1 /dev/sdb2 Volume group "vol_sdb" successfully created [root@tzPC ~]# vgdisplay vol_sdb --- Volume group --- VG Name vol_sdb System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 3.99 GiB PE Size 4.00 MiB Total PE 1022 Alloc PE / Size 0 / 0 Free PE / Size 1022 / 3.99 GiB VG UUID Tpydaw-urdy-8Ac7-84uf-D9kL-smb2-zT7jgD
创建逻辑卷
-l选项定义了为逻辑卷指定多少可用的卷组空间,可按百分比指定
[root@tzPC ~]# lvcreate -l 100%FREE -n lv_sdb vol_sdb Logical volume "lv_sdb" created.
查看
[root@tzPC ~]# lvdisplay vol_sdb --- Logical volume --- LV Path /dev/vol_sdb/lv_sdb LV Name lv_sdb VG Name vol_sdb LV UUID bbB64W-Z2kZ-Q7hn-b1jL-G7jF-mdU0-cqUCic LV Write Access read/write LV Creation host, time tzPC, 2020-08-08 18:27:56 +0800 LV Status available # open 0 LV Size 3.99 GiB Current LE 1022 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
格式化
[root@tzPC ~]# mkfs.ext4 /dev/vol_sdb/lv_sdb
挂载
[root@tzPC ~]# mkdir /mnt/vol_sdb [root@tzPC ~]# mount /dev/vol_sdb/lv_sdb /mnt/vol_sdb/ [root@tzPC ~]# df -hT /mnt/vol_sdb/ Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vol_sdb-lv_sdb ext4 3.9G 16M 3.7G 1% /mnt/vol_sdb
修改LVM大小
#待补充