Add diskdump support in the helper (part 1)
authorNikos Skalkotos <skalkoto@grnet.gr>
Thu, 3 Nov 2011 16:23:59 +0000 (18:23 +0200)
committerNikos Skalkotos <skalkoto@grnet.gr>
Thu, 3 Nov 2011 16:23:59 +0000 (18:23 +0200)
snf-image-helper/common.sh
snf-image-helper/snf-image-helper.in
snf-image-helper/tasks/10FixPartitionTable.in [new file with mode: 0644]
snf-image-helper/tasks/20FilesystemResizeUnmounted.in [moved from snf-image-helper/tasks/10ResizeUnmounted.in with 56% similarity]
snf-image-helper/tasks/30MountImage.in
snf-image-helper/tasks/40FilesystemResizeMounted.in [moved from snf-image-helper/tasks/40ResizeMounted.in with 75% similarity]

index 1724361..f852875 100644 (file)
@@ -35,6 +35,7 @@ PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
 # Programs
 XMLSTARLET=xmlstarlet
 RESIZE2FS=resize2fs
+PARTED=parted
 
 CLEANUP=( )
 
@@ -92,6 +93,35 @@ get_distro() {
     fi
 }
 
+get_last_partition() {
+    local dev="$1"
+
+    "$PARTED" -s -m "$dev" unit s print | tail -1
+}
+
+get_partition() {
+    local dev="$1"
+    local id="$2"
+
+    "$PARTED" -s -m "$dev" unit s print | grep "^$id" 
+}
+
+get_partition_count() {
+    local dev="$1"
+
+     expr $("$PARTED" -s -m "$dev" unit s print | wc -l) - 2
+}
+
+get_last_free_sector() {
+    local dev="$1"
+    local last_line=$("$PARTED" -s -m "$dev" unit s print free | tail -1)
+    local type=$(echo "$last_line" | cut -d: -f 5)
+
+    if [ "$type" = "free;" ]; then
+        echo "$last_line" | cut -d: -f 3
+    fi
+}
+
 cleanup() {
     # if something fails here, it shouldn't call cleanup again...
     trap - EXIT
index b42598f..1ca4a45 100644 (file)
@@ -64,6 +64,7 @@ target=$(mktemp -d --tmpdir target.XXXXXX)
 add_cleanup rmdir "$target"
 
 export SNF_IMAGE_TARGET="$target"
+export SNF_IMAGE_ROOTDEV="${SNF_IMAGE_DEV}${SNF_IMAGE_ROOT}"
 
 if [ ! -d "@tasksdir@" ]; then
     log_error "snf-image/tasks directory is missing"
diff --git a/snf-image-helper/tasks/10FixPartitionTable.in b/snf-image-helper/tasks/10FixPartitionTable.in
new file mode 100644 (file)
index 0000000..6a10013
--- /dev/null
@@ -0,0 +1,52 @@
+#! /bin/bash
+
+### BEGIN TASK INFO
+# Provides:            FixPartitionTable
+# RunBefore:           FilesystemResizeUnmounted
+# Short-Description:   Resize filesystem to use all the available space
+### END TASK INFO
+
+set -e
+. "@commondir@/common.sh"
+
+if [ ! -b "$SNF_IMAGE_DEV" ]; then
+    log_error "Device file:\`${SNF_IMAGE_DEV}' is not a block device"
+fi
+
+if [ $(get_partition_count "$SNF_IMAGE_DEV") -eq 0 ]; then
+    log_error "Device: \`${SNF_IMAGE_DEV}' does not contain any partition"
+fi
+
+retval=$(get_last_partition "$SNF_IMAGE_DEV")
+
+id=$(echo $retval | cut -d: -f1)
+
+if [ $id -gt 4 ]; then
+    log_error "We don't support logical volumes"
+fi
+
+pstart=$(echo $retval | cut -d: -f2)
+pend=$(echo $retval | cut -d: -f3)
+ptype=$(echo $retval | cut -d: -f5)
+
+if [ x"$ptype" = "x" ]; then
+    # Don't know how to handle this
+    echo "Warning: Last partition with id: \`$id' is empty" \
+        "or has unknown filesystem"
+
+    exit 0
+fi
+
+new_pend=$(get_last_free_sector "$SNF_IMAGE_DEV")
+
+#Extend the partition
+
+$PARTED -s -m "$SNF_IMAGE_DEV" rm $id
+$PARTED -s -m "$SNF_IMAGE_DEV" mkpart primary "$ptype" "$pstart" "$new_pend"
+
+#inform the kernel about the changes
+partprobe "$SNF_IMAGE_DEV"
+
+exit 0
+
+# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
@@ -1,8 +1,9 @@
 #! /bin/bash
 
 ### BEGIN TASK INFO
-# Provides:            ResizeUnmounted
+# Provides:            FilesystemResizeUnmounted
 # RunBefore:           MountImage
+# RunAfter:             FixPartitionTable
 # Short-Description:   Resize filesystem to use all the available space
 ### END TASK INFO
 
@@ -11,15 +12,20 @@ set -e
 
 if [ ! -b "$SNF_IMAGE_DEV" ]; then
     log_error "Device file:\`${SNF_IMAGE_DEV}' is not a block device"
-
 fi
+
 if [ -z "$SNF_IMAGE_TYPE" ]; then
     log_error "Image type does not exist"
 fi
 
-if [ "$SNF_IMAGE_TYPE" = "extdump" ]; then
-    "$RESIZE2FS" "$SNF_IMAGE_DEV"
-fi     
+last_partition=$(get_last_partition "$SNF_IMAGE_DEV")
+id=$(echo "$last_partition" | cut -d: -f1)
+ptype=$(echo "$last_partition" | cut -d: -f5)
+
+if [[ "$ptype" =~ ext[234] ]]; then
+    device="$SNF_IMAGE_DEV""$id"
+    "$RESIZE2FS" "$device"
+fi
 
 exit 0
 
index 9a893e0..ed62419 100644 (file)
@@ -13,11 +13,11 @@ if [ ! -d "$SNF_IMAGE_TARGET" ]; then
     log_error "Target dir:\`$SNF_IMAGE_TARGET' is missing"
 fi
 
-if [ ! -b "$SNF_IMAGE_DEV" ]; then
-    log_error "Device file:\`$SNF_IMAGE_DEV' is not a block device"
+if [ ! -b "$SNF_IMAGE_ROOTDEV" ]; then
+    log_error "Device file:\`$SNF_IMAGE_ROOTDEV' is not a block device"
 fi
 
-mount "$SNF_IMAGE_DEV" "$SNF_IMAGE_TARGET"
+mount "$SNF_IMAGE_ROOTDEV" "$SNF_IMAGE_TARGET"
 
 exit 0
 
@@ -1,7 +1,7 @@
 #! /bin/bash
 
 ### BEGIN TASK INFO
-# Provides:            ResizeMounted
+# Provides:            FilesystemResizeMounted
 # RunBefore:            UmountImage
 # RunAfter:            MountImage
 # Short-Description:   Resize filesystem to use all the available space
@@ -14,13 +14,17 @@ if [ ! -d "$SNF_IMAGE_TARGET" ]; then
     log_error "Target directory \`$SNF_IMAGE_TARGET' is missing"
 fi
 
-if [ "$SNF_IMAGE_TYPE" = "ntfsdump" ]; then
+last_partition=$(get_last_partition "$SNF_IMAGE_DEV")
+id=$(echo "$last_partition" | cut -d: -f1)
+ptype=$(echo "$last_partition" | cut -d: -f5)
+
+if [ "$ptype" = "ntfs" ]; then
     # Write a diskpart script to %SystemDrive%\Windows\SnfScripts. Sysprep will
     # try to execute this script during the specialize pass.
     mkdir -p "$SNF_IMAGE_TARGET/Windows/SnfScripts"
     cat > "$SNF_IMAGE_TARGET/Windows/SnfScripts/ExtendFilesystem" <<EOF
 select disk 0
-select volume 1
+select volume $id
 extend filesystem
 exit
 EOF