linux磁盘与文件系统管理

管理分区
列出所有块设备

[root@centos7 ~]#lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0   50G  0 part /
├─sda3   8:3    0    4G  0 part [SWAP]
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0   30G  0 part /data
sr0     11:0    1  8.8G  0 rom  /run/media/root/CentOS 7 x86_64

创建分区使用:

• fdisk 创建MBR分区
• gdisk 创建GPT分区
• parted 高级分区操作

重新设置内存中的内核分区表版本
• partprobe

生成uuid 128bit的二进制组成十六进制的uuid

[root@centos7 ~]#uuidgen
44e3e725-610d-4730-8515-3cad2c55d3d3

parted命令

! parted的操作都是实时生效的,小心使用 !

用法:parted [选项]... [设备 [命令 [参数]...]...]  
parted /dev/sdb mklabel gpt|msdos  
parted /dev/sdb print  
parted /dev/sdb mkpart primary 1 200 (默认M)  
parted /dev/sdb rm 1  
parted –l 列出分区信息  

清空分区表

[root@centos7 ~]#dd if=/dev/zero of=/dev/sdb bs=1 count=512
512+0 records in
512+0 records out
512 bytes (512 B) copied, 0.00343371 s, 149 kB/s

进行磁盘分区

[root@centos7 ~]#fdisk /dev/sdb                                             <== 对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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x512ef772.

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

   Device Boot      Start         End      Blocks   Id  System

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-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +1G  <== 分1G
Partition 1 of type Linux and of size 1 GiB is set

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): e                                              <== e为扩展分区
Partition number (3,4, default 3): 3
First sector (6293504-41943039, default 6293504): 
Using default value 6293504
Last sector, +sectors or +size{K,M,G} (6293504-41943039, default 41943039): +10G
Partition 3 of type Extended and of size 10 GiB is set

Command (m for help): 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: 0x512ef772

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux
/dev/sdb2         2099200     6293503     2097152   83  Linux
/dev/sdb3         6293504    27265023    10485760    5  Extended     <==  扩展分区的id 5

Command (m for help): 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: 0x512ef772

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux
Command (m for help): n                                            <== 继续分
Partition type:
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): l                                              <== 选择l 再扩展分区里分区
Adding logical partition 5
First sector (6295552-27265023, default 6295552): 
Using default value 6295552
Last sector, +sectors or +size{K,M,G} (6295552-27265023, default 27265023): +1G  
Partition 5 of type Linux and of size 1 GiB is set


Command (m for help):  w                                                    <== 退出存盘
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

查看硬盘分区的方法

[root@centos7 ~]#lsblk                     <== 查看内存中的分区
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0   50G  0 part /
├─sda3   8:3    0    4G  0 part [SWAP]
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0   30G  0 part /data
sdb      8:16   0   20G  0 disk 
├─sdb1   8:17   0    1G  0 part 
├─sdb2   8:18   0    2G  0 part 
├─sdb3   8:19   0    1K  0 part 
├─sdb5   8:21   0    1G  0 part 
├─sdb6   8:22   0    2G  0 part 
└─sdb7   8:23   0    2G  0 part 
sr0     11:0    1  8.8G  0 rom  /run/media/root/CentOS 7 x86_64

[root@centos7 ~]#ls /dev/sdb*          <== 查看内存中的分区
/dev/sdb  /dev/sdb1  /dev/sdb2  /dev/sdb3  /dev/sdb5  /dev/sdb6  /dev/sdb7

[root@centos7 ~]#cat /proc/partitions   <== 查看内存中的分区
major minor  #blocks  name

   8       16   20971520 sdb
   8       17    1048576 sdb1
   8       18    2097152 sdb2
   8       19          1 sdb3
   8       21    1048576 sdb5
   8       22    2097152 sdb6
   8       23    2097152 sdb7
   8        0  209715200 sda
   8        1    1048576 sda1
   8        2   52428800 sda2
   8        3    4194304 sda3
   8        4          1 sda4
   8        5   31457280 sda5
  11        0    9177088 sr0

[root@centos7 ~]#fdisk -l /dev/sdb   <== 只有这个才能看到硬盘中的分区数据详情

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: 0x512ef772

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux
/dev/sdb2         2099200     6293503     2097152   83  Linux
/dev/sdb3         6293504    27265023    10485760    5  Extended
/dev/sdb5         6295552     8392703     1048576   83  Linux
/dev/sdb6         8394752    12589055     2097152   83  Linux
/dev/sdb7        12591104    16785407     2097152   83  Linux

