linux命令之磁盘与文件系统管理命令(上)

时间:2023-03-09 04:44:12
linux命令之磁盘与文件系统管理命令(上)

1.fdisk:磁盘分区工具

该命令是linux下常用的磁盘分区工具,但是只能给小于2TB的磁盘划分分区。

常用参数为-l,显示所有磁盘分区的信息。

示例:

1)显示磁盘分区列表

[root@boxiaoyuan ~]# fdisk -l  # 查看当前系统的所有磁盘分区信息
Disk /dev/sda: 21.5 GB, bytes # 磁盘/dev/sda的大小
heads, sectors/track, cylinders # 255个虚拟磁头,63个扇区/磁道,2610个柱面
Units = cylinders of * = bytes # 一个柱面的大小为8225280bytes
Sector size (logical/physical): bytes / bytes #每个扇区的字节数
I/O size (minimum/optimal): bytes / bytes # 每次读写的字节数
Disk identifier: 0x0000db9b Device Boot Start End Blocks Id System
/dev/sda1 Linux swap / Solaris
/dev/sda2 * Linux

说明:Device表示分区;Boot表示启动分区,用*表示;Start表示开始的柱面;End表示结束的柱面;Blocks表示快数量;Id表示分区类型Id;System表示分区类型。

2)模拟分区实战

[root@boxiaoyuan ~]# 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 0x784b7b62.
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 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): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only) Command (m for help): n
Command action
e extended
p primary partition (-)
p
Partition number (-):
First cylinder (-, default
):
Using default value
Last cylinder, +cylinders or +size{K,M,G} (-, default ): +
100M Command (m for help): p Disk /dev/sdb: MB, bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk identifier: 0x784b7b62 Device Boot Start End Blocks Id System
/dev/sdb1 + Linux Command (m for help): n
Command action
e extended
p primary partition (-)
e
Partition number (-):
First cylinder (-, default
):
Using default value
Last cylinder, +cylinders or +size{K,M,G} (-, default
):
Using default value Command (m for help): p Disk /dev/sdb: MB, bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk identifier: 0x784b7b62 Device Boot Start End Blocks Id System
/dev/sdb1 + Linux
/dev/sdb2 Extended

Command (m
for help): n
Command action
l logical ( or over)
p primary partition (-)
p
Partition number (-):
No free sectors available Command (m for help): n
Command action
l logical ( or over)
p primary partition (-)
l
First cylinder (-, default ):
Using default value
Last cylinder, +cylinders or +size{K,M,G} (-, default ): +
400M Command (m for help): p Disk /dev/sdb: MB, bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk identifier: 0x784b7b62 Device Boot Start End Blocks Id System
/dev/sdb1 + Linux
/dev/sdb2 Extended
/dev/sdb5 + Linux Command (m for help): n
Command action
l logical ( or over)
p primary partition (-)
l
First cylinder (-, default ):
Using default value
Last cylinder, +cylinders or +size{K,M,G} (-, default
):
Using default value Command (m for help): p Disk /dev/sdb: MB, bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk identifier: 0x784b7b62 Device Boot Start End Blocks Id System
/dev/sdb1 + Linux
/dev/sdb2 Extended
/dev/sdb5 + Linux
/dev/sdb6 + Linux Command (m for help): w
The partition table has been altered! Calling ioctl() to re-read partition table.
Syncing disks.
[root@boxiaoyuan ~]# partprobe /dev/sdb # 执行该命令通知内核分区表已更新,不需要重启系统就可以让分区生效

格式化磁盘

[root@boxiaoyuan ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.41. (-May-)
文件系统标签=
操作系统:Linux
块大小= (log=)
分块大小= (log=)
Stride= blocks, Stripe width= blocks
inodes, blocks
blocks (5.00%) reserved for the super user
第一个数据块=
Maximum filesystem blocks=
block groups
blocks per group, fragments per group
inodes per group
Superblock backups stored on blocks:
, , , , 正在写入inode表: 完成
Creating journal ( blocks): 完成
Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every mounts or
days, whichever comes first. Use tune2fs -c or -i to override.

挂载磁盘

[root@boxiaoyuan ~]# mount /dev/sdb1 /mnt
[root@boxiaoyuan ~]# df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda2 ext4 % /
tmpfs tmpfs % /dev/shm
/dev/sr0 iso9660 % /media/CentOS_6.8_Final
/dev/sdb1 ext4 % /mnt

2.partprobe:更新内核的硬盘分区表信息

该命令用于在硬盘分区发生变化时,更新内核的硬盘分区表数据,可以在不重启系统时就更新分区表信息。

[root@boxiaoyuan ~]# partprobe /dev/sdb

3.parted:磁盘分区工具

该命令用于对大于2TB的磁盘进行分区,并且要将磁盘转换为GPT格式。

常用的参数选项为-l,显示所有磁盘分区的信息。

通过parted -h或者直接通过parted进入交互模式后通过-h查看帮助。

[root@boxiaoyuan ~]# parted
GNU Parted 2.1
使用 /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) h
align-check TYPE N
check NUMBER
cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER
help [COMMAND]
mklabel,mktable LABEL-TYPE # 创建分区表
mkfs NUMBER FS-TYPE
mkpart PART-TYPE [FS-TYPE] START END # 创建分区
mkpartfs PART-TYPE FS-
TYPE START END # 创建带有文件系统的分区
move NUMBER START END
name NUMBER NAME
print [devices|free|list,all|NUMBER] # 显示分区表信息
quit
rescue START END
resize NUMBER START END
rm NUMBER # 删除编号NUMBER的分区
select DEVICE
set NUMBER FLAG STATE
toggle [NUMBER [FLAG]]
unit UNIT
version
(parted)

示例:

1)显示分区情况

[root@boxiaoyuan ~]# parted -l
Model: VMware, VMware Virtual S (scsi) # 磁盘类型
Disk /dev/sda: .5GB # 磁盘大小
Sector size (logical/physical): 512B/512B # 扇区大小
Partition Table: msdos # 分区表类型 Number Start End Size Type File system 标志
1049kB 8591MB 8590MB primary linux-swap(v1)
8591MB .5GB .9GB primary ext4 启动

2)模拟分区实战

[root@boxiaoyuan ~]# parted /dev/sdb  # parted直接接需要分区的设备
GNU Parted 2.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt # 为sdb磁盘创建GPT分区表,大于2TB的磁盘必须执行这一步
(parted) mkpart primary
# 创建主分区,500M
警告: The resulting partition is not properly aligned for best performance.
忽略/Ignore/放弃/Cancel? Ignore # 忽略警告
(parted) p # 显示分区表信息
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt Number Start End Size File system Name 标志
.4kB 500MB 500MB primary (parted) mkpart logical # 创建逻辑分区,大小为500MB
(parted) p # 显示分区表信息

Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt Number Start End Size File system Name 标志
.4kB 500MB 500MB primary
501MB 1000MB 499MB logical (parted) quit # 退出
信息: You may need to update /etc/fstab. [root@boxiaoyuan ~]# ls /dev/sdb*
/dev/sdb /dev/sdb1 /dev/sdb2
[root@bixiaoyuan ~]# mkfs.ext4 /dev/sdb1 # 格式化
mke2fs 1.41. (-May-)
文件系统标签=
操作系统:Linux
块大小= (log=)
分块大小= (log=)
Stride= blocks, Stripe width= blocks
inodes, blocks
blocks (5.00%) reserved for the super user
第一个数据块=
Maximum filesystem blocks=
block groups
blocks per group, fragments per group
inodes per group
Superblock backups stored on blocks:
, , , , , , , 正在写入inode表: 完成
Creating journal ( blocks): 完成
Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every mounts or
days, whichever comes first. Use tune2fs -c or -i to override.
[root@boxiaoyuan ~]# mount /dev/sdb1 /mnt #挂载

4.mkfs:创建linux文件系统

该命令用于在指定的设备或者分区上创建格式化并创建文件系统。

常用的参数有-t:指定要创建的文件系统类型,也可以直接使用mkfs.ext4创建ext4文件系统。

5.df:报告文件系统磁盘空间的使用情况

该命令用于显示文件系统磁盘空间的使用情况。

参数 说明
-h(常用) 以容易理解的格式显示磁盘的使用情况
-i(常用) 显示文件系统的inode信息
-T(常用) 列出文件系统的类型

示例:

1)显示磁盘的使用情况

[root@boxiaoyuan ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 % /
tmpfs % /dev/shm

2)-h选项

[root@boxiaoyuan ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 12G .2G .0G % /
tmpfs 931M 72K 931M % /dev/shm

3)列出文件系统的类型

[root@boxiaoyuan ~]# df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda2 ext4 % /
tmpfs tmpfs % /dev/shm

注:本文内容为《跟老男孩学linux运维 核心系统命令实践》的学习笔记。