Find knowledge base article(s) by searching for keywords in the title e.g. type linux in the search box below
Find knowledge base article(s) by browsing the subject categories of articles
Technology quick references, cheatsheets, user manuals etc.
Shop Online through ShopifyLite
Tutorials on various IT applications.
Search Title    (UL:0 |SS:f)

Software >> OS >> Unix >> Linux >> RHEL >> 7 >> RHCE >> System configuration and management part 3

Configure a system as either an iSCSI target or initiator that persistently mounts an iSCSI target

 
### Setup iSCSI target on rhel7server1

## assume empty disk /dev/sdb available

[root@rhel7server1 ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x7742edc6

   Device Boot      Start         End      Blocks   Id  System



## install targetcli pkg & enable iSCSI target

[root@rhel7server1 ~]# yum install -y targetcli
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Package targetcli-2.1.fb46-1.el7.noarch already installed and latest version
Nothing to do

[root@rhel7server1 ~]# systemctl enable target
Created symlink from /etc/systemd/system/multi-user.target.wants/target.service to /usr/lib/systemd/system/target.service.

## configure the target

[root@rhel7server1 ~]# targetcli
Warning: Could not load preferences file /root/.targetcli/prefs.bin.
targetcli shell version 2.1.fb46
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.

/> backstores/block/ create block1 /dev/sdb
Created block storage object block1 using /dev/sdb.

/> ls
o- / ..................................................................................................... [...]
  o- backstores .......................................................................................... [...]
  | o- block .............................................................................. [Storage Objects: 1]
  | | o- block1 .................................................... [/dev/sdb (10.0GiB) write-thru deactivated]
  | |   o- alua ............................................................................... [ALUA Groups: 1]
  | |     o- default_tg_pt_gp ................................................... [ALUA state: Active/optimized]
  | o- fileio ............................................................................. [Storage Objects: 0]
  | o- pscsi .............................................................................. [Storage Objects: 0]
  | o- ramdisk ............................................................................ [Storage Objects: 0]
  o- iscsi ........................................................................................ [Targets: 0]
  o- loopback ..................................................................................... [Targets: 0]

/> iscsi/ create iqn.2020-01.com.myexample:t1
Created target iqn.2020-01.com.myexample:t1.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.

/> cd iscsi/iqn.2020-01.com.myexample:t1/tpg1/
/iscsi/iqn.20...ample:t1/tpg1> ls
o- tpg1 ................................................................................. [no-gen-acls, no-auth]
  o- acls ............................................................................................ [ACLs: 0]
  o- luns ............................................................................................ [LUNs: 0]
  o- portals ...................................................................................... [Portals: 1]
    o- 0.0.0.0:3260 ....................................................................................... [OK]

/iscsi/iqn.20...ample:t1/tpg1> luns/ create /backstores/block/block1
Created LUN 0.

/iscsi/iqn.20...ample:t1/tpg1> ls
o- tpg1 ................................................................................. [no-gen-acls, no-auth]
  o- acls ............................................................................................ [ACLs: 0]
  o- luns ............................................................................................ [LUNs: 1]
  | o- lun0 ....................................................... [block/block1 (/dev/sdb) (default_tg_pt_gp)]
  o- portals ...................................................................................... [Portals: 1]
    o- 0.0.0.0:3260 ....................................................................................... [OK]

/iscsi/iqn.20...ample:t1/tpg1> acls/ create iqn.2020-01.com.myexample:client
Created Node ACL for iqn.2020-01.com.myexample:client
Created mapped LUN 0.

/iscsi/iqn.20...ample:t1/tpg1> cd acls/iqn.2020-01.com.myexample:client/

/iscsi/iqn.20...ample:client> set auth userid=lunuser
Parameter userid is now 'lunuser'.

/iscsi/iqn.20...ample:client> set auth password=password
Parameter password is now 'password'.

/iscsi/iqn.20...ample:client> cd ../..

/iscsi/iqn.20...ample:t1/tpg1> ls
o- tpg1 ................................................................................. [no-gen-acls, no-auth]
  o- acls ............................................................................................ [ACLs: 1]
  | o- iqn.2020-01.com.myexample:client ....................................................... [Mapped LUNs: 1]
  |   o- mapped_lun0 .................................................................. [lun0 block/block1 (rw)]
  o- luns ............................................................................................ [LUNs: 1]
  | o- lun0 ....................................................... [block/block1 (/dev/sdb) (default_tg_pt_gp)]
  o- portals ...................................................................................... [Portals: 1]
    o- 0.0.0.0:3260 ....................................................................................... [OK]

/iscsi/iqn.20...ample:t1/tpg1> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json

[root@rhel7server1 ~]# firewall-cmd --permanent --add-port=3260/tcp
success

[root@rhel7server1 ~]# firewall-cmd --reload
success

[root@rhel7server1 ~]# systemctl start target

back to Objectives


### Set up iSCSI Initiator on rhel7client1

[root@rhel7client1 ~]# yum install -y iscsi-initiator-utils
....
  Installing : iscsi-initiator-utils-6.2.0.874-4.el7.x86_64                                                 1/2
  Installing : iscsi-initiator-utils-iscsiuio-6.2.0.874-4.el7.x86_64                                        2/2
  Verifying  : iscsi-initiator-utils-iscsiuio-6.2.0.874-4.el7.x86_64                                        1/2
  Verifying  : iscsi-initiator-utils-6.2.0.874-4.el7.x86_64                                                 2/2

Installed:
  iscsi-initiator-utils.x86_64 0:6.2.0.874-4.el7                                                               

Dependency Installed:
  iscsi-initiator-utils-iscsiuio.x86_64 0:6.2.0.874-4.el7             

[root@rhel7client1 ~]# cd /etc/iscsi

[root@rhel7client1 iscsi]# ls -l
total 20
-rw-r--r--. 1 root root    49 Apr 21  2019 initiatorname.iscsi
-rw-------. 1 root root 12329 Jun 27  2017 iscsid.conf

[root@rhel7client1 iscsi]# vi initiatorname.iscsi

/etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2020-01.com.myexample:client


[root@rhel7client1 iscsi]# vi iscsid.conf

/etc/iscsi/iscsid.conf
# uncomment and configure these parameters
node.session.auth.authmethod = CHAP
node.session.auth.username = lunuser
node.session.auth.password = password


[root@rhel7client1 iscsi]# systemctl start iscsi

[root@rhel7client1 iscsi]# systemctl enable iscsi

[root@rhel7client1 iscsi]# iscsiadm --mode discovery --type sendtargets --portal rhel7server1
192.168.0.10:3260,1 iqn.2020-01.com.myexample:t1

[root@rhel7client1 iscsi]# iscsiadm --mode node --targetname iqn.2020-01.com.myexample:t1 --portal rhel7server1 --login
Logging in to [iface: default, target: iqn.2020-01.com.myexample:t1, portal: 192.168.0.10,3260] (multiple)
Login to [iface: default, target: iqn.2020-01.com.myexample:t1, portal: 192.168.0.10,3260] successful.


[root@rhel7client1 iscsi]# lsblk --scsi
NAME HCTL       TYPE VENDOR   MODEL             REV TRAN
sda  0:0:0:0    disk VBOX     HARDDISK         1.0  sas
sdb  0:0:1:0    disk VBOX     HARDDISK         1.0  sas
sdc  3:0:0:0    disk LIO-ORG  block1           4.0  iscsi  
sr0  2:0:0:0    rom  VBOX     CD-ROM           1.0  ata

root@rhel7client1 iscsi]# mkfs.ext4  /dev/sdc
mke2fs 1.42.9 (28-Dec-2013)
/dev/sdc is entire device, not just one partition!
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=1024 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
80 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

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