查看开机时间

[root@centos7 ~]#uptime           <== 查看开机时间
 19:50:04 up  2:19,  2 users,  load average: 0.00, 0.01, 0.05

同步分区到内存中 生效

[root@centos7 ~]#partprobe /dev/sdb    <== cenos5 ,7 可以用这个 6不行

[root@centos7 ~]#partx -a /dev/sdb    <==  这是centos6专用的 -a是增加分区是用的
[root@centos7 ~]#partx -d --nr 6  /dev/sdb <== -d是同步删除分区 6=是sdb6 在跟上设备名

gdisk 分区 擅长管理gtp分区

gdisk /dev/sdb 类fdisk 的GPT分区工具
fdisk -l [-u] [device…] 查看分区
fdisk /dev/sdb 管理分区
子命令:
p 分区列表
t 更改分区类型
n 创建新分区
d 删除分区
v 校验分区
u 转换单位
w 保存并退出
q 不保存并退出

硬盘分好区后不能使用 要格式化

创建文件系统

[root@centos7 ~]#lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0   50G  0 part /
├─sda3   8:3    0    4G  0 part [SWAP]
├─sda4   8:4    0    1K  0 part         <== 扩展分区不能创建文件系统
└─sda5   8:5    0   30G  0 part /data
sdb      8:16   0   20G  0 disk 
├─sdb1   8:17   0    1G  0 part 
├─sdb2   8:18   0    2G  0 part 
├─sdb3   8:19   0    1K  0 part         <== 扩展分区不能创建文件系统
├─sdb5   8:21   0    1G  0 part 
├─sdb6   8:22   0    2G  0 part 
└─sdb7   8:23   0    2G  0 part 
[root@centos7 ~]#ls /lib/modules/3.10.0-862.el7.x86_64/kernel/fs <== 查看系统支持的文件系统
binfmt_misc.ko.xz  ceph    dlm    fat      gfs2   lockd          nfs_common  overlayfs  udf
btrfs              cifs    exofs  fscache  isofs  mbcache.ko.xz  nfsd        pstore     xfs
cachefiles         cramfs  ext4   fuse     jbd2   nfs            nls         squashfs
[root@centos7 ~]#df -T    <== 查看一挂在磁盘的文件系统
Filesystem     Type     1K-blocks    Used Available Use% Mounted on
/dev/sda2      xfs       52403200 5018204  47384996  10% /
devtmpfs       devtmpfs    999156       0    999156   0% /dev
tmpfs          tmpfs      1015084       0   1015084   0% /dev/shm
tmpfs          tmpfs      1015084   10184   1004900   2% /run
tmpfs          tmpfs      1015084       0   1015084   0% /sys/fs/cgroup
/dev/sda5      xfs       31441920   33024  31408896   1% /data
/dev/sda1      xfs        1038336  158040    880296  16% /boot
tmpfs          tmpfs       203020       8    203012   1% /run/user/0
/dev/sr0       iso9660    9176232 9176232         0 100% /run/media/root/CentOS 7 x86_64

格式化文件系统

[root@centos7 ~]#mke2fs /dev/sdb1   mke2fs e2是ext2文件系统格式
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done

[root@centos7 ~]#blkid /dev/sdb1       <== 查看sdb1的文件系统格式
/dev/sdb1: UUID="3f84c9bf-60d4-47aa-8a0c-33afb688e845" TYPE="ext2"
[root@centos7 ~]#tune2fs -l /dev/sdb1    <=== 查看
tune2fs 1.42.9 (28-Dec-2013)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          3f84c9bf-60d4-47aa-8a0c-33afb688e845
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      ext_attr resize_inode dir_index filetype sparse_super large_file  <==没有日志功能
Filesystem flags:         signed_directory_hash 
Default mount options:    user_xattr acl     <== 挂载选项 默认挂载属性
Filesystem state:         clean             <== 文件系统的状态
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              65536           <== 节点的数量
Block count:              262144       <== 块的数量
Reserved block count:     13107          <== 保留的块数量
Free blocks:              257701
Free inodes:              65525         <== 空闲的节点数
First block:              0
Block size:               4096          <== 指块大小  分配给一个单位的块  1k个文件也占用一个块
Fragment size:            4096
Reserved GDT blocks:      63
Blocks per group:         32768         <== 一个组多少块
Fragments per group:      32768          同上
Inodes per group:         8192
Inode blocks per group:   512
Filesystem created:       Sun Oct 21 13:31:21 2018
Last mount time:          n/a
Last write time:          Sun Oct 21 13:31:21 2018
Mount count:              0               <== 挂在的次数
Maximum mount count:      -1              <== 最多挂载次数
Last checked:             Sun Oct 21 13:31:21 2018
Check interval:           0 (<none>)          <== 检查间隔
Reserved blocks uid:      0 (user root)        <==保留的使用者
Reserved blocks gid:      0 (group root)       <==保留的使用者     
First inode:              11
Inode size:	          256                       <== 一个源数据在磁盘上占用256个字节
Required extra isize:     28
Desired extra isize:      28
Default directory hash:   half_md4
Directory Hash Seed:      d645257d-548e-40e9-93fd-5059641733f6

