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.
/> 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.
/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: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@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]# 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
# 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
## 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.
# 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)
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