Skip to content

Commit

Permalink
Add get_part_device_name_format() function
Browse files Browse the repository at this point in the history
  • Loading branch information
schabrolles committed Aug 30, 2017
1 parent 63a01df commit 14c0626
Showing 1 changed file with 69 additions and 84 deletions.
153 changes: 69 additions & 84 deletions usr/share/rear/lib/layout-functions.sh
Expand Up @@ -665,13 +665,13 @@ function UdevQueryName() {
### apply-mappings function
#
# Functions used in apply-mappings() function
add_replacement() {
function add_replacement() {
echo "$1 _REAR${replaced_count}_" >> "$replacement_file"
let replaced_count++
}
#
# Functions used in apply-mappings() function
has_replacement() {
function has_replacement() {
if grep -q "^$1 " "$replacement_file" ; then
return 0
else
Expand All @@ -680,21 +680,80 @@ has_replacement() {
}
#
# Functions used in apply-mappings() function
get_replacement() {
function get_replacement() {
local item replacement junk
read item replacement junk < <(grep "^$1 " $replacement_file)
echo "$replacement"
}
# Guess the part device name from a device, based on the OS distro Level.
function get_part_device_name_format() {
if [ -z $1 ] ; then
BugError "get_part_device_name_format function called without argument (device)"
else
device_name="$1"
fi

part_name="$device_name"

case "$device_name" in
(*mmcblk[0-9]*|*nvme[0-9]*n[1-9]*|*rd[/!]c[0-9]*d[0-9]*|*cciss[/!]c[0-9]*d[0-9]*|*ida[/!]c[0-9]*d[0-9]*|*amiraid[/!]ar[0-9]*|*emd[/!][0-9]*|*ataraid[/!]d[0-9]*|*carmel[/!][0-9]*)
part_name="${device_name}p" # append p between main device and partitions
;;
(*mapper[/!]*)
case $OS_MASTER_VENDOR in

(SUSE)
# SUSE Linux SLE12 put a "-part" between [mpath device name] and [part number].
# For example /dev/mapper/3600507680c82004cf8000000000000d8-part1.
# But SLES11 uses a "_part" instead. (Let's assume it is the same for SLES10 )
if (( $OS_MASTER_VERSION < 12 )) ; then
# For SUSE before version 12
part_name="${device_name}_part" # append _part between main device and partitions
else
# For SUSE 12 or above
part_name="${device_name}-part" # append -part between main device and partitions
fi
;;

(Fedora)
# RHEL 7 and above seems to named partitions on multipathed devices with
# [mpath device name] + [part number] like standard disk.
# For example: /dev/mapper/mpatha1

# But the scheme in RHEL 6 need a "p" between [mpath device name] and [part number].
# For exemple: /dev/mapper/mpathap1
if (( $OS_MASTER_VERSION < 7 )) ; then
part_name="${device_name}p" # append p between main device and partitions
fi
;;

(Debian)
# Ubuntu 16.04 (need to check for other version) named muiltipathed partitions with
# [mpath device name] + "-part" + [part number]
# for example : /dev/mapper/mpatha-part1
part_name="${device_name}-part" # append -part between main device and partitions
;;

(*)
# For all the other case, use /dev/mapper/mpatha1 type
part_name="$device_name"
;;
esac
;;
esac

echo "$part_name"
}
#
# apply-mappings function migrate disk device reference from an old system and
# replace them with new one (from current system).
# the relationship between OLD and NEW device is provided by $MAPPING_FILE
# (usually disk_mappings file in $VAR_DIR).
apply-mappings() {
function apply-mappings() {

# apply-mappings need one argument (file which contains disk device to migrate).
if [ -z "$1" ] ; then
LogError "apply-mappings function called without argument (file_to_migrate)"
if [ -z $1 ] ; then
BugError "apply-mappings function called without argument (file_to_migrate)"
else
file_to_migrate="$1"
fi
Expand Down Expand Up @@ -722,47 +781,9 @@ apply-mappings() {
# Replace all originals with their replacements.
while read original replacement junk ; do
# Replace partitions (we normalize cciss/c0d0p1 to _REAR5_1)
part_base="$original"
case "$original" in
*mmcblk[0-9]*|*nvme[0-9]*n[1-9]*|*rd[/!]c[0-9]*d[0-9]*|*cciss[/!]c[0-9]*d[0-9]*|*ida[/!]c[0-9]*d[0-9]*|*amiraid[/!]ar[0-9]*|*emd[/!][0-9]*|*ataraid[/!]d[0-9]*|*carmel[/!][0-9]*)
part_base="${original}p" # append p between main device and partitions
;;
*mapper[/!]*)
case $OS_VENDOR in
SUSE_LINUX)
# SUSE Linux SLE12 put a "-part" between [mpath device name] and [part number].
# For example /dev/mapper/3600507680c82004cf8000000000000d8-part1.
# But SLES11 uses a "_part" instead. (Let's assume it is the same for SLES10 )
if (( $OS_VERSION < 12 )) ; then
# For SUSE before version 12
part_base="${original}_part" # append _part between main device and partitions
else
# For SUSE 12 or above
part_base="${original}-part" # append -part between main device and partitions
fi
;;
RedHatEnterpriseServer)
# RHEL 7 and above seems to named partitions on multipathed devices with
# [mpath device name] + [part number] like standard disk.
# For example: /dev/mapper/mpatha1

# But the scheme in RHEL 6 need a "p" between [mpath device name] and [part number].
# For exemple: /dev/mapper/mpathap1
if (( $OS_VERSION < 7 )) ; then
part_base="${original}p" # append p between main device and partitions
fi
;;
Ubuntu)
# Ubuntu 16.04 (need to check for other version) named muiltipathed partitions with
# [mapth device name] + "-part" + [part number]
# for example : /dev/mapper/mpatha-part1
part_base="${original}-part" # append -part between main device and partitions
;;

esac
;;
esac
part_base=$(get_part_device_name_format "$original")
sed -i -r "\|$original|s|${part_base}([0-9]+)|$replacement\1|g" "$file_to_migrate"

# Replace whole devices
### note that / is a word boundary, so is matched by \<, hence the extra /
sed -i -r "\|$original|s|/\<${original#/}\>|${replacement}|g" "$file_to_migrate"
Expand All @@ -773,45 +794,9 @@ apply-mappings() {
replacement=$(get_replacement "$source")
# Replace whole device
sed -i -r "\|$replacement|s|$replacement\>|$target|g" "$file_to_migrate"

# Replace partitions
case "$target" in
*mmcblk[0-9]|*nvme[0-9]*n[1-9]|*rd[/!]c[0-9]d[0-9]|*cciss[/!]c[0-9]d[0-9]|*ida[/!]c[0-9]d[0-9]|*amiraid[/!]ar[0-9]|*emd[/!][0-9]|*ataraid[/!]d[0-9]|*carmel[/!][0-9])
target="${target}p" # append p between main device and partitions
;;
*mapper[/!]*)
case $OS_VENDOR in
SUSE_LINUX)
# SUSE Linux SLE12 put a "-part" between [mpath device name] and [part number].
# For example /dev/mapper/3600507680c82004cf8000000000000d8-part1.
# But SLES11 uses a "_part" instead. (Let's assume it is the same for SLES10 )
if (( $OS_VERSION < 12 )) ; then
# For SUSE before version 12
target="${target}_part" # append _part between main device and partitions
else
# For SUSE 12 or above
target="${target}-part" # append -part between main device and partitions
fi
;;
RedHatEnterpriseServer)
# RHEL 7 and above seems to named partitions on multipathed devices with
# [mpath device name] + [part number] like standard disk.
# For example: /dev/mapper/mpatha1

# But the scheme in RHEL 6 need a "p" between [mpath device name] and [part number].
# For exemple: /dev/mapper/mpathap1
if (( $OS_VERSION < 7 )) ; then
target="${target}p" # append p between main device and partitions
fi
;;
Ubuntu)
# Ubuntu 16.04 (need to check for other version) named muiltipathed partitions with
# [mapth device name] + "-part" + [part number]
# for example : /dev/mapper/mpatha-part1
target="${target}-part" # append -part between main device and partitions
;;
esac
;;
esac
target=$(get_part_device_name_format "$target")
sed -i -r "\|$replacement|s|$replacement([0-9]+)|$target\1|g" "$file_to_migrate"
done < "$MAPPING_FILE"
}

0 comments on commit 14c0626

Please sign in to comment.