Linux 分区、挂载附加硬盘

一、背景

前几天介绍了华为公有云升级 Ubuntu 14.04 LTS 到 Ubuntu 16.04 LTS 的方法,今天以华为云 Ubuntu 系统为例介绍在 Linux 下如何挂载云硬盘。之所以单独开篇说这个,是因为云硬盘默认是不可使用的,即即使在控制面板上挂载了,但是在系统里仍然是无法识别这块硬盘的:

默认磁盘情况.jpg

二、挂载

在硬盘附加到实例后,通过命令查看附加情况:

# fdisk -l
Disk /dev/xvda: 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: dos
Disk identifier: 0x00054506

Device     Boot    Start      End  Sectors Size Id Type
/dev/xvda1 *        2048 75495423 75493376  36G 83 Linux
/dev/xvda2      75497470 83884031  8386562   4G  5 Extended
/dev/xvda5      75497472 83884031  8386560   4G 82 Linux swap / Solaris

Disk /dev/xvdb: 100 GiB, 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
Disklabel type: dos
Disk identifier: 0x9df421d4

可以看到当前的服务器有两块磁盘,“/dev/xvda” 是系统盘,“/dev/xvdb” 是数据盘。

拓展:分区格式

硬盘分区有诸多格式(Disklabel type),可以根据需要在分区时选择恰当的格式,具体有:.

  • bsd
  • dvh
  • gpt - this is a GPT partition table
  • loop - this is raw disk access without a partition table
  • mac
  • msdos - this is a standard MBR partition table
  • pc98 sun

设置命令:

# parted -s /dev/xvdb mklabel msdos

分区操作如下:

# fdisk /dev/xvdb       //选定硬盘
fdisk /dev/xvdb
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): n      //新建分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p        //设为主分区,e 为扩展分区
Partition number (1-4, default 1): 1          //第一个分区
First sector (2048-209715199, default 2048): 2048    //设置初始磁柱区域
Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199):   209715199    //设置结束磁柱区域

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

Command (m for help): p    //分区新建完成,验证一下
Disk /dev/xvdb: 100 GiB, 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
Disklabel type: dos
Disk identifier: 0x9df421d4

Device     Boot Start       End   Sectors  Size Id Type
/dev/xvdb1       2048 209715199 209713152  100G 83 Linux

Command (m for help): w      //确认无误,保存
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

分区建立完成,格式化并挂载:

# mkfs -t ext4 /dev/xvdb1
Creating filesystem with 26214144 4k blocks and 6553600 inodes
Filesystem UUID: 5927b99d-9de1-4494-a3d0-858e93567c84
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 /data   //硬盘挂载前必须先创建挂载点,如挂在 /data 下
                //如果要挂载在已存在的文件夹下,注意要把文件夹下的文件先挪走。

# mount /dev/xvdb1 /data
# e2fsck -f /dev/xvdb1   //检查下分区情况
e2fsck 1.42.13 (17-May-2015)
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/xvdb1: 11/6553600 files (0.0% non-contiguous), 459349/26213888 blocks

# resize2fs /dev/xvdb1   //调整大小
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/xvdb1 to 26214144 (4k) blocks.
The filesystem on /dev/xvdb1 is now 26214144 (4k) blocks long.

查看挂载情况:

# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            1.9G     0  1.9G   0% /dev
tmpfs           393M  5.5M  388M   2% /run
/dev/xvda1       36G  3.7G   30G  11% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
cgmfs           100K     0  100K   0% /run/cgmanager/fs
tmpfs           394M     0  394M   0% /run/user/0
/dev/xvdb1       99G   60M   94G   1% /data

可以看到新硬盘已挂载成功,但是工作还没完成,因为这样就结束了的话重启后会发现 /data 又不见了,因此我们需要设置开启自动挂载:

# vim /etc/mount.sh 
   #!/bin/bash
   mount /dev/xvdb1 /data
# chmod +x /etc/mount.sh
# echo '/etc/mount.sh' >> /etc/rc.d/rc.local

17/10/26 更新:
如果发现还是挂载不了,执行以下操作:

# chmod +x /etc/rc.d/rc.local   //CentOS
# chmod +x /etc/rc.local        //Ubuntu

自动挂载也可以通过修改分区表 /etc/fstab 的方式实现,但不熟悉的话建议尽量不要去动分区表。

fstab 挂载方法

# vim /etc/fstab
UUID=5927b99d-9de1-4494-a3d0-858e93567c84 /data  ext4 defaults 0 0

UUID 可通过 blkid 命令查询。

至此,附加云硬盘已成功挂载,也实现了开机自动挂载。

成功挂载.jpg


参考文章:
1、数据盘初始化(Linux)- 华为企业云


ArmxMod for Typecho
个性化、自适应、功能强大的响应式主题

推广

 继续浏览关于 linux阿里云ubuntu华为公有云腾讯云硬盘挂载 的文章

 本文最后更新于 2019/03/04 08:53:49,可能因经年累月而与现状有所差异

 引用转载请注明:VirCloud's Blog > 运维 > Linux 分区、挂载附加硬盘