下面格式化并指定块大小

[root@centos7 ~]#mkfs.ext4 -b 1024 /dev/sdb1  <== 一块1024  选项-b 
[root@centos7 ~]#mkfs.ext4 -b 1024 -m 0.1 /dev/sdb1  <== -m0.1 保留的块大小 %0.1 给管理员用的
[root@centos7 ~]#mkfs.ext4 -b 1024 -m 0.1 -L "/mut/sdb1" /dev/sdb1  <== -L卷标名字
[root@centos7 ~]#blkid  /dev/sdb1                                   <== 双引号里的卷标信息
/dev/sdb1: LABEL="/mut/sdb1" UUID="c0c719af-cc3d-407b-be98-3257b45f1e47" TYPE="ext4" 
[root@centos7 ~]#blkid -U 8a474365-580f-4413-8f9e-a3cb4a220938      <== -U查询uid设别名
/dev/sda1
[root@centos7 ~]#e2label /dev/sdb1 /mnt/sdb1            <== 设置卷标 设备名在前 名在后  名是挂载路径
[root@centos7 ~]#blkid -L /mnt/sdb1                                 <== 查询卷标名对应的名
/dev/sdb1
[root@centos7 ~]#sed -rn 's#^UUID=(.*) / .*#\1#p' /etc/fstab        <== sed查询uuid设备名
e3fccf98-adeb-4428-8b7b-0f761cd30a60
[root@centos7 ~]#blkid -U `sed -rn 's#^UUID=(.*) / .*#\1#p' /etc/fstab `
/dev/sda2
[root@centos6 ~]#e2label /dev/sda1                       <== 查看卷标信息 /boot: 卷标名
/boot
[root@centos6 ~]#blkid /dev/sda1
/dev/sda1: LABEL="/boot" UUID="739573ac-9e94-424d-abe0-d253b8dcecdd" TYPE="ext4" 

findfs:查找分区
findfs [options] LABEL=
findfs [opyions] UUID=
tune2fs

tune2fs:重新设定ext系列文件系统可调整参数的值

-l 查看指定文件系统超级块信息;super block
-L 'LABEL‘ 修改卷标
-m # 修预留给管理员的空间百分比
-j 将ext2升级为ext3
-O 文件系统属性启用或禁用, –O ^has_journal
-o 调整文件系统的默认挂载选项,–o ^acl
-U UUID 修改UUID号
dumpe2fs:
块分组管理,32768块
-h:查看超级块信息,不显示分组信息

[root@centos7 ~]#mount /dev/sdb1 /mnt/sdb1/     <== 挂载sdb1到mnt sdb1下
     
[root@centos7 ~]#cd /mnt/sdb1/

[root@centos7 sdb1]#ls
lost+found     <== 挂载成功后就有这个目录文件
[root@centos7 sdb1]# touch f1 这就挂在完了  可以使用
[root@centos7 sdb1]#ls
f1  lost+found

mount -u uuid挂载方式:

[root@centos7 ~]#mount -U 92e727b4-9535-4672-b1b9-f86ea688669c  /mnt/sdb1/  blkid查看uuid
[root@centos7 ~]#df -h                                         就可以查看到挂在的设备了
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        50G  4.8G   46G  10% /
devtmpfs        976M     0  976M   0% /dev
tmpfs           992M     0  992M   0% /dev/shm
tmpfs           992M   11M  981M   2% /run
tmpfs           992M     0  992M   0% /sys/fs/cgroup
/dev/sda5        30G   33M   30G   1% /data
/dev/sda1      1014M  155M  860M  16% /boot
tmpfs           199M   36K  199M   1% /run/user/0
/dev/sr0        8.8G  8.8G     0 100% /run/media/root/CentOS 7 x86_64
/dev/sdb1       976M  2.6M  907M   1% /mnt/sdb1

