虚拟机新增/扩充硬盘、分区

目录

 

1、虚拟机新增硬盘

2、分区

3、删除分区槽

4、磁盘格式化

5、挂载-mount (重启会还原)

6、卸载挂载-umount 

7、开机挂载 (/etc/fstab)


1、虚拟机新增硬盘

2、分区

列出本系统下的所有磁盘与磁盘内的分区信息

[root@localhost ~]# lsblk    
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0    1G  0 disk     # 新增了一个sdb
sr0              11:0    1  8.1G  0 rom 

 查看硬盘及分区情况

[root@localhost ~]# fdisk -l

Disk /dev/sda: 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: 0x0009a51e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

# 新增的1G 硬盘 /dev/sdb;此时是未分区
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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 /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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

使用fdisk分区

MBR 分区表使用 fdisk 分区, GPT 分区表使用 gdisk 分区!!  两个命令使用方法基本一致。

MBR 最多支持2T,GPT理论上是没有限度的。所以一般都是使用的fdisk ,只有达到T级别的硬盘才使用gdisk。

# 查看 /dev/sdb 磁盘的相关数据
[root@localhost ~]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)         # 磁盘的模块名称(厂商)
Disk /dev/sdb: 1074MB                          # 磁盘总量
Sector size (logical/physical): 512B/512B      # 每个逻辑/物理扇区容量
Partition Table: msdos                         # 分区表格式(MBR/GPT)
Disk Flags: 

Number  Start   End    Size   Type     File system  Flags    # 分区数据
 1      1049kB  525MB  524MB  primary
# 对/dev/sdb做分区
[root@localhost ~]# fdisk /dev/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 0x7cd5d1a2.

Command (m for help): p    # 印出分区表

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x7cd5d1a2

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n    # 增加一个分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended     
#  l   logical   # 存在扩展分区时的选项
# primary 主分区。可以做引导分区,最多不能超过四个
# extended 扩展分区,最多一个
# logical 逻辑分区
Select (default p): p
Partition number (1-4, default 1): 1    # 分区编号(默认即可)
First sector (2048-2097151, default 2048):     # 起始位置(默认即可)
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +500M    
Partition 1 of type Linux and of size 500 MiB is set    # 结束位置+500MiB大小
# 不需要计算扇区号码,直接使用 ‘+容量’ 的方式(+500M/+1G)

Command (m for help): p    # 查看检验
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x7cd5d1a2

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     1026047      512000   83  Linux    

