linux磁盘分区和挂载
linux磁盘分区和挂载
1.lsblk -f 查看系统分区和挂载情况
root@qsrobot:~# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sr0 iso9660 Ubuntu 16.04.6 LTS amd64
2019-02-27-09-57-36-00 /media/rob
sda
├─sda2
├─sda5 swap 8d41e7be-46cf-4de6-afab-fb5242d10d4f [SWAP]
└─sda1 ext4 95a7ce2a-6891-44f6-b0db-729319f8de8d /
2.通过虚拟机添加硬盘
root@qsrobot:~# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sdb
sr0 iso9660 Ubuntu 16.04.6 LTS amd64
2019-02-27-09-57-36-00 /media/rob
sda
├─sda2
├─sda5 swap 8d41e7be-46cf-4de6-afab-fb5242d10d4f [SWAP]
└─sda1 ext4 95a7ce2a-6891-44f6-b0db-729319f8de8d /
3.新增分区
fdisk /dev/sdb开始对/sdb分区
• m 显示命令列表
• p 显示磁盘分区 同 fdisk –l
• n 新增分区
• d 删除分区
• w 写入并退出
fdisk /dev/sdb
Welcome to fdisk (util-linux 2.27.1).
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 0x4dbeae7b.
命令(输入 m 获取帮助): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
命令(输入 m 获取帮助): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
分区号 (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039):
Created a new partition 1 of type 'Linux' and of size 20 GiB.
命令(输入 m 获取帮助): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
root@qsrobot:~# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sdb
└─sdb1
sr0 iso9660 Ubuntu 16.04.6 LTS amd64
2019-02-27-09-57-36-00 /media/rob
sda
├─sda2
├─sda5 swap 8d41e7be-46cf-4de6-afab-fb5242d10d4f [SWAP]
└─sda1 ext4 95a7ce2a-6891-44f6-b0db-729319f8de8d /
说明: 开始分区后输入n新增分区,然后选择p设置分区类型为主分区。1指的是主分区的第1块分区。两次回车默认剩余空间。最后输入w写入分区并退出,若不保存退出输入q。
lsblk -f,就会看到新增的分区sdb1
4.格式化磁盘
mkfs -t ext4 /dev/sdb1格式化分区sdb1,分区类型是ext4
lsblk -f,就会看到格式化后的分区sdb1
root@qsrobot:~# mkfs -t ext4 /dev/sdb1
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 5242624 4k blocks and 1310720 inodes
Filesystem UUID: 8b5b2376-41d1-4f54-a4bc-237699d5246f
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
root@qsrobot:~# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sdb
└─sdb1 ext4 8b5b2376-41d1-4f54-a4bc-237699d5246f
sr0 iso9660 Ubuntu 16.04.6 LTS amd64
2019-02-27-09-57-36-00 /media/rob
sda
├─sda2
├─sda5 swap 8d41e7be-46cf-4de6-afab-fb5242d10d4f [SWAP]
└─sda1 ext4 95a7ce2a-6891-44f6-b0db-729319f8de8d /
挂载
挂载:将一个分区与一个目录联系起来
mount 设备名 挂载目录
例如:mount /dev/sdb1 /home/newdisk
附:卸载:umount 设备名称 或者 挂载目录
例如:umount /dev/sdb1 或者 umount /home/newdisk
设置自动挂载
通过修改/etc/fstab实现自动挂载
添加完成后,执行mount -a 即可生效
在文件中添加:
/dev/sdb1 /home /newdisk ext4 defaults 00将分区sdb1和目录 /home /newdisk挂载起来
特殊说明
/dev/sdb1:分区名称
ext3:分区的格式
defaults:挂载时所要设定的参数(只读,读写,启用quota等)
1:使用dump是否要记录,0是不要
2:开机时检查的顺序,boot系统文件为1,其他文件系统为2,如不要检查为0
mount -a 使挂载立即生效
umount /dev/sdb1或umount /home /newdisk卸载挂载
建立第一个主分区
fdisk /dev/sdb
Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
命令(输入 m 获取帮助): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
分区号 (1-4, default 1): 1
First sector (2048-41943039, default 2048): +4G
Value out of range.
First sector (2048-41943039, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +4G
Created a new partition 1 of type 'Linux' and of size 4 GiB.
命令(输入 m 获取帮助): p
Disk /dev/sdb: 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
Disklabel type: dos
Disk identifier: 0x4dbeae7b
设备 启动 Start 末尾 扇区 Size Id 类型
/dev/sdb1 2048 8390655 8388608 4G 83 Linux
建立第二个主分区
命令(输入 m 获取帮助): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
分区号 (2-4, default 2):
First sector (8390656-41943039, default 8390656):
Last sector, +sectors or +size{K,M,G,T,P} (8390656-41943039, default 41943039): +4G
Created a new partition 2 of type 'Linux' and of size 4 GiB.
命令(输入 m 获取帮助): p
Disk /dev/sdb: 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
Disklabel type: dos
Disk identifier: 0x4dbeae7b
设备 启动 Start 末尾 扇区 Size Id 类型
/dev/sdb1 2048 8390655 8388608 4G 83 Linux
/dev/sdb2 8390656 16779263 8388608 4G 83 Linux
建立第三个主分区
命令(输入 m 获取帮助): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p): p
分区号 (3,4, default 3):
First sector (16779264-41943039, default 16779264):
Last sector, +sectors or +size{K,M,G,T,P} (16779264-41943039, default 41943039): +4G
Created a new partition 3 of type 'Linux' and of size 4 GiB.
命令(输入 m 获取帮助): p
Disk /dev/sdb: 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
Disklabel type: dos
Disk identifier: 0x4dbeae7b
设备 启动 Start 末尾 扇区 Size Id 类型
/dev/sdb1 2048 8390655 8388608 4G 83 Linux
/dev/sdb2 8390656 16779263 8388608 4G 83 Linux
/dev/sdb3 16779264 25167871 8388608 4G 83 Linux
建立第一个扩展区
命令(输入 m 获取帮助): n
Partition type
p primary (3 primary, 0 extended, 1 free)
e extended (container for logical partitions)
Select (default e): e
Selected partition 4
First sector (25167872-41943039, default 25167872):
Last sector, +sectors or +size{K,M,G,T,P} (25167872-41943039, default 41943039): +6G
Created a new partition 4 of type 'Extended' and of size 6 GiB.
建立两个逻辑分区
命令(输入 m 获取帮助): n
All primary partitions are in use.
Adding logical partition 5
First sector (25169920-37750783, default 25169920):
Last sector, +sectors or +size{K,M,G,T,P} (25169920-37750783, default 37750783): +1G
Created a new partition 5 of type 'Linux' and of size 1 GiB.
命令(输入 m 获取帮助): p
Disk /dev/sdb: 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
Disklabel type: dos
Disk identifier: 0x4dbeae7b
设备 启动 Start 末尾 扇区 Size Id 类型
/dev/sdb1 2048 8390655 8388608 4G 83 Linux
/dev/sdb2 8390656 16779263 8388608 4G 83 Linux
/dev/sdb3 16779264 25167871 8388608 4G 83 Linux
/dev/sdb4 25167872 37750783 12582912 6G 5 扩展
/dev/sdb5 25169920 27267071 2097152 1G 83 Linux
命令(输入 m 获取帮助): n
All primary partitions are in use.
Adding logical partition 6
First sector (27269120-37750783, default 27269120):
Last sector, +sectors or +size{K,M,G,T,P} (27269120-37750783, default 37750783): +1G
Created a new partition 6 of type 'Linux' and of size 1 GiB.
命令(输入 m 获取帮助): p
Disk /dev/sdb: 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
Disklabel type: dos
Disk identifier: 0x4dbeae7b
设备 启动 Start 末尾 扇区 Size Id 类型
/dev/sdb1 2048 8390655 8388608 4G 83 Linux
/dev/sdb2 8390656 16779263 8388608 4G 83 Linux
/dev/sdb3 16779264 25167871 8388608 4G 83 Linux
/dev/sdb4 25167872 37750783 12582912 6G 5 扩展
/dev/sdb5 25169920 27267071 2097152 1G 83 Linux
/dev/sdb6 27269120 29366271 2097152 1G 83 Linux
删除分区
命令(输入 m 获取帮助): d
分区号 (1-6, default 6):
Partition 6 has been deleted.
命令(输入 m 获取帮助): p
Disk /dev/sdb: 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
Disklabel type: dos
Disk identifier: 0x4dbeae7b
设备 启动 Start 末尾 扇区 Size Id 类型
/dev/sdb1 2048 8390655 8388608 4G 83 Linux
/dev/sdb2 8390656 16779263 8388608 4G 83 Linux
/dev/sdb3 16779264 25167871 8388608 4G 83 Linux
/dev/sdb4 25167872 37750783 12582912 6G 5 扩展
/dev/sdb5 25169920 27267071 2097152 1G 83 Linux