Skip to content
Snippets Groups Projects
Commit 5daea406 authored by Jérôme Charaoui's avatar Jérôme Charaoui
Browse files

sys: improve device/partition discovery

parent bb609a43
No related branches found
No related tags found
No related merge requests found
...@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [core] wrap report email body to 1000 characters by default (DEBBUG-871793) - [core] wrap report email body to 1000 characters by default (DEBBUG-871793)
- [core] improve error handling around `reporthost` feature - [core] improve error handling around `reporthost` feature
- [docs] add missing parameters to `backupninja.conf` manpage - [docs] add missing parameters to `backupninja.conf` manpage
- [sys] improve device selection for MBR backup (#11303)
## [1.2.0] - 2021-01-21 ## [1.2.0] - 2021-01-21
......
...@@ -554,24 +554,39 @@ fi ...@@ -554,24 +554,39 @@ fi
## PARTITIONS ############################# ## PARTITIONS #############################
# here we use sfdisk to dump a listing of all the partitions. if [ "$partitions" == "yes" ] || [ "$luksheaders" == "yes" ] || [ "$mbr" == "yes" ]; then
# these files can be used to directly partition a disk of the same size. # get a list of block devices on the system
debug "LC_ALL=C $LSBLK --output NAME,TYPE --list --paths 2>/dev/null | grep \"disk$\" | grep -v '^/dev/zram' | @AWK@ '{print \$1}'"
if [ "$partitions" == "yes" ]; then
if [ "$dosfdisk" == "yes" ]; then
devices=`LC_ALL=C $LSBLK --output NAME,TYPE --list --paths 2>/dev/null | grep "disk$" | grep -v '^/dev/zram' | @AWK@ '{print $1}'` devices=`LC_ALL=C $LSBLK --output NAME,TYPE --list --paths 2>/dev/null | grep "disk$" | grep -v '^/dev/zram' | @AWK@ '{print $1}'`
partitions=`LC_ALL=C $SFDISK -l 2>/dev/null |grep "^/dev" | @AWK@ '{print $1}'`
if [ "$devices" == "" ]; then if [ "$devices" == "" ]; then
warning "No harddisks found" warning "Unable to find any block devices on this system."
else
info "$(echo "Devices found: $devices" | tr "\n" " ")"
fi fi
# get a list of block device partitions on the system
debug "LC_ALL=C $SFDISK -l 2>/dev/null | grep \"^/dev\" | @AWK@ '{print \$1}'"
devparts=`LC_ALL=C $SFDISK -l 2>/dev/null | grep "^/dev" | @AWK@ '{print $1}'`
if [ "$devparts" == "" ]; then
info "No partitions found on this system."
else
info "$(echo "Partitions found: $partitions" | tr "\n" " ")"
fi
fi
if [ "$partitions" == "yes" ]; then
if [ "$dosfdisk" == "yes" ]; then
for dev in $devices; do for dev in $devices; do
debug "$SFDISK will try to backup partition tables for device $dev"
[ -b $dev ] || continue [ -b $dev ] || continue
echo "${partitions}" | grep -q "${dev}" if ! echo "${devparts}" | grep -q "${dev}"; then
if [ $? -ne 0 ] ; then
info "The device $dev does not appear to have any partitions" info "The device $dev does not appear to have any partitions"
continue continue
fi fi
# here we use sfdisk to dump a listing of all the partitions.
# these files can be used to directly partition a disk of the same size.
debug "$SFDISK will try to backup partition tables for device $dev"
label=${dev#/dev/} label=${dev#/dev/}
label=${label//\//-} label=${label//\//-}
outputfile=${partitionsfile//__star__/$label} outputfile=${partitionsfile//__star__/$label}
...@@ -590,24 +605,13 @@ if [ "$partitions" == "yes" ]; then ...@@ -590,24 +605,13 @@ if [ "$partitions" == "yes" ]; then
fi fi
if [ "$luksheaders" == "yes" ]; then if [ "$luksheaders" == "yes" ]; then
devices=`LC_ALL=C $LSBLK --output NAME,TYPE --list --paths 2>/dev/null | grep "disk$" | grep -v '^/dev/zram' | @AWK@ '{print $1}'` for dev in $devices $devparts; do
if [ "$devices" == "" ]; then
warning "No harddisks found"
fi
partitions=`LC_ALL=C $SFDISK -l 2>/dev/null |grep "^/dev" | @AWK@ '{print $1}'`
[ -n "$partitions" ] || warning "No partitions found"
targetdevices=""
for dev in $devices $partitions; do
[ -b $dev ] || continue [ -b $dev ] || continue
debug "$CRYPTSETUP isLuks $dev" if $CRYPTSETUP isLuks $dev; then
$CRYPTSETUP isLuks $dev
[ $? -eq 0 ] && targetdevices="$targetdevices $dev"
done
for dev in $targetdevices; do
label=${dev#/dev/} label=${dev#/dev/}
label=${label//\//-} label=${label//\//-}
outputfile=${luksheadersfile//__star__/$label} outputfile=${luksheadersfile//__star__/$label}
debug "Let us backup the LUKS header of $dev" debug "$CRYPTSETUP will try to backup the LUKS header for device $dev"
debug "$CRYPTSETUP luksHeaderBackup \"${dev}\" --header-backup-file \"${outputfile}\"" debug "$CRYPTSETUP luksHeaderBackup \"${dev}\" --header-backup-file \"${outputfile}\""
output=`$CRYPTSETUP luksHeaderBackup "${dev}" --header-backup-file "${outputfile}" 2>&1` output=`$CRYPTSETUP luksHeaderBackup "${dev}" --header-backup-file "${outputfile}" 2>&1`
exit_code=$? exit_code=$?
...@@ -618,17 +622,15 @@ if [ "$luksheaders" == "yes" ]; then ...@@ -618,17 +622,15 @@ if [ "$luksheaders" == "yes" ]; then
debug "$output" debug "$output"
fatal "The LUKS header of $dev could not be saved." fatal "The LUKS header of $dev could not be saved."
fi fi
fi
done done
fi fi
if [ "$mbr" == "yes" ]; then if [ "$mbr" == "yes" ]; then
devices=`LC_ALL=C $SFDISK -l 2>/dev/null | grep "^Disk /dev" | @AWK@ '{print $2}' | cut -d: -f1`
if [ "$devices" == "" ]; then
warning "No harddisks found"
fi
for dev in $devices; do for dev in $devices; do
debug "Will try to backup MBR tables for device $dev"
[ -b $dev ] || continue [ -b $dev ] || continue
if $SFDISK -d $dev 2>/dev/null | head -n1 | grep "label: dos"; then
debug "$SFDISK will try to backup MBR tables for device $dev"
label=${dev#/dev/} label=${dev#/dev/}
label=${label//\//-} label=${label//\//-}
outputfile=${mbrfile//__star__/$label} outputfile=${mbrfile//__star__/$label}
...@@ -637,6 +639,9 @@ if [ "$mbr" == "yes" ]; then ...@@ -637,6 +639,9 @@ if [ "$mbr" == "yes" ]; then
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
warning "The MBR for $dev could not be saved." warning "The MBR for $dev could not be saved."
fi fi
else
info "The device $dev does not appear to contain an MBR."
fi
done done
fi fi
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment