#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL

#
export LC_ALL=C

# Load DRBL setting and functions
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"

. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
. /etc/drbl/drbl-ocs.conf
. $DRBL_SCRIPT_PATH/sbin/ocs-functions

#
prog="$(basename $0)"
USAGE() {
   echo "Usage: $prog [OPTION] PARTITION VALUE1 VALUE2..."
   echo "PARTITION is the partition device name. e.g. /dev/sda1"
   echo "VALUE is one of these value: start, end, size, type, filesystem or flags"
   echo "OPTION:"
   echo "-u, --unit UNIT     When the VALUE1, VALUE2... is about size, show the size with unit UNIT (s, kB, MB, GB... which parted accepts)."
   echo "Ex:" 
   echo "To list the /dev/sda1 size with unit sector"
   echo "  $prog -u s /dev/sda1 size"
}

#
check_col_idx() {
  # DEV is like /dev/hda
  # wanted is like "start", "end", "file" (since it's "file system" in SuSE 10.0, and filesystem in Redhat/FC)
  # Example in FC5 with parted-1.7.1-15.fc5
  #[root@fc5101 tmp]# LC_ALL=C parted -s /dev/hda p
  #
  #Disk /dev/hda: 4295MB
  #Sector size (logical/physical): 512B/512B
  #Partition Table: msdos
  #
  #Number  Start   End     Size    Type     File system  Flags
  # 1      32.3kB  4285MB  4285MB  primary  ntfs         boot
  #
  #For Mac OS GPT:
  #Model: ATA Hitachi HTS54168 (scsi)
  #Disk /dev/sda: 80.0GB
  #Sector size (logical/physical): 512B/512B
  #Partition Table: gpt
  #
  #Number  Start   End     Size    File system  Name                  Flags
  # 1      20.5kB  210MB   210MB   fat32        EFI System Partition  boot
  # 2      210MB   38.3GB  38.1GB  hfs+         Untitled
  # 3      38.4GB  41.6GB  3221MB  fat32        Untitled
  #
  # For multipath device (e.g. cciss):
  #root@cciss:~# parted -s /dev/cciss/c0d0 print
  #Model: Compaq Smart Array (cpqarray)
  #Disk /dev/cciss/c0d0: 440GB
  #Sector size (logical/physical): 512B/512B
  #Partition Table: msdos
  #
  #Number  Start   End    Size    Type      File system     Flags
  # 1      1049kB  107GB  107GB   primary   ext4
  # 2      107GB   440GB  333GB   extended
  # 5      107GB   161GB  53.7GB  logical   reiserfs
  # 6      161GB   172GB  10.7GB  logical   linux-swap(v1)
  # 7      172GB   440GB  269GB   logical   xfs
  #
  # Different versions of parted have different output formats, that's why it's so complicated here.
  local DEV=$1
  local wanted=$2
  local part_info_tmp
  part_info_tmp="$(mktemp /tmp/part_tmp.XXXXXX)"
  LC_ALL=C parted -s $DEV print | sed -r -e "s/File system/Filesystem/g" > $part_info_tmp 2>/dev/null
  # The seq is put in the order of possibility in clonezilla. Because filesystem is get by blkid, we put size then type.
  for i in 4 5 6 3 2 1 7; do
    col="$(grep -i flags $part_info_tmp | awk -F" " "{print \$$i}")"
    if [ -n "$(echo $col | grep -i $wanted)" ]; then
      col_idx=$i
      break
    fi
  done
  [ -f "$part_info_tmp" ] && rm -f $part_info_tmp
  echo $col_idx
} # end of check_col_idx