Command (m for help): w  # 保存配置并退出;q 不保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
# 查看新增分区
[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0    1G  0 disk 
└─sdb1            8:17   0  500M  0 part         # 新建分区
sr0              11:0    1  8.1G  0 rom 

可以使用  partprobe 更新 Linux 核心的分区表信息:

#  partprobe -s

3、删除分区槽

[root@localhost ~]# fdisk /dev/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.


Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x7cd5d1a2

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     1026047      512000   83  Linux
/dev/sdb2         1026048     2050047      512000   83  Linux
/dev/sdb3         2050048     2097151       23552   83  Linux

Command (m for help): d    # d 删除
Partition number (1-3, default 3): 3
Partition 3 is deleted

Command (m for help): p    # /dev/sdb3 已删除

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x7cd5d1a2

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     1026047      512000   83  Linux
/dev/sdb2         1026048     2050047      512000   83  Linux

Command (m for help): w    # 保存并退出
The partition table has been altered!

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

4、磁盘格式化

# mkfs.xfs [-b bsize] [-d parms] [-i parms] [-l parms] [-L label] [-f] [-r parms] 装置名称
选项与参数:
关于单位:底下只要谈到『数值』时,没有加单位则为 bytes 值,可以用 k,m,g,t,p (小写)等来解释
 比较特殊的是 s 这个单位,它指的是 sector 的『个数』喔!
-b :后面接的是 block 容量,可由 512 到 64k,不过最大容量限制为 Linux 的 4k 喔!
-d :后面接的是重要的 data section 的相关参数值,主要的值有:
 agcount=数值 :设定需要几个储存群组的意思(AG),通常与 CPU 有关
 agsize=数值 :每个 AG 设定为多少容量的意思,通常 agcount/agsize 只选一个设定即可
 file :指的是『格式化的装置是个文件而不是个装置』的意思!(例如虚拟磁盘)
 size=数值 :data section 的容量,亦即你可以不将全部的装置容量用完的意思
 su=数值 :当有 RAID 时,那个 stripe 数值的意思,与底下的 sw 搭配使用
 sw=数值 :当有 RAID 时,用于储存数据的磁盘数量(须扣除备份碟与备用碟)
 sunit=数值 :与 su 相当,不过单位使用的是『几个 sector(512bytes 大小)』的意思
 swidth=数值 :就是 su*sw 的数值,但是以『几个 sector(512bytes 大小)』来设定
-f :如果装置内已经有文件系统,则需要使用这个 -f 来强制格式化才行!
-i :与 inode 有较相关的设定,主要的设定值有:
 size=数值 :最小是 256bytes 最大是 2k,一般保留 256 就足够使用了!
 internal=[0|1]:log 装置是否为内建?预设为 1 内建,如果要用外部装置,使用底下设定
 logdev=device :log 装置为后面接的那个装置上头的意思,需设定 internal=0 才可!
 size=数值 :指定这块登录区的容量,通常最小得要有 512 个 block,大约 2M 以上才行!
-L :后面接这个文件系统的标头名称 Label name 的意思!
-r :指定 realtime section 的相关设定值,常见的有:
 extsize=数值 :就是那个重要的 extent 数值,一般不须设定,但有 RAID 时,
 最好设定与 swidth 的数值相同较佳!最小为 4K 最大为 1G 。

# /dev/sdb1 格式化为 xfs 文件系统;默认值即可
[root@localhost ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=32000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=128000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=855, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

[root@localhost ~]# blkid /dev/sdb1
/dev/sdb1: UUID="3e831335-d1e5-4343-a9d6-8410b6081040" TYPE="xfs" 

其他文件系统

[root@localhost ~]# mkfs   按两下tab
mkfs         mkfs.btrfs   mkfs.cramfs  mkfs.ext2    mkfs.ext3    mkfs.ext4    mkfs.minix   mkfs.xfs  

5、挂载-mount (重启会还原)

没有格式化就挂载,会报错

[root@localhost /]# mount /dev/sdb5 /mnt/test5
mount: /dev/sdb5 is write-protected, mounting read-only
mount: unknown filesystem type '(null)'

格式化后挂载

1、 mount [装置文件名] [挂载点]

2、 mount [UUID="..."] [挂载点]

[root@localhost ~]# mkfs.ext4 /dev/sdb5    # 格式化

[root@localhost ~]# blkid /dev/sdb5            # 查看UUID
/dev/sdb5: UUID="782aaa82-2064-4b78-a0ef-877f18b569a7" TYPE="ext4" 

# 挂载前
[root@localhost /]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.2G   16G   7% /
devtmpfs                 901M     0  901M   0% /dev
tmpfs                    912M     0  912M   0% /dev/shm
tmpfs                    912M  8.6M  904M   1% /run
tmpfs                    912M     0  912M   0% /sys/fs/cgroup
/dev/sda1               1014M  143M  872M  15% /boot
tmpfs                    183M     0  183M   0% /run/user/0
[root@localhost /]# 
[root@localhost /]# cd /mnt
[root@localhost mnt]# mkdir test5    #新建挂载目录
[root@localhost mnt]# mount /dev/sdb5 /mnt/test5    # 挂载
[root@localhost mnt]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.2G   16G   7% /
devtmpfs                 901M     0  901M   0% /dev
tmpfs                    912M     0  912M   0% /dev/shm
tmpfs                    912M  8.6M  904M   1% /run
tmpfs                    912M     0  912M   0% /sys/fs/cgroup
/dev/sda1               1014M  143M  872M  15% /boot
tmpfs                    183M     0  183M   0% /run/user/0
/dev/sdb5                 19M  332K   17M   2% /mnt/test5    # 挂载后

6、卸载挂载-umount 

[root@localhost ~]# df -h    # 卸载前
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.2G   16G   7% /
devtmpfs                 901M     0  901M   0% /dev
tmpfs                    912M     0  912M   0% /dev/shm
tmpfs                    912M  8.6M  904M   1% /run
tmpfs                    912M     0  912M   0% /sys/fs/cgroup
/dev/sda1               1014M  143M  872M  15% /boot
tmpfs                    183M     0  183M   0% /run/user/0
/dev/sdb5                 19M  332K   17M   2% /mnt/test5
/dev/sdb6                 19M  332K   17M   2% /mnt/test6
[root@localhost ~]# 
[root@localhost ~]# umount /mnt/test5    # 通过挂载点卸载
[root@localhost ~]# umount /dev/sdb6     # 通过装置名称卸载
[root@localhost ~]# 
[root@localhost ~]# df -h    # 卸载后
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.2G   16G   7% /
devtmpfs                 901M     0  901M   0% /dev
tmpfs                    912M     0  912M   0% /dev/shm
tmpfs                    912M  8.6M  904M   1% /run
tmpfs                    912M     0  912M   0% /sys/fs/cgroup
/dev/sda1               1014M  143M  872M  15% /boot
tmpfs                    183M     0  183M   0% /run/user/0

7、开机挂载 (/etc/fstab)

/etc/fstab 是开机时的配置文件,不过实际 filesystem 的挂载是记录到 /etc/mtab 与 /proc/mounts 这 两个文件当中的。每次我们在更动 filesystem 的挂载时,也会同时更新这两个文件。

第一栏:三种写法如下:

  • 文件系统或磁盘的装置文件名,如 /dev/vda2 等
  • 文件系统的 UUID 名称,如 UUID=xxx
  • 文件系统的 LABEL 名称,例如 LABEL=xxx

第二栏:挂载点

  • 一定是目录

第三栏:磁盘分区槽的文件系统

  • 在手动挂载时可以让系统自动测试挂载,但在这个文件当中我们必须要手动写入文件系统才行! 包括 xfs, ext4, vfat, reiserfs, nfs 等等

第四栏:文件系统参数

第五栏:能否被 dump 备份指令作用

  • dump 是一个用来做为备份的指令,不过现在有太多的备份方案了,所以这个项目可以不要理会啦! 直接输入 0 就好了

第六栏:是否以 fsck 检验扇区

  • 早期开机的流程中,会有一段时间去检验本机的文件系统,看看文件系统是否完整 (clean)。 不过 这个方式使用的主要是透过 fsck 去做的,我们现在用的 xfs 文件系统就没有办法适用,因为 xfs 会自己进行检验,不需要额外进行这个动作!所以直接填 0 就好了 

挂载步骤:

# 先查看下分区的UUID和文件系统!
[root@localhost ~]# blkid /dev/sdb5
/dev/sdb5: UUID="782aaa82-2064-4b78-a0ef-877f18b569a7" TYPE="ext4" 
 
[root@localhost ~]# blkid /dev/sdb6
/dev/sdb6: UUID="f254bb63-8e6f-4304-bfb0-8a0c11c86b3c" TYPE="ext4" 

[root@localhost ~]# vim /etc/fstab 
/dev/mapper/centos-root /                       xfs     defaults     0 0
UUID=c3c20273-7091-4147-b6d0-352fc0ac3b75 /boot xfs     defaults     0 0
/dev/mapper/centos-swap swap                    swap    defaults     0 0
# 这里使用两种方式,推荐使用UUID,唯一性
/dev/sdb5                                   /mnt/test5  ext4 defaults 0 0
UUID="f254bb63-8e6f-4304-bfb0-8a0c11c86b3c" /mnt/test6  ext4 defaults 0 0

[root@localhost ~]# reboot    # 重启
[root@localhost ~]# df -h     # 观察已经完成开机挂载
Filesystem               Size  Used Avail Use% Mounted on
...
/dev/sdb5                 19M  332K   17M   2% /mnt/test5  
/dev/sdb6                 19M  332K   17M   2% /mnt/test6
tmpfs                    183M     0  183M   0% /run/user/0

配置出错的话,会启动不了的情况:

这时直接输入root密码,就进入进入单人维护模式;

然后修改错误,或者删除出错的地方或从新挂载,保存退出重启即可。

如果显示只读(read only)状态,执行:

# mount -n -o remount,rw /
# 之后就可以正常修改/etc/fstab了

容易出错的就是文件系统写错: xfs, ext4, vfat, reiserfs, nfs 等等

猜你喜欢

转载自blog.csdn.net/qq_41210783/article/details/112360370
今日推荐