Bash script example 使用parted可以非交互式的做磁盘分区操作

#!/bin/bash
# Startup check
case "$#" in
2)
    echo "Install package: ${1} on device: ${2}, start working..." ;;
*)
    echo "Usage: ${0} <l4t buildbrain file path> <SD card device file>"
    exit ;;
esac

function get_disk_info() {
  # look for a "given" file somewhere in the path upwards from the device
  local dev_path=/sys/block/${1}/device
  while [ -d "${dev_path}" -a "${dev_path}" != "/sys" ]; do
    if [ -f "${dev_path}/${2}" ]; then
      cat "${dev_path}/${2}"
      return
    fi
    dev_path=$(readlink -f ${dev_path}/..)
  done
  echo '[Unknown]'
}

# Generates a descriptive string of a removable device. Includes the
# manufacturer (if non-empty), product and a human-readable size.
function get_disk_string() {
  local disk="${1##*/}"
  local manufacturer_string=$(get_disk_info $disk manufacturer)
  local product_string=$(get_disk_info $disk product)
  local disk_size=$(sudo fdisk -l /dev/$disk 2>/dev/null | grep Disk |
                    head -n 1 | cut -d' ' -f3-4 | sed 's/,//g')
  # I've seen one case where manufacturer only contains spaces, hence the test.
  if [ -n "${manufacturer_string// }" ]; then
    echo -n "${manufacturer_string} "
  fi
  echo "${product_string}, ${disk_size}"
}

# Prompt for user confirmation. Default is no, which will gracefully terminate
# the script.
function are_you_sure() {
  local sure
  read -p "Are you sure (y/N)? " sure
  if [ "${sure}" != "y" ]; then
    echo "Ok, better safe than sorry."
    exit
  fi
}

echo "Checking l4t buildbrain package exists..."
if [ ! -f "${1}" ]; then
        echo "L4T buildbrain package: ${1} doesn't exist, quit..."
        exit
fi
echo "Checking device file exists..."
if [ ! -b "${2}" ]; then
        echo "SD card device file: ${2} doesn't exist OR is not a block device file, quit..."
        exit
fi

# Enter sudo passwd first
sudo ls /root >& /dev/null

echo -n "WARNING: This will erase all data in ${2}: "
get_disk_string ${2}
are_you_sure

# Umount any mounts of the disk
mount_list=$(mount | grep ^"${2}" | awk '{print $3}')
if [ -n "${mount_list}" ]; then
    echo "Attempting to unmount any mounts on the target device..."
    for i in ${mount_list}; do
        sudo umount "$i" >& /dev/null
    done
    sleep 3
fi

echo "Repartition ${2}..."
sudo parted -s ${2} mklabel msdos
sudo parted -s ${2} mkpart primary ext3 0 1024 >& /dev/null

echo "Formating ${2}1..."
sudo mkfs.ext3 ${2}1 >& /dev/null

echo "Start preparing the l4t disk..."
sudo rm -rf full_linux_for_tegra
pkgdir=$(dirname ${1})
cd ${pkgdir}
tar -xpf ${1}
cd full_linux_for_tegra
tar -xpf linux_for_tegra.tbz2
cd Linux_for_Tegra/rootfs/
sudo tar xpf http://www.cnblogs.com/sample_fs.tgz
sudo tar jxpf http://www.cnblogs.com/restricted_binaries.tbz2
sudo tar jxpf http://www.cnblogs.com/restricted_codecs.tbz2
sudo tar jxpf http://www.cnblogs.com/nvidia_use_only.tbz2
sudo tar zxpf http://www.cnblogs.com/tests_output.tgz
cd ../
sudo chown -R `cat rootfs/etc/passwd | grep ubuntu | cut -d : -f 3-4` rootfs/home/ubuntu/
sudo ./apply_binaries.sh

# Start writing to sd card
echo "Start writing to ${2}..."
sudo mount ${2}1 /mnt
cd rootfs
sudo tar -cpf - * | ( cd /mnt/ ; sudo tar -xpf - )
sudo umount /mnt
cd ..

echo "Done."

1. 使用parted这个工具可以以非交互的方式来对磁盘分区进行操作。而且parted支持GPT partition table。命令parted -s /dev/sdb mklabel msdos可以将当前的分区表全部清空,然后创建成指定的分区表格式,这个非常有用。之前网上的方法:dd if=/dev/zero of=/dev/sdb bs=512 count=1这种方式是不能支持GPT table的,普通分区表可以。

原文地址:https://www.cnblogs.com/super119/p/2501410.html