#!/bin/bash if ! [ -e "/.writable_devices" ]; then echo "/.writable_devices not found" exit 1 fi mapfile -t writable_devices < /.writable_devices declare -rg ntfs_list="/run/openslx/.thin-ntfs-candidates" . slx-tools if [ -s "$ntfs_list" ] || [[ "${#writable_devices[@]}" -gt 1 ]]; then # More than one device, and/or NTFS space, need linear echo "Have more than one writable device, creating linear target" tbl="/run/openslx/dmsetup-linear-id44" pos=0 grow_max_sz=9999999999 for dev in "${writable_devices[@]}"; do max="$(( grow_max_sz - pos ))" (( max <= 0 )) && break sz="$( blockdev --getsz "$dev" )" (( sz > 0 )) || continue (( sz > max )) && sz="$max" echo "$pos $sz linear $dev 0" (( pos += sz )) done > "$tbl" if [ -s "$ntfs_list" ]; then sum= while read -r sum dev _ || [ -n "$sum" ]; do # each dev echo "Appending NTFS partition $dev..." word= while read -r word range_start_b _ range_sz _ || [ -n "$word" ]; do # each slice of dev [ "$word" = "Range" ] || continue (( range_sz > 0 )) || continue slice_sz="$(( grow_max_sz - pos ))" (( slice_sz <= 0 )) && break (( slice_sz > range_sz )) && slice_sz="$range_sz" # Append line if echo "$pos $slice_sz linear $dev $range_start_b" >> "$tbl"; then # Update counter (( pos += slice_sz )) else echo "Could not write new table row into $tbl" fi done < <( ntfsfree --block-size 512 --min-size "$(( 256 * 1024 * 1024 ))" "$dev" ) done < "$ntfs_list" # Don't try to add NTFS space again later sed -i "s/^SLX_NTFSFREE.*$/# & # disabled in stage3\nSLX_NTFSFREE='never'/" "/etc/openslx" rm -f -- "$ntfs_list" fi # See if we need a linear target at all if ! [ -s "$tbl" ]; then echo "Empty tmp/id44 table, fallback to RAM" elif [ "$( wc -l < "$tbl" )" -eq 1 ] && [[ "${#writable_devices[@]}" -ge 1 ]]; then # Only one line, have writable device -> use directly echo "Table somehow ended up with one entry, discarding" writable_device="${writable_devices[0]}" else # set up linear device echo "Setting up linear id44 device with $( wc -l < "$tbl" ) slices" if ! dmsetup_create_noudevsync "id44-group" < "$tbl"; then echo "Error creating group of writable devices. Fallback to RAM :-(" else writable_device="/dev/mapper/id44-group" fi fi else # Single device echo "Have a single writable device, using it directly" writable_device="${writable_devices[0]}" fi if [ -z "$writable_device" ]; then echo "Could not find any suitable writable devices." fi echo "$writable_device" > "/.writable_device" exit 0