가상 머신에 하드 디스크 및 파티션 추가/확장

목차

 

1. 가상 머신에 하드 디스크 추가

2. 파티션

3. 파티션 슬롯 삭제

4. 디스크 포맷

5. 마운트 -마운트(다시 시작하면 복원됨)

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가 일반적으로 사용되고 gdisk는 T 레벨에 도달한 하드 디스크에만 사용됩니다.

# 查看 /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] 장치 이름 옵션 및 매개변수: 장치에 대한 정보:
그냥
이야기 아래에 대해 "값"에 관해서는 단위가 추가되지 않은 경우 바이트 값입니다. k, m, g, t, p(소문자)를 사용하여 설명할 수 있습니다. 특수 단위는 s입니다
 . 섹터의 "번호"를 나타냅니다. !
-b: 512에서 64k까지 가능하지만 Linux에서는 최대 용량이 4k로 제한되는 블록 용량이 뒤따릅니다!
-d: 중요한 데이터 섹션의 관련 매개변수 값이 뒤따르며, 주요 값은 다음과 같습니다.  agcount
 =value: 필요한 스토리지 그룹(AG) 수의 의미를 설정합니다.
=값: 각 AG 용량이 얼마나 설정되어 있는지, 일반적으로 agcount/agsize는 하나의 설정 파일로만 설정할 수 있습니다
 : "포맷된 장치는 장치가 아니라 파일입니다"라는 의미를 나타냅니다! (예: 가상 디스크)
 size=value: 데이터 섹션의 용량, 이는 장치 용량을 모두 사용할 수 없음을 의미합니다. su=value: RAID가 있는 경우 스트라이프 값의 의미,  sw
 와 함께 사용 sw 이하
=값: RAID가 있는 경우 데이터를 저장하는 데 사용되는 디스크 수(백업 디스크 및 예비 디스크는 차감해야 함)
 sunit=값: su와 동일하지만 단위는 "여러 섹터(512bytes 크기)"의 의미를 사용합니다. )"
 width= 값: su*sw의 값이지만 "여러 섹터(512바이트 크기)"에 설정
-f: 장치에 이미 파일 시스템이 있는 경우 이 -f를 사용하여 강제로 포맷해야 합니다!
-i : inode와 관련된 설정이 있으며 주요 설정 값은
 다음과 같습니다.
 internal=[0|1]: 로그 기능이 내장되어 있습니까? 기본값은 1이며 내장되어 있습니다 외부 장치를 사용하려면 다음 설정을 사용하십시오
 .
 size=Number: 이 로그인 영역의 용량을 지정하십시오. 일반적으로 최소 512 블록, 약 2M 이상입니다!
-L: 뒤에 파일 시스템의 헤더 이름 레이블 이름이 의미합니다!
-r: 실시간 섹션의 관련 설정 값을 지정하며, 공통 사항은 다음과 같습니다.
 extsize=숫자: 중요한 익스텐트 값으로 일반적으로 설정할 필요는 없지만 RAID가 있는 경우에는 다음과 같이
 설정하는 것이 좋습니다. 너비와 같은 값! 최소값은 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. 마운트 -마운트(다시 시작하면 복원됨)

포맷하지 않고 마운트하면 오류가 보고 됩니다 .

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

포맷 후 마운트 :

1. mount [장치 파일 이름] [마운트 지점]

2. 마운트 [ 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은 부팅 시 설정 파일이지만 실제 파일시스템 마운트는 /etc/mtab과 /proc/mounts 두 파일에 기록된다. 파일 시스템의 마운트를 변경할 때마다 이 두 파일도 동시에 업데이트됩니다.

열 1: 세 가지 작성 방법은 다음과 같습니다.

  • 파일 시스템 또는 디스크의 장치 파일 이름(예: /dev/vda2 등)
  • 파일 시스템의 UUID 이름(예: UUID=xxx)
  • 파일 시스템의 LABEL 이름(예: LABEL=xxx)

두 번째 열: 마운트 지점

  • 디렉토리여야 합니다.

세 번째 열: 디스크 파티션 슬롯의 파일 시스템

  • 수동으로 마운트하면 시스템이 자동으로 마운트를 테스트할 수 있지만 이 파일에서는 수동으로 파일 시스템에 기록해야 합니다! xfs , ext4 , vfat , reiserfs , nfs포함

네 번째 열: 파일 시스템 매개변수

열 5: dump backup 명령에서 사용할 수 있는지 여부

  • dump는 백업으로 사용되는 명령어인데 지금은 백업 솔루션이 너무 많아 이 항목은 무시해도 됩니다! 0을 직접 입력하세요.

열 6: fsck로 섹터를 확인할지 여부

  • 초기 부팅 프로세스에서 파일 시스템이 완전한지(깨끗한지) 확인하기 위해 시스템의 파일 시스템을 확인하는 시간이 있습니다. 하지만 이 방법은 주로 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

구성이 잘못된 경우 시작되지 않습니다.

이때 루트 암호를 직접 입력하여 1인 유지 관리 모드로 들어가십시오.

그런 다음 오류를 수정하거나 잘못된 위치를 삭제하거나 다시 마운트하고 저장하고 종료하고 다시 시작하십시오.

읽기 전용 상태가 표시되면 다음을 실행합니다.

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

오류가 발생하기 쉬운 것은 xfs , ext4 , vfat , reiserfs , nfs의 파일 시스템을 잘못 쓰는 것입니다.

추천

출처blog.csdn.net/qq_41210783/article/details/112360370