查看谁在使用这个磁盘设备:

[root@centos7 ~]#lsof /mnt/sdb1             <== 谁在使用
[root@centos7 ~]#fuser -v /mnt/sdb1         <== 谁在使用
                     USER        PID ACCESS COMMAND
/mnt/sdb1:           root     kernel mount /mnt/sdb1   <== root在使用

用mount命令挂载文件系统

挂载方法:mount DEVICE MOUNT_POINT
mount:通过查看/etc/mtab文件显示当前已挂载的所有设备
mount [-fnrsvw] [-t vfstype] [-o options] device dir

device:指明要挂载的设备;
(1) 设备文件:例如/dev/sda5
(2) 卷标:-L ‘LABEL’, 例如 -L ‘MYDATA’
(3) UUID, -U ‘UUID’:例如 -U ‘0c50523c-43f1-45e7-
85c0-a126711d406e’
(4) 伪文件系统名称:proc, sysfs, devtmpfs, configfs
dir:挂载点

事先存在;建议使用空目录
进程正在使用中的设备无法被卸载

mount常用命令选项

-t vsftype 指定要挂载的设备上的文件系统类型
-r readonly, 只读挂载
-w read and write, 读写挂载
-n 不更新/etc/mtab,mount不可见
-a 自动挂载所有支持自动挂载的设备(定义在了/etc/fstab文件中,且挂载选项中有auto功能)

-L ‘LABEL’ 以卷标指定挂载设备
-U ‘UUID’ 以UUID指定要挂载的设备
-B, --bind 绑定目录到另一个目录上

查看内核追踪到的已挂载的所有设备
cat /proc/mounts

mount常用命令选项

-o options: (挂载文件系统的选项),多个选项使用逗号分隔
async 异步模式 sync 同步模式,内存更改时,同时写磁盘
atime/noatime 包含目录和文件
diratime/nodiratime 目录的访问时间戳
auto/noauto 是否支持自动挂载,是否支持-a选项
exec/noexec 是否支持将文件系统上运行应用程序
dev/nodev 是否支持在此文件系统上使用设备文件
suid/nosuid 是否支持suid和sgid权限
remount 重新挂载
ro只读 rw读写
user/nouser 是否允许普通用户挂载此设备,/etc/fstab使用
acl 启用此文件系统上的acl功能
loop 使用loop设备

判断这个文件加 是不是个挂载点


[root@centos7 ~]#findmnt  /data 判断data
TARGET SOURCE    FSTYPE OPTIONS
/data  /dev/sda5 xfs    rw,relatime,attr2,inode64,noquota  <== 确定是个挂载点
[root@centos7 ~]#findmnt  /etc   <== 判断etc是不是挂载点
[root@centos7 ~]#echo $?       <== 0  不是
1                             <== 1代表不是挂载点

查看swap的类型 82 :

[root@centos7 ~]#fdisk -l /dev/sda

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000a6848

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   106956799    52428800   83  Linux
/dev/sda3       106956800   115345407     4194304   82  Linux swap / Solaris
/dev/sda4       115345408   419430399   152042496    5  Extended
/dev/sda5       115347456   178262015    31457280   83  Linux

增加swap

创建一个新的磁盘 sdc

[root@centos7 ~]#fdisk /dev/sdc               <== 分区
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 0x1f8a9087.

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-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +4G                         <==4g大小 
Partition 1 of type Linux and of size 4 GiB is set

Command (m for help): p

Disk /dev/sdc: 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: 0x1f8a9087

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048     8390655     4194304   83  Linux

Command (m for help): t       <== 指定 swap id号 82
Selected partition 1
Hex code (type L to list all codes): l    <== l列出所有id号

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list all codes): 82   <== 设置82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): p

Disk /dev/sdc: 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: 0x1f8a9087

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048     8390655     4194304   82  Linux swap / Solaris

