| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh |
| 4 |
type udev_pending >/dev/null 2>&1 || . /lib/kiwi-lib.sh |
| 5 |
|
| 6 |
function lvm_system { |
| 7 |
declare kiwi_lvm=${kiwi_lvm} |
| 8 |
if [ "${kiwi_lvm}" = "true" ];then |
| 9 |
return 0 |
| 10 |
fi |
| 11 |
return 1 |
| 12 |
} |
| 13 |
|
| 14 |
function setup_lvm_config { |
| 15 |
mkdir -p /etc/lvm |
| 16 |
cat > /etc/lvm/lvm.conf <<- EOF |
| 17 |
global { |
| 18 |
locking_type = 0 |
| 19 |
use_lvmetad = 0 |
| 20 |
} |
| 21 |
EOF |
| 22 |
} |
| 23 |
|
| 24 |
function activate_volume_group { |
| 25 |
declare kiwi_lvmgroup=${kiwi_lvmgroup} |
| 26 |
local vg_count=0 |
| 27 |
local vg_name |
| 28 |
if lvm_system;then |
| 29 |
for vg_name in $(vgs --noheadings -o vg_name 2>/dev/null);do |
| 30 |
if [ "${vg_name}" = "${kiwi_lvmgroup}" ];then |
| 31 |
vg_count=$((vg_count + 1)) |
| 32 |
fi |
| 33 |
done |
| 34 |
if [ ${vg_count} -gt 1 ];then |
| 35 |
die "Duplicate VolumeGroup name ${kiwi_lvmgroup} found !" |
| 36 |
fi |
| 37 |
vgchange -a y "${kiwi_lvmgroup}" |
| 38 |
udev_pending |
| 39 |
fi |
| 40 |
} |
| 41 |
|
| 42 |
function deactivate_volume_group { |
| 43 |
declare kiwi_lvmgroup=${kiwi_lvmgroup} |
| 44 |
vgchange -a n "${kiwi_lvmgroup}" |
| 45 |
udev_pending |
| 46 |
} |
| 47 |
|
| 48 |
function create_volume { |
| 49 |
declare kiwi_lvmgroup=${kiwi_lvmgroup} |
| 50 |
local volume_name=$1 |
| 51 |
local volume_mb_size=$2 |
| 52 |
if [ ! -f "$(get_volume_path_for_volume "${volume_name}")" ];then |
| 53 |
lvcreate --yes --wipesignatures y \ |
| 54 |
--size "${volume_mb_size}M" -n "${volume_name}" "${kiwi_lvmgroup}" |
| 55 |
fi |
| 56 |
} |
| 57 |
|
| 58 |
function get_volume_path_for_volume { |
| 59 |
declare kiwi_lvmgroup=${kiwi_lvmgroup} |
| 60 |
echo "/dev/${kiwi_lvmgroup}/$1" |
| 61 |
} |
| 62 |
|
| 63 |
function resize_pyhiscal_volumes { |
| 64 |
local device |
| 65 |
device=$(get_root_map) |
| 66 |
set_device_lock "${device}" pvresize "${device}" |
| 67 |
} |
| 68 |
|
| 69 |
function resize_lvm_volumes_and_filesystems { |
| 70 |
declare kiwi_lvmgroup=${kiwi_lvmgroup} |
| 71 |
local volume_name |
| 72 |
local resize_mode |
| 73 |
local resize_prefix |
| 74 |
local volume_size |
| 75 |
local volspec |
| 76 |
local all_free_volume |
| 77 |
for volspec in $(read_volume_setup "/.profile" "skip_all_free_volume");do |
| 78 |
resize_prefix='' |
| 79 |
volume_name=$(get_volume_name "${volspec}") |
| 80 |
resize_mode=$(get_volume_size_mode "${volspec}") |
| 81 |
volume_size=$(get_volume_size "${volspec}") |
| 82 |
info "Resizing volume ${volume_name}..." |
| 83 |
if [ ! "${resize_mode}" = "size" ];then |
| 84 |
resize_prefix=+ |
| 85 |
fi |
| 86 |
if lvextend -L "${resize_prefix}${volume_size}M" \ |
| 87 |
"/dev/${kiwi_lvmgroup}/${volume_name}" |
| 88 |
then |
| 89 |
resize_filesystem "/dev/${kiwi_lvmgroup}/${volume_name}" |
| 90 |
else |
| 91 |
warn "Warning: requested size cannot be reached !" |
| 92 |
fi |
| 93 |
done |
| 94 |
all_free_volume=$(get_all_free_volume) |
| 95 |
if [ -n "${all_free_volume}" ];then |
| 96 |
if lvextend -l +100%FREE "/dev/${kiwi_lvmgroup}/${all_free_volume}";then |
| 97 |
resize_filesystem "/dev/${kiwi_lvmgroup}/${all_free_volume}" |
| 98 |
fi |
| 99 |
fi |
| 100 |
} |
| 101 |
|
| 102 |
function read_volume_setup { |
| 103 |
# """ |
| 104 |
# read the volume setup from the profile file and return |
| 105 |
# a list with the following values: |
| 106 |
# |
| 107 |
# volume_name,resize_mode,requested_size,mount_point ... |
| 108 |
# """ |
| 109 |
local profile=$1 |
| 110 |
local skip_all_free_volume=$2 |
| 111 |
local volume |
| 112 |
local name |
| 113 |
local mode |
| 114 |
local size |
| 115 |
local mpoint |
| 116 |
local result |
| 117 |
local volspec |
| 118 |
result=$( |
| 119 |
while read -r volspec;do |
| 120 |
if ! [[ "${volspec}" =~ "kiwi_Volume_" ]];then |
| 121 |
continue |
| 122 |
fi |
| 123 |
volume=$(echo "${volspec}" | cut -f2 -d= | tr -d \' | tr -d \") |
| 124 |
size=$(echo "${volume}" | cut -f2 -d\| | cut -f2 -d:) |
| 125 |
if [ "${size}" = "all" ] && [ -n "${skip_all_free_volume}" ];then |
| 126 |
continue |
| 127 |
fi |
| 128 |
mode=$(echo "${volume}" | cut -f2 -d\| | cut -f1 -d:) |
| 129 |
name=$(echo "${volume}" | cut -f1 -d\|) |
| 130 |
mpoint=$(echo "${volume}" | cut -f3 -d\|) |
| 131 |
if [ -z "${mpoint}" ];then |
| 132 |
mpoint=noop |
| 133 |
fi |
| 134 |
echo "${name},${mode},${size},${mpoint}" |
| 135 |
done < "${profile}" |
| 136 |
) |
| 137 |
echo "${result}" |
| 138 |
} |
| 139 |
|
| 140 |
function read_volume_setup_all_free { |
| 141 |
# """ |
| 142 |
# read the volume setup from the profile file and |
| 143 |
# lookup the volume that should take the rest space |
| 144 |
# available in the volume group. |
| 145 |
# |
| 146 |
# return a list with the following values: |
| 147 |
# |
| 148 |
# volume_name,mount_point |
| 149 |
# |
| 150 |
# If no @root volume is configured in the kiwi XML |
| 151 |
# description the default LVRoot volume will be used |
| 152 |
# to take the rest space. |
| 153 |
# """ |
| 154 |
local profile=$1 |
| 155 |
local size |
| 156 |
local volume |
| 157 |
local name |
| 158 |
local mpoint |
| 159 |
while read -r volspec;do |
| 160 |
if ! [[ "${volspec}" =~ "kiwi_Volume_" ]];then |
| 161 |
continue |
| 162 |
fi |
| 163 |
volume=$(echo "${volspec}" | cut -f2 -d= | tr -d \' | tr -d \") |
| 164 |
size=$(echo "${volume}" | cut -f2 -d\| | cut -f2 -d:) |
| 165 |
name=$(echo "${volume}" | cut -f1 -d\|) |
| 166 |
if [ "${size}" = "all" ];then |
| 167 |
mpoint=$(echo "${volume}" | cut -f3 -d\|) |
| 168 |
echo "${name},${mpoint}" |
| 169 |
return |
| 170 |
fi |
| 171 |
done < "${profile}" |
| 172 |
} |
| 173 |
|
| 174 |
function get_all_free_volume { |
| 175 |
read_volume_setup_all_free "/.profile" | cut -f1 -d, |
| 176 |
} |
| 177 |
|
| 178 |
function get_volume_name { |
| 179 |
echo "$1" | cut -f1 -d, |
| 180 |
} |
| 181 |
|
| 182 |
function get_volume_mount_point { |
| 183 |
echo "$1" | cut -f4 -d, |
| 184 |
} |
| 185 |
|
| 186 |
function get_volume_size_mode { |
| 187 |
echo "$1" | cut -f2 -d, |
| 188 |
} |
| 189 |
|
| 190 |
function get_volume_size { |
| 191 |
echo "$1" | cut -f3 -d, |
| 192 |
} |