#
get_from_parted() {
  local part_info
  target_hd="/dev/$(get_diskname $part)"
  # For cciss partition, e.g. /dev/cciss/c0d0p2 will be "p2" which is got from $(get_part_number /dev/cciss/c0d0p2)", here the part_index we want is only number, hence we need to trip the leading characters.
  part_index="$(LC_ALL=C get_part_number $part | sed -r -e "s|^[^[:digit:]]*||g")"
  
  part_info="$(mktemp /tmp/part.XXXXXX)"
  [ -n "$unit" ] && unit_opt="unit $unit"
  LC_ALL=C parted -s $target_hd $unit_opt print > $part_info 2>/dev/null
  
  case "$wanted" in
    start)
       start_idx="$(check_col_idx $target_hd start)"
       if [ -z "$start_idx" ]; then
         [ -f "$part_info" ] && rm -f $part_info
	 return 1
       fi
       part_start=$(LC_ALL=C grep -E "^[[:space:]]*$part_index\>" $part_info | awk -F" " "{print \$$start_idx}")
       echo "$part_start"
       ;;
    end)
       end_idx="$(check_col_idx $target_hd end)"
       if [ -z "$end_idx" ]; then
         [ -f "$part_info" ] && rm -f $part_info
	 return 1
       fi
       part_end=$(LC_ALL=C grep -E "^[[:space:]]*$part_index\>" $part_info | awk -F" " "{print \$$end_idx}")
       echo "$part_end"
       ;;
    size)
       size_idx="$(check_col_idx $target_hd size)"
       if [ -z "$size_idx" ]; then
         [ -f "$part_info" ] && rm -f $part_info
	 return 1
       fi
       part_size=$(LC_ALL=C grep -E "^[[:space:]]*$part_index\>" $part_info | awk -F" " "{print \$$size_idx}")
       echo "$part_size"
       ;;
    type)
       type_idx="$(check_col_idx $target_hd type)"
       if [ -z "$type_idx" ]; then
         [ -f "$part_info" ] && rm -f $part_info
	 return 1
       fi
       part_type=$(LC_ALL=C grep -E "^[[:space:]]*$part_index\>" $part_info | awk -F" " "{print \$$type_idx}")
       echo "$part_type"
       ;;
    filesystem|fs)
       filesystem_idx="$(check_col_idx $target_hd file)"
       if [ -z "$filesystem_idx" ]; then
         [ -f "$part_info" ] && rm -f $part_info
	 return 1
       fi
       part_fs=$(LC_ALL=C grep -E "^[[:space:]]*$part_index\>" $part_info | awk -F" " "{print \$$filesystem_idx}")
       echo "$part_fs"
       ;;
    flags)
       flags_idx="$(check_col_idx $target_hd flags)"
       if [ -z "$flags_idx" ]; then
         [ -f "$part_info" ] && rm -f $part_info
	 return 1
       fi
       part_flags=$(LC_ALL=C grep -E "^[[:space:]]*$part_index\>" $part_info | awk -F" " "{print \$$flags_idx}")
       echo "$part_flags"
       ;;
  esac
  
  [ -f "$part_info" ] && rm -f $part_info
} # end of get_from_parted