[root@rhel7client1 iscsi]# blkid /dev/sdc
/dev/sdc: UUID="9da9629b-aded-4bcc-9403-10c909bcf488" TYPE="ext4"


## add the following entry into /etc/fstab

/etc/fstab
# not the additional option _netdev which is for the dependency on
# network being started up first before mounting

UUID=9da9629b-aded-4bcc-9403-10c909bcf488    /mnt/iscsi  ext4    _netdev   0 0


## mkdir the mountpoint directory and then mount

[root@rhel7client1 iscsi]# mkdir /mnt/iscsi

[root@rhel7client1 iscsi]# mount -a

[root@rhel7client1 iscsi]# df -h  | grep iscsi
Filesystem             Size  Used Avail Use% Mounted on
/dev/sdc               9.8G   37M  9.2G   1% /mnt/iscsi

## check the iscsi session

[root@rhel7client1 iscsi]# iscsiadm -m session -P 3
iSCSI Transport Class version 2.0-870
version 6.2.0.874-2
Target: iqn.2020-01.com.myexample.rhel7server1:t1 (non-flash)
    Current Portal: 192.168.0.10:3260,1
    Persistent Portal: 192.168.0.10:3260,1
        **********
        Interface:
        **********
        Iface Name: default
        Iface Transport: tcp
        Iface Initiatorname: iqn.2020-01.com.myexample:client
        Iface IPaddress: 192.168.0.20
        Iface HWaddress: <empty>
        Iface Netdev: <empty>
        SID: 1
        iSCSI Connection State: LOGGED IN
        iSCSI Session State: LOGGED_IN
        Internal iscsid Session State: NO CHANGE
        *********
        Timeouts:
        *********
        Recovery Timeout: 120
        Target Reset Timeout: 30
        LUN Reset Timeout: 30
        Abort Timeout: 15
        *****
        CHAP:
        *****
        username: lunuser
        password: ********
        username_in: <empty>
        password_in: ********
        ************************
        Negotiated iSCSI params:
        ************************
        HeaderDigest: None
        DataDigest: None
        MaxRecvDataSegmentLength: 262144
        MaxXmitDataSegmentLength: 262144
        FirstBurstLength: 65536
        MaxBurstLength: 262144
        ImmediateData: Yes
        InitialR2T: Yes
        MaxOutstandingR2T: 1
        ************************
        Attached SCSI devices:
        ************************
        Host Number: 3    State: running
        scsi3 Channel 00 Id 0 Lun: 0
            Attached scsi disk sdc        State: running

