mirror of
				https://github.com/xcat2/xcat-core.git
				synced 2025-10-25 00:15:43 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			168 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
			
		
		
	
	
			168 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
| #!/bin/bash
 | |
| 
 | |
| #-----------------------------------------------------------
 | |
| #
 | |
| # Get proper disk to install OS
 | |
| #
 | |
| # 1. Check all partitions list in /proc/partitions, whether 
 | |
| #    there is disk had OS installed. Select the one with the
 | |
| #    smallest WWN.
 | |
| # 2. Check all disks list in /proc/partitions, sort them by
 | |
| #    WWN and driver type, select the first one.
 | |
| # 3. Select the default one: /dev/sda.   
 | |
| #
 | |
| # Output: /tmp/install_disk
 | |
| #
 | |
| #-----------------------------------------------------------
 | |
| 
 | |
| install_disk=""
 | |
| rm /tmp/install_disk
 | |
| tmpdir="/tmp/xcat.getinstalldisk"
 | |
| mkdir -p $tmpdir
 | |
| logfile="/tmp/xcat_getinstalldisk_log"
 | |
| tmpfile=$tmpdir"/getinstalldisk_"
 | |
| 
 | |
| # Check if any disk have installed OS
 | |
| if [ -z "$install_disk" ]; then
 | |
| 
 | |
|     entries=$(awk -F ' '  '{print $4}' /proc/partitions | grep -v "name")
 | |
|     
 | |
|     for entry in $entries; do
 | |
| 
 | |
|         dev_type=$(udevadm info --query=property --name=/dev/$entry | grep -i "DEVTYPE" | awk -F = '{print $2}' | tr A-Z a-z)
 | |
|         
 | |
|         if [ "$dev_type" == "disk" ]; then
 | |
|             disks=$disks" $entry"
 | |
|         elif [ "$dev_type" == "partition" ]; then
 | |
|             partitions=$partitions" $entry"
 | |
|         fi
 | |
| 
 | |
|     done
 | |
| 
 | |
|     mount_dir=$tmpdir"/xcat.getinstalldisk.mount"
 | |
|     mkdir -p $mount_dir;
 | |
| 
 | |
|     for partition in $partitions; do
 | |
| 
 | |
|         echo "Check the partition $partition." >> $logfile
 | |
| 
 | |
|         if [ -e "$tmpfile${partition%%[0-9]*}" ]; then
 | |
|             echo "    The disk ${partition%%[0-9]*} had OS installed, check next partition." >> $logfile
 | |
|             continue
 | |
|         fi
 | |
| 
 | |
|         fs_type=$(udevadm info --query=property --name=/dev/$partition | grep -i "FS_TYPE" | awk -F = '{print $2}')
 | |
| 
 | |
|         rc=255
 | |
| 
 | |
|         # mount partition based on fs type, if fs_type is swap, do not mount it, jump to next partition.
 | |
|         if [ -z "$fs_type" ]; then
 | |
|             mount /dev/$partition $mount_dir
 | |
|             rc=$?
 | |
|         elif [ "$fs_type" != "swap" ]; then
 | |
|             mount -t $fs_type /dev/$partition $mount_dir
 | |
|             rc=$?
 | |
|         fi
 | |
| 
 | |
|         if [ $rc -eq 0 ]; then
 | |
|       
 | |
|             haskernel=0
 | |
| 
 | |
|             echo "    Partition $partition mount success." >> $logfile
 | |
| 
 | |
|             ker_dir=$mount_dir            
 | |
|             
 | |
|             if [ -d "$mount_dir/boot" ]; then
 | |
|                 ker_dir="$mount_dir/boot"
 | |
|             fi
 | |
| 
 | |
|             for i in $ker_dir/vmlinuz*; do
 | |
|                 disk_part=${partition%%[0-9]*}
 | |
|                 disk_wwn=$(udevadm info --query=property --name=$disk_part | grep '\<ID_WWN\>' | cut -d "=" -f2 | tr A-Z a-z)
 | |
|                 disk_array=$disk_array"$disk_part $disk_wwn\n"
 | |
|                 touch "$tmpfile$disk_part"
 | |
|                 echo -e "    The partition $partition has kernel file." >> $logfile
 | |
|                 haskernel=1
 | |
