Add partial support for NetBSD and OpenBSD
[snf-image] / snf-image-helper / common.sh
index 6f12104..e17ef2a 100644 (file)
@@ -32,6 +32,8 @@ BLOCKDEV=blockdev
 REGLOOKUP=reglookup
 CHNTPW=chntpw
 SGDISK=sgdisk
+GROWFS_UFS=growfs.ufs
+DUMPFS_UFS=dumpfs.ufs
 DATE="date -u" # Time in UTC
 EATMYDATA=eatmydata
 MOUNT="mount -n"
@@ -130,10 +132,10 @@ report_error() {
     msg=""
     if [ ${#ERRORS[*]} -eq 0 ]; then
         # No error message. Print stderr
-        local lines
-        lines=$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE" | wc -l)
-        msg="STDERR:${lines}"
-        msg+=$(tail --lines=$lines  "$STDERR_FILE")
+        local lines stderr
+        stderr="$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE")"
+        lines=$(wc -l <<< "$stderr")
+        msg="STDERR:${lines}:$stderr"
     else
         for line in "${ERRORS[@]}"; do
             msg+="ERROR:$line"$'\n'
@@ -146,7 +148,6 @@ report_error() {
 log_error() {
     ERRORS+=("$*")
 
-    send_monitor_message_${HYPERVISOR} "ERROR: $@"
     send_result_${HYPERVISOR} "ERROR: $@"
 
     # Use return instead of exit. The set -x options will terminate the script
@@ -190,6 +191,8 @@ get_base_distro() {
         echo "gentoo"
     elif [ -e "$root_dir/etc/arch-release" ]; then
         echo "arch"
+    elif [ -e "$root_dir/etc/freebsd-update.conf" ]; then
+        echo "freebsd"
     else
         warn "Unknown base distro."
     fi
@@ -222,6 +225,8 @@ get_distro() {
         echo "gentoo"
     elif [ -e "$root_dir/etc/arch-release" ]; then
         echo "arch"
+    elif [ -e "$root_dir/etc/freebsd-update.conf" ]; then
+        echo "freebsd"
     else
         warn "Unknown distro."
     fi
@@ -274,7 +279,7 @@ is_extended_partition() {
     local dev="$1"
     local part_num="$2"
 
-    id=$($SFDISK --print-id "$dev" "$part_num")
+    id=$($SFDISK --force --print-id "$dev" "$part_num")
     if [ "$id" = "5" -o "$id" = "f" ]; then
         echo "yes"
     else
@@ -331,6 +336,9 @@ get_partition_to_resize() {
     dev="$1"
 
     table=$(get_partition_table "$dev")
+    if [ -z "$table" ]; then
+        return 0
+    fi
 
     if [ $(get_partition_count "$table") -eq 0 ]; then
         return 0
@@ -371,10 +379,17 @@ create_partition() {
     local name="${fields[5]}"
     local flags="${fields[6]//,/ }"
 
-    $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
-    for flag in $flags; do
-        $PARTED -s -m $device set "$id" "$flag" on
-    done
+    if [ "$ptype" = "primary" -o "$ptype" = "logical" -o "$ptype" = "extended" ]; then
+        $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
+        for flag in $flags; do
+            $PARTED -s -m $device set "$id" "$flag" on
+        done
+    else
+        # For gpt
+        start=${start:0:${#start}-1} # remove the s at the end
+        end=${end:0:${#end}-1} # remove the s at the end
+        $SGDISK -n "$id":"$start":"$end" -t "$id":"$ptype" "$device"
+    fi
 }
 
 enlarge_partition() {
@@ -459,6 +474,26 @@ umount_all() {
     done
 }
 
+get_ufstype() {
+    local device ufs
+
+    device="$1"
+    ufs="$($DUMPFS_UFS "$device" | head -1 | awk '{ match ($3, /\((.+)\)/, ufs); print ufs[1] }')"
+
+    case "$ufs" in
+        UFS1)
+            echo 44bsd
+            ;;
+        UFS2)
+            echo ufs2
+            ;;
+        *)
+            log_error "Unsupported UFS type: \`$ufs' in device $device"
+            echo ""
+            ;;
+    esac
+}
+
 cleanup() {
     # if something fails here, it shouldn't call cleanup again...
     trap - EXIT
@@ -515,7 +550,6 @@ check_if_excluded() {
     return 0
 }
 
-
 return_success() {
     send_result_${HYPERVISOR} "SUCCESS"
 }