Ubuntu 分区和创建文件系统
Ubuntu 分区和创建文件系统
目录
1、查看磁盘信息
df -h
查看分区容量和挂载情况
albert@ubuntu:~$ df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 93M 1.2M 91M 2% /run
/dev/sda3 20G 3.3G 16G 18% /
tmpfs 461M 0 461M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 93M 4.0K 93M 1% /run/user/1000
lsblk
lsblk 命令列出块设备的基本信息,查看现有的磁盘以及分区情况
lsblk -f 更详细,可以查看到文件系统类型,和文件系统的uuid和挂载点
下图中sda已经分区并挂载,sdb和sdc是刚添加的硬盘
albert@ubuntu:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 40.4M 1 loop /snap/snapd/20671
loop1 7:1 0 63.9M 1 loop /snap/core20/2105
loop2 7:2 0 87M 1 loop /snap/lxd/27037
loop3 7:3 0 44.4M 1 loop /snap/snapd/23545
loop4 7:4 0 63.7M 1 loop /snap/core20/2496
loop5 7:5 0 89.4M 1 loop /snap/lxd/31333
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 100M 0 part [SWAP]
└─sda3 8:3 0 19.9G 0 part /
sdb 8:16 0 20G 0 disk
sdc 8:32 0 20G 0 disk
albert@ubuntu:~$ lsblk -f
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS
loop0 squashfs 4.0 0 100% /snap/snapd/20671
loop1 squashfs 4.0 0 100% /snap/core20/2105
loop2 squashfs 4.0 0 100% /snap/lxd/27037
loop3 squashfs 4.0 0 100% /snap/snapd/23545
loop4 0 100% /snap/core20/2496
loop5 0 100% /snap/lxd/31333
sda
├─sda1
├─sda2 swap 1 62faad92-4e8f-4409-8dcb-c773d94f45c4 [SWAP]
└─sda3 ext4 1.0 2c248df6-446b-49f2-9db0-86d1a563be77 15.2G 17% /
sdb
sdc
blkid
一般用来文件系统类型和uuid。
albert@ubuntu:~$ blkid
/dev/sda2: UUID="62faad92-4e8f-4409-8dcb-c773d94f45c4" TYPE="swap" PARTUUID="cf06405b-c11e-4042-83a1-5dbff6eed39a"
/dev/sda3: UUID="2c248df6-446b-49f2-9db0-86d1a563be77" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="b2bd48c0-c831-415e-bc6e-10f87bd167b7"
fdisk -l
sudo fdisk -l 查看分区的基本信息,比如分区格式是mbr(linux中表述为dos或msdos)还是gpt
albert@ubuntu:~$ sudo fdisk -l
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 2DAE60A0-90A4-47AC-BCF8-81ADB4A4B407
Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 208895 204800 100M Linux swap
/dev/sda3 208896 41940991 41732096 19.9G Linux filesystem
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
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/sdc: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
parted -l
和fdisk -l 类似,个人推荐用parted -l
albert@ubuntu:~$ sudo parted -l
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 2097kB 1049kB bios_grub
2 2097kB 107MB 105MB linux-swap(v1) swap
3 107MB 21.5GB 21.4GB ext4
Error: /dev/sdb: unrecognised disk label
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
Error: /dev/sdc: unrecognised disk label
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
2、磁盘分区
使用fdisk分区工具
用fdisk 给 sdb分两个区,分区表为mbr格式:
# 指定要分区的磁盘
albert@ubuntu:~$ sudo fdisk /dev/sdb
# 默认创建DOS分区表(即mbr)
Welcome to fdisk (util-linux 2.37.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.
Created a new DOS disklabel with disk identifier 0x6e7f28f8.
Command (m for help):
# 输入p查看当前的分区情况,当前磁盘没有分区
Command (m for help): p
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6e7f28f8
# 输入n创建新分区。
Command (m for help): n
Partition type # 选择主分区还是扩展分区
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p # 这里选择主分区
Partition number (1-4, default 1): 1 # 选择创建第几个分区。该数字决定sdb*。一般从1开始或者默认。
First sector (2048-41943039, default 2048): # 默认即可
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039): +10G # 设置分区大小,默认分配全部。
Created a new partition 1 of type 'Linux' and of size 10 GiB.
# 创建第二个分区,和上面同理
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2):
First sector (20973568-41943039, default 20973568):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (20973568-41943039, default 41943039):
Created a new partition 2 of type 'Linux' and of size 10 GiB.
# 输入p查看当前分区情况(此时还没有保存生效)
Command (m for help): p
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6e7f28f8
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 20973567 20971520 10G 83 Linux
/dev/sdb2 20973568 41943039 20969472 10G 83 Linux
# 输入w 保存并退出
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
# 格式化为而ext4
sudo mkfs.ext4 /dev/sdb1
sudo mkfs.ext4 /dev/sdb2
# 挂载(临时)
albert@ubuntu:~$ mkdir sdb1
albert@ubuntu:~$ mkdir sdb2
albert@ubuntu:~$ sudo mount /dev/sdb1 sdb1
albert@ubuntu:~$ sudo mount /dev/sdb2 sdb2
使用parted分区工具,推荐
使用parted给 sdc分两个区,分区表为gpt格式:
注意,parted操作是实时生效的
# q或quit是退出
# 指定要分区的磁盘,这里用交互模式
albert@ubuntu:~$ sudo parted /dev/sdc
GNU Parted 3.4
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
# 创建分区表,一般有gpt和msdos(即mbr)
(parted) mklabel
New disk label type? gpt
Warning: The existing disk label on /dev/sdc will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? yes
# 输入print或p验证
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt # 已创建gpt分区表
# 创建2个分区并划分大小
(parted) mkpart # 创建第一个分区
Partition name? []? part1 # 给第一个分区取个名字。名字好像没什么用
File system type? [ext2]? ext4 # 这里指定文件系统其实没有意义,是不生效的。建议后续用mkfs命令设置文件系统
Start? 0% # 指定分区起始位置,可以用具体大小,或者百分比表示
End? 8G # 指定分区结束位置
(parted) mkpart # 创建第二个分区
Partition name? []? part2 # 给第一个分区取个名字。
File system type? [ext2]? ext4
Start? 8G
End? 100%
# 输入print验证
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 8000MB 7999MB part1
2 8000MB 21.5GB 13.5GB part2
# 删除分区
(parted) rm
Partition number? 1 # 指定删除的分区序号
非交互式parted分区
# 查看/dev/sdc分区情况
albert@ubuntu:~$ sudo parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
2 8000MB 21.5GB 13.5GB part2
# 创建gpt分区表
sudo parted /dev/sdc mklabel gpt
3 创建分区
parted /dev/sdc mkpart part1 0% 10GB
一定要注意,虽然parted工具中指定了文件系统,但是并没有意义,它仍需要手动进行格式化并指定分区类型。实际上,在parted中文件系统是可以不用指定的,即使是非交互模式下也可以省略。

浙公网安备 33010602011771号