|                 break
 | |
|             done
 | |
|         
 | |
|             if [ $haskernel -ne 1 ]; then
 | |
|                 echo "    The partition $partition does not have kernel file." >> $logfile
 | |
|             fi
 | |
|              
 | |
|             umount $mount_dir || echo "    $partition umount failed." >> $logfile
 | |
|         else
 | |
|             echo "    Partition $partition mount failed or the partition is swap." >> $logfile
 | |
|         fi
 | |
|     done
 | |
| 
 | |
|     if [ "$disk_array" ]; then    
 | |
|         echo -e "The disk_array:" >> $logfile
 | |
|         echo -e "    $disk_array" >> $logfile
 | |
| 
 | |
|         install_disk=/dev/$(echo -e $disk_array | grep -v "^$" | sort -t : -k 2 -b | cut -d " " -f1 | head -n 1)
 | |
|    
 | |
|         echo -e "The install_disk is $install_disk." >> $logfile
 | |
|     fi
 | |
|     rmdir $mount_dir;
 | |
|     rm $tmpfile*;
 | |
| fi
 | |
| 
 | |
| # Sort all disks based on their WWN and driver type, choose the first one
 | |
| if [ -z "$install_disk" ]; then
 | |
|     
 | |
|     for disk in $disks; do
 | |
| 
 | |
|         disk_wwn=$(udevadm info --query=property --name=$disk | grep '\<ID_WWN\>' | cut -d "=" -f2 | tr A-Z a-z)
 | |
|  
 | |
|         if [ "$disk_wwn" ]; then
 | |
| 
 | |
|             disk_driver=$(udevadm info --attribute-walk --name=$disk | grep DRIVERS| grep -v '""'| grep -v '"sd"'| head -n 1| sed -e 's/[^"]*"//' -e 's/"//' | tr A-Z a-z)
 | |
| 
 | |
|             echo -e "The disk $disk information: disk_wwn=$disk_wwn disk_driver=$disk_driver." >> $logfile
 | |
| 
 | |
|             case "$disk_driver" in
 | |
| 
 | |
|             "ata_piix4"|"PMC MaxRAID"|"ahci"|"megaraid_sas")
 | |
|                 echo "$disk $disk_wwn" >> "$tmpfile""firstdisks"
 | |
|                 echo "Add disk: $disk $driver $disk_wwn into firstdisks" >> $logfile
 | |
|                 ;;
 | |
|             "mptsas"|"mpt2sas"|"mpt3sas")
 | |
|                 echo "$disk $disk_wwn" >> "$tmpfile""seconddisks"
 | |
|                 echo "Add disk: $disk $driver $disk_wwn into seconddisks" >> $logfile
 | |
|                 ;;
 | |
|             *)
 | |
|                 echo "$disk $disk_wwn" >> "$tmpfile""thirddisks"
 | |
|                 echo "Add disk: $disk $driver $disk_wwn into thirddisks" >> $logfile
 | |
|                 ;;
 | |
|  
 | |
|             esac
 | |
|         fi
 | |
|     done
 | |
| 
 | |
|     for seq in first second third; do
 | |
|         if [ -s $tmpfile${seq}disks ]; then
 | |
|             install_file="$tmpfile${seq}disks"
 | |
|             break
 | |
|         fi
 | |
|     done
 | |
|         
 | |
| 
 | |
|     if [ "$install_file" ] && [ -s $install_file ]; then
 | |
|         install_disk=/dev/$(cat $install_file | grep -v "^$" | sort -t : -k 2 -b | cut -d " " -f1 | head -n 1)
 | |
|         echo -e "The install_disk is $install_disk by sorting WWN and DRIVER." >> $logfile
 | |
|     fi    
 | |
|     rm $tmpfile*;
 | |
| fi
 | |
| 
 | |
| rm -rf $tmpdir;
 | |
| 
 | |
| # Cannot find proper disk for OS install, select the default one "/dev/sda"
 | |
| if [ -z "$install_disk" ]; then
 | |
|     install_disk="/dev/sda"
 | |
|     echo -e "The default install_disk is $install_disk." >> $logfile
 | |
| fi
 | |
| 
 | |
| # Output the result to /tmp/install_disk file
 | |
| echo $install_disk > /tmp/install_disk
 |