ubuntu 硬盘挂载

参考:https://www.iteye.com/blog/vinnie424-2276444


第一行为输入命令,接下来是输入命令后的显示

1. 用 sudo fdisk -l查看卷标,可以看到下面要挂载的 300GB的 /dev/vdb

(base) ubuntu@VM-32-14-ubuntu:~$ sudo fdisk -l
Disk /dev/vda: 50 GiB, 53687091200 bytes, 104857600 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: 0x3fa1d255

Device     Boot Start       End   Sectors Size Id Type
/dev/vda1  *     2048 104857566 104855519  50G 83 Linux


Disk /dev/vdb: 300 GiB, 322122547200 bytes, 629145600 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

2. 使用fdisk工具对/dev/vdb进行分区

这里需要多次输入,显示 # 注释的地方需要再次输入


(base) ubuntu@VM-32-14-ubuntu:~$ sudo fdisk /dev/vdb

Welcome to fdisk (util-linux 2.31.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 0xea2bbc54.

Command (m for help): 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


Command (m for help): n  #输入“n”增加一个分区
Partition type  # --选择是建立主分区还是扩展分区
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): P #--输入“p”建立主分区
Partition number (1-4, default 1): 1 # --输入分区号
First sector (2048-629145599, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-629145599, default 629145599):

Created a new partition 1 of type 'Linux' and of size 300 GiB.

Command (m for help): w # --写入分区表并退出
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

3. 分区完毕,可以使用 fdisk -l查看是否分区成功

可以看到,出现了300GB的 /dev/vdb1

(base) ubuntu@VM-32-14-ubuntu:~$ sudo fdisk -l
Disk /dev/vda: 50 GiB, 53687091200 bytes, 104857600 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: 0x3fa1d255

Device     Boot Start       End   Sectors Size Id Type
/dev/vda1  *     2048 104857566 104855519  50G 83 Linux


Disk /dev/vdb: 300 GiB, 322122547200 bytes, 629145600 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: 0xea2bbc54

Device     Boot Start       End   Sectors  Size Id Type
/dev/vdb1        2048 629145599 629143552  300G 83 Linux

4. 如果是挂载新硬盘,需要进行格式化,一般使用ext4格式,格式化请谨慎,如果有数据会造成数据丢失


(base) ubuntu@VM-32-14-ubuntu:~$ sudo mkfs.ext4 /dev/vdb1
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 78642944 4k blocks and 19660800 inodes
Filesystem UUID: a9a0fb6c-a295-4d2c-afc7-ba16f8312444
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616

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

5. 将分区挂载到建立好的目录上

这里在主目录上新建了一个 new_disk 文件夹,再进行挂载即可


(base) ubuntu@VM-32-14-ubuntu:~$ sudo mount /dev/vdb1 new_disk/

6. 最后使用 df -h即可看到

(base) ubuntu@VM-32-14-ubuntu:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            1.9G     0  1.9G   0% /dev
tmpfs           379M  5.2M  374M   2% /run
/dev/vda1        50G   28G   20G  60% /
tmpfs           1.9G   24K  1.9G   1% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
tmpfs           379M     0  379M   0% /run/user/500
/dev/vdb1       295G   65M  280G   1% /home/ubuntu/new_disk

7. fstab配置挂载信息,使得重启仍然在挂载

到 /etc/fstab 下配置挂载信息,添加一条记录,如有就复制一条,修改一下即可(十分重要) 如下:

/dev/vdb1      /home/ubuntu/new_disk    auto    defaults,nofail,comment=cloudconfig     0       2

添加完毕以后可以试一下fstab文件是否能正常运行。

sudo mount -a 

没有输入或其他错误则表示成果,如果出现错误,不要重启,将fastab配置的内容先删掉

8. 解除挂载

sudo umount /dev/vdb1

你可能感兴趣的:(ubuntu 硬盘挂载)