c4rt1y

磁盘操作

0x01.介绍

本文将简单讲解下,硬盘的操作,如分区、删除分区、格式化、挂载、卸载。

0x02.实践

## 查看当前节点
df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        69G   44G   23G  67% /
devtmpfs        1.9G     0  1.9G   0% /dev
tmpfs           1.9G   32K  1.9G   1% /dev/shm
tmpfs           1.9G  340K  1.9G   1% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
tmpfs           380M     0  380M   0% /run/user/1000

## 查看磁盘情况,存在一个未挂载的磁盘
fdisk -l

Disk /dev/vda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x0008d207

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048   125827071    62912512   83  Linux

Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 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 /dev/vdb
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 0x7cf373af.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set

Command (m for help): wq
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

## 查看磁盘
fdisk -l

Disk /dev/vda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x0008d207

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048   125827071    62912512   83  Linux

Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x7cf373af

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048   209715199   104856576   83  Linux

## 以ext4格式格式化磁盘
mkfs.ext4 /dev/vdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26214144 blocks
1310707 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2174746624
800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
	4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

## 创建目录
mkdir /datadisk

## 挂载磁盘
mount /dev/vdb1 /datadisk

## 查看当前服务器磁盘情况
df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        59G   21G   36G  37% /
devtmpfs         16G     0   16G   0% /dev
tmpfs            16G   32K   16G   1% /dev/shm
tmpfs            16G  368K   16G   1% /run
tmpfs            16G     0   16G   0% /sys/fs/cgroup
tmpfs           3.1G     0  3.1G   0% /run/user/1000
/dev/vdb1        99G   61M   94G   1% /datadisk

## 这里要注意,目前的操作只是一次性的,需要在/etc/fstab文件负责配置Linux开机时自动挂载的分区
第一列可以是实际分区名,也可以是实际分区的卷标(Lable)
第二列是挂载点,挂载点必须为当前已经存在的目录
第三列为此分区的文件系统类型
第四列是挂载的选项,用于设置挂载的参数
auto: 系统自动挂载,fstab默认就是这个选项
defaults: rw, suid, dev, exec, auto, nouser, and async.
noauto 开机不自动挂载
nouser 只有超级用户可以挂载
ro 按只读权限挂载
rw 按可读可写权限挂载
user 任何用户都可以挂载
请注意光驱和软驱只有在装有介质时才可以进行挂载,因此它是noauto
第五列是dump备份设置,当其值设置为1时,将允许dump备份程序备份;设置为0时,忽略备份操作;
第六列是fsck磁盘检查设置,其值是一个顺序。当其值为0时,永远不检查;而 / 根目录分区永远都为1。其它分区从2开始,数字越小越先检查,如果两个分区的数字相同,则同时检查。

echo "/dev/vdb1            /datadisk1           ext4       defaults              0 0" >> /etc/fstab

## 卸载操作
unmount /dev/vdb1

## 查看当前服务器磁盘情况
df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        59G   21G   36G  37% /
devtmpfs         16G     0   16G   0% /dev
tmpfs            16G   32K   16G   1% /dev/shm
tmpfs            16G  368K   16G   1% /run
tmpfs            16G     0   16G   0% /sys/fs/cgroup
tmpfs           3.1G     0  3.1G   0% /run/user/1000

## 上述为常用操作,也可以对一块磁盘进行多次分区
fdisk /dev/vdb
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): d
Selected partition 1
Partition 1 is deleted

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): 19715199
Partition 1 of type Linux and of size 9.4 GiB is set

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2):
First sector (19715200-209715199, default 19716096):
Using default value 19716096
Last sector, +sectors or +size{K,M,G} (19716096-209715199, default 209715199): 59715199
Partition 2 of type Linux and of size 19.1 GiB is set

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3):
First sector (19715200-209715199, default 59715584): 109715199
Last sector, +sectors or +size{K,M,G} (109715199-209715199, default 209715199):
Using default value 209715199
Partition 3 of type Linux and of size 47.7 GiB is set

Command (m for help): n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): p
Selected partition 4
First sector (19715200-209715199, default 59715584):
Using default value 59715584
Last sector, +sectors or +size{K,M,G} (59715584-109715198, default 109715198):
Using default value 109715198
Partition 4 of type Linux and of size 23.9 GiB is set

Command (m for help): wq
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

## 查看磁盘情况
fdisk -l

Disk /dev/vda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x0008d207

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048   125827071    62912512   83  Linux

Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x7cf373af

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    19715199     9856576   83  Linux
/dev/vdb2        19716096    59715199    19999552   83  Linux
/dev/vdb3       109715199   209715199    50000000+  83  Linux
/dev/vdb4        59715584   109715198    24999807+  83  Linux

Partition table entries are not in disk order

## 对/dev/vdb的分区进行删除
fdisk /dev/vdb
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): d
Partition number (1-4, default 4): 4
Partition 4 is deleted

Command (m for help): d
Partition number (1-3, default 3): 2
Partition 2 is deleted

Command (m for help): wq
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

## 查看磁盘情况
fdisk -l

Disk /dev/vda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x0008d207

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048   125827071    62912512   83  Linux

Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x7cf373af

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    19715199     9856576   83  Linux
/dev/vdb3       109715199   209715199    50000000+  83  Linux

0x03.资料来源

https://www.jqhtml.com/56844.html
GoTop