20180926 磁盘格式化、磁盘挂载、增加swap空间

倖福魔咒の 提交于 2020-04-14 16:56:04

【推荐阅读】微服务还能火多久?>>>

磁盘格式化

查看当前操作系统支持的文件系统格式 :

[root@centos01 ~]# cat /etc/filesystems 
ext4
ext3
ext2
nodev proc
nodev devpts
iso9660
vfat
hfs
hfsplus

磁盘格式化

# mkfs.ext4 等价于 mke2fs -t ext4
[root@centos01 ~]# mke2fs -t xfs -b 2048 /dev/sdb1  
mke2fs 1.42.9 (28-Dec-2013)

Your mke2fs.conf file does not define the xfs filesystem type.
Aborting...
# 使用mke2fs格式化为xfs不行,可以换为mkfs.xfs命令

[root@centos01 ~]# mkfs.xfs  /dev/sdb1
meta-data=/dev/sdb1              isize=256    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=524288, 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@centos01 ~]# blkid /dev/sdb1  # 查看未挂载的分区的文件系统类型
/dev/sdb1: UUID="ae4a7961-3080-4625-bf79-f9d422aaa147" TYPE="xfs"

[root@centos01 ~]# mke2fs -t ext4 /dev/sdb1 # 格式化为ext4文件类型
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
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

[root@centos01 ~]# blkid /dev/sdb1
/dev/sdb1: UUID="aeeab36c-5a73-4942-b7ae-a466934c649c" TYPE="ext4"

[root@centos01 ~]# mke2fs -b 8192 /dev/sdb1 # -b 设置块大小
Warning: blocksize 8192 not usable on most systems.
mke2fs 1.42.9 (28-Dec-2013)
mke2fs: 8192-byte blocks too big for system (max 4096)
Proceed anyway? (y,n) n
# 超过系统最大块大小时会提示 

[root@centos01 ~]# mke2fs -m 4 /dev/sdb1 # -m 设置root用户的block百分比
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
131072 inodes, 524288 blocks
20971 blocks (4.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

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

# -i 多少字节对应一个inode,最小是一个块大小
[root@centos01 ~]# mke2fs -i 8192 -t ext4 /dev/sdb1 
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
262144 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

磁盘挂载

[root@centos01 ~]# mount /dev/sdb /mnt/  # 把/dev/sdb分区挂载到/mnt/目录
[root@centos01 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        26G  862M   25G   4% /
devtmpfs        489M     0  489M   0% /dev
tmpfs           494M     0  494M   0% /dev/shm
tmpfs           494M  6.7M  487M   2% /run
tmpfs           494M     0  494M   0% /sys/fs/cgroup
/dev/sda1       197M   75M  123M  38% /boot
/dev/sdb        5.0G   33M  5.0G   1% /mnt

通过分区UUID挂载

[root@centos01 /]# blkid /dev/sdb
/dev/sdb: UUID="d259e6a3-dd14-4bab-91ac-3a5c54314782" TYPE="xfs"
[root@centos01 /]# cd
[root@centos01 ~]# mount UUID="d259e6a3-dd14-4bab-91ac-3a5c54314782" /mnt  .
[root@centos01 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        26G  862M   25G   4% /
devtmpfs        489M     0  489M   0% /dev
tmpfs           494M     0  494M   0% /dev/shm
tmpfs           494M  6.7M  487M   2% /run
tmpfs           494M     0  494M   0% /sys/fs/cgroup
/dev/sda1       197M   75M  123M  38% /boot
/dev/sdb        5.0G   33M  5.0G   1% /mnt

umount 挂载点或分区 # 卸载挂载 umount /mnt 或 umount /dev/sdb

[root@centos01 ~]# mount /dev/sdb /mnt
[root@centos01 ~]# cd /mnt/
[root@centos01 mnt]# ls
# 如果在/mnt里,卸载时会有提示,此时要么从/mnt目录退出或者加-l
[root@centos01 mnt]# umount /dev/sdb
umount: /mnt: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@centos01 mnt]# cd ..
[root@centos01 /]# umount /dev/sdb

[root@centos01 mnt]# umount /dev/sdb
umount: /mnt: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@centos01 mnt]# umount /mnt/
umount: /mnt: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@centos01 mnt]# umount -l /dev/sdb

/etc/fstab 配置文件配置系统启动时需要挂载的各个分区

第1列是分区的标识,可以写分区的LABEL、分区的UUID、分区名(/dev/sda1)
第2列是挂载点
第3列是分区格式
第4列是mount的一些挂载参数
第5列表示是否被dump备份。1表示备份,0表示不备份 第6列表示开机时是否自检磁盘,1和2都表示检测,0表示不检测。自检时,1比2优先级高,先检测1,再检测2

手动增加swap

步骤为: 建立swapfile → 格式化为swap格式 →启用该虚拟磁盘
建立swapfile

[root@centos01 ~]# dd if=/dev/zero of=/tmp/newdisk bs=1M count=128
128+0 records in
128+0 records out
134217728 bytes (134 MB) copied, 5.06216 s, 26.5 MB/s

格式化为swap格式

[root@centos01 ~]# mkswap -f /tmp/newdisk
Setting up swapspace version 1, size = 131068 KiB
no label, UUID=abb8af33-f7d9-49bc-9ef9-87aa17fa4b7f
[root@centos01 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           987        373        613          6          0        212
-/+ buffers/cache:        160        827
Swap:         4095          0       4095

启用与卸载

[root@centos01 ~]# swapon /tmp/newdisk # 启用
swapon: /tmp/newdisk: insecure permissions 0644, 0600 suggested.
[root@centos01 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           987        373        613          6          0        212
-/+ buffers/cache:        160        827
Swap:         4223          0       4223
[root@centos01 ~]# swapoff /tmp/newdisk # 卸载
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!