linux系统中外接硬盘的使用逻辑:
磁盘分区---分区格式化---挂载
linux系统中一台主机上可以有多快硬盘,内核中的udev设备管理器会自动对识别的硬盘进行命名
系统采用a~p来代表16块不同的硬盘(默认从a开始),如/dev/sda 代表系统识别的同种型号接口的第一块硬盘,
/dev/sdb代表系统识别的同种型号接口的第二快硬盘,依次类推,直到/dev/sdp。
硬件命名规则知道之后要想使用硬盘,则先需要进行磁盘分区:
1、首先查看硬盘情况:
[root@linuxprobe /]# cd /dev ## 外接硬盘的默认路径
[root@linuxprobe dev]# find sd* ## 查看几块硬盘,一共两块,sda 和 sdb(sda1和sda2是sda的两个分区)
sda
sda1
sda2
sdb
2、使用fdisk命令进行磁盘分区
使用格式: fdisk + 硬盘路径
fdisk + 硬盘路径 使用的是交互式命令
fdisk命令中的参数及其作用(linux就该这么学p125)
测试:
[root@linuxprobe dev]# fdisk /dev/sdb ## 使用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 ## 这里输入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: 0xd55cd25f
Device Boot Start End Blocks Id System
Command (m for help): n ## 输入n,添加新的分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p ## 这里输入p选择主分区
Partition number (1-4, default 1): 1 ## 选择编号1
First sector (2048-41943039, default 2048): ## 这里定义起始的扇区位置,直接敲击回车键保留默认设置
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +3G ## 这里选择结束扇区的位置,实际是分配分区的大小,输入 +3G 表示分区大小3G
Partition 1 of type Linux and of size 3 GiB is set
Command (m for help): p ## 这里输入p查看分区信息,已经出现 /dev/sdb1分区
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: 0xd55cd25f
Device Boot Start End Blocks Id System
/dev/sdb1 2048 6293503 3145728 83 Linux
Command (m for help): w ## 这里输入w 保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@linuxprobe dev]# find sd* ## 查看硬盘sdb的分区信息,已经多出sdb1
sda
sda1
sda2
sdb
sdb1
[root@linuxprobe dev]# file /dev/sdb1
/dev/sdb1: block special
注:有时候分区信息未能及时同步内核,在终端输入:partprobe命令,连续执行两次,如果仍然没有同步内核,重启系统即可
3、使用mkfs命令对磁盘分区进行格式化(指定文件系统)
mkfs命令的使用方式是在终端输入mkfs,然后连续按两下Tab键,会弹出常用的文件系统。
[root@linuxprobe dev]# mkfs ## 此处输入两下Tab键
mkfs mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs
mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.vfat
[root@linuxprobe dev]# mkfs.xfs /dev/sdb1 ## rhel7版本,使用 xfs系统
mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
[root@linuxprobe dev]# mkfs.xfs -f /dev/sdb1 ## 格式化成功
meta-data=/dev/sdb1 isize=256 agcount=4, agsize=196608 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=786432, 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
4、对格式话成功的分区进行挂载
[root@linuxprobe dev]# df -h ## 查看当前挂载
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root 18G 3.4G 15G 20% /
devtmpfs 985M 0 985M 0% /dev
tmpfs 994M 84K 994M 1% /dev/shm
tmpfs 994M 8.8M 986M 1% /run
tmpfs 994M 0 994M 0% /sys/fs/cgroup
/dev/sr0 3.5G 3.5G 0 100% /media/mounttest
/dev/sda1 497M 119M 379M 24% /boot
[root@linuxprobe dev]# mkdir /mnt/sdb1test ## 创建挂载目录
[root@linuxprobe dev]# mount /dev/sdb1 /mnt/sdb1test/ ## 挂载
[root@linuxprobe dev]# df -h ## 查看挂载
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root 18G 3.4G 15G 20% /
devtmpfs 985M 0 985M 0% /dev
tmpfs 994M 84K 994M 1% /dev/shm
tmpfs 994M 8.8M 986M 1% /run
tmpfs 994M 0 994M 0% /sys/fs/cgroup
/dev/sr0 3.5G 3.5G 0 100% /media/mounttest
/dev/sda1 497M 119M 379M 24% /boot
/dev/sdb1 3.0G 33M 3.0G 2% /mnt/sdb1test
5、进行第二快分区
[root@linuxprobe dev]# fdisk /dev/sdb ## fdisk + 硬盘,进行交互式操作
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): n ## 此处输入n添加分区
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p ## 此处选择p,再添加一个主分区
Partition number (2-4, default 2): 2 ## 编号选择2
First sector (6293504-41943039, default 6293504): ## 这里选择扇区的起始位置,按回车就行
Using default value 6293504
Last sector, +sectors or +size{K,M,G} (6293504-41943039, default 41943039): +5G ## 这里设定分区大小,设为5G
Partition 2 of type Linux and of size 5 GiB is set
Command (m for help): w ## 这里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.
[root@linuxprobe dev]# find sdb* ## 查看分区,没有!可能是没有及时同步内核
sdb
sdb1
[root@linuxprobe dev]# partprobe ## 输入partprobe命令
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@linuxprobe dev]# partprobe ## 同上
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@linuxprobe dev]# find sdb* ## 再次查看分区,已经同步
sdb
sdb1
sdb2
[root@linuxprobe dev]# mkfs ## 这里按两下Tab键
mkfs mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs
mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.vfat
[root@linuxprobe dev]# mkfs.xfs /dev/sdb2 ## 对分区进行格式话
meta-data=/dev/sdb2 isize=256 agcount=4, agsize=327680 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=1310720, 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
[root@linuxprobe dev]# df -h ## 查看当前分区
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root 18G 3.4G 15G 20% /
devtmpfs 985M 0 985M 0% /dev
tmpfs 994M 84K 994M 1% /dev/shm
tmpfs 994M 8.8M 986M 1% /run
tmpfs 994M 0 994M 0% /sys/fs/cgroup
/dev/sr0 3.5G 3.5G 0 100% /media/mounttest
/dev/sda1 497M 119M 379M 24% /boot
/dev/sdb1 3.0G 33M 3.0G 2% /mnt/sdb1test
[root@linuxprobe dev]# mkdir /mnt/sdb2test ## 创建挂载目录
[root@linuxprobe dev]# mount /dev/sdb2 /mnt/sdb2test/ ## 挂载
[root@linuxprobe dev]# df -h ## 查看挂载
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root 18G 3.4G 15G 20% /
devtmpfs 985M 0 985M 0% /dev
tmpfs 994M 84K 994M 1% /dev/shm
tmpfs 994M 8.8M 986M 1% /run
tmpfs 994M 0 994M 0% /sys/fs/cgroup
/dev/sr0 3.5G 3.5G 0 100% /media/mounttest
/dev/sda1 497M 119M 379M 24% /boot
/dev/sdb1 3.0G 33M 3.0G 2% /mnt/sdb1test
/dev/sdb2 5.0G 33M 5.0G 1% /mnt/sdb2test
6、如何删除分区
## 首先进行卸载
[root@linuxprobe dev]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root 18G 3.4G 15G 20% /
devtmpfs 985M 0 985M 0% /dev
tmpfs 994M 84K 994M 1% /dev/shm
tmpfs 994M 8.8M 986M 1% /run
tmpfs 994M 0 994M 0% /sys/fs/cgroup
/dev/sr0 3.5G 3.5G 0 100% /media/mounttest
/dev/sda1 497M 119M 379M 24% /boot
/dev/sdb1 3.0G 33M 3.0G 2% /mnt/sdb1test
/dev/sdb2 5.0G 33M 5.0G 1% /mnt/sdb2test
[root@linuxprobe dev]# umount /mnt/sdb2test/ ## 卸载
[root@linuxprobe dev]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root 18G 3.4G 15G 20% /
devtmpfs 985M 0 985M 0% /dev
tmpfs 994M 84K 994M 1% /dev/shm
tmpfs 994M 8.8M 986M 1% /run
tmpfs 994M 0 994M 0% /sys/fs/cgroup
/dev/sr0 3.5G 3.5G 0 100% /media/mounttest
/dev/sda1 497M 119M 379M 24% /boot
/dev/sdb1 3.0G 33M 3.0G 2% /mnt/sdb1test
[root@linuxprobe dev]# fdisk /dev/sdb ## 使用fdisk + 硬盘进行交互式操作
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): d ## 这里输入d
Partition number (1,2, default 2): 2 ## 这里删除编号为2的主分区
Partition 2 is deleted
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.
[root@linuxprobe dev]# find sdb* ## 查看,还在
sdb
sdb1
sdb2
[root@linuxprobe dev]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@linuxprobe dev]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@linuxprobe dev]# find sdb* ## 再次查看,已经没有第二个分区
sdb
sdb1
##注: 以上操作在/dev 目录下,如果其他目录需要指定路径