#
get_fs_from_blkid() {
  local part_="$1"  # e.g. sda1, cciss/c0d0p1, dm-1
  local blkinfo TYPE hdtmp ptno pt_id p_type pt_info
  if type blkid &>/dev/null; then
    # blikd output example:
    # /dev/sda1: LABEL="/boot" UUID="a9cb47b6-e3ff-4a26-80e2-3fa8d30702e8" SEC_TYPE="ext2" TYPE="ext3" 
    # /dev/VolGroup00/LogVol00: UUID="110bc035-6920-4732-b117-5124843b93b9" SEC_TYPE="ext2" TYPE="ext3" 
    # /dev/VolGroup00/LogVol01: TYPE="swap" UUID="af85b756-39fa-48f0-80e7-b2128d5c87ab"
    # We want to filter to filter them as: TYPE="ext3" only
    # If $part_ is /dev/dm-1 in /proc/partitions, blkid still can get the correct type for /dev/dm-1 even the output of blkid is /dev/mapper/...:
    # ===============
    # root@fakeraid:~# cat /proc/partitions 
    # major minor  #blocks  name
    # 
    #   8        0  976762584 sda
    #   8        1  104857600 sda1
    #   8        2  314572800 sda2
    #   8        3  524288000 sda3
    #   8       16  976762584 sdb
    #  11        0    1048575 sr0
    #   7        0      90452 loop0
    # 254        0 1953519872 dm-0
    # 254        1  104857600 dm-1
    # 254        2  314572800 dm-2
    # 254        3  524288000 dm-3
    # ===============
    # The output of blkid:
    # root@fakeraid:~# blkid
    # /dev/sdb: TYPE="isw_raid_member" 
    # /dev/mapper/isw_deaeieebih_Volume01: UUID="d2350cad-2c66-4ac5-accb-bec596522958" TYPE="xfs" 
    # /dev/mapper/isw_deaeieebih_Volume02: UUID="300ed329-7dff-4b55-bb50-a3fab87d24b3" TYPE="xfs" 
    # /dev/mapper/isw_deaeieebih_Volume03: UUID="ab6cf10a-c74d-43e4-91d5-18817ff3d03f" TYPE="xfs" 
    # /dev/loop0: TYPE="squashfs" 
    # root@fakeraid:~# blkid -o export -c /dev/null /dev/sde30
    # DEVNAME=/dev/sde30
    # UUID_SUB=4e672e07-887dbdcf-4350-000c292c5b04
    # UUID=4e672e07-7dbf61e7-08e6-000c292c5b04
    # TYPE=VMFS_volume_member
    # PARTUUID=0004b3fb-1e
    # //NOTE// "TYPE" in the output of blkid is actually for file system.
    TYPE="$(LC_ALL=C blkid -o export -c /dev/null $part_ | grep -E "^TYPE=.*" | sed -r -e "s|^TYPE=||")"
    # If the result is vfat, it's too fuzzy, since there are fat12/fat16/fat32, and partimage does not support fat12.
    if [ -n "$(echo $TYPE | grep -iE "vfat")" ]; then
      if [ "$(file -Ls $part_ | grep -iw "fat" | grep -iw "12 bit")" ]; then
        # Ex. for FAT12:
	# /dev/sda1: x86 boot sector, mkdosfs boot message display, code offset 0x3c, OEM-ID " mkdosfs", sectors/cluster 16, root entries 512, sectors 63252 (volumes <=32 MB) , Media descriptor 0xf8, sectors/FAT 12, heads 255, serial number 0x487771fc, label: "           ", FAT (12 bit)
       TYPE="fat12"
      fi
    fi
    # UFS:
    # For ufs, it might be false one, since if the Id=a5, it's FreeBSD slice
    # E.g. The disk sdb has 3 slices (sdb1, sdb2, sdb3), and slice sdb1 contains partitions sdb5, sdb6, sdb7, slice sdb2 contains partitions sdb8, sdb9, sdb10, and sdb3 contains partitions sdb11, sdb12, sdb13. As shown in the following.
    # Then blkid will show /dev/sdb1 as ufs, but it's only slice, we only have to save the first 16 sectors by "dd if=/dev/sdb1 of=slice1-table.dd bs=512 count=16"
    # Therefore if blkid shows "ufs", we have to dig more.
    #
    # # sfdisk -l -uS /dev/sdb
    # Disk /dev/sdb: 2219 cylinders, 255 heads, 63 sectors/track
    # Units = sectors of 512 bytes, counting from 0
    #
    #    Device Boot    Start       End   #sectors  Id  System
    #    /dev/sdb1   *        63  20479094   20479032  a5  FreeBSD
    #    /dev/sdb2      20479095  24574724    4095630  a5  FreeBSD
    #    /dev/sdb3      24574725  33554114    8979390  a5  FreeBSD
    #    /dev/sdb4             0         -          0   0  Empty
    #    /dev/sdb5            63  10479094   10479032
    #    /dev/sdb6      10479095  20479094   10000000
    #    /dev/sdb7      21574725  22574724    1000000
    #    /dev/sdb8      20479095  21574724    1095630
    #    /dev/sdb9      22574725  24574724    2000000
    #    /dev/sdb10     24704115  25804114    1100000
    #    /dev/sdb11     24574725  24704114     129390
    #    /dev/sdb12     26304115  33554114    7250000
    #    /dev/sdb13     25804115  26304114     500000
    #
    # # disktype /dev/sdb
    # --- /dev/sdb
    # Block device, size 17 GiB (18253611008 bytes)
    # DOS/MBR partition map
    # Partition 1: 9.765 GiB (10485264384 bytes, 20479032 sectors from 63, bootable)
    #   Type 0xA5 (FreeBSD)
    #   FreeBSD boot loader (i386 boot1 at sector 0)
    #   FreeBSD boot loader (i386 boot2/BTX 1.02 at sector 2)
    #   BSD disklabel (at sector 1), 8 partitions
    #   Partition a: 4.997 GiB (5365264384 bytes, 10479032 sectors from 63)
    #     Type 7 (4.2BSD fast file system)
    #     Includes the disklabel and boot code
    #     UFS2 file system, 64 KiB offset, little-endian
    #       Last mounted at "/"
    #   Partition b: 4.768 GiB (5120000000 bytes, 10000000 sectors from 10479095)
    #     Type 1 (swap)
    #     Blank disk/medium
    #   Partition c: 9.765 GiB (10485264384 bytes, 20479032 sectors from 63)
    #     Type 0 (Unused)
    # Partition 2: 1.953 GiB (2096962560 bytes, 4095630 sectors from 20479095)
    #   Type 0xA5 (FreeBSD)
    #   FreeBSD boot loader (i386 boot1 at sector 0)
    #   FreeBSD boot loader (i386 boot2/BTX 1.02 at sector 2)
    #   BSD disklabel (at sector 1), 8 partitions
    #   Partition b: 488.3 MiB (512000000 bytes, 1000000 sectors from 21574725)
    #     Type 1 (swap)
    #     Blank disk/medium
    #   Partition c: 1.953 GiB (2096962560 bytes, 4095630 sectors from 20479095)
    #     Type 0 (Unused)
    #   Partition d: 535.0 MiB (560962560 bytes, 1095630 sectors from 20479095)
    #     Type 7 (4.2BSD fast file system)
    #     Includes the disklabel and boot code
    #     UFS2 file system, 64 KiB offset, little-endian
    #       Last mounted at "/home"
    #   Partition e: 976.6 MiB (1024000000 bytes, 2000000 sectors from 22574725)
    #     Type 1 (swap)
    #     Blank disk/medium
    # Partition 3: 4.282 GiB (4597447680 bytes, 8979390 sectors from 24574725)
    #   Type 0xA5 (FreeBSD)
    #   FreeBSD boot loader (i386 boot1 at sector 0)
    #   FreeBSD boot loader (i386 boot2/BTX 1.02 at sector 2)
    #   BSD disklabel (at sector 1), 8 partitions
    #   Partition b: 537.1 MiB (563200000 bytes, 1100000 sectors from 24704115)
    #     Type 1 (swap)
    #     Blank disk/medium
    #   Partition c: 4.282 GiB (4597447680 bytes, 8979390 sectors from 24574725)
    #     Type 0 (Unused)
    #   Partition d: 63.18 MiB (66247680 bytes, 129390 sectors from 24574725)
    #     Type 7 (4.2BSD fast file system)
    #     Includes the disklabel and boot code
    #     UFS2 file system, 64 KiB offset, little-endian
    #       Last mounted at "/opt"
    #   Partition e: 3.457 GiB (3712000000 bytes, 7250000 sectors from 26304115)
    #     Type 7 (4.2BSD fast file system)
    #     UFS2 file system, 64 KiB offset, little-endian
    #       Last mounted at "/home2"
    #   Partition h: 244.1 MiB (256000000 bytes, 500000 sectors from 25804115)
    #     Type 1 (swap)
    #     Blank disk/medium
    #
    # # blkid
    # /dev/sdb1: TYPE="ufs"
    # /dev/sdb2: TYPE="ufs"
    # /dev/sdb3: TYPE="ufs"
    # /dev/sdb5: TYPE="ufs"
    # /dev/sdb8: TYPE="ufs"
    # /dev/sdb11: TYPE="ufs"
    # /dev/sdb12: TYPE="ufs"
    #
    if [ -n "$(echo $TYPE | grep -iE "ufs")" ]; then
      # (1) MBR
      # The example for sfdisk command to get partition id:
      # sfdisk --print-id /dev/sdb 1
      # a5
      # (2) GPT
      # root@debian:~#  blkid
      # /dev/sda2: TYPE="ufs"
      # /dev/sda3: TYPE="ufs"
      # /dev/sda4: TYPE="ufs"
      # /dev/sda5: TYPE="ufs"
      # /dev/loop0: TYPE="squashfs"
      # 
      # root@debian:~#  sgdisk -p /dev/sda
      # Disk /dev/sda: 41943040 sectors, 20.0 GiB
      # Logical sector size: 512 bytes
      # Disk identifier (GUID): 0D3CC1B2-3DF6-11E1-AF1F-000C2976A9B0
      # Partition table holds up to 128 entries
      # First usable sector is 34, last usable sector is 41943006
      # Partitions will be aligned on 2-sector boundaries
      # Total free space is 1 sectors (512 bytes)
      # 
      # Number  Start (sector)    End (sector)  Size       Code  Name
      #    1              34             161   64.0 KiB    A501
      #    2             162         2097313   1024.0 MiB  A502
      #    3         2097314        12583073   5.0 GiB     A503
      #    4        12583074        16777377   2.0 GiB     A503
      #    5        16777378        41943005   12.0 GiB    A503
      # 
      # root@debian:~#  sgdisk -i 1 /dev/sda
      # Partition GUID code: 83BD6B9D-7F41-11DC-BE0B-001560B84F0F (FreeBSD boot)
      # Partition unique GUID: 0D3D0C89-3DF6-11E1-AF1F-000C2976A9B0
      # First sector: 34 (at 17.0 KiB)
      # Last sector: 161 (at 80.5 KiB)
      # Partition size: 128 sectors (64.0 KiB)
      # Attribute flags: 0000000000000000
      # Partition name: ''
      # 
      # root@debian:~#  sgdisk -i 2 /dev/sda
      # Partition GUID code: 516E7CB5-6ECF-11D6-8FF8-00022D09712B (FreeBSD swap)
      # Partition unique GUID: 39B6CECD-3DF6-11E1-AF1F-000C2976A9B0
      # First sector: 162 (at 81.0 KiB)
      # Last sector: 2097313 (at 1.0 GiB)
      # Partition size: 2097152 sectors (1024.0 MiB)
      # Attribute flags: 0000000000000000
      # Partition name: ''
      # 
      # root@debian:~#  sgdisk -i 3 /dev/sda
      # Partition GUID code: 516E7CB6-6ECF-11D6-8FF8-00022D09712B (FreeBSD UFS)
      # Partition unique GUID: 436931F9-3DF6-11E1-AF1F-000C2976A9B0
      # First sector: 2097314 (at 1.0 GiB)
      # Last sector: 12583073 (at 6.0 GiB)
      # Partition size: 10485760 sectors (5.0 GiB)
      # Attribute flags: 0000000000000000
      # Partition name: ''
      # ===
      # /dev/sda5 -> /dev/sda 5
      hdtmp="/dev/$(get_diskname ${part_})"
      ptno="$(get_part_number ${part_})"
      p_type="$(get_partition_table_type_from_disk $hdtmp)"
      case "$p_type" in
      mbr)
        # sfdisk >= 2.26 "--print-id" is deprecated in favour of "--part-type"
	if [ -n "$(LC_ALL=C sfdisk --help 2>&1 | grep -- "--part-type")" ]; then
	  print_id_opt="--part-type"
        else
          # old option "--print-id"
	  print_id_opt="--print-id"
	fi
        pt_id="$(LC_ALL=C sfdisk $print_id_opt $hdtmp $ptno 2>/dev/null)"
        # Partition ID a5 (FreeBSD), a6 (OpenBSD), a9 (NetBSD) are slices
        if [ "$(echo "$pt_id" | grep -Ewi "(a5|a6|a9)")" ]; then
          TYPE="BSD_slice"
        fi
	;;
      gpt)
	pt_info="$(LC_ALL=C sgdisk -i $ptno $hdtmp 2>/dev/null | grep -Ei "^Partition GUID code:")"
	if [ -n "$(echo $pt_info | grep -i "FreeBSD boot")" ]; then
          TYPE="BSD_boot"
	elif [ -n "$(echo $pt_info | grep -i "FreeBSD swap")" ]; then
          # Do not use any word with "swap" in TYPE, otherwise it won't be saved by dd, since we do not good mechanism for BSD swap imaging/cloning.
          TYPE="BSD_part"
	fi
	;;
      esac
    fi
    # For Ext, especially on ChromiumOS/ChromeOS, the root filesystem (/dev/sda3) is like:
    # /dev/sda1: LABEL="STATE" UUID="0b2b024a-2226-44d6-b482-5304c0ba1ad3" TYPE="ext4" PARTLABEL="STATE" PARTUUID="aff6ee6c-1f3d-e640-ac20-662a4e4321ac"
    # /dev/sda3: LABEL="ROOT-A" SEC_TYPE="ext2" TYPE="ext4" PARTLABEL="ROOT-A" PARTUUID="d84d1434-e366-994a-9b63-07230c2ba241"
    # /dev/sda8: LABEL="OEM" UUID="9c727706-030f-4fc8-9860-9cd49f301c99" TYPE="ext4" PARTLABEL="OEM" PARTUUID="a2db8f6d-6171-f14a-8c9e-409655a1dfdf"
    # /dev/sda12: SEC_TYPE="msdos" LABEL="EFI-SYSTEM" UUID="C9BF-10DC" TYPE="vfat" PARTLABEL="EFI-SYSTEM" PARTUUID="de94c6c7-2d57-9c44-8ecd-d1271d66afc2"
    # /dev/sda2: PARTLABEL="KERN-A" PARTUUID="290e0120-49f1-5246-aff4-95790db5419e"
    # /dev/sda4: PARTLABEL="KERN-B" PARTUUID="98361589-6471-6c46-8ebf-28225baec874"
    # /dev/sda5: PARTLABEL="ROOT-B" PARTUUID="75f18b32-d4fe-4d43-be2c-f44edb73c463"
    # /dev/sda11: PARTLABEL="RWFW" PARTUUID="d1d19fae-1141-554d-915b-e1534ef4333d"
    # It implements some special features (not compatible with Linux Extfs) which could not be saved correctly by Partclone:
    # e2fsck -n /dev/sda3
    # e2fsck 1.42.12 (29-Aug-2014)
    # /dev/sda3 has unsupported feature(s): FEATURE_R24 FEATURE_R25 FEATURE_R26 FEATURE_R27 FEATURE_R28 FEATURE_R29 FEATURE_R30 FEATURE_R31
    # e2fsck: Get a newer version of e2fsck!
    # ROOT-A: ********** WARNING: Filesystem still has errors **********
    if [ -n "$(LC_ALl=C echo $TYPE | grep -iE "ext")" ]; then
      if [ -n "$(LC_ALL=C blkid -c /dev/null $part_ | grep -iw 'LABEL="ROOT-A"' )" ]; then
         # /dev/sda3: LABEL="ROOT-A" SEC_TYPE="ext2" TYPE="ext4" PARTLABEL="ROOT-A" PARTUUID="d84d1434-e366-994a-9b63-07230c2ba241"
	 if [ -n "$(LC_ALL=C e2fsck -n $part_ 2>&1 | grep -iw "has unsupported feature(s): FEATURE_.*")" ]; then
	   TYPE="chromium_ext4"
	 fi
      fi
    fi
    echo "$TYPE"
  fi
} # end of get_fs_from_blkid
#
get_fs_from_partclone_fstype() {
  local part_="$1"  # e.g. sda1, cciss/c0d0p1, dm-1
  local fstypeinfo TYPE
  if type partclone.fstype &>/dev/null; then
    fstypeinfo="$(mktemp /tmp/fstypeinfo.XXXXXX)"
    # partclone.fstype output example:
    # partclone.fstype /dev/hda3 2>/dev/null
    # TYPE="vmfs"
    # We want to filter to filter them as: TYPE="vmfs" only
    LC_ALL=C partclone.fstype $part_ 2>/dev/null| grep -o -E '\<TYPE="[^[:space:]]*"($|[[:space:]]+)' > $fstypeinfo
    TYPE=""
    . $fstypeinfo
    echo "$TYPE"
    [ -f "$fstypeinfo" ] && rm -f $fstypeinfo
  fi
} # end of get_fs_from_partclone_fstype
#
get_size_from_lsblk() {
  local part_="$1"  # e.g. /dev/sda1, /dev/cciss/c0d0p1
  if type lsblk &>/dev/null; then
    part_size="$(LC_ALL=C lsblk -n -d -o size ${part_} | sed -r -e "s/^[[:space:]]*//g")"
    echo "$part_size"
  fi
} # end of get_size_from_lsblk
#
get_type_from_lsblk() {
  # This function is intended to get if a parition is swap or extended. 
  # Because the mechanism got from parted is too slow, we use lsblk to simulate it.
  local part_="$1"  # e.g. /dev/sda1, /dev/cciss/c0d0p1
  if type lsblk &>/dev/null; then
    # For swap, e.g.
    # lsblk -n -o FSTYPE /dev/sda6
    part_type="$(LC_ALL=C lsblk -n -d -o FSTYPE ${part_} | sed -r -e "s/^[[:space:]]*//g")"
    if [ "$part_type" != "swap" ]; then
      # We only care if it's swap or extended. For other file system, just ignore it.
      part_type=""
    fi
    if [ -z "$part_type" ]; then
      # For extended, e.g.
      # lsblk -n -o PARTTYPE /dev/sda2
      part_type="$(LC_ALL=C lsblk -n -d -o PARTTYPE ${part_} | sed -r -e "s/^[[:space:]]*//g")"
      if [ "$part_type" = "0x5" -o \
           "$part_type" = "0xf" ]; then
        # Convert to human readable
        part_type="extended"
      fi
    fi
    echo "$part_type"
  fi
} # end of get_type_from_lsblk
#
check_if_bitlocker_fs() {
  local rc_n rc_f
  #if type dislocker-find &>/dev/null; then
  #  dislocker-find $part &>/dev/null
  #  # //NOTE// Here the return code for dislocker-find is the number of bitlocker partition. Not the true/false.
  #  rc_n=$?
  #  if [ "$rc_n" -eq 1 ]; then
  #    rc_f="bitlocker"
  #  else
  #    rc_f=""
  #  fi
  #  echo "$rc_f"
  #fi
  # Temporarily disable dislocker-find since:
  # (1) It's broken in Debian Sid: https://bugs.debian.org/906430
  # (2) It seems dislocker-find (depeds on libmbedcrypto1) can not deal with
  #     Linux kernel >= 4.17 on Debian when in early booted status. Hangs in waiting for getrandom...
  #     https://sourceforge.net/p/clonezilla/discussion/Open_discussion/thread/e1264596
  #     Maybe related to https://bugs.debian.org/897572 and https://bugs.debian.org/897599
  echo
} # end of check_if_bitlocker_fs
#
check_if_MSR_part() {
  local rc_n rc_f
  # Microsoft Reserved Partition
  # The GPT label for this partition type is E3C9E316-0B5C-4DB8-817D-F92DF00215AE.
  # Only exists on GPT partition table. It's not UUID of file system, nor the PARTUUID for the GPT partition. Actually there is no any file system for this. Here we just want to identify it.
  #
  rc_n="$(LC_ALL=C sfdisk -d "/dev/$(get_diskname $part)" 2>/dev/null | grep -Ew "$part" | grep -Eo type=[[:alnum:]-]+)"
  rc_f=""
  if [ "$(echo $rc_n | grep -Ewi "E3C9E316-0B5C-4DB8-817D-F92DF00215AE")" ]; then
   rc_f="MS_Reserved_Partition"
  fi
  echo "$rc_f"
} # end of check_if_MSR_part

