Linux拓展硬盘容量

环境采用VMware虚拟环境,操作系统:Ubuntu Server 18.04

新建硬盘

首先在设置中新增一块硬盘,大小容量为2G,硬盘格式为SCSI。

然后重启系统

查看系统中的硬盘数量

Linux 硬盘识别: 2.6 kernel以后,linux会将识别到的硬件设备,在/dev/下建立相应的设备文件. 如: sda 表示第1块SCSI硬盘,第二块是sdb,以此类推 hda 表示第1块IDE硬盘(即连接在第1个IDE接口的Master口上) scd0 表示第1个USB光驱. 当添加了新硬盘后,在/dev目录下会有相应的设备文件产生.cciss的硬盘是个例外,它的 设备文件在/dev/cciss/目录下.

因为我们添加的是scsi类型的硬盘,因此使用如下命令:
ll -h /dev/sd*
输出:

1
2
3
4
brw-rw---- 1 root disk 8,  0 Jun 26 06:56 /dev/sda
brw-rw---- 1 root disk 8, 1 Jun 26 06:56 /dev/sda1
brw-rw---- 1 root disk 8, 2 Jun 26 06:56 /dev/sda2
brw-rw---- 1 root disk 8, 16 Jun 26 06:56 /dev/sdb

从上面我们可以看到sdb这块硬盘没有分区

建立分区

因为我们希望使用的分区格式是GPT,因此这里使用gdisk工具(如果使用MBR格式,请使用fdisk)。

1
gdisk /dev/sdb

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
root@venus:/home/kevin# gdisk /dev/sdb
GPT fdisk (gdisk) version 1.0.3

Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present

Creating new GPT entries.

Command (? for help): n #使用n命令新建一个分区
Partition number (1-128, default 1):
First sector (34-4194270, default = 2048) or {+-}size{KMGTP}: #这里使用默认值
Last sector (2048-4194270, default = 4194270) or {+-}size{KMGTP}: 1G #想要分区的大小,默认使用全部硬盘容量
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): p #打印当前分区信息
Disk /dev/sdb: 4194304 sectors, 2.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 23400633-3468-4F2F-81D9-5A7B517A73DE
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 4194270
Partitions will be aligned on 2048-sector boundaries
Total free space is 2099132 sectors (1025.0 MiB)

Number Start (sector) End (sector) Size Code Name
1 2048 2097152 1023.0 MiB 8300 Linux filesystem

Command (? for help): w #保存修改

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.

分区完成

使用命令查看分区:

1
cat /proc/partitions

或者

1
lsblk

输出:

1
2
3
4
5
6
7
8
9
10
11
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 1.4M 0 disk
loop0 7:0 0 86.6M 1 loop /snap/core/4486
loop1 7:1 0 86.9M 1 loop /snap/core/4830
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1M 0 part
└─sda2 8:2 0 20G 0 part /
sdb 8:16 0 2G 0 disk
└─sdb1 8:17 0 1023M 0 part
sr0 11:0 1 62M 0 rom
sr1 11:1 1 806M 0 rom

可以看到我们的sdb1分区已经建立了。

格式化

  1. 格式sdb1为ext4格式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    mkfs.ext4 -L test /dev/sdb1
    mke2fs 1.44.1 (24-Mar-2018)
    Creating filesystem with 261888 4k blocks and 65536 inodes
    Filesystem UUID: d88b542f-cf9e-4cce-9c75-0647f4765892
    Superblock backups stored on blocks:
    32768, 98304, 163840, 229376

    Allocating group tables: done
    Writing inode tables: done
    Creating journal (4096 blocks): done
    Writing superblocks and filesystem accounting information: done
  2. 格式sdb2为xfs格式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    root@venus:/home/kevin# mkfs.xfs -L test-xfs /dev/sdb2
    meta-data=/dev/sdb2 isize=512 agcount=4, agsize=65471 blks
    = sectsz=512 attr=2, projid32bit=1
    = crc=1 finobt=1, sparse=0, rmapbt=0, reflink=0
    data = bsize=4096 blocks=261883, 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

查看一下分区信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
root@venus:/home/kevin# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
fd0
loop0 squashfs /snap/core/4486
loop1 squashfs /snap/core/4830
sda
├─sda1
└─sda2 ext4 d9fd6b0c-7821-11e8-a1d7-000c29773964 /
sdb
├─sdb1 ext4 test d88b542f-cf9e-4cce-9c75-0647f4765892
└─sdb2 xfs test-xfs 078a6aab-84ff-42f5-96e6-bf957943e83c
sr0 iso9660 CDROM 2018-06-25-10-40-19-00
sr1 iso9660 Ubuntu-Server 18.04 LTS amd64 2018-04-26-19-48-54-00

你的支持我的动力