Initializing a Linux Data Disk (parted)

Scenarios

This section uses CentOS 7.0 64bit to describe how to initialize a data disk attached to a server running Linux and use parted to partition the data disk.

The method for initializing an EVS disk varies depending on the OS running on the server. This document is used for reference only. For the detailed operations and differences, see the product documents of the OSs running on the corresponding servers.

Prerequisites

  • You have logged in to the server as user root.
    • For details about how to log in to an ECS, see section Logging In to an ECS in the Elastic Cloud Server User Guide.
    • For details about how to log in to a BMS, see section Logging In to a BMS in the Bare Metal Server User Guide.
  • A data disk has been attached to a server and has not been initialized.

Creating Partitions and Mounting a Disk

The following example shows you how new partitions can be created on a new data disk that has been attached to a server. The partitions will be created using parted, and GPT is used as the partition style. Furthermore, the partition will be formatted using the ext4 file system, mounted on /mnt/sdc, and configured automatic mounting upon system start.

  1. Run the following command to query information about the added data disk:

    lsblk

    Information similar to the following is displayed:

    [root@ecs-centos-70 linux]# lsblk
    NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    xvda    202:0    0   40G  0 disk 
    ├─xvda1 202:1    0    4G  0 part [SWAP]
    └─xvda2 202:2    0   36G  0 part /
    xvdb    202:16   0  100G  0 disk

    In the command output, the server contains two disks. /dev/xvda is the system disk, and /dev/xvdb is the added data disk.

  2. Run the following command to enter parted to partition the added data disk:

    parted Added data disk

    In this example, /dev/xvdb is the newly added data disk.

    parted /dev/xvdb

    Information similar to the following is displayed:

    [root@ecs-centos-70 linux]# parted /dev/xvdb
    GNU Parted 3.1
    Using /dev/xvdb
    Welcome to GNU Parted! Type 'help' to view a list of commands.

  3. Enter p and press Enter to view the current disk partition style.

    Information similar to the following is displayed:

    (parted) p
    Error: /dev/xvdb: unrecognised disk label
    Model: Xen Virtual Block Device (xvd)                                     
    Disk /dev/xvdb: 107GB
    Sector size (logical/physical): 512B/512B
    Partition Table: unknown
    Disk Flags:   

    In the command output, the Partition Table value is unknown, indicating that the disk partition style is unknown.

  4. Run the following command to set the disk partition style:

    mklabel Disk partition style

    For example, run the following command to set the partition style to GPT: (The disk partition styles include MBR and GPT.)

    mklabel gpt

    NOTICE:

    If you change the disk partition style after the disk has been used, the original data on the disk will be cleared. Therefore, select a proper disk partition style when initializing the disk.

  5. Enter p and press Enter to view the disk partition style.

    Information similar to the following is displayed:

    (parted) mklabel gpt                                              
    (parted) p                                                        
    Model: Xen Virtual Block Device (xvd)
    Disk /dev/xvdb: 209715200s
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start  End  Size  File system  Name  Flags

  6. Enter mkpart opt 0 100% and press Enter.

    In this example, one partition is created for the added data disk. Value 0 indicates the disk start capacity, and value 100% indicates the disk end capacity. The two values are used for reference only. You can determine the partition quantity and capacity based on your service requirements.

    Information similar to the following is displayed:

    (parted) mkpart opt 0 100%
    Warning: The resulting partition is not properly aligned for best performance.
    Ignore/Cancel? Ignore 

    Enter Ignore to ignore the best performance warning.

  7. Enter p and press Enter to view the details about the created partition.

    Information similar to the following is displayed:

    (parted) p                                                                
    Model: Xen Virtual Block Device (xvd)
    Disk /dev/xvdb: 107GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start   End    Size   File system  Name  Flags
     1      17.4kB  107GB  107GB               opt

    Details about the /dev/xvdb1 partition are displayed.

  8. Enter q and press Enter to exit parted.
  9. Run the following command to view the disk partition information:

    lsblk

    Information similar to the following is displayed:

    [root@ecs-centos-70 linux]# lsblk                                 
    NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    xvda    202:0    0   40G  0 disk 
    ├─xvda1 202:1    0    4G  0 part [SWAP]
    └─xvda2 202:2    0   36G  0 part /
    xvdb    202:16   0  100G  0 disk 
    └─xvdb1 202:17   0  100G  0 part 

    In the command output, /dev/xvdb1 is the partition you created.

  10. Run the following command to set the format for the file system of the newly created partition:

    mkfs -t File system format /dev/xvdb1

    For example, run the following command to set the ext4 file system for the /dev/xvdb1 partition:

    mkfs -t ext4 /dev/xvdb1

    Information similar to the following is displayed:

    [root@ecs-centos-70 linux]# mkfs -t ext4 /dev/xvdb1
    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, 26214391 blocks
    1310719 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  

    The formatting takes a period of time. Observe the system running status, and do not exit.

    NOTICE:

    The partition quantity and size supported by different file systems vary. Therefore, you are advised to choose an appropriate file system based on your service requirements.

  11. Run the following command to create a mount point:

    mkdir Mount point

    For example, run the following command to create the /mnt/sdc mount point:

    mkdir /mnt/sdc

  12. Run the following command to mount the new partition on the created mount point:

    mount /dev/xvdb1 Mount point

    For example, run the following command to mount the newly created partition on /mnt/sdc:

    mount /dev/xvdb1 /mnt/sdc

  13. Run the following command to view the mount result:

    df -TH

    Information similar to the following is displayed:

    [root@ecs-centos-70 linux]# df -TH
    Filesystem     Type      Size  Used Avail Use% Mounted on
    /dev/xvda2     xfs        39G  4.0G   35G  11% /
    devtmpfs       devtmpfs  946M     0  946M   0% /dev
    tmpfs          tmpfs     954M     0  954M   0% /dev/shm
    tmpfs          tmpfs     954M  9.1M  945M   1% /run
    tmpfs          tmpfs     954M     0  954M   0% /sys/fs/cgroup
    /dev/xvdb1     ext4      106G   63M  101G   1% /mnt/sdc

    The newly created /dev/xvdb1 is mounted on /mnt/sdc.

Setting Automatic Disk Mounting at System Start

To automatically mount a disk when a server starts, you should not specify its partition, for example /dev/xvdb1, in /etc/fstab. Because the sequence of cloud devices, and therefore their names may change during the server stop and start. You are advised to use the universally unique identifier (UUID) in /etc/fstab to automatically mount a disk at system start.

NOTE:

The UUID is the unique character string for disk partitions in a Linux system.

  1. Run the following command to query the partition UUID:

    blkid Disk partition

    For example, run the following command to query the UUID of /dev/xvdb1:

    blkid /dev/xvdb1

    Information similar to the following is displayed:

    [root@ecs-b656 test]# blkid /dev/xvdb1
    /dev/xvdb1: UUID="1851e23f-1c57-40ab-86bb-5fc5fc606ffa" TYPE="ext4"

    The UUID of /dev/xvdb1 is displayed.

  2. Run the following command to open the fstab file using the vi editor:

    vi /etc/fstab

  3. Press i to enter the editing mode.
  4. Move the cursor to the end of the file and press Enter. Then add the following information:

    UUID=1851e23f-1c57-40ab-86bb-5fc5fc606ffa /mnt/sdc      ext4 defaults     0   2

  5. Press Esc, enter :wq, and press Enter.

    The system saves the configurations and exits the vi editor.

Registration