ubuntu磁盘分区与挂载
1 查看分区
# 查看硬盘分区
# sudo fdisk -l
# 其中/dev/sda表示第一块硬盘,/dev/sda1表示第一个分区;/dev/sdb表示第二块硬盘;/dev/mapper/ubuntu--vg-ubuntu--lv表示逻辑卷
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 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
Disklabel type: gpt
Disk identifier: 3DA1C701-896C-4A1B-AD24-B37772D433C6
Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 2101247 2097152 1G Linux filesystem
/dev/sda3 2101248 83884031 81782784 39G Linux filesystem
Disk /dev/sdb: 4 GiB, 4294967296 bytes, 8388608 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/ubuntu--vg-ubuntu--lv: 20 GiB, 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
# 查看磁盘数量和磁盘分区
sudo lsblk
# 逻辑卷挂载在/目录下
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 39G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 20G 0 lvm /
sdb 8:16 0 4G 0 disk
sr0 11:0 1 1024M 0 rom
2 查看空闲磁盘
2.1 使用fdisk
# 进入硬盘/dev/sdb,注意,进入后不要随便点击,操作磁盘要慎重!
# sudo fdisk /dev/sdb
# 进入以后,m表示帮助,F表示查看空闲的磁盘,q表示退出,d表示删除分区等
# Command (m for help): F
# 空闲的有4G空间
Unpartitioned space /dev/sdb: 4 GiB, 4293918720 bytes, 8386560 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
Start End Sectors Size
2048 8388607 8386560 4G
2.2 使用parted
# 进入parted,进入后输入help或者list查看使用帮助,进入后慎重操作!
# sudo parted /dev/sda
# 输入print free查看空闲的空间,quit表示退出,
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
17.4kB 1049kB 1031kB Free Space
1 1049kB 2097kB 1049kB bios_grub
2 2097kB 1076MB 1074MB ext4
3 1076MB 42.9GB 41.9GB
42.9GB 42.9GB 1032kB Free Space
3 硬盘分区
在硬盘/dev/sdb上添加1个2G的扩展分区和1个500M的逻辑分区。
分区包括主分区、扩展分区和逻辑分区,主分区和扩展最多只能有4个,扩展分区最多只能有1个,逻辑分区是从扩展分区中切割出来的。
主分区和逻辑分区用来存储数据,可以格式化;扩展分区用来切分逻辑分区,不能格式化;
3.1 对硬盘格式化
# 对硬盘/dev/sdb进行格式化
# !!!注意,如果/dev/sdb硬盘上有数据,确保上面的数据是无用数据再格式化!
# !!!慎重!慎重!慎重!
# 建议尽量不要操作此步骤,我的/dev/sdb没有数据所以才格式化
# 将硬盘/dev/sdb格式化为ext4文件系统
# sudo mkfs -t ext4 /dev/sdb
mke2fs 1.44.1 (24-Mar-2018)
/dev/sdb contains a ext4 file system
created on Thu Jul 8 01:10:11 2021
Proceed anyway? (y,N) y
Creating filesystem with 1048576 4k blocks and 262144 inodes
Filesystem UUID: 5af8fc78-0710-4083-a29d-d33c5983b45e
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
3.2 创建扩展分区
# (1)进入硬盘
# sudo fdisk /dev/sdb
# (2)新建分区
# 注意:因为/dev/sdb是一块新硬盘,因此只有主分区p和扩展分区e(扩展分区不能格式化)
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
# (3)选择扩展分区
Select (default p): e
# (4)选择分区1
Partition number (1-4, default 1): 1
# (5)设置分区开始位置,默认即可,点击 enter
First sector (2048-8388607, default 2048):
# (6)设置分区的空间 “+2048M”
Last sector, +sectors or +size{K,M,G,T,P} (2048-8388607, default 8388607): +2048M
Created a new partition 1 of type 'Extended' and of size 2 GiB.
# (7)查看分区是否添加成功
Command (m for help): p
Disk /dev/sdb: 4 GiB, 4294967296 bytes, 8388608 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
Disklabel type: dos
Disk identifier: 0xd03a1ebc
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 5 Extended
# (8)保存分区,一定要保存;如果按q退出,分区将不能保存
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
3.3 创建逻辑分区
# !注意扩展分区无法格式化
sudo mkfs -t ext4 /dev/sdb1
mke2fs 1.44.1 (24-Mar-2018)
Found a dos partition table in /dev/sdb1
Proceed anyway? (y,N) y
mkfs.ext4: inode_size (128) * inodes_count (0) too big for a
filesystem with 0 blocks, specify higher inode_ratio (-i)
or lower inode count (-N).
# (1)进入硬盘,注意不是进入/dev/sdb1
# sudo fdisk /dev/sdb
# (2)新建分区
# 注意:因为/dev/sdb已经创建过扩展分区,因此此处有主分区p和逻辑分区1
Command (m for help): n
Partition type
p primary (0 primary, 1 extended, 3 free)
l logical (numbered from 5)
# (3)选择逻辑分区,此处系统自动设置分区编号
Select (default p): l
Adding logical partition 5
# (4)设置分区开始位置,默认即可,点击 enter
First sector (4096-4196351, default 4096):
# (5)设置分区的空间 “+500M”
Last sector, +sectors or +size{K,M,G,T,P} (4096-4196351, default 4196351): +500M
Created a new partition 5 of type 'Linux' and of size 500 MiB.
# (6)保存分区
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
# (7)查看分区信息,注意旧的系统可能需要重启系统分区才起作用
# sudo lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 39G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 20G 0 lvm /
sdb 8:16 0 4G 0 disk
├─sdb1 8:17 0 1K 0 part
└─sdb5 8:21 0 500M 0 part
sr0 11:0 1 1024M 0 rom
4 磁盘挂载
# 对新分区/dev/sdb5进行格式化,注意,不格式化不能对分区进行挂载
# sudo mkfs -t ext4 /dev/sdb5
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 512000 1k blocks and 128016 inodes
Filesystem UUID: 374ae7d4-f898-4a33-8650-90635dd577d4
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
# 对分区进行挂载,将/dev/sdb5挂载在/home/mason/目录下
# 此方法只能临时挂载,系统重启后就失效
# sudo mount /dev/sdb5 /home/mason/
# 设置开机自动挂载,编辑/etc/fstab文件,添加如下内容
# sudo vim /etc/fstab
/dev/sdb5 /home/mason ext4 defaults 0 0
# 查看磁盘使用情况和挂载目录
# sudo df -h
Filesystem Size Used Avail Use% Mounted on
udev 1.9G 0 1.9G 0% /dev
tmpfs 395M 700K 394M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 20G 5.8G 13G 31% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda2 976M 78M 832M 9% /boot
tmpfs 395M 0 395M 0% /run/user/1000
/dev/sdb5 477M 2.3M 445M 1% /home/mason
5 更改文件系统大小
# (1)卸载挂载的磁盘
sudo umount /home/mason
# (2)检测分区
sudo e2fsck -f /dev/sdb5
e2fsck 1.44.1 (24-Mar-2018)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb5: 12/128016 files (0.0% non-contiguous), 26659/512000 blocks
# (3)调整文件系统大小,将500M调整为400M
# 注意他不能调整分区的大小,我的分区是500M,resize2fs不能将大小调整大于500M
sudo resize2fs /dev/sdb5 400M
Resizing the filesystem on /dev/sdb5 to 409600 (1k) blocks.
The filesystem on /dev/sdb5 is now 409600 (1k) blocks long.
版权声明:本文为make_progress原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。