Script to scan the disks and make file system


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to scan the disks and make file system
# 1  
Old 10-20-2017
Script to scan the disks and make file system

Hi
What I'm trying to do(manually) is logging into the server
and running the below mentioned commands

Code:
ls /sys/class/scsi_device/ | while read i; do echo "- - -" > /sys/class/scsi_device/$i/device/rescan;done

lsblk
echo -e "o\nn\np\n1\n\n\nw" | fdisk /dev/sdd
partx -a /dev/sdd1
mkfs.ext4 /dev/sdd1
blkid  /dev/sdd1 | awk '{print $2}' | cut -d "\"" -f2
Finding the blkid of the disk and updating the /etc/fstab
echo "UUID=$(blkid -s UUID -o value /dev/sda1)" /root/abcd  ext4 defaults 0 0 >> /etc/fstab

so i have to do this on multiple machines logging into each machine
scanning for new disk where the disk may be any (/dev/sdg /dev/sdb or /dev/sdi like wise but the newly added disk will be of 6G
so though of writing simple shell script but a little mistake can mess up the whole thing here i need to partition only the disk which i have scanned and has been added recently otherwise i may loose the data
i thought of something like this



Code:
ls /sys/class/scsi_device/ | while read i; do echo "- - -" > /sys/class/scsi_device/$i/device/rescan;done
b=$(lsblk | grep 6G|awk '{print $1}')
echo -e "o\nn\np\n1\n\n\nw" | fdisk /dev/$b
partx -a /dev/$b
mkfs.ext4 /dev/$b1
echo "UUID=$(blkid -s UUID -o value /dev/$b1)" /root/abcd  ext4 defaults 0 0 >> /etc/fstab

Please suggest me the best way to do this.
# 2  
Old 10-20-2017
First of all, you cannot check enough, or you risk to damage existing file systems!
Suggestion for a script
Code:
#!/bin/bash
# scan_add_fstab.sh
for i in /sys/class/scsi_device/*
do
  # must be a directory
  [ -d "$i" ] &&  echo "- - -" > "$i/device/rescan"
done
for b in $(lsblk -d | awk '$4=="6G" {print $1}')
do
  # jump to next loop cycle if /dev/$b is in fstab
  grep -q "^[^#]*/dev/$b\>" /etc/fstab && continue
  uuid=$(blkid -s UUID -o value /dev/$b)
  # jump to next loop cycle if its UUID is present and in fstab
  [ -n "$uuid" ] && grep -q "^[^#]*$uuid" /etc/fstab && continue
  # echo -e is discouraged, the echo options can differ between shells
  printf "o\nn\np\n1\n\n\nw\n" | fdisk /dev/$b
  partx -a /dev/$b
  mkfs.ext4 /dev/$b1
  uuid=$(blkid -s UUID -o value /dev/$b)
  echo "UUID=$uuid  /root/abcd  ext4 defaults 0 0" >> /etc/fstab
done

Then pass this script ("scan_add_fstab.sh") to /bin/bash on each server
Code:
for i in server1 server2
do
  ssh -x "$i" "/bin/bash" < scan_add_fstab.sh
done

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 10-20-2017
what is the difference between using
Code:
ls /sys/class/scsi_device/ | while read i; do echo "- - -" > /sys/class/scsi_device/$i/device/rescan;done

and

Code:
for i in /sys/class/scsi_device/*
do
  # must be a directory
  [ -d "$i" ] &&  echo "- - -" > "$i/device/rescan"
done


Last edited by Don Cragun; 10-20-2017 at 05:15 PM.. Reason: Add CODE tags again.
# 4  
Old 10-20-2017
Quote:
Originally Posted by James0806
what is the difference between using
Code:
ls /sys/class/scsi_device/ | while read i; do echo "- - -" > /sys/class/scsi_device/$i/device/rescan;done

and

Code:
for i in /sys/class/scsi_device/*
do
  # must be a directory
  [ -d "$i" ] &&  echo "- - -" > "$i/device/rescan"
done

At first glance, it appears that as long as all files in the directory /sys/class/scsi_device are directories, they should produce similar results. If a file in that directory is not a directory, the 1st script will get an error trying to write to an invalid pathname while the 2nd script will silently ignore any files that are not directories.

In both cases, the scripts make the assumption that every directory in that directory has a subdirectory named device and that the user running the script has permission to create or overwrite a file named rescan in that subdirectory.

In the 2nd script, the shell reads the directory to get the list of files to process and then processes the files found in that list. In the 1st script, ls must be invoked to get the list of files but ls and the rest of the script can run in parallel.

As MadeInGermany said, neither of these demonstrate what would be called production code.
# 5  
Old 10-21-2017
And, knowing that we must never create new files in /sys, the following is more consequent
Code:
for i in /sys/class/scsi_device/*/device/rescan
do
  # unless nullglob is set, we must ensure it exists
  [ -e "$i" ] &&  echo "- - -" > "$i"
done

--
The ls command truncates the given path. Can be useful, but in the while loop one needed to add it again.
# 6  
Old 10-21-2017
Just one more caveat: if your shop is similar to my customer you have to deal with several versions of several Linux distributions. I am no expert for Linux by any stretch, but i remember older Linux distributions didn't have that /sys-tree but files in /dev to deal with devices.

I don't know when this changed (as i said, i don't work regularly with Linux) but if there is any chance you might hit such a system you should perhaps put some safety test of the version you are running on in there prior to such a deep-impact procedure.

