×
Community Blog Creating and Attaching a Block Storage Disk onto an ECS

Creating and Attaching a Block Storage Disk onto an ECS

In this article, we will learn how to create and attach a block storage disk onto an Alibaba Cloud ECS instance.

By Anish Nath, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud's incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

In Alibaba Cloud Elastic Compute Service (ECS) Linux servers, whenever you need to add additional storage capacity in an ECS server, adding disk is a possible and convenient solution. This tutorial is all about

  1. Creating a disk in Alibaba Cloud ECS instance.
  2. mounting this disk to a running ECS instance
  3. partitioning the cloud disk using fdisk.
  4. Unmounting the disk
  5. Remounting the Disk without performing partition.

Alibaba Cloud allows you to create a virtual block storage device (disks) that you can attach in to your virtual machines (ECS Instance).

Before You Begin

Make sure you have a valid Alibaba Cloud account to try out this experiment. You'll also need an Alibaba Cloud ECS instance to follow the tutorial. The example shown in this document will only work on UNIX-based (Linux & OS X) machines which is running on Alibaba Cloud ECS instances.

Creating a Volume

Now let's get started on creating the first Volume.

In the Alibaba cloud ECS console dashboard, click the Disks menu under Block storage.

1

Select the following (this may vary based on user need)

  1. Regions and zones vary based on the ECS regions,
  2. the Allocated disk sizes

2

Carefully review the setting of the new disk and finally hit the create button

3

Once the disk is Activated, it's status will go to unmounted state

4

The first step is to mount or attach this disk to the running ECS instance

5

Once the disk is attached to the ECS instance, the disk status will be shown as In-Use.

6

You will see now in the "Mounting Point" column that the disk is attached into the ECS instance, in a device called "/dev/xvdb"

7

Now let's login to ECS Linux Server instance as root.

To verify if the volume was attached properly, execute: fdisk -l

You should see some message like this:

root@webserver-01:~# fdisk -l 

Disk /dev/vda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 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
Disk identifier: 0x00040f1a

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    41940991    20969472   83  Linux

Disk /dev/vdb: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders, total 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
Disk identifier: 0x00000000

Disk /dev/vdb doesn't contain a valid partition table

If you didn't see the /dev/vdb getting detected, try rebooting the instance and check it again.

To format your device, execute

root@webserver-01:~# fdisk /dev/vdb

It will output something like this:

root@webserver-01:~# fdisk /dev/vdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x8e0e6a42.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

  1. Press n to create a new disk partition.
  2. Press p to create a primary disk partition.
  3. Press 1 to denote it as 1st disk partition.
  4. Press ENTER twice to accept the default of 1st and last cylinder – to convert the remainder of hard disk to a single disk partition

It will output something like this:

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

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): 
Using default value 1
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): 
Using default value 41943039

Now, the next thing to do is enter these commands:

  1. Press t, then select the new partition you made.
  2. Press 83 change your new partition to 83, i.e. Linux partition type.
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list codes): 83

Now, the next thing to do is to enter these commands:

  1. Press p, to display the partition
  2. Press w to write the partition table.
Command (m for help): p

Disk /dev/vdb: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders, total 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
Disk identifier: 0x8e0e6a42

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    41943039    20970496   83  Linux

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

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

Finally, reboot the server

root@webserver-01:~# reboot

Check if the partition table is available: The new disk /dev/vdb is available for mounting

root@webserver-01:~# fdisk -l 

Disk /dev/vda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 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
Disk identifier: 0x00040f1a

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    41940991    20969472   83  Linux

Disk /dev/vdb: 21.5 GB, 21474836480 bytes
3 heads, 34 sectors/track, 411206 cylinders, total 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
Disk identifier: 0x8e0e6a42

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    41943039    20970496   83  Linux

Now create the filesystem in the /dev/vdb partition by mkfs.ext4

root@webserver-01:~# mkfs.ext4 /dev/vdb1
mke2fs 1.42.9 (4-Feb-2014)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242624 blocks
262131 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
160 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

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

Warning This step assumes that you're mounting an empty disk volume. If you're mounting a volume that already has data on it, don't use mkfs before mounting the volume Otherwise, you'll format the volume and delete the existing data, refer to the section below.

Check the UUID of the volume by executing:

