summaryrefslogblamecommitdiffstats
path: root/builder/modules.d/dnbd3-rootfs/hooks/prepare-root-partition.sh
blob: 2d9e059c8510227a984b98f61e34839fe6747d95 (plain) (tree)
1
2
3
4
5
6
7
8
9
                   
                
                                
                                       
                      

                   
                                                                 
           

              
                   

                                

                                                                      
 
                            
                 
        
             
                                    
                                         
                                                                            
                                                           
                                    
                     

             

                 
 
                                


                                                                       
          
  
           
                                       






                                                                     
                                            
                                    
 
           
                                                 
                                                          
                                                      
                                   


                                               
                                                     
                                                                    
      
    






                                                                    
  
                                                              
 


                                         
                                       
           
 


                
                                              

                                                





                                                     
#!/usr/bin/env bash
# region imports
source '/usr/lib/rebash/core.sh'
core.import '/usr/lib/openslx/tools.sh'
core.import exceptions
core.import utils
core.import logging
type emergency_shell >/dev/null 2>&1 || source /lib/dracut-lib.sh
# endregion
exceptions.try
{
source /etc/openslx
logging.set_commands_level debug
logging.set_level debug
[[ "$SLX_LOG_FILE_PATH" == "" ]] && SLX_LOG_FILE_PATH=/var/log/openslx
logging.set_log_file "$SLX_LOG_FILE_PATH"

# region connect dnbd3 image
IFS_backup="$IFS"
IFS=", "
return_code=1
for host in ${SLX_DNBD3_SERVERS}; do
    logging.info "Trying host \"$host\"."
    if systemd-preserve-process-marker dnbd3-client --host "$host" --image \
        "${SLX_DNBD3_IMAGE}" --device "$SLX_DNBD3_DEVICE" \
        --rid "$SLX_DNBD3_RID"; then
        return_code=0
        break
    fi
done
IFS="$IFS_backup"

if [[ $return_code != 0 ]]; then
    logging.warn "Failed to connect \"${SLX_DNBD3_IMAGE}\" (revision" \
        "\"$SLX_DNBD3_RID\") from one of \"$SLX_DNBD3_SERVERS\" to" \
        "\"$SLX_DNBD3_DEVICE\"."
    exit 1
fi
# endregion
# region unpack dnbd3 image with xmount
if [ "$SLX_LOG_FILE_PATH" != "" ]; then
    read_only_device="$(container-unpack-xmount "$SLX_DNBD3_DEVICE" \
        2>>"$SLX_LOG_FILE_PATH")"
else
    read_only_device="$(container-unpack-xmount "$SLX_DNBD3_DEVICE")"
fi

# Fail fast if unpacking dnbd3 image failed.
[ -z "$read_only_device" ] && exit 1

# endregion
# region find system partition within dnbd3 image
if [ -z "$SLX_SYSTEM_PARTITION_PREPARATION_SCRIPT" ]; then
    if [ -z "$SLX_SYSTEM_PARTITION_IDENTIFIER" ]; then
        # if empty use whole device
        read_only_partition="$read_only_device"
        true
    else
        read_only_partition="$(get-partitions-by-id \
            "$read_only_device" "$SLX_SYSTEM_PARTITION_IDENTIFIER")"
    fi
else
    eval "$SLX_SYSTEM_PARTITION_PREPARATION_SCRIPT"
fi
if [[ ! $? || -z "$read_only_partition" ]]; then
    logging.error "Failed to find unique device with identifier" \
        "\"${SLX_SYSTEM_PARTITION_IDENTIFIER}\"; matched devices:" \
        "\"${read_only_partition}\""
    exit 1
fi
logging.info "Using read-only partition: $read_only_partition"

# endregion

# region RW layer through slx-partitioner
dmsetup-slx-device $read_only_partition
# endregion

}
exceptions.catch
{
    logging.error "$exceptions_last_traceback"
    emergency_shell "error in ${BASH_SOURCE[0]}"
}
# region vim modline

# vim: set tabstop=4 shiftwidth=4 expandtab:
# vim: foldmethod=marker foldmarker=region,endregion:

# endregion