Linux4.2添加磁盘

磁盘分区

  虚拟机需要通过设置手动添加硬盘,本例设置为10GB。在机房服务器中是支持热拔插硬盘的。

  磁盘一般要先分区,再格式化,最后挂载才能使用。但是也可以不分区直接格式化挂载使用。

[root@chy002 ~]# fdisk
用法:
 fdisk [选项] <磁盘>    更改分区表
 fdisk [选项] -l <磁盘> 列出分区表
 fdisk -s <分区>        给出分区大小(块数)

选项:
 -b <大小>             扇区大小(512、1024、2048或4096)
 -c[=<模式>]           兼容模式:“dos”或“nondos”(默认)
 -h                    打印此帮助文本
 -u[=<单位>]           显示单位:“cylinders”(柱面)或“sectors”(扇区,默认)
 -v                    打印程序版本
 -C <数字>             指定柱面数
 -H <数字>             指定磁头数
 -S <数字>             指定每个磁道的扇区数

  重启计算机之后,可以看到新增加的硬盘。

[root@chy002 ~]# fdisk -l

磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000c9e18

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648     4605951     2097152   82  Linux swap / Solaris
/dev/sda3         4605952    41943039    18668544   83  Linux

磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

  开始进行分区

[root@chy002 ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0x0214a8e0 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):m
命令操作
   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)

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):1
起始 扇区 (2048-20971519,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-20971519,默认为 20971519):+2G
分区 1 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0214a8e0

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux

命令(输入 m 获取帮助):n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
分区号 (2-4,默认 2):
起始 扇区 (4196352-20971519,默认为 4196352):
将使用默认值 4196352
Last 扇区, +扇区 or +size{K,M,G} (4196352-20971519,默认为 20971519):+2G
分区 2 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0214a8e0

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     8390655     2097152   83  Linux

命令(输入 m 获取帮助):n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
分区号 (3,4,默认 3):
起始 扇区 (8390656-20971519,默认为 8390656):
将使用默认值 8390656
Last 扇区, +扇区 or +size{K,M,G} (8390656-20971519,默认为 20971519):+2G
分区 3 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): p
已选择分区 4
起始 扇区 (12584960-20971519,默认为 12584960):
将使用默认值 12584960
Last 扇区, +扇区 or +size{K,M,G} (12584960-20971519,默认为 20971519):
将使用默认值 20971519
分区 4 已设置为 Linux 类型,大小设为 4 GiB

命令(输入 m 获取帮助):n
If you want to create more than four partitions, you must replace a
primary partition with an extended partition first.   

#可以发现,4个主分区后,就不能再创建分区。主分区+扩展分区=<4.
#如果还需要添加磁盘,可以在扩展分区里,再进行分逻辑分区

命令(输入 m 获取帮助):d
分区号 (1-4,默认 4):
分区 4 已删除

命令(输入 m 获取帮助):n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): e
已选择分区 4
起始 扇区 (12584960-20971519,默认为 12584960):
Last 扇区, +扇区 or +size{K,M,G} (12584960-20971519,默认为 20971519):
将使用默认值 20971519
分区 4 已设置为 Extended 类型,大小设为 4 GiB

命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 5
起始 扇区 (12587008-20971519,默认为 12587008):
将使用默认值 12587008
Last 扇区, +扇区 or +size{K,M,G} (12587008-20971519,默认为 20971519):+1G
分区 5 已设置为 Linux 类型,大小设为 1 GiB

#可以发现只能添加逻辑分区

命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 6
起始 扇区 (14686208-20971519,默认为 14686208):
将使用默认值 14686208
Last 扇区, +扇区 or +size{K,M,G} (14686208-20971519,默认为 20971519):+1G
分区 6 已设置为 Linux 类型,大小设为 1 GiB

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0214a8e0

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     8390655     2097152   83  Linux
/dev/sdb3         8390656    12584959     2097152   83  Linux
/dev/sdb4        12584960    20971519     4193280    5  Extended
/dev/sdb5        12587008    14684159     1048576   83  Linux
/dev/sdb6        14686208    16783359     1048576   83  Linux

命令(输入 m 获取帮助):d
分区号 (1-6,默认 6):5
分区 5 已删除

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0214a8e0

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   83  Linux
/dev/sdb2         4196352     8390655     2097152   83  Linux
/dev/sdb3         8390656    12584959     2097152   83  Linux
/dev/sdb4        12584960    20971519     4193280    5  Extended
/dev/sdb5        14686208    16783359     1048576   83  Linux

#可以发现,删除逻辑分区,数后的磁盘会紧随跟上,数字不会留空
#但是主分区会留空
命令(输入 m 获取帮助):d
分区号 (1-5,默认 5):1
分区 1 已删除

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0214a8e0

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb2         4196352     8390655     2097152   83  Linux
/dev/sdb3         8390656    12584959     2097152   83  Linux
/dev/sdb4        12584960    20971519     4193280    5  Extended
/dev/sdb5        14686208    16783359     1048576   83  Linux


命令(输入 m 获取帮助):d
分区号 (2-5,默认 5):4
分区 4 已删除

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0214a8e0

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb2         4196352     8390655     2097152   83  Linux
/dev/sdb3         8390656    12584959     2097152   83  Linux

#可以发现删除扩展分区4后,逻辑分区5也会消失

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。


#查看分区可以发现,多了两个分区
[root@chy002 ~]# fdisk -l

磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000c9e18

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648     4605951     2097152   82  Linux swap / Solaris
/dev/sda3         4605952    41943039    18668544   83  Linux

磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0214a8e0

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb2         4196352     8390655     2097152   83  Linux
/dev/sdb3         8390656    12584959     2097152   83  Linux

磁盘格式化

   查看已经挂载的分区文件系统格式

[root@chy002 ~]# mount    #最后几行可以看到,centos7磁盘格式为xfs
... ...

/dev/sda3 on / type xfs (rw,relatime,attr2,inode64,noquota)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=32,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
mqueue on /dev/mqueue type mqueue (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime)
/dev/sda1 on /boot type xfs (rw,relatime,attr2,inode64,noquota)

  关于块和文件时间大小,说一下;

[root@chy002 ~]# touch  /tmp/123123.txt
[root@chy002 ~]# ls -l /tmp/123123.txt
-rw-r--r-- 1 root root 0 11月  3 05:23 /tmp/123123.txt
[root@chy002 ~]# du -sh !$
du -sh /tmp/123123.txt
0       /tmp/123123.txt
[root@chy002 ~]# echo "1"> !$
echo "1"> /tmp/123123.txt
[root@chy002 ~]# ls -l /tmp/123123.txt
-rw-r--r-- 1 root root 2 11月  3 05:23 /tmp/123123.txt
[root@chy002 ~]# du -sh /tmp/123123.txt                    
4.0K    /tmp/123123.txt

#可以发现,仅仅写了一个数字,也是占用了4.0k一个块结构。

  

#mke2fs   -t 格式    -b块大小 [一般为4k]     磁盘       该命令不支持 xfs格式
[root@chy002 ~]# mke2fs -t ext4 /dev/sdb2       #mkfs.ext4相同效果
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
第一个数据块=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: 完成
正在写入inode表: 完成
Creating journal (16384 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

  如果需要格式话为xfs格式,使用下面的命令。

[root@chy002 ~]# mkfs.xfs -f /dev/sdb3
meta-data=/dev/sdb3              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@chy002 ~]# blkid /dev/sdb3      #没有挂载的话,mount看不到sdb3,使用该命令可以
/dev/sdb3: UUID="c98d48c4-e175-4bf6-a33b-001bb4e7c1c5" TYPE="xfs"

  其中的 -b  参数,可以指定块的大小, -m 指定分区预留大小。

[root@chy002 ~]# mke2fs -b 8192 -m 1 /dev/sdb2
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)
无论如何也要继续? (y,n) y
Warning: 8192-byte blocks too big for system (max 4096), forced to continue
warning: 32 blocks unused.

文件系统标签=
OS type: Linux
块大小=8192 (log=3)
分块大小=8192 (log=3)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 262112 blocks
2620 blocks (1.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=268402688
4 block groups
65528 blocks per group, 65528 fragments per group
32768 inodes per group
Superblock backups stored on blocks:
        65528, 196584

Allocating group tables: 完成
正在写入inode表: 完成
Writing superblocks and filesystem accounting information: 完成

  mkfs.ext4  与  mke2fs  支持选项是一致的

  -i 指定多少个字节对应一个 inode,最低一个块对应一个inode。

[root@chy002 ~]# mke2fs -i 8192 -t ext4 /dev/sdb2
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
262144 inodes, 524288 blocks               #可以发现变成了  1:2,默认1:4   16384字节
26214 blocks (5.00%) reserved for the super user
第一个数据块=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: 完成
正在写入inode表: 完成
Creating journal (16384 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

磁盘挂载

#磁盘挂载之后才能使用
#挂载命令
#mount   盘符(UUID或者分区名)     挂载文件

#卸载           卸载前保证路径不在当下磁盘,
#umount -l  在磁盘路径下也可以卸载
#             -a  自动挂载fstab里面的

[root@chy002 ~]# mount /dev/sdb2  /mnt/
[root@chy002 ~]# df -h
文件系统        容量  已用  可用 已用% 挂载点
/dev/sda3        18G  1.1G   17G    6% /
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/sdb2       1.9G  6.0M  1.8G    1% /mnt
[root@chy002 ~]# mount    #通过mount可以看到磁盘的格式
... ...
/dev/sdb2 on /mnt type ext4 (rw,relatime,data=ordered)

  mount有挂载选项。

[root@chy002 ~]# man mount       #默认这么多选项,可以自行添加删去
       defaults
              Use default options: rw, suid,  dev,  exec,  auto,  nouser,  and
              async.

#-o 选项   rw只读/suid允许分区suid/dev用不到/exec允许可执行,,改成no exec则磁盘内文件就变成不可执行  /auto自动挂载/nouser不允许普通用户挂载/async磁盘和内存不同步,隔段时间内存写入磁盘

  文件  /etc/fstab

[root@chy002 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Mon Oct 16 22:15:47 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=680bd219-7a25-4856-821d-77f250733e5e /                       xfs     defaults        1 1
UUID=95c13422-958b-4bb0-8968-c7154e34b754 /boot                   xfs     defaults        1 2
UUID=079a651c-3b8d-46a5-ad21-8543bbf79bdf swap                    swap    defaults        0 0



#/etc/fstab   系统启动时会挂载分区,就是通过该文件配置的
#第一列:分区标识(UUID/分区名)  
#第二列:挂载点   
#第三列:分区的格式    
#第四列:一般写defaults,就是上面的-o后面的六个默认选项     
#第五列:是否被dump备份,现在几乎没用,设置0即可 
#第六列:开机自检,根分区必须为1,fastab表仅允许一个1,检测完了才去检测2
原文地址:https://www.cnblogs.com/chyuanliu/p/7774943.html