## when about to reboot, it is recommended to umount the iscsi mountpoint and stop iscsi initiator service

## after reboot, the service will be autostarted and the mountpoint automounted


back to Objectives

 

Produce and deliver reports on system utilization (processor, memory, disk, and network)

 

 
### Produce Reports on System Utilization

## REAL TIME monitoring of System Utilization - use dstat

# install dstat

[root@rhel7server1 ~]# yum install -y dstat

# run to show utilization in real time

[root@rhel7server1 ~]# dstat
You did not select any stats, using -cdngy by default.
----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai hiq siq| read  writ| recv  send|  in   out | int   csw
  7   1  92   1   0   0|  82k   13k|   0     0 |   0     0 | 205   205
 25   1  74   0   0   0|   0     0 |   0     0 |   0     0 | 428   277
 18   1  81   0   0   0|   0     0 | 139B    0 |   0     0 | 380   276
 24   0  76   0   0   0|   0     0 | 232B    0 |   0     0 | 378   212
...
<Ctrl-C to abort>

# display CPU utilization (only) in real time

[root@rhel7server1 ~]# dstat -c
----total-cpu-usage----
usr sys idl wai hiq siq
  7   1  92   1   0   0
 28   1  71   0   0   0
 28   1  71   0   0   0

# display Memory utilization (only) in realtime

[root@rhel7server1 ~]# dstat -m
------memory-usage-----
 used  buff  cach  free
 899M 2224k  610M 2280M
 899M 2224k  610M 2280M
 899M 2224k  610M 2280M^

# display Disk io utilization (only) in real time
[root@rhel7server1 ~]# dstat -d
-dsk/total-
 read  writ
  80k   13k
   0     0
   0     0
   0     0

# display paging (only) in real time

[root@rhel7server1 ~]# dstat -g
---paging--
  in   out
   0     0
   0     0
   0     0

# display multiple utilization parameters in real time

[root@rhel7server1 ~]# dstat -tcm
----system---- ----total-cpu-usage---- ------memory-usage-----
     time     |usr sys idl wai hiq siq| used  buff  cach  free
13-03 10:17:58|  7   1  92   1   0   0| 901M 2224k  612M 2276M
13-03 10:17:59| 24   1  75   0   0   0| 901M 2224k  612M 2276M
13-03 10:18:00| 12   1  87   0   0   0| 901M 2224k  612M 2276M
13-03 10:18:01| 25   1  74   0   0   0| 901M 2224k  612M 2276M

## COLLECTING HISTORICAL System Utilization - use sysstat service

## install sysstat