root@webserver-01:~# blkid
/dev/vda1: LABEL="/" UUID="165ce746-c938-4265-b23f-09ef38d270ac" TYPE="ext4" 
/dev/vdb1: UUID="b70ec0bd-042f-4204-948d-59061c006e21" TYPE="ext4"

The line with /dev/vdb1 is what we are interested in the b70ec0bd-042f-4204-948d-59061c006e21 is the UUID

Create the Data folder

root@webserver-01:~# mkdir -p /data

Mount the volume to data directory using the volume UUID and the directory you just created

root@webserver-01:~# mount /dev/disk/by-uuid/b70ec0bd-042f-4204-948d-59061c006e21 /data

Verify the disk space, the new data directory is shown

root@webserver-01:~# df -kh 
Filesystem      Size  Used Avail Use% Mounted on
udev            232M  8.0K  232M   1% /dev
tmpfs            49M  352K   49M   1% /run
/dev/vda1        20G  3.3G   16G  18% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none            5.0M     0  5.0M   0% /run/lock
none            245M     0  245M   0% /run/shm
none            100M     0  100M   0% /run/user
/dev/vdb1        20G   44M   19G   1% /data

Use the lsblk command to view your available disk devices and their mount points

root@webserver-01:/data# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sr0     11:0    1  1024M  0 rom  
vda    253:0    0    20G  0 disk 
└─vda1 253:1    0    20G  0 part /
vdb    253:16   0    20G  0 disk 
└─vdb1 253:17   0    20G  0 part /data

mount to persist after a reboot edit your fstab file to add the mount command:

/dev/disk/by-uuid/b70ec0bd-042f-4204-948d-59061c006e21 /data auto defaults,nofail 0  1

Warning: Errors in the /etc/fstab file can render a system unbootable. Do not shut down a system that has errors in the /etc/fstab file.

Congratulations! you had now attached a Disk to an ECS instance successfully, the same step is documented in Video Demo here

Unmount File Systems and Remounting Again

This disk contains the data, by any chance you need to add this data disk to another instance then you can perform the following step.

unmount the disk partition by using umount command, and remove the entry containing this disk information from the fstab

root@webserver-01:/# umount /data
root@webserver-01:/# df -kh 
Filesystem      Size  Used Avail Use% Mounted on
udev            232M  8.0K  232M   1% /dev
tmpfs            49M  352K   49M   1% /run
/dev/vda1        20G  3.3G   16G  18% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none            5.0M     0  5.0M   0% /run/lock
none            245M     0  245M   0% /run/shm
none            100M     0  100M   0% /run/user

and from the ECS console, select the disk and click on Unmount option as shown in below diagram.

8

After successful of the above operation, the disk can be mount again

9

Important:

you're mounting a volume that already has data on it, don't use mkfs before mounting the volume otherwise, you'll format the volume and delete the existing data.

Repeat the step of mounting the disk from the ECS console to another running ECS instance. Once the ECS console operation done successfully, carefully execute the below step, this will preserve the data as no mkfs command is used.

root@webserver-02:/# fdisk /dev/vdb1
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x5f10d5d4.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
Using default value 1
First sector (2048-41940991, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41940991, default 41940991): 
Using default value 41940991

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 83

Command (m for help): p

Disk /dev/vdb1: 21.5 GB, 21473787904 bytes
16 heads, 63 sectors/track, 41608 cylinders, total 41940992 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 identifier: 0x5f10d5d4

     Device Boot      Start         End      Blocks   Id  System
/dev/vdb1p1            2048    41940991    20969472   83  Linux

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 22: Invalid argument.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Mount the volume and verify the operation.

root@webserver-02:/# rebootroot@webserver-01:~# mount /dev/disk/by-uuid/b70ec0bd-042f-4204-948d-59061c006e21 /data

The data is persisted in the disk

root@webserver-02:~# cd /data/
root@webserver-02:/data# ls
a  

Check the disk Usage of the new partition.

root@webserver-01:/data# df -kh 
Filesystem      Size  Used Avail Use% Mounted on
udev            232M  4.0K  232M   1% /dev
tmpfs            49M  352K   49M   1% /run
/dev/vda1        20G  3.3G   16G  18% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none            5.0M     0  5.0M   0% /run/lock
none            245M     0  245M   0% /run/shm
none            100M     0  100M   0% /run/user
/dev/vdb1        20G   44M   19G   1% /data
0 0 0
Share on

Alibaba Clouder

2,605 posts | 747 followers

You may also like

Comments