# Some notes:
# First priority is to use blkid to get filesystem, 2 reasons:
# (1) Since parted seems to have problem with ntfs in some case:
# LC_ALL=C parted -s /dev/hdb p
# Model: VMware Virtual IDE Hard Drive (ide)
# Disk /dev/hdb: 8590MB
# Sector size (logical/physical): 512B/512B
# Partition Table: msdos
# 
# Number  Start   End     Size    Type     File system  Flags
#  1      32.3kB  8579MB  8579MB  primary  ntfs         boot
# 
# [root@drblrhvmnet101 ~]# LC_ALL=C parted -s /dev/hdb1 p
# Error: Can't have a partition outside the disk!
#
# (2) Moreover, the method in get_from_parted does NOT work for LV in LVM... blkid can solve the problem. The weakness is blkid does not provide size info, but it's not so used very often in clonezilla, and normally size is not important when cloning.

#
##############
#### MAIN ####
##############
#
while [ $# -gt 0 ]; do
  case "$1" in
    -u|--unit)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
	      unit="$1"
              shift
            fi
            [ -z "$unit" ] && USAGE >&2 && exit 1
	    ;;
    -*)     echo "${0}: ${1}: invalid option" >&2
            USAGE >& 2
            exit 2 ;;
    *)      break ;;
  esac