# Note:
# 1. collects sar data in /var/log/sa
# 2. configured cron jobs in /etc/crond.d/
# 3. sadf is the command to view sar files

[root@rhel7server1 ~]# yum install -y sysstat

# enable and start the sysstat service

root@rhel7server1 ~]# systemctl enable sysstat
Created symlink from /etc/systemd/system/multi-user.target.wants/sysstat.service to /usr/lib/systemd/system/sysstat.service.

[root@rhel7server1 ~]# systemctl start sysstat

[root@rhel7server1 ~]# systemctl status sysstat
● sysstat.service - Resets System Activity Logs
   Loaded: loaded (/usr/lib/systemd/system/sysstat.service; enabled; vendor preset: enabled)
   Active: active (exited) since Fri 2020-03-13 10:30:37 EDT; 7s ago
  Process: 7254 ExecStart=/usr/lib64/sa/sa1 --boot (code=exited, status=0/SUCCESS)
 Main PID: 7254 (code=exited, status=0/SUCCESS)

Mar 13 10:30:37 rhel7server1 systemd[1]: Starting Resets System Activity Logs...
Mar 13 10:30:37 rhel7server1 systemd[1]: Started Resets System Activity Logs.


# note that sadf is part of this sysstat package

[root@rhel7server1 ~]# rpm -qf /usr/bin/sadf
sysstat-10.1.5-12.el7.x86_64

# eg display the SAR data between 09:00 and 09:40 on the 10th of the month
# -s : start time
# -e : end time
# -T : displaying local timestamp (instead of UTC)

root@rhel7server1 ~]# sadf -s 08:00:00 -e 10:00:00 -T /var/log/sa/sa10
rhel7server1    -1    2020-03-10 09:12:37    LINUX-RESTART
rhel7server1    598    2020-03-10 09:30:01    all    %user    0.21
rhel7server1    598    2020-03-10 09:30:01    all    %nice    0.00
rhel7server1    598    2020-03-10 09:30:01    all    %system    0.34
rhel7server1    598    2020-03-10 09:30:01    all    %iowait    0.04
rhel7server1    598    2020-03-10 09:30:01    all    %steal    0.00
rhel7server1    598    2020-03-10 09:30:01    all    %idle    99.41
rhel7server1    599    2020-03-10 09:40:01    all    %user    0.10
rhel7server1    599    2020-03-10 09:40:01    all    %nice    0.00
rhel7server1    599    2020-03-10 09:40:01    all    %system    0.21
rhel7server1    599    2020-03-10 09:40:01    all    %iowait    0.00
rhel7server1    599    2020-03-10 09:40:01    all    %steal    0.00
rhel7server1    599    2020-03-10 09:40:01    all    %idle    99.69
rhel7server1    598    2020-03-10 09:50:01    all    %user    0.10
rhel7server1    598    2020-03-10 09:50:01    all    %nice    0.00
rhel7server1    598    2020-03-10 09:50:01    all    %system    0.21
rhel7server1    598    2020-03-10 09:50:01    all    %iowait    0.00
rhel7server1    598    2020-03-10 09:50:01    all    %steal    0.00
rhel7server1    598    2020-03-10 09:50:01    all    %idle    99.69

# e.g. the same data in semicolon delimited output (-d)

root@rhel7server1 ~]# sadf -s 08:00:00 -e 10:00:00 -T -d /var/log/sa/sa10
rhel7server1;-1;2020-03-10 09:12:37;LINUX-RESTART
# hostname;interval;timestamp;CPU;%user;%nice;%system;%iowait;%steal;%idle
rhel7server1;598;2020-03-10 09:30:01;-1;0.21;0.00;0.34;0.04;0.00;99.41
rhel7server1;599;2020-03-10 09:40:01;-1;0.10;0.00;0.21;0.00;0.00;99.69
rhel7server1;598;2020-03-10 09:50:01;-1;0.10;0.00;0.21;0.00;0.00;99.69

## adding sar options to the same report
## the parameters following -- are the sar options

