Skip to content

Commit

Permalink
Split disk methods into lib/diskutils
Browse files Browse the repository at this point in the history
Signed-off-by: Yariv Rachmani <[email protected]>
  • Loading branch information
Yarboa committed Aug 29, 2024
1 parent b13e466 commit 14e5ce0
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 72 deletions.
105 changes: 105 additions & 0 deletions tests/e2e/lib/diskutils
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/bin/bash
#
# Copyright 2023 The qm Authors
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.

select_disk_to_partition(){
###########################################################################
# Description: #
# Select free partition with size in "G" #
# #
# Arguments: should be be exported by calling script #
# DEVICE, set free /dev/ device #
# PART_ID, set device partition #
###########################################################################

local disk_table
disk_table=$(lsblk --noheadings --raw)
local disks_arr
disks_arr=$(echo "$disk_table" | awk '$1~// && $6=="disk" {print $1}')
info_message "select_disk_to_partition, found ${disks_arr}"
info_message "=============================="

for disk in $disks_arr; do
if [[ ${disk} == "vda" || \
$(echo "${disk_table}" | grep -c "${disk}" ) -eq 1 && ${disk} != "zram0" ]];then
new_part="$( (echo n; echo p; echo ; echo ; echo ; echo w) | fdisk "/dev/${disk}")"
part_id="$(echo "${new_part}" | grep -Po "new partition \K([0-9])")"
DISK="${disk}"
if [[ $(echo "${disk}" | grep -c nvme) -eq 1 ]]; then
part_id=p${part_id}
PART_ID="${part_id}"
info_message "select_disk_to_partition, found /dev/${DISK}${PART_ID}"
info_message "=============================="
break;
fi
fi
if [ -e /sys/devices/soc0/machine ]; then
if grep -qi SA8775P /sys/devices/soc0/machine; then
if [[ ${disk} == "sde" ]]; then
DISK=${disk}
part=$(echo "${disk_table}" | grep "G" | grep "sde[0-9]." | grep -v "/" | cut -d" " -f1 )
PART_ID=${part:3}
break;
fi
fi
fi
done
}

create_qm_var_part() {
###########################################################################
# Description: #
# Create relevant filesystem #
# support fstype, non ostree, xfs (c9s vm), ext4(soc) #
# ARGUMENTS: should be be exported by calling script #
# DEVICE, set free /dev/ device #
# PART_ID, set device partition #
###########################################################################

select_disk_to_partition

local slash_var
slash_var="/var"

# Check is SoC aboot exist, else regular image
# VM use case
if [ -e /sys/devices/soc0/machine ]; then
if grep -qi SA8775P /sys/devices/soc0/machine; then
mkfs.ext4 "/dev/${DISK}${PART_ID}"
info_message "Create_qm_disks, prepare SoC ext4 fs"
info_message "=============================="
fi
else
mkfs.xfs "/dev/${DISK}${PART_ID}"
info_message "Create_qm_disks, prepare regular xfs fs"
info_message "=============================="

fi

mkdir -p /new_var
mount "/dev/${DISK}${PART_ID}" /new_var
rsync -aqxP /var/* /new_var
systemctl stop var-lib-nfs-rpc_pipefs.mount
umount /new_var
rm -rf "${slash_var:?}"/*
info_message "Create_qm_disks, prepare and mount ${slash_var}"
info_message "=============================="
mount "/dev/${DISK}${PART_ID}" "${slash_var}"
systemctl start var-lib-nfs-rpc_pipefs.mount
}



76 changes: 4 additions & 72 deletions tests/e2e/set-ffi-env-e2e
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ source "${SCRIPT_DIR}"/lib/container
source "${SCRIPT_DIR}"/lib/systemd
# shellcheck disable=SC1091
source "${SCRIPT_DIR}"/lib/tests
# shellcheck disable=SC1091
source "${SCRIPT_DIR}"/lib/diskutils

# GLOBALS
export CONFIG_NODE_AGENT_PATH="/etc/bluechi/agent.conf.d/agent.conf"
Expand Down Expand Up @@ -142,77 +144,6 @@ while [ $# -gt 0 ]; do
esac
done

select_disk_to_partition(){

local disk_table
disk_table=$(lsblk --noheadings --raw)
local disks_arr
disks_arr=$(echo "$disk_table" | awk '$1~// && $6=="disk" {print $1}')
info_message "select_disk_to_partition, found ${disks_arr}"
info_message "=============================="

for disk in $disks_arr; do
if [[ ${disk} == "vda" || \
$(echo "${disk_table}" | grep -c "${disk}" ) -eq 1 && ${disk} != "zram0" ]];then
new_part="$( (echo n; echo p; echo ; echo ; echo ; echo w) | fdisk "/dev/${disk}")"
part_id="$(echo "${new_part}" | grep -Po "new partition \K([0-9])")"
DISK="${disk}"
if [[ $(echo "${disk}" | grep -c nvme) -eq 1 ]]; then
part_id=p${part_id}
PART_ID="${part_id}"
info_message "select_disk_to_partition, found /dev/${DISK}${PART_ID}"
info_message "=============================="
break;
fi
fi
if [ -e /sys/devices/soc0/machine ]; then
if grep -qi SA8775P /sys/devices/soc0/machine; then
if [[ ${disk} == "sde" ]]; then
DISK=${disk}
part=$(echo "${disk_table}" | grep "G" | grep "sde[0-9]." | grep -v "/" | cut -d" " -f1 )
PART_ID=${part:3}
break;
fi
fi
fi
done
}

create_qm_disks() {

select_disk_to_partition

local slash_var
slash_var="/var"

# Check is SoC aboot exist, else regular image
# VM use case
if [ -e /sys/devices/soc0/machine ]; then
if grep -qi SA8775P /sys/devices/soc0/machine; then
mkfs.ext4 "/dev/${DISK}${PART_ID}"
info_message "Create_qm_disks, prepare SoC ext4 fs"
info_message "=============================="
fi
else
mkfs.xfs "/dev/${DISK}${PART_ID}"
info_message "Create_qm_disks, prepare regular xfs fs"
info_message "=============================="

fi

mkdir -p /new_var
mount "/dev/${DISK}${PART_ID}" /new_var
rsync -aqxP /var/* /new_var
systemctl stop var-lib-nfs-rpc_pipefs.mount
umount /new_var
rm -rf "${slash_var:?}"/*
info_message "Create_qm_disks, prepare and mount ${slash_var}"
info_message "=============================="
mount "/dev/${DISK}${PART_ID}" "${slash_var}"
systemctl start var-lib-nfs-rpc_pipefs.mount
systemctl restart qm
}

install_qm_rpms() {

info_message "Installing qm setup rpm"
Expand Down Expand Up @@ -321,7 +252,7 @@ echo
info_message "Check if qm requires additional partition"
info_message "=============================="
if [ -n "${SET_QM_PART}" ]; then
create_qm_disks
create_qm_var_part
fi

echo
Expand All @@ -330,6 +261,7 @@ info_message "=============================="
QM_STATUS="$(systemctl is-enabled qm 2>&1)"
if [ "$QM_STATUS" == "generated" ]; then
if [ "$(systemctl is-active qm)" == "active" ]; then
systemctl restart qm
info_message "QM Enabled and Active"
info_message "=============================="
exit 0
Expand Down

0 comments on commit 14e5ce0

Please sign in to comment.