Linux之磁盘管理

时间:2022-01-31 21:13:50

本章重点提示:

1):理解基础命令,df,fdisk.

2):磁盘分区的理论基础。

1:查看当前系统分区与挂载情况:

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

2:查看系统所有能识别的分区情况(包括系统已识别,未挂载的分区):

 [root@localhost ~]# fdisk -l

 Disk /dev/sda: 21.5 GB,  bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk identifier: 0x00077367 Device Boot Start End Blocks Id System
/dev/sda1 * Linux
Partition does not end on cylinder boundary.
/dev/sda2 Linux
/dev/sda3 Linux swap / Solaris Disk /dev/sdb: 21.5 GB, bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk identifier: 0x00000000 Disk /dev/sdc: 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: 0xcb2e3fe7 Device Boot Start End Blocks Id System

其中Disk /dev/sdc: 7776 MB,是外接的8G U盘

3:进入分区(8G U盘):

[root@localhost ~]# fdisk /dev/sdc

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):

4:m,查看帮助命令:

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)

5:n,新增分区:

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 ): + 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): w
The partition table has been altered! Calling ioctl() to re-read partition table.
Syncing disks.

6:显示分区信息,可以发现刚才新建的分区:

[root@localhost ~]# fdisk -l

Disk /dev/sda: 21.5 GB,  bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk identifier: 0x00077367 Device Boot Start End Blocks Id System
/dev/sda1 * Linux
Partition does not end on cylinder boundary.
/dev/sda2 Linux
/dev/sda3 Linux swap / Solaris Disk /dev/sdb: 21.5 GB, bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk identifier: 0x00000000 Disk /dev/sdc: 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: 0xcb2e3fe7 Device Boot Start End Blocks Id System
/dev/sdc1 Linux

7:重复刚才的操作,先新建一个扩展分区,再新建一个逻辑分区:

[root@localhost ~]# fdisk -l

Disk /dev/sda: 21.5 GB,  bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk identifier: 0x00077367 Device Boot Start End Blocks Id System
/dev/sda1 * Linux
Partition does not end on cylinder boundary.
/dev/sda2 Linux
/dev/sda3 Linux swap / Solaris Disk /dev/sdb: 21.5 GB, bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk identifier: 0x00000000 Disk /dev/sdc: 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: 0xcb2e3fe7 Device Boot Start End Blocks Id System
/dev/sdc1 Linux
[root@localhost ~]# fdisk /dev/sdc 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 (-)
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): 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): w
The partition table has been altered! Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# fdisk -l Disk /dev/sda: 21.5 GB, bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk identifier: 0x00077367 Device Boot Start End Blocks Id System
/dev/sda1 * Linux
Partition does not end on cylinder boundary.
/dev/sda2 Linux
/dev/sda3 Linux swap / Solaris Disk /dev/sdb: 21.5 GB, bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk identifier: 0x00000000 Disk /dev/sdc: 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: 0xcb2e3fe7 Device Boot Start End Blocks Id System
/dev/sdc1 Linux
/dev/sdc2 Extended
[root@localhost ~]# fdisk /dev/sdc 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
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): w
The partition table has been altered! Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# fdisk -l Disk /dev/sda: 21.5 GB, bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk identifier: 0x00077367 Device Boot Start End Blocks Id System
/dev/sda1 * Linux
Partition does not end on cylinder boundary.
/dev/sda2 Linux
/dev/sda3 Linux swap / Solaris Disk /dev/sdb: 21.5 GB, bytes
heads, sectors/track, cylinders
Units = cylinders of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disk identifier: 0x00000000 Disk /dev/sdc: 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: 0xcb2e3fe7 Device Boot Start End Blocks Id System
/dev/sdc1 Linux
/dev/sdc2 Extended
/dev/sdc5 Linux

8:查看当前系统格式:

[root@localhost ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 19G .1G 15G % /
tmpfs tmpfs 515M 82k 515M % /dev/shm
/dev/sda1 ext4 297M 30M 252M % /boot

9:格式化分区:

[root@localhost ~]# ]mkfs -t ext3 /dev/sdc1
-bash: ]mkfs: command not found
[root@localhost ~]# mkfs -t ext4 /dev/sdc1
mke2fs 1.41. (-May-)
Filesystem label=
OS type: Linux
Block size= (log=)
Fragment size= (log=)
Stride= blocks, Stripe width= blocks
inodes, blocks
blocks (5.00%) reserved for the super user
First data block=
Maximum filesystem blocks=
block groups
blocks per group, fragments per group
inodes per group
Superblock backups stored on blocks:
, , , , , , Writing inode tables: done
Creating journal ( blocks): done
Writing superblocks and filesystem accounting information:
done This filesystem will be automatically checked every mounts or
days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# mkfs -t ext4 /dev/sdc5
mke2fs 1.41. (-May-)
Filesystem label=
OS type: Linux
Block size= (log=)
Fragment size= (log=)
Stride= blocks, Stripe width= blocks
inodes, blocks
blocks (5.00%) reserved for the super user
First data block=
Maximum filesystem blocks=
block groups
blocks per group, fragments per group
inodes per group
Superblock backups stored on blocks:
, , , , , , Writing inode tables: done
Creating journal ( blocks): done
Writing superblocks and filesystem accounting information:
done This filesystem will be automatically checked every mounts or
days, whichever comes first. Use tune2fs -c or -i to override.

10:挂载分区:

[root@localhost ~]# mount /dev/sdc1 /mnt/; mount /dev/sdc5 /mnt/

11:查看当前系统挂载分区信息:

[root@localhost ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 19G .1G 15G % /
tmpfs tmpfs 515M 82k 515M % /dev/shm
/dev/sda1 ext4 297M 30M 252M % /boot
/dev/sdc1 ext4 .9G 7.8M .7G % /mnt
/dev/sdc5 ext4 .9G 7.8M .7G % /mnt