I hope this helps.

bakunin
# 7  
Old 10-23-2017
The purpose of /sys is to give some insight into the kernel memory. Typically you can read (and patch) kernel parameters here. But also device drivers can plug in, so you can set their properties.
Sounds like /proc? Indeed the implementation of /sys is similar, and at the beginning they (mis)used the /proc for kernel parameters.
The /dev tree is still for the main functionality: give the user land a file-like interface, and handle it with device drivers in the kernel.
Of cause many parameters in /sys and /proc have little drivers that present the binary kernel values as text (and in the case of writing/patching convert the text to binary).
And yes, /sys sometimes changes. One should carefully check the existing /sys structure when using it; that's better than making asssumptions because of a certain OS version.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Red Hat

Scan for new LUN and create a new file system

Hi Please I dont have a lot of redhat skills, but I need some help on creating a file system. I need to rescan for this new LUN, so I try to check existing LUNs: fdisk -l Disk /dev/sda: 299.4 GB, 299439751168 bytes 255 heads, 63 sectors/track, 36404 cylinders Units = cylinders of... (8 Replies)
Discussion started by: fretagi
8 Replies

2. Shell Programming and Scripting

Make 48 disks(files) on solaris 10

Hello, to simulate an environment with 48 disks using Solaris 10 x86, i try toMake 48 disks (files) with MKFILE : #for i in c{0,1,2,3,4,5}t{0,1,2,3,4,5,6,7}d0 >do > mkfile 100m $i >done But i received like result : #ls /test_zfs c{0,1,2,3,4,5}t{0,1,2,3,4,5,6,7}d0 Any help... (2 Replies)
Discussion started by: herbich1985
2 Replies

3. UNIX for Advanced & Expert Users

Scan all mail messages through the server and save a copy into file system following a rule

In our company we work for our customer with a job# philosophy, managing all the informations about a job in a share with directories whose name is starting with job number. Under this entry point we have a standard structure of folders, comprising a "communications" folder. When we send emails... (0 Replies)
Discussion started by: vroby67
0 Replies

4. UNIX for Advanced & Expert Users

how to make a full system backup excluding data and restoring it to a new system

Hi, In order to have a sand box machine that I could use to test some system changes before going to production state, I'd like to duplicate a working system to a virtual one. Ideally, I'd like to manage to do it this way : - Make a full system backup excluding the user file system (this... (7 Replies)
Discussion started by: pagaille
7 Replies

5. Fedora

Read only disks on Linux system

Hi guys I have a SSL server that is running Fedora 9. I wanted to create a directory but get: mkdir: cannot create directory `test': Read-only file system Any ideas? (4 Replies)
Discussion started by: wbdevilliers
4 Replies

6. Shell Programming and Scripting

Shell Script to continuously scan a log file

Hello members, I have some doubts on how to write a script that can reports success / failure of a batch job ? 1. Run a batch job: 2. Wait and search for a particular string in the Log file: tail -f log01*.txt | egrep -v "^SUCCESSFUL" echo "continue with the other tasks" ... (1 Reply)
Discussion started by: novice82
1 Replies

7. UNIX for Advanced & Expert Users

Using newfs to make file system on a sata disk using Acard adaptor

Hi All: I am using an adaptor between a 1TB SATA hard drive and solaris 8 box with 68 pin scsi. I use the format utility to partition the HD which works fine but when I use newfs, I get some errors. I will place them below. I have blocked and the error message is in red. Anybody got any... (1 Reply)
Discussion started by: mndavies
1 Replies

8. AIX

system disks on aix 5.3

hello i'm running on P570 box aix 5.3 8 cpus 24G ram there are 1850 users loged in to this box the problem is that the two sysytem disks busy all the time hdisk0 100% busy hdisk1 100% busy some one have an idea what writing to this disks? thanks ariec (9 Replies)
Discussion started by: ariec
9 Replies

9. UNIX for Dummies Questions & Answers

How To Make Stealth Scan

hi all can anybdy plz tell me how to make a stealth port scan in unix c. if i want to send *just* ack/fin etc how do i send? using libnet or what? thankx (7 Replies)
Discussion started by: ambar
7 Replies
Login or Register to Ask a Question