done
# part is like /dev/hda1
# request is one of start, end, size, type, filesystem or flags.
part="$1"
shift
request="$*"

[ -z "$part" -o -z "$request" ] && exit 1

#
for wanted in $request; do
  case "$wanted" in
    filesystem|fs)
       # We use partclone.fstype to tell vmfs3 and vmfs5 first, otherwise blkid will show them as VMFS_volume_member, which we can not tell it's vmfs3 or vmfs5.
       fs="$(get_fs_from_partclone_fstype $part)"
       if [ -z "$fs" ]; then
         fs="$(get_fs_from_blkid $part)"
       fi
       if [ -z "$fs" ]; then
	 fs="$(check_if_bitlocker_fs $part)"
       fi
       if [ -z "$fs" ]; then
	 fs="$(check_if_MSR_part $part)"
       fi
       if [ -z "$fs" ]; then
         get_from_parted
       else
         echo "$fs"
       fi
       ;;
    size)
       psize="$(get_size_from_lsblk $part)"
       if [ -z "$psize" ]; then
         get_from_parted
       else
         echo "$psize"
       fi
       ;;
    type)
       ptype="$(get_type_from_lsblk $part)"
       if [ -z "$ptype" ]; then
         get_from_parted
       else
         echo "$ptype"
       fi
       ;;
    *)
       get_from_parted
       ;;
  esac
done