Command (m for help): w            <== 存盘退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos7 ~]#lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0   50G  0 part /
├─sda3   8:3    0    4G  0 part [SWAP]
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0   30G  0 part /data
sdb      8:16   0   20G  0 disk 
├─sdb1   8:17   0    1G  0 part /mnt/sdb1
├─sdb2   8:18   0    2G  0 part 
├─sdb3   8:19   0    1K  0 part 
├─sdb5   8:21   0    1G  0 part 
├─sdb6   8:22   0    2G  0 part 
└─sdb7   8:23   0    2G  0 part 
sdc      8:32   0   20G  0 disk 
└─sdc1   8:33   0    4G  0 part 
sr0     11:0    1  8.8G  0 rom  /run/media/root/CentOS 7 x86_64
[root@centos7 ~]#df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2       52403200 5015224  47387976  10% /
devtmpfs          999156       0    999156   0% /dev
tmpfs            1015084       0   1015084   0% /dev/shm
tmpfs            1015084   10684   1004400   2% /run
tmpfs            1015084       0   1015084   0% /sys/fs/cgroup
/dev/sda5       31441920   33024  31408896   1% /data
/dev/sdb1         999320    2564    927944   1% /mnt/sdb1
/dev/sda1        1038336  158040    880296  16% /boot
tmpfs             203020      28    202992   1% /run/user/0
/dev/sr0         9176232 9176232         0 100% /run/media/root/CentOS 7 x86_64
[root@centos7 ~]#mkswap /dev/sdc1    <== 建立swap分区
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=d823147f-4fed-4b96-8970-97d8d2580f5c
[root@centos7 ~]#blkid
/dev/sda1: UUID="8a474365-580f-4413-8f9e-a3cb4a220938" TYPE="xfs" 
/dev/sda2: UUID="e3fccf98-adeb-4428-8b7b-0f761cd30a60" TYPE="xfs" 
/dev/sda3: UUID="1018f445-9c4c-43db-88cb-ae04ee7b4f63" TYPE="swap" 
/dev/sda5: UUID="72a23ce1-e9c0-4669-8829-f8313f90b176" TYPE="xfs" 
/dev/sdb1: LABEL="/mnt/sdb1" UUID="92e727b4-9535-4672-b1b9-f86ea688669c" TYPE="ext4" 
/dev/sdb2: UUID="03e3a7a9-a331-4899-a03b-95c6ff729617" TYPE="xfs" 
/dev/sr0: UUID="2018-05-07-12-53-47-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/sdc1: UUID="d823147f-4fed-4b96-8970-97d8d2580f5c" TYPE="swap" 
[root@centos7 ~]#vim /etc/fstab   <== 打开配置文件 添加进去生效
[root@centos7 ~]#free -h          <== 原有swap大小 和内存使用情况
              total        used        free      shared  buff/cache   available
Mem:           1.9G        715M        769M         10M        497M        1.0G
Swap:          4.0G          0B        4.0G
[root@centos7 ~]#swapon  -a       <== 同步新的4g swap
[root@centos7 ~]#free -h          <== 生效了   一共8g
              total        used        free        shared  buff/cache   available
Mem:           1.9G        718M        766M         10M        497M        1.0G
Swap:          8.0G          0B        8.0G

swap 优先级提高

[root@centos7 ~]#vim /etc/fstab <==打开挂载配置文件
UUID=d823147f-4fed-4b96-8970-97d8d2580f5c swap                    swap    pri=10 <== 添加此项  >        0 0    
[root@centos7 ~]#free -h
              total        used        free      shared  buff/cache   available
Mem:           1.9G        715M        769M         10M        497M        1.0G
Swap:          4.0G          0B        4.0G
[root@centos7 ~]#swapon  -a
[root@centos7 ~]#free -h
              total        used        free      shared  buff/cache   available
Mem:           1.9G        718M        766M         10M        497M        1.0G
Swap:          8.0G          0B        8.0G
[root@centos7 ~]#vim /etc/fstab 
[root@centos7 ~]#cat /proc/swaps 
Filename				Type		Size	Used	Priority
/dev/sda3                               partition	4194300	0	-1
/dev/sdc1                               partition	4194300	0	-2
[root@centos7 ~]#swapoff /dev/sdc1 <==先禁用他
[root@centos7 ~]#cat /proc/swaps 
Filename				Type		Size	Used	Priority
/dev/sda3                               partition	4194300	0	-1

[root@centos7 ~]#swapon -a 再启用他
[root@centos7 ~]#cat /proc/swaps <== 就ok了
Filename Type Size Used Priority
/dev/sda3 partition 4194300 0 -1
/dev/sdc1 partition 4194300 0 10

你可能感兴趣的:(linux磁盘与文件系统管理)