[root@rhel7server1 ~]# sadf -s 08:00:00 -e 10:00:00 -T /var/log/sa/sa10 -- -r -dp -n DEV | head -10
rhel7server1    -1    2020-03-10 09:12:37    LINUX-RESTART
rhel7server1    598    2020-03-10 09:30:01    -    kbmemfree    3168588
rhel7server1    598    2020-03-10 09:30:01    -    kbmemused    713292
rhel7server1    598    2020-03-10 09:30:01    -    %memused    18.37
rhel7server1    598    2020-03-10 09:30:01    -    kbbuffers    2224
rhel7server1    598    2020-03-10 09:30:01    -    kbcached    329120
rhel7server1    598    2020-03-10 09:30:01    -    kbcommit    1305152
rhel7server1    598    2020-03-10 09:30:01    -    %commit    16.43
rhel7server1    598    2020-03-10 09:30:01    -    kbactive    309736
rhel7server1    598    2020-03-10 09:30:01    -    kbinact    251060

## SYNTAX

sadf  [ -C ] [ -d | -j | -p | -x ] [ -H ] [ -h ] [ -T | -t | -U ] [ -V ] [ -P { cpu [,...] | ALL } ] [
       -s [ hh:mm:ss ] ] [ -e [ hh:mm:ss ] ] [ -- sar_options ] [ interval [ count ] ] [ datafile ]

 back to Objectives


 

Use shell scripting to automate system maintenance tasks

 

Example - ASCII greater/less than comparison

veg1=carrots
veg2=tomatoes

if [[ "$veg1" < "$veg2" ]]
then
  echo "Although $veg1 precede $veg2 in the dictionary,"
  echo -n "this does not necessarily imply anything "
  echo "about my culinary preferences."
else
  echo "What kind of dictionary are you using, anyhow?"
fi


Exammple - NOT

if [ ! -f $FILENAME ]
then
  ...


Example - OR

if [ $condition1 ] || [ $condition2 ]
# Same as:  if [ $condition1 -o $condition2 ]
# Returns true if either condition1 or condition2 holds true...

if [[ $condition1 || $condition2 ]]    # Also works.
#  Note that || operator not permitted inside brackets
#+ of a [ ... ] construct.


Example - AND

if [ $condition1 ] && [ $condition2 ]
#  Same as:  if [ $condition1 -a $condition2 ]
#  Returns true if both condition1 and condition2 hold true...

if [[ $condition1 && $condition2 ]]    # Also works.
#  Note that && operator not permitted inside brackets
#+ of [ ... ] construct.


Example - variable assignment

a=28
echo $a   # 28


Example - arithmetic

## using let
let z=5+3 # no spaces, double quotes optional
# OR
let "z = 5 + 3"   # enclose in double quote if spaces used
echo $z     # 8

## using backtick
z=`expr 5 + 3`    # space separator is mandatory
echo $z

## using double parentheses
z=$((5+3))
echo $z


Example - $ variables

$! pid of the last job run in the background
$? is the exit status of the last command
$_ final argumant of previous command
$$ pid of the script itself
$n - positional parameter n e.g. replacebegin:content:replaceend
$# - total number of command line arguments
$* - all of the positional parameters as a single word
$@ - all of the positional paramters


Example - LOOPS - while

while [ condition ]
do
 command(s)...
done


Example - Reading and parsing files

cat $filename |   # Supply input from a file.
while read line   # As long as there is another line to read ...
do
  ...
done

OR

while read line
do
  ...
done < $filename


Example - LOOPS - until

#!/bin/bash

END_CONDITION=end

until [ "$var1" = "$END_CONDITION" ]
# Tests condition here, at top of loop.
do
  echo "Input variable #1 "
  echo "($END_CONDITION to exit)"
  read var1
  echo "variable #1 = $var1"
  echo
done  


Example - Branching - Case

#!/bin/bash

echo; echo "Hit a key, then hit return."
read Keypress

case "$Keypress" in
  [[:lower:]]   )
                  echo "Lowercase letter"
                  ;;
  [[:upper:]]   )
                  echo "Uppercase letter"
                  ;;
  [0-9]         )
                  echo "Digit"
                  ;;
  *             )
                  echo "Punctuation, whitespace, or other"
                  ;;
esac      
#  ranges of characters in [square brackets],
#  POSIX ranges in [[double square brackets]].


Example - Select menu

select choice in one two three
do
 you entered $choice
 break
done

## Select the numeric buttet point number besides the option and not the option value itself


Example - Here document

COMMAND <<InputComesFromHERE
...
...
...
InputComesFromHERE

 

[ © 2008-2021 myfaqbase.com - A property of WPDC Consulting ]