Tag: backtobasics

  • Linux – Creating Local RAID5 using fdisk

    This blogs lists the steps around creating RAID5 volume on Linux using local disks.

    We will start by installing software RAID on Linux system

    # yum install mdadm
    Loaded plugins: ulninfo
    Resolving Dependencies
    --> Running transaction check
    ---> Package mdadm.x86_64 0:4.1-1.0.1.el7 will be installed
    --> Processing Dependency: libreport-filesystem for package: mdadm-4.1-1.0.1.el7.x86_64
    --> Running transaction check
    ---> Package libreport-filesystem.x86_64 0:2.1.11-43.0.1.el7 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ================================================================================
     Package                  Arch       Version               Repository      Size
    ================================================================================
    Installing:
     mdadm                    x86_64     4.1-1.0.1.el7         ol7_latest     435 k
    Installing for dependencies:
     libreport-filesystem     x86_64     2.1.11-43.0.1.el7     ol7_latest      40 k
    
    Transaction Summary
    ================================================================================
    Install  1 Package (+1 Dependent package)
    
    Total download size: 475 k
    Installed size: 1.0 M
    Is this ok [y/d/N]: y
    Downloading packages:
    (1/2): libreport-filesystem-2.1.11-43.0.1.el7.x86_64.rpm   |  40 kB   00:00
    (2/2): mdadm-4.1-1.0.1.el7.x86_64.rpm                      | 435 kB   00:00
    --------------------------------------------------------------------------------
    Total                                              1.8 MB/s | 475 kB  00:00
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : libreport-filesystem-2.1.11-43.0.1.el7.x86_64                1/2
      Installing : mdadm-4.1-1.0.1.el7.x86_64                                   2/2
      Verifying  : mdadm-4.1-1.0.1.el7.x86_64                                   1/2
      Verifying  : libreport-filesystem-2.1.11-43.0.1.el7.x86_64                2/2
    
    Installed:
      mdadm.x86_64 0:4.1-1.0.1.el7
    
    Dependency Installed:
      libreport-filesystem.x86_64 0:2.1.11-43.0.1.el7
    
    Complete!

    Now let’s identify disks which we will use for creating RAID5 volume. These disks can be local or storage LUNs. You can list the disks using running below command. In this example we will use 5 local disks – /dev/xvd(b-f)

    # fdisk -l | grep /dev/
    Disk /dev/xvda: 53.7 GB, 53687091200 bytes, 104857600 sectors
    /dev/xvda1   *        2048     4098047     2048000   83  Linux
    /dev/xvda2         4098048    86018047    40960000   83  Linux
    /dev/xvda3        86018048   104857599     9419776   82  Linux swap / Solaris
    Disk /dev/xvdd: 53.7 GB, 53687091200 bytes, 104857600 sectors
    Disk /dev/xvde: 53.7 GB, 53687091200 bytes, 104857600 sectors
    Disk /dev/xvdb: 53.7 GB, 53687091200 bytes, 104857600 sectors
    Disk /dev/xvdc: 53.7 GB, 53687091200 bytes, 104857600 sectors
    Disk /dev/xvdf: 53.7 GB, 53687091200 bytes, 104857600 sectors
    

    Before you begin make sure that selected drives do not have existing RAID configured.

    # mdadm -E /dev/xvd[b-f]
    mdadm: No md superblock detected on /dev/xvdb.
    mdadm: No md superblock detected on /dev/xvdc.
    mdadm: No md superblock detected on /dev/xvdd.
    mdadm: No md superblock detected on /dev/xvde.
    mdadm: No md superblock detected on /dev/xvdf.

    Next step is to create partition on selected disks using fdisk. Follow below steps for each selected disks – /dev/xvd(b-f) in this example

    # fdisk /dev/xvdc
    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 0x6845b853.
    
    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-104857599, default 2048):
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599):
    Using default value 104857599
    Partition 1 of type Linux and of size 50 GiB is set
    
    Command (m for help): t
    Selected partition 1
    Hex code (type L to list all codes): fd
    Changed type of partition 'Linux' to 'Linux raid autodetect'
    
    Command (m for help): p
    
    Disk /dev/xvdc: 53.7 GB, 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
    Disk label type: dos
    Disk identifier: 0x6845b853
    
        Device Boot      Start         End      Blocks   Id  System
    /dev/xvdc1            2048   104857599    52427776   fd  Linux raid autodetect
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.

    Once you’ve successfully created the partitions for all disks, you can verify the changes using below command

    # mdadm -E /dev/xvd[b-f]
    /dev/xvdb:
       MBR Magic : aa55
    Partition[0] :    104855552 sectors at         2048 (type fd)
    /dev/xvdc:
       MBR Magic : aa55
    Partition[0] :    104855552 sectors at         2048 (type fd)
    /dev/xvdd:
       MBR Magic : aa55
    Partition[0] :    104855552 sectors at         2048 (type fd)
    /dev/xvde:
       MBR Magic : aa55
    Partition[0] :    104855552 sectors at         2048 (type fd)
    /dev/xvdf:
       MBR Magic : aa55
    Partition[0] :    104855552 sectors at         2048 (type fd)

    Now create RAID device (/dev/md1 in below example) using all the newly created disk partitions

    # mdadm --create /dev/md1 --level=5 --raid-devices=5 /dev/xvdb1 /dev/xvdc1 /dev/xvdd1 /dev/xvde1 /dev/xvdf1
    mdadm: Defaulting to version 1.2 metadata
    mdadm: array /dev/md1 started.

    Note that it takes some time to complete the RAID creation. You can monitor the progress using below command. Below command will show you current % completed.

    # cat /proc/mdstat
    Personalities : [raid6] [raid5] [raid4]
    md1 : active raid5 xvdf1[5] xvde1[3] xvdd1[2] xvdc1[1] xvdb1[0]
          209575936 blocks super 1.2 level 5, 512k chunk, algorithm 2 [5/4] [UUUU_]
          [=>...................]  recovery =  7.2% (3808256/52393984) finish=13.1min speed=61363K/sec
    
    unused devices: <none>
    

    But you prefer to watch screen till 100% completion then run below command. Result of this command will be persistent on the screen and will show you real time progress.

    # watch -n1 cat /proc/mdstat
    Every 1.0s: cat /proc/mdstat                            Mon Dec  2 09:41:04 2019
    
    Personalities : [raid6] [raid5] [raid4]
    md1 : active raid5 xvdf1[5] xvde1[3] xvdd1[2] xvdc1[1] xvdb1[0]
          209575936 blocks super 1.2 level 5, 512k chunk, algorithm 2 [5/4] [UUUU_]
          [==>..................]  recovery = 13.9% (7335304/52393984) finish=12.1mi
    n speed=61622K/sec
    
    unused devices: <none>

    Once RAID creation is 100% you can run below command to see the summary of the RAID device configuration. RAID device is /dev/md1 in below example with 5 devices – /dev/xvd(b-f)1

    # mdadm --detail /dev/md1
    /dev/md1:
               Version : 1.2
         Creation Time : Mon Dec  2 09:39:05 2019
            Raid Level : raid5
            Array Size : 209575936 (199.87 GiB 214.61 GB)
         Used Dev Size : 52393984 (49.97 GiB 53.65 GB)
          Raid Devices : 5
         Total Devices : 5
           Persistence : Superblock is persistent
    
           Update Time : Mon Dec  2 09:53:20 2019
                 State : clean
        Active Devices : 5
       Working Devices : 5
        Failed Devices : 0
         Spare Devices : 0
    
                Layout : left-symmetric
            Chunk Size : 512K
    
    Consistency Policy : resync
    
                  Name : ip-10-10-10-207:1  (local to host ip-10-10-10-207)
                  UUID : 07f28a7b:3c70d798:1deecf9a:292ddd2c
                Events : 18
    
        Number   Major   Minor   RaidDevice State
           0     202       17        0      active sync   /dev/xvdb1
           1     202       33        1      active sync   /dev/xvdc1
           2     202       49        2      active sync   /dev/xvdd1
           3     202       65        3      active sync   /dev/xvde1
           5     202       81        4      active sync   /dev/xvdf1
    

    At this point we have RAID5 device created (/dev/md1). Now we need to create file system. In below example we are creating EXT4 file system

    # mkfs.ext4 /dev/md1
    mke2fs 1.42.9 (28-Dec-2013)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=128 blocks, Stripe width=512 blocks
    13099008 inodes, 52393984 blocks
    2619699 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=2199912448
    1599 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
    

    Once the file system is created on RAID5 device, create the mount point and mount the RAID5 device on the same.

    # mkdir /mnt/ebs_r5
    # mount /dev/md1 /mnt/ebs_r5
    # df -h
    Filesystem        Size  Used Avail Use% Mounted on
    devtmpfs           32G     0   32G   0% /dev
    tmpfs              32G     0   32G   0% /dev/shm
    tmpfs              32G   17M   32G   1% /run
    tmpfs              32G     0   32G   0% /sys/fs/cgroup
    /dev/xvda2         39G  2.9G   34G   8% /
    /dev/xvda1        1.9G  234M  1.6G  13% /boot
    tmpfs             6.3G     0  6.3G   0% /run/user/1000
    /dev/md1          197G   61M  187G   1% /mnt/ebs_r5

    Also make sure you’ve added entry for new mount point in the /etc/fstab file. So that mount point will be persistent across system reboots.

    # vi etc/fstab
    /dev/md1                /mnt/ebs_r5              ext4    defaults        0 0
    

    Lastly most important step is to save the RAID configuration. Run below command to make sure RAID configuration is saved.

    # mdadm --detail --scan --verbose >> /etc/mdadm.conf

    At this point you’ve successfully created RAID5 device on Linux.

  • Linux – Configure iSCSI Initiator

    This blog is not to explain what is iSCSI but rather a reference to steps which an administrator should follow for configuring iSCSI on their Linux host (CentOS in my case)

    Prerequisites

    1. Make sure Linux host has IP connectivity to storage appliance which is acting as iSCSI target
    2. Storage appliance has iSCSI configured and service is running

    Steps

    Install iSCSI initiator on Linux host. Below is the example

    # yum install iscsi-initiator-utils
    Loaded plugins: ulninfo
    Resolving Dependencies
    --> Running transaction check
    ---> Package iscsi-initiator-utils.x86_64 0:6.2.0.874-11.0.1.el7 will be installed
    --> Processing Dependency: iscsi-initiator-utils-iscsiuio >= 6.2.0.874-11.0.1.el7 for package: iscsi-initiator-utils-6.2.0.874-11.0.1.el7.x86_64
    --> Running transaction check
    ---> Package iscsi-initiator-utils-iscsiuio.x86_64 0:6.2.0.874-11.0.1.el7 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    =======================================================================================================================================================
     Package                                          Arch                     Version                                  Repository                    Size
    =======================================================================================================================================================
    Installing:
     iscsi-initiator-utils                            x86_64                   6.2.0.874-11.0.1.el7                     ol7_latest                   428 k
    Installing for dependencies:
     iscsi-initiator-utils-iscsiuio                   x86_64                   6.2.0.874-11.0.1.el7                     ol7_latest                    93 k
    
    Transaction Summary
    =======================================================================================================================================================
    Install  1 Package (+1 Dependent package)
    
    Total download size: 521 k
    Installed size: 2.6 M
    Is this ok [y/d/N]: y
    Downloading packages:
    (1/2): iscsi-initiator-utils-6.2.0.874-11.0.1.el7.x86_64.rpm                                                                    | 428 kB  00:00:00
    (2/2): iscsi-initiator-utils-iscsiuio-6.2.0.874-11.0.1.el7.x86_64.rpm                                                           |  93 kB  00:00:00
    -------------------------------------------------------------------------------------------------------------------------------------------------------
    Total                                                                                                                  1.1 MB/s | 521 kB  00:00:00
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : iscsi-initiator-utils-iscsiuio-6.2.0.874-11.0.1.el7.x86_64                                                                          1/2
      Installing : iscsi-initiator-utils-6.2.0.874-11.0.1.el7.x86_64                                                                                   2/2
      Verifying  : iscsi-initiator-utils-6.2.0.874-11.0.1.el7.x86_64                                                                                   1/2
      Verifying  : iscsi-initiator-utils-iscsiuio-6.2.0.874-11.0.1.el7.x86_64                                                                          2/2
    
    Installed:
      iscsi-initiator-utils.x86_64 0:6.2.0.874-11.0.1.el7
    
    Dependency Installed:
      iscsi-initiator-utils-iscsiuio.x86_64 0:6.2.0.874-11.0.1.el7
    
    Complete!

    Once iSCSI package is installed then edit the iSCSI configuration file and enable node.startup = automatic. You will need to find node.startup in the config file and remove the # from start of the line. This will make sure that iSCSI is logging to target automatically on startup

    vi /etc/iscsi/iscsid.conf
    
    node.startup = automatic

    Now configure the iSCSI service to start automatically. Run below command

    # systemctl enable iscsid
    Created symlink from /etc/systemd/system/multi-user.target.wants/iscsid.service to /usr/lib/systemd/system/iscsid.service.
    

    Start iSCSI service

    # systemctl start iscsid

    Now at this point you’ve iSCSI enabled on your Linux server. If you want to view iSCSI initiator name of your server then run below command. Note that in most cases iSCSI initiator name will reflect into logged in initiators on your storage appliance in some cases you’ll need to provide the initiator name.

    # cat /etc/iscsi/initiatorname.iscsi
    InitiatorName=iqn.1988-12.com.oracle:b0ed4f9c9b9d

    Now let’s discover iSCSI target nodes using your storage system iSCSI IP address.

    # iscsiadm -m discovery -t st -p 172.16.1.11
    172.16.1.11:3260,2 iqn.1992-04.com.emc:cx.asfnsdbgfkdjfgdk.a2
    172.16.1.12:3260,1 iqn.1992-04.com.emc:cx.asfnsdbgfkdjfgdk.b2

    You can run below commands to list discovered iSCSI nodes

    # iscsiadm -m node
    172.16.1.11:3260,2 iqn.1992-04.com.emc:cx.asfnsdbgfkdjfgdk.a2
    172.16.1.12:3260,1 iqn.1992-04.com.emc:cx.asfnsdbgfkdjfgdk.b2
    
    or 
    
    # ls /var/lib/iscsi/send_targets
    172.16.1.11,3260
    
    # ls /var/lib/iscsi/nodes
    iqn.1992-04.com.emc:cx.asfnsdbgfkdjfgdk.a2
    iqn.1992-04.com.emc:cx.asfnsdbgfkdjfgdk.b2
    

    Once iSCSI nodes are discovered you can run below command to login into iSCSI target

    # iscsiadm -m node --targetname "iqn.1992-04.com.emc:cx.asfnsdbgfkdjfgdk.a2" --portal "172.16.1.11:3260" --login
    Logging in to [iface: default, target: iqn.1992-04.com.emc:cx.asfnsdbgfkdjfgdk.a2, portal: 172.16.1.11,3260] (multiple)
    Login to [iface: default, target: iqn.1992-04.com.emc:cx.asfnsdbgfkdjfgdk.a2, portal: 172.16.1.11,3260] successful.
    
    # iscsiadm -m node --targetname "iqn.1992-04.com.emc:cx.asfnsdbgfkdjfgdk.b2" --portal "172.16.1.12:3260" --login
    Logging in to [iface: default, target: iqn.1992-04.com.emc:cx.asfnsdbgfkdjfgdk.b2, portal: 172.16.1.12,3260] (multiple)
    Login to [iface: default, target: iqn.1992-04.com.emc:cx.asfnsdbgfkdjfgdk.b2, portal: 172.16.1.12,3260] successful.

    At this stage you’ve Linux host configured as iSCSI initiator and logged in successfully to target storage. Now you can ask storage admin to create LUNs and assign to Linux host.

    Once LUNs are assigned to Linux host you can run below command to scan new devices

    # iscsiadm -m session --rescan
    Rescanning session [sid: 1, target: iqn.1992-04.com.emc:cx.asfnsdbgfkdjfgdk.a2, portal: 172.16.1.11,3260]
    Rescanning session [sid: 2, target: iqn.1992-04.com.emc:cx.asfnsdbgfkdjfgdk.b2, portal: 172.16.1.12,3260]

    You can view new devices using fdisk -l or multipath -l command

  • Ansible Master – Enabling SSH Key Based Authentication

    Ansible Master – Enabling SSH Key Based Authentication

    This section seems pretty straight-forward but is the must for all the Ansible deployments.

    Follow below steps to get through the password maze in Ansible environments

    Firstly we will need to generate the SSH authentication keys. This needs to be done on the server from which you want to login to other hosts. In my case it’s Ansible master server.

    Generate your key – Run the ssh-keygen command (as below). By default system will create and store the SSH key into /root/.ssh/id_rsa, which you can change. Passphrase is optional and you can leave it blank.

    Note that if you already have generated the SSH key you have option to overwrite the same, but make sure that earlier key is not used anywhere for authentication. Below example shows option to replace existing SSH keys

    [root@ansible-master ~]# ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa):
    /root/.ssh/id_rsa already exists.
    Overwrite (y/n)? y
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:D52Rvc2F7JMlbikpj7G96Ys3bSLRUz4tNmsfNP4E2Vg root@ansible-master
    The key's randomart image is:
    +---[RSA 2048]----+
    |                 |
    |           o . . |
    |          o . + E|
    |         . o B.@ |
    |        S =.+o&+.|
    |         o.BooOoo|
    |          +.o+ B.|
    |          ..+o=.o|
    |          .+=* .o|
    +----[SHA256]-----+
    

    If you want to use existing keys then you can simply read the existing key which is saved in the file. Below is the example of the reading existing public key

    [root@ansible-master ~]# cat /root/.ssh/id_rsa.pub
    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDDoN5+wFiV3Nold4Y7KDGkvIGBqulvdDxDm3qPj7SGsHsnEZKKxbQtO2QJNDJW2tXhLLDxY7vnyQ3xiL0bXbUTkeHHANFY7yZnMH4LH1k51yAkgDdVzKYUdvVJ81qRmYaJUJnLCsKvZMFmg7/KzecuA1QU8WVDLCH02HiRsmqVPaykcR2EOKCgX7i92Q/khZycR66GvoKcVwU9LuDLlADQt9GTaFDFiXPITNayvXkWvL7bwe9arqMCJzmEpvveglJI+vLvh5793yxrszTScTuMpjvpp4Nz5Tk0NSvQqo73yiHAsFF6M7l2hnVskyj3cAOL1dzxmopC/E1qd3DrWSOX root@ansible-master
    

    Once you have generated the SSH key next step is to add the key into target system.

    For copying the SSH keys into target system simply follow below process.

    [root@ansible-master ~]# ssh-copy-id root@192.168.1.101
    /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
    The authenticity of host '192.168.1.101 (192.168.1.101)' can't be established.
    ECDSA key fingerprint is SHA256:qnOPcE09JqKALwLLaL+SqNMy2oubvhF/EevGCcK9S9A.
    ECDSA key fingerprint is MD5:4d:65:b7:e0:e8:09:41:a8:f9:4c:2d:b6:47:55:b2:09.
    Are you sure you want to continue connecting (yes/no)? yes
    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
    root@192.168.1.101's password:
    
    Number of key(s) added: 1
    
    Now try logging into the machine, with:   "ssh 'root@192.168.1.101'"
    and check to make sure that only the key(s) you wanted were added.
    

    You’ll notice in the command output that this process has added the key into the target system. Note that 192.168.1.101 in above example is target system in which I wanted Ansible master to login without username and password.

    Once the SSH keys are added into the target system now you can try SSH into the target system and it won’t ask for password anymore.

    [root@ansible-master ~]# ssh root@192.168.1.101
    Last login: Tue Nov 26 09:01:16 2019 from gateway
    [root@target-system ~]#
    

    Hope this saves time for many of us.