1、本地存储设备的识别
(1) fdisk -l ##真实存在的设备(必须是root用户)
(2)cat /proc/partitions ##系统识别的设备
现在插入U盘
[[email protected] ~]# fdisk -l
Disk /dev/sda: 500.1 GB, 500107862016 bytes, 976773168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x2cc35748
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 104861695 52429824 7 HPFS/NTFS/exFAT
/dev/sda2 104861696 976773119 435955712 f W95 Ext'd (LBA)
/dev/sda5 104863744 396369919 145753088 7 HPFS/NTFS/exFAT
/dev/sda6 396371968 687878143 145753088 7 HPFS/NTFS/exFAT
/dev/sda7 687880192 689977343 1048576 83 Linux
/dev/sda8 689979392 976773119 143396864 8e Linux LVM
Disk /dev/mapper/rhel_foundation68-root: 143.6 GB, 143608774656 bytes, 280485888 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/mapper/rhel_foundation68-swap: 3221 MB, 3221225472 bytes, 6291456 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/sdb: 8053 MB, 8053063680 bytes, 15728640 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: 0x029bd91d
Device Boot Start End Blocks Id System
/dev/sdb1 * 64 15728639 7864288 b W95 FAT32 ##U盘
[[email protected] ~]#
[[email protected] ~]# cat /proc/partitions
major minor #blocks name
8 0 488386584 sda
8 1 52429824 sda1
8 2 1 sda2
8 5 145753088 sda5
8 6 145753088 sda6
8 7 1048576 sda7
8 8 143396864 sda8
253 0 140242944 dm-0
253 1 3145728 dm-1
8 16 7864320 sdb
8 17 7864288 sdb1
[[email protected] ~]#
sdb1为U盘设备
(3)blkid ##系统可使用的设备
(4)df ##系统正在挂载的设备
2、设备的挂载和卸载
(1)设备名称
/dev/d
例如;/dev/hd0 , /dev/hd1, /dev/sda/,/dev/sdb/,/dev/sda1/,/dev/sda2/,/dev/sdb2
(2)设备的挂载
1>格式:mount 设备 挂载点
[[email protected] ~]# mkdir /gz
[[email protected] ~]# mount /dev/sdb1 /gz
[[email protected] ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/rhel_foundation68-root 140174468 20087644 120086824 15% /
devtmpfs 725724 0 725724 0% /dev
tmpfs 740032 228 739804 1% /dev/shm
tmpfs 740032 9056 730976 2% /run
tmpfs 740032 0 740032 0% /sys/fs/cgroup
/dev/sda7 1038336 145380 892956 15% /boot
tmpfs 148008 36 147972 1% /run/user/1002
/dev/sdb1 7848936 369836 7479100 5% /gz
[[email protected] ~]#
2>查看挂载信息(mount)
此时U盘的权限是读写
3>只读挂载
需现卸载再重新挂载
[[email protected] ~]# umount /dev/sdb1
[[email protected] ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/rhel_foundation68-root 140174468 20086736 120087732 15% /
devtmpfs 725724 0 725724 0% /dev
tmpfs 740032 228 739804 1% /dev/shm
tmpfs 740032 9048 730984 2% /run
tmpfs 740032 0 740032 0% /sys/fs/cgroup
/dev/sda7 1038336 145380 892956 15% /boot
tmpfs 148008 36 147972 1% /run/user/1002
[[email protected] ~]#
4>重新读写挂载
[[email protected] ~]# mount -o ro /dev/sdb1 /gz
[[email protected] ~]# mount
5>解决设备正忙情况
在现在shell没有卸载U盘的情况下重新=打开shell卸载U盘
[[email protected] ~]# umount /gz
umount: /gz: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[[email protected] ~]#
解决方案1:
fuser -kvm /gz
umont /mnt
[[email protected] ~]# fuser -kvm /gz
USER PID ACCESS COMMAND
/gz: root kernel mount /gz
root 15713 ..c.. bash
[[email protected] ~]# umount /gz
[[email protected] ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/rhel_foundation68-root 140174468 20086884 120087584 15% /
devtmpfs 725724 0 725724 0% /dev
tmpfs 740032 228 739804 1% /dev/shm
tmpfs 740032 9048 730984 2% /run
tmpfs 740032 0 740032 0% /sys/fs/cgroup
/dev/sda7 1038336 145380 892956 15% /boot
tmpfs 148008 36 147972 1% /run/user/1
解决方案2:
lsof
3、磁盘分区
磁盘0磁道1扇区的512个字节中记录的信息如下
512=446 + 64 + 2
| | |
mbr(主引导记录) mpt(主分区标) 55aa(硬盘的有效性标识)
主引导记录(mbr):(0磁道0分区):大小:446
磁盘分区标(64),1分区占用16个字节,一块硬盘最多可以划分4个主分区
主分区表:64字节
主分区(p) 扩展分区
扩展分区里面的分区叫逻辑分区(l)
扩展分区不能直接使用
(1)查看系统现有那些硬盘
[[email protected] ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x00013f3e
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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
[[email protected] ~]#
/dev/vda已经是容器了,所以只能分区/dev/vdb
(2)分区/dev/vdb
(3)格式化挂载
[[email protected] ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
/dev/sr0: UUID="2015-10-30-11-11-49-00" LABEL="RHEL-7.2 Server.x86_64" TYPE="iso9660" PTTYPE="dos"
此时系统可使用的设备没有分区出来的/dev/vdb1 ,需要进行格式化及挂载
[[email protected] ~]# mkfs.xfs /dev/vdb1
meta-data=/dev/vdb1 isize=256 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=25600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
挂载分区
注意:不同设备不能挂载在同一目录(不能挂载在同一挂载点)
[[email protected] ~]# mkdir /gz
[[email protected] ~]# mount /dev/vdb1 /gz
[[email protected] ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3133344 7340556 30% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 80 942568 1% /dev/shm
tmpfs 942648 17052 925596 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
/dev/vdb1 98988 5280 93708 6% /gz
[[email protected] ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
/dev/sr0: UUID="2015-10-30-11-11-49-00" LABEL="RHEL-7.2 Server.x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/vdb1: UUID="1775ac90-ade5-429e-b8d5-1751a6766c10" TYPE="xfs"
[[email protected] ~]#
(5)再次分区并设置设备自动挂载
设备自动挂载
开机自动挂载策略文件/etc/fstab
新添加/dev/vdb2
Command (m for help): p
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x540f340d
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 411647 102400 83 Linux
Command (m for help): wq
The partition table has been altered!
[[email protected] ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
/dev/sr0: UUID="2015-10-30-11-11-49-00" LABEL="RHEL-7.2 Server.x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/vdb1: UUID="1775ac90-ade5-429e-b8d5-1751a6766c10" TYPE="xfs"
/dev/vdb2: UUID="5da4c4d2-8529-4fad-bad6-85d4065eaa98" TYPE="xfs"
[[email protected] ~]#
重新添加分区后退出保存有告警需要手动加载 partprobe 否则格式化时提示没有文件或目录
设备自动挂载
[[email protected] /]#mkdir /gz1
[[email protected] /]# vim /etc/fstab
/dev/vdb2 /gz1 xfs defaults 0 0
设备 挂载点 文件系统 挂载参数 是否备份 是否检测
[[email protected] /]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3133448 7340452 30% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 80 942568 1% /dev/shm
tmpfs 942648 17020 925628 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
[[email protected] /]#
[[email protected] /]# mount -a
[[email protected] /]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3131460 7342440 30% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 80 942568 1% /dev/shm
tmpfs 942648 17020 925628 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
/dev/vdb2 98988 5280 93708 6% /gz1
[[email protected] /]#
(6)划分其他分区(/dev/vdb3)
[[email protected] /]# fdisk /dev/vdb
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
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3):
First sector (411648-20971519, default 411648):
Using default value 411648
Last sector, +sectors or +size{K,M,G} (411648-20971519, default 20971519): +100M
Partition 3 of type Linux and of size 100 MiB is set
Command (m for help): p
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x540f340d
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 411647 102400 83 Linux
/dev/vdb3 411648 616447 102400 83 Linux
Command (m for help): wq
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.
[[email protected] /]# mkfs.xfs /dev/vdb3
/dev/vdb3: No such file or directory
Usage: mkfs.xfs
/* blocksize */ [-b log=n|size=num]
/* metadata */ [-m crc=[0|1]
/* data subvol */ [-d agcount=n,agsize=n,file,name=xxx,size=num,
(sunit=value,swidth=value|su=num,sw=num|noalign),
sectlog=n|sectsize=num
/* force overwrite */ [-f]
/* inode size */ [-i log=n|perblock=n|size=num,maxpct=n,attr=0|1|2,
projid32bit=0|1]
/* no discard */ [-K]
/* log subvol */ [-l agnum=n,internal,size=num,logdev=xxx,version=n
sunit=value|su=num,sectlog=n|sectsize=num,
lazy-count=0|1]
/* label */ [-L label (maximum 12 characters)]
/* naming */ [-n log=n|size=num,version=2|ci,ftype=0|1]
/* no-op info only */ [-N]
/* prototype file */ [-p fname]
/* quiet */ [-q]
/* realtime subvol */ [-r extsize=num,size=num,rtdev=xxx]
/* sectorsize */ [-s log=n|size=num]
/* version */ [-V]
devicename
<devicename> is required unless -d name=xxx is given.
<num> is xxx (bytes), xxxs (sectors), xxxb (fs blocks), xxxk (xxx KiB),
xxxm (xxx MiB), xxxg (xxx GiB), xxxt (xxx TiB) or xxxp (xxx PiB).
<value> is xxx (512 byte blocks).
[[email protected] /]# 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.
[[email protected] /]# mkfs.xfs /dev/vdb3
meta-data=/dev/vdb3 isize=256 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=25600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[[email protected] /]# cat /proc/partitions
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 102400 vdb1
253 18 102400 vdb2
253 19 102400 vdb3
11 0 3948544 sr0
[[email protected] /]# mkdir /gz3
[[email protected] /]# vim /etc/fstab
[[email protected] /]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3131480 7342420 30% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 80 942568 1% /dev/shm
tmpfs 942648 17024 925624 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
/dev/vdb2 98988 5280 93708 6% /gz1
[[email protected] /]# vim /etc/fstab
[[email protected] /]# cat /etc/fstab
[[email protected] /]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3131480 7342420 30% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 80 942568 1% /dev/shm
tmpfs 942648 17024 925624 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
/dev/vdb2 98988 5280 93708 6% /gz1
[[email protected] /]# mount -a
[[email protected] /]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3131480 7342420 30% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 80 942568 1% /dev/shm
tmpfs 942648 17024 925624 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
/dev/vdb2 98988 5280 93708 6% /gz1
/dev/vdb1 98988 5280 93708 6% /gz
/dev/vdb3 98988 5280 93708 6% /gz2
[[email protected] /]#
(7)划分可扩展分区
扩展分区不能格式化
[[email protected] /]# 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.
[[email protected] /]# mkfs.xfs /dev/vdb4
mkfs.xfs: /dev/vdb4 appears to contain a partition table (dos).
mkfs.xfs: Use the -f option to force overwrite.
[[email protected] /]#
[[email protected] /]# cat /proc/partitions
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 102400 vdb1
253 18 102400 vdb2
253 19 102400 vdb3
253 20 1 vdb4
11 0 3948544 sr0
[[email protected] /]#
(8)划分逻辑分区
[[email protected] /]# fdisk /dev/vdb
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):
Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (618496-1640447, default 618496):
Using default value 618496
Last sector, +sectors or +size{K,M,G} (618496-1640447, default 1640447): +100M
Partition 5 of type Linux and of size 100 MiB is set
Command (m for help): p
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x540f340d
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 411647 102400 83 Linux
/dev/vdb3 411648 616447 102400 83 Linux
/dev/vdb4 616448 1640447 512000 5 Extended
/dev/vdb5 618496 823295 102400 83 Linux
Command (m for help): wq
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.
[[email protected] /]# partprobe
[[email protected] /]# cat /proc/partitions
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 102400 vdb1
253 18 102400 vdb2
253 19 102400 vdb3
253 20 1 vdb4
253 21 102400 vdb5
11 0 3948544 sr0
[[email protected] /]#
四、swap分区管理
swap分区,即交换区,系统在物理内存不够时,与swap进行交换
(1)划分分区
[[email protected] /]# fdisk /dev/vdb
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
All primary partitions are in use
Adding logical partition 6
First sector (825344-1640447, default 825344):
Using default value 825344
Last sector, +sectors or +size{K,M,G} (825344-1640447, default 1640447): +100M
Partition 6 of type Linux and of size 100 MiB is set
Command (m for help): p
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x540f340d
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 411647 102400 83 Linux
/dev/vdb3 411648 616447 102400 83 Linux
/dev/vdb4 616448 1640447 512000 5 Extended
/dev/vdb5 618496 823295 102400 83 Linux
/dev/vdb6 825344 1030143 102400 83 Linux
Command (m for help): wq
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.
[[email protected] /]# fdisk /dev/vdb
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 ##更改标签
Partition number (1-6, default 6):
Hex code (type L to list all codes): l ##查看都有那些标签
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'
Command (m for help): p
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x540f340d
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 411647 102400 83 Linux
/dev/vdb3 411648 616447 102400 83 Linux
/dev/vdb4 616448 1640447 512000 5 Extended
/dev/vdb5 618496 823295 102400 83 Linux
/dev/vdb6 825344 1030143 102400 82 Linux swap / Solaris
Command (m for help): wq
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.
[[email protected] /]#
[[email protected] /]# partprobe
[[email protected] /]# mkswap /dev/vdb6 ##格式化
Setting up swapspace version 1, size = 102396 KiB
no label, UUID=c355c390-cf15-470b-ad1d-909cc3361b45
[[email protected] /]# swapon -a /dev/vdb6
[[email protected] /]# swapon -s
Filename Type Size Used Priority
/dev/vdb6 partition 102396 0 -1
[[email protected] /]#
永久挂载
[[email protected] /]# vim /etc/fstab
[[email protected] /]#
[[email protected] /]# swapon -a ##检测swapon是否正常
[[email protected] /]#
(2)swap分区删除
[[email protected] /]# swapoff /dev/vdb6 ##关闭/dev/vdb5
[[email protected] /]# swapon -s
[[email protected] /]# swapon -a ##启动
[[email protected] /]# swapon -s
Filename Type Size Used Priority
/dev/vdb6 partition 102396 0 -1
[[email protected] /]#
在/dev/sdb下删除设备(分区)时必须先停掉设备(swapoff /dev/vdb5)因此删除成功,否则报错