磁盘管理
存储是每个计算机系统的基本需求,red hat提供了一些使用的工具
fdisk 用于管理磁盘分区
fdisk -l 列出磁盘分区信息
blkid 列出系统中正在使用设备id
df 查看系统中正在使用设备信息
1:分区
[root@server11 Desktop]# 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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xa69fd0a7.
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 #删除分区
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
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 #修改分区功能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 #新建分区
Partition type:
p primary (0 primary, 0 extended, 4 free) #分区类型主分区
e extended #分区类型为拓展分区
Select (default p): p
Partition number (1-4, default 1): #主分区id
First sector (2048-20971519, default 2048): #分区起始位置
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M #主分区大小
Partition 1 of type Linux and of size 100 MiB is set
Command (m for help): wq #保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@server11 Desktop]# cat /proc/partitions
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 102400 vdb1
[root@server11 Desktop]#
partprobe ##同步分区表
2:文件系统
文件系统比较
ext4 是 Red Hat Enterprise Linux 6 的标准文件系统。
它非常强大可靠 , 具有多项可以提高现代工作量处理性能的功
能
ext2 是常用于 linux 中的较旧的文件系统。它简单可靠 ,
非常适合小型存储设备 , 但是效率低于 ext4
vfat 支持包括一系列相关文件系统 ( VFAT/FAT16
,FAT32 ), 这些文件系统针对较旧版本的 Microsoft
Windows 开发 , 在大量的系统和设备上受支持
xfs 在 Red Hat Enterprise Linux 7 的标准文件系统其具
备数据完全、性能稳定、扩展性强 ( 8eb-1byte ) 、传输速率
高 ( 7G/s )
分区示例
[root@server11 Desktop]# mkfs #两下tab键可查看有什么文件系统可以选择
mkfs mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs
mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.vfat
[root@server11 Desktop]# mkfs
[root@server11 Desktop]# 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
[root@server11 Desktop]# mount /dev/vdb1 /mnt #临时挂载
[root@server11 Desktop]# vim /etc/fstab #在里面添加条目永久挂载
编写格式:
###################################################################
# device mountpoint ftype defaults(mountpoint) 0 0 #
# /dev/vdb1 /mnt xfs defaults 0 0 #
# mount -a ##使/etc/fastab中记录的挂载生效 #
##################################################################
[root@server11 Desktop]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3809644 6664256 37% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 140 942520 1% /dev/shm
tmpfs 942660 17028 925632 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
/dev/vdb1 98988 5280 93708 6% /mnt
3:取消挂载
[root@server11 Desktop]# umount /mnt/
[root@server11 Desktop]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3809640 6664260 37% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 140 942520 1% /dev/shm
tmpfs 942660 17024 925636 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
[root@server11 Desktop]# mount -a
[root@server11 Desktop]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3809664 6664236 37% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 140 942520 1% /dev/shm
tmpfs 942660 17024 925636 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
/dev/vdb1 98988 5280 93708 6% /mnt
[root@server11 Desktop]#
取消永久挂载
[root@server11 Desktop]# vim /etc/fstab
[root@server11 Desktop]# umount /mnt/
[root@server11 Desktop]# 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): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
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): d #删除分区
Selected partition 1
Partition 1 is deleted
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@server11 Desktop]# partprobe #同步分区
[root@server11 Desktop]# cat /proc/partitions
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
[root@server11 Desktop]#
4:修改分区方式
[root@server11 Desktop]# parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
align-check help mktable quit select unit
disk_set mklabel name rescue set version
disk_toggle mkpart print rm toggle
(parted) mklabel
New disk label type?
aix amiga bsd dvh gpt loop mac msdos pc98 sun
New disk label type? gpt
Warning: The existing disk label on /dev/vdb will be destroyed and all data on
this disk will be lost. Do you want to continue?
Yes/No? yes
(parted) quit
Information: You may need to update /etc/fstab.
测试
[root@server11 Desktop]# fdisk /dev/vdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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 number (1-128, default 1): [root@server11 Desktop]# vim /etc/fstab
[root@server11 Desktop]# umount /mnt/
[root@server11 Desktop]# 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 (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +1G
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): t
Selected partition 1
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: 0x0003496d
Device Boot Start End Blocks Id System
/dev/vdb1 2048 2099199 1048576 82 Linux swap / Solaris
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@server11 Desktop]# partprobe
5:添加swap分区
fdisk /dev/vdb ##添加一个分区
[root@localhost ~]# fdisk /dev/vdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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 number (2-128, default 2):
First sector (34-20971486, default 2099200):
Last sector, +sectors or +size{K,M,G,T,P} (2099200-20971486, default 20971486): +4G
Created partition 2
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: gpt
tart End Size Type Name
1 2048 2099199 1G Linux filesyste
2 2099200 10487807 4G Linux filesyste
Command (m for help): t #改变分区类型
Partition number (1,2, default 2): 2 #将那个作为swap分区
Partition type (type L to list all types): l #列出可供选择的分区类型
1 EFI System C12A7328-F81F-11D2-BA4B-00A0C93EC93B
2 MBR partition scheme 024DEE41-33E7-11D3-9D69-0008C781F39F
3 BIOS boot partition 21686148-6449-6E6F-744E-656564454649
4 Microsoft reserved E3C9E316-0B5C-4DB8-817D-F92DF00215AE
5 Microsoft basic data EBD0A0A2-B9E5-4433-87C0-68B6B72699C7
6 Microsoft LDM metadata 5808C8AA-7E8F-42E0-85D2-E1E90434CFB3
7 Microsoft LDM data AF9B60A0-1431-4F62-BC68-3311714A69AD
8 Windows recovery evironmnet DE94BBA4-06D1-4D40-A16A-BFD50179D6AC
9 IBM General Parallel Fs 37AFFC90-EF7D-4E96-91C3-2D7AE055B174
10 HP-UX data partition 75894C1E-3AEB-11D3-B7C1-7B03A0000000
11 HP-UX service partition E2A1E728-32E3-11D6-A682-7B03A0000000
12 Linux filesystem 0FC63DAF-8483-4772-8E79-3D69D8477DE4
13 Linux RAID A19D880F-05FC-4D3B-A006-743F0F84911E
14 Linux swap 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F
15 Linux LVM E6D6D379-F507-44C2-A23C-238F2A3DF928
16 Linux reserved 8DA63339-0007-60C0-C436-083AC8230908
17 FreeBSD data 516E7CB4-6ECF-11D6-8FF8-00022D09712B
18 FreeBSD boot 83BD6B9D-7F41-11DC-BE0B-001560B84F0F
19 FreeBSD swap 516E7CB5-6ECF-11D6-8FF8-00022D09712B
20 FreeBSD UFS 516E7CB6-6ECF-11D6-8FF8-00022D09712B
21 FreeBSD ZFS 516E7CBA-6ECF-11D6-8FF8-00022D09712B
22 FreeBSD Vinum 516E7CB8-6ECF-11D6-8FF8-00022D09712B
23 Apple HFS/HFS+ 48465300-0000-11AA-AA11-00306543ECAC
24 Apple UFS 55465300-0000-11AA-AA11-00306543ECAC
25 Apple RAID 52414944-0000-11AA-AA11-00306543ECAC
26 Apple RAID offline 52414944-5F4F-11AA-AA11-00306543ECAC
27 Apple boot 426F6F74-0000-11AA-AA11-00306543ECAC
28 Apple label 4C616265-6C00-11AA-AA11-00306543ECAC
29 Apple TV recovery 5265636F-7665-11AA-AA11-00306543ECAC
30 Apple Core storage 53746F72-6167-11AA-AA11-00306543ECAC
31 Solaris boot 6A82CB45-1DD2-11B2-99A6-080020736631
32 Solaris root 6A85CF4D-1DD2-11B2-99A6-080020736631
33 Solaris /usr & Apple ZFS 6A898CC3-1DD2-11B2-99A6-080020736631
34 Solaris swap 6A87C46F-1DD2-11B2-99A6-080020736631
35 Solaris backup 6A8B642B-1DD2-11B2-99A6-080020736631
36 Solaris /var 6A8EF2E9-1DD2-11B2-99A6-080020736631
37 Solaris /home 6A90BA39-1DD2-11B2-99A6-080020736631
38 Solaris alternate sector 6A9283A5-1DD2-11B2-99A6-080020736631
39 Solaris reserved 1 6A945A3B-1DD2-11B2-99A6-080020736631
40 Solaris reserved 2 6A9630D1-1DD2-11B2-99A6-080020736631
41 Solaris reserved 3 6A980767-1DD2-11B2-99A6-080020736631
42 Solaris reserved 4 6A96237F-1DD2-11B2-99A6-080020736631
43 Solaris reserved 5 6A8D2AC7-1DD2-11B2-99A6-080020736631
44 NetBSD swap 49F48D32-B10E-11DC-B99B-0019D1879648
45 NetBSD FFS 49F48D5A-B10E-11DC-B99B-0019D1879648
46 NetBSD LFS 49F48D82-B10E-11DC-B99B-0019D1879648
47 NetBSD concatenated 2DB519C4-B10E-11DC-B99B-0019D1879648
48 NetBSD encrypted 2DB519EC-B10E-11DC-B99B-0019D1879648
49 NetBSD RAID 49F48DAA-B10E-11DC-B99B-0019D1879648
50 ChromeOS kernel FE3A2A5D-4F32-41A7-B725-ACCC3285A309
51 ChromeOS root fs 3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC
52 ChromeOS reserved 2E0A753D-9E48-43B0-8337-B15192CB1B5E
53 MidnightBSD data 85D5E45A-237C-11E1-B4B3-E89A8F7FC3A7
54 MidnightBSD boot 85D5E45E-237C-11E1-B4B3-E89A8F7FC3A7
55 MidnightBSD swap 85D5E45B-237C-11E1-B4B3-E89A8F7FC3A7
56 MidnightBSD UFS 0394Ef8B-237C-11E1-B4B3-E89A8F7FC3A7
57 MidnightBSD ZFS 85D5E45D-237C-11E1-B4B3-E89A8F7FC3A7
58 MidnightBSD Vinum 85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7
Partition type (type L to list all types): 14 #选择14 linux swap 分区
Changed type of partition 'Linux filesystem' to 'Linux swap'
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: gpt
Start End Size Type Name
1 2048 2099199 1G Linux filesyste
2 2099200 10487807 4G Linux swap
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.
[root@localhost ~]#
[root@localhost ~]# partprobe ##同步分区表
[root@localhost ~]# mkswap /dev/vdb2 ##格式化成swap类型
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=b933737a-1409-48b5-8f92-d663d388134b
[root@localhost ~]# swapon -a /dev/vdb2 #激活swap分区
[root@localhost ~]# swapon -s #显示当前交换区状态
Filename Type Size Used Priority
/dev/vdb2 partition 4194300 0 -1
[root@localhost ~]#
6:用文件来添加swap
dd if=/dev/zero of=/swapfile bs=1M count=1000 ##创建一个1G大小的文件
mkswap /swapfile ##格式化为swap类型
swapon -a /swapfile ##临时添加到swap里
-p + 数字 ##更改优先级
vim /etc/fstab ##永久添加swap分区
类型:
/swapfile swap swap defaults,pri=1 0 0 #pri优先级
[root@server11 Desktop]# dd if=/dev/zero of=/swapfile bs=1M count=1000 #创建文件
du -sh /swap1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 21.5441 s, 48.7 MB/s
[root@server11 Desktop]# du -sh /swapfile
1000M /swapfile
[root@server11 Desktop]# mkswap /swapfile #格式化成swap类型
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=abf6e26d-c782-4190-81f6-a603483cad6c
[root@server11 Desktop]# swapon -a /swapfile #临时添加
swapon: /swapfile: insecure permissions 0644, 0600 suggested.
[root@server11 Desktop]# swapon -s
Filename Type Size Used Priority
/dev/vdb1 partition 1048572 172 -1
/swapfile file 1023996 0 -2
[root@server11 Desktop]# swapoff /swapfile
[root@server11 Desktop]# swapon -s
Filename Type Size Used Priority
/dev/vdb1 partition 1048572 172 -1
[root@server11 Desktop]# swapon -a /swapfile -p 0 #修改swapfile优先级
swapon: /swapfile: insecure permissions 0644, 0600 suggested.
Similar command is: 'swapon'
[root@server11 Desktop]# swapon -s ##显示交换区状态
Filename Type Size Used Priority
/dev/vdb1 partition 1048572 172 -1
/swapfile file 1023996 0 0
[root@server11 Desktop]# swapon -a /swapfile -p 1 #修改失败原因是修改前必须先关闭swapfile
swapon: /swapfile: insecure permissions 0644, 0600 suggested.
swapon: /swapfile: swapon failed: Device or resource busy
[root@server11 Desktop]# swapoff /swapfile
[root@server11 Desktop]# swapoff /dev/vdb1
[root@server11 Desktop]# vim /etc/fstab
############################################################
# /swapfile swap swap defaults,pri=1 0 0 #pri优先级 # 在最后一行添加
############################################################
[root@server11 Desktop]# swapon -a
swapon: /swapfile: insecure permissions 0644, 0600 suggested.
[root@server11 Desktop]# swapon -s
Filename Type Size Used Priority
/dev/vdb1 partition 1048572 0 -1
/swapfile file 1023996 0 1
[root@server11 Desktop]#
7:删除swap
vim /etc/fstab ##删除此文件中添加的swap行
swapoff /swapfile ##断开swap文件链接
swapoff /dev/vdb1 ##断开swap磁盘链接
rm -rf /swapfile ##删除文件
fdisk /dev/vdb ##删除磁盘分区
partprobe ##同步分区表