From 5308605532d6d95360ff076242676ab591283cd2 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Jan 23 2018 17:19:39 +0000 Subject: pythonfy arm-iamge-installer --- diff --git a/arm-image-installer b/arm-image-installer index c2f8502..acccf14 100755 --- a/arm-image-installer +++ b/arm-image-installer @@ -1,231 +1,151 @@ -#!/bin/bash +#!/bin/python3 +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 -# Copyright (C) 2014-2015 Red Hat Inc. +# Copyright (C) 2014-2016 Red Hat Inc. # SPDX-License-Identifier: GPL-2.0+ -# Automate Media Creation for Fedora ARM -# Current version -VERSION=1.99.13 +VERSION="1.99.13" + +# Automate Media Creation for Fedora ARM +import argparse +import sys + +import dbus + +parser = argparse.ArgumentParser() +parser.add_argument('--image', help='xz compressed image file name', type=str,) +parser.add_argument('--target', help='target board, see --list-boards', type=str,) +parser.add_argument('--media', help='media device file (/dev/[sdX|mmcblkX])', type=str,) +parser.add_argument('--noselinux', help='Turn SELinux off/on as needed', action='store_false') +parser.add_argument('--norootpass', help='Remove the root password', action='store_false') +parser.add_argument('-y', help='Assumes yes, will not wait for confirmation', action='store_true') +parser.add_argument('--resizefs', help='Resize root filesystem to fill media device', action='store_true') +parser.add_argument('--addconsole', help='Add system console to extlinux.conf', action='store_true') +parser.add_argument('--addkey', help='/path/to/ssh-public-key', type=argparse.FileType('r')) +parser.add_argument('--list-boards', help='print a list of available ARM boards', action='store_true') + +BOARDS = { +'ALLWINNER': + ['A10-OLinuXino-Lime', 'A10s-OLinuXino-M', 'A13-OLinuXino', 'A13-OLinuXinoM', + 'A20-Olimex-SOM-EVB', 'A20-OLinuXino-Lime', 'A20-OLinuXino-Lime2', + 'A20-OLinuXino_MICRO', 'Ampe_A76', 'Auxtek-T003', 'Auxtek-T004', 'ba10_tv_box', + 'Bananapi', 'Bananapro', 'CHIP', 'Chuwi_V7_CW0825', 'Colombus', 'colorfly_e708_q1', + 'CSQ_CS908', 'Cubieboard', 'Cubieboard2', 'Cubietruck', 'Cubietruck_plus', + 'difrnce_dit4350', 'dserve_dsrv9703c', 'Hummingbird_A31', 'Hyundai_A7HD', + 'i12-tvbox', 'icnova-a20-swac', 'inet86dz', 'iNet_86VS', 'Itead_Ibox_A20', + 'jesurun_q5', 'Lamobo_R1', 'Linksprite_pcDuino', 'Linksprite_pcDuino3', + 'Linksprite_pcDuino3_Nano', 'Marsboard_A10', 'Mele_A1000', 'Mele_A1000G_quad', + 'Mele_I7', 'Mele_M3', 'Mele_M5', 'Mele_M9', 'Mini-X', 'mk802', 'mk802_a10s', + 'mk802ii', 'MK808C', 'MSI_Primo73', 'MSI_Primo81', 'Orangepi', 'orangepi_2', + 'orangepi_lite', 'Orangepi_mini', 'orangepi_pc', 'orangepi_plus', 'polaroid_mid2809pxe04', + 'pov_protab2_ips9', 'q8_a13_tablet', 'q8_a23_tablet_800x480', 'q8_a33_tablet_1024x600', + 'q8_a33_tablet_800x480', 'r7-tv-dongle', 'Sinlinx_SinA31s', 'sunxi_Gemei_G9', + 'UTOO_P66', 'Wexler_TAB7200', 'Wits_Pro_A20_DKT', 'Yones_Toptech_BS1078_V2'], +'IMX6': + ['cm_fx6', 'mx6cuboxi', 'novena', 'riotboard', 'wandboard'], +'NONE': + ['jetson-tk1', 'none', 'rpi2', 'rpi3', 'trimslice'], +'OMAP4': + ['omap4_panda'], +'OMAP': + ['am335x_boneblack', 'am57xx_evm', 'kc1', 'omap3_beagle', 'omap5_uevm'], +'MVEBU': + ['mvebu', 'clearfog'], +'ARNDALE': + ['arndale'] +} -# usage message -usage() { - echo " -Usage: $(basename ${0}) +BOARD_SETTINGS = { + 'ALLWINNERS': { + "dd": ({"if": "u-boot-sunxi-with-spl.bin", "bs": 1024, "seek": 8},), + "syscon": "ttyS0,115200" + }, + 'IMX6': { + "dd": ({"if": "SPL", "bs": 1024, "seek": 1}, + {"if": "u-boot.img", "bs": 1024, "seek": 69}), + "syscon": "ttymxc0,115200", + }, + 'NONE': { + }, + 'OMAP4': { + "dd": ({"if": "MLO", "bs": 131072, "seek": 1, "count": 1, "conv": "notrunc"}, + {"if": "u-boot.img", "bs": 393216, "seek": 1, "count": 2}), + "syscon": "tty02,115200", + }, + 'OMAP': { + "dd": ({"if": "MLO", "bs": 131072, "seek": 1, "count": 1, "conv": "notrunc"}, + {"if": "u-boot.img", "bs": 393216, "count": 2, "seek": 1, "conv": "notrunc"}), + "syscon": "tty0,115200", + }, + 'MVEBU': { + "dd": ({"if": "u-boot-spl.kwb", "bs": 512, "seek": 1},), + } +} - --image=IMAGE - xz compressed image file name - --target=TARGET - target board - --media=DEVICE - media device file (/dev/[sdX|mmcblkX]) - --selinux=ON/OFF - Turn SELinux off/on as needed - --norootpass - Remove the root password - -y - Assumes yes, will not wait for confirmation - --version - Display version and exit - --resizefs - Resize root filesystem to fill media device - --addconsole - Add system console to extlinux.conf - --addkey= - /path/to/ssh-public-key -Example: $(basename ${0}) --image=Fedora-Rawhide.xz --target=Bananapi --media=/dev/mmcblk0 +def get_removable_devices (): + systembus = dbus.SystemBus () + storage_devices = systembus.call_blocking (bus_name="org.freedesktop.UDisks", + object_path="/org/freedesktop/UDisks", + dbus_interface="org.freedesktop.UDisks", + method="EnumerateDevices", + signature="", + args="") + removable_devices = [] + for device in storage_devices: + obj = systembus.get_object (bus_name="org.freedesktop.UDisks", + object_path=device) + if not obj.Get ("org.freedesktop.UDisks", "DeviceIsRemovable"): + continue -For list of supported boards please check SUPPORTED-BOARDS file. -" -} + removable_devices.append ((device, + obj.Get ("org.freedesktop.UDisks", "DeviceFile"), + obj.Get ("org.freedesktop.UDisks", "DevicePresentationName"))) -# Set some global variables for the command directory, target board directory, -# and valid targets. -DIR=$(dirname $0) -if [ -d "/usr/share/arm-image-installer/boards.d" ]; then - BOARDDIR="/usr/share/arm-image-installer/boards.d" -else - DIR=$(dirname $0) - BOARDDIR="${DIR}/boards.d" -fi + return removable_devices -# check the args -while [ $# -gt 0 ]; do - case $1 in - --debug) - set -x - ;; - -h|--help) - usage - exit 0 - ;; - --target*) - if echo $1 | grep '=' >/dev/null ; then - TARGET=$(echo $1 | sed 's/^--target=//') - else - TARGET=$2 - shift - fi - ;; - --image*) - if echo $1 | grep '=' >/dev/null ; then - IMAGE=$(echo $1 | sed 's/^--image=//') - else - IMAGE=$2 - shift - fi - ;; - --media*) - if echo $1 | grep '=' >/dev/null ; then - MEDIA=$(echo $1 | sed 's/^--media=//') - else - MEDIA=$2 - shift - fi - ;; - --addkey*) - if echo $1 | grep '=' >/dev/null ; then - SSH_KEY=$(echo $1 | sed 's/^--addkey=//') - else - SSH_KEY=$2 - shift - fi - ;; - --selinux*) - if echo $1 | grep '=' >/dev/null ; then - SELINUX=$(echo $1 | sed 's/^--selinux=//') - else - SELINUX=$2 - shift - fi - ;; - --norootpass) - NOROOTPASS=1 - ;; - --resizefs) - RESIZEFS=1 - ;; - --addconsole) - CONSOLE=1 - ;; - --version) - echo "$(basename ${0})-"$VERSION"" - exit 0 - ;; - -y) - NOASK=1 - ;; - *) - echo "$(basename ${0}): Error - ${1}" - usage - exit 1 - ;; - esac - shift -done -contains() { - string="$1" - substring="$2" - if test "${string#*$substring}" != "$string" - then - return 0 # $substring is in $string - else - return 1 # $substring is not in $string - fi -} +def main (): + args = parser.parse_args() + if args.list_boards: + for key in BOARDS: + for board in BOARDS[key]: + print (board) -# ensure sudo user -if [ "$(whoami)" != "root" ] ; then - echo "Error: This script requires 'sudo' privileges in order to write to disk & mount media." - exit 1 -fi + if not args.image: + print ("--image file was not specified", file=sys.stderr) + parser.print_help () + if not args.target: + print ("--target board was not specified", file=sys.stderr) + parser.print_help () + if not args.media: + print ("--media file was not specified", file=sys.stderr) + parser.print_help () -# check to make sure populated -if [ "$MEDIA" = "" ] ; then - usage - exit 1 -fi + if not (args.image or args.target or args.media): + sys.exit (1) -# change cubietruck target to uppercase -if [ "$TARGET" = "cubietruck" ]; then - TARGET="Cubietruck" -fi + print (get_removable_devices ()) -# check for boards -if [ "$TARGET" != "" -a ! -e "${BOARDDIR}/${TARGET}" ] ; then - echo "Error: You must choose a supported board or none at all." - usage - exit 1 -fi -# image exists -if [ ! -f "$IMAGE" ] && [ "$IMAGE" != "" ] ; then - echo "Error: $IMAGE not found! Please choose an existing image." - exit 1 -fi +if __name__ == "__main__": + main () -# device exists -if [ ! -e "$MEDIA" ] ; then - echo "Error: $MEDIA not found! Please choose an existing device." - exit 1 +""" +# Set some global variables for the command directory, target board directory, +# and valid targets. +DIR=$(dirname $0) +if [ -d "/usr/share/arm-image-installer/boards.d" ]; then + BOARDDIR="/usr/share/arm-image-installer/boards.d" +else + DIR=$(dirname $0) + BOARDDIR="${DIR}/boards.d" fi -clear -# Last chance to back out -echo " " -echo "=====================================================" -# Image if included -if [ "$IMAGE" != "" ] ; then - echo "= Selected Image: " - echo "= $IMAGE" -fi -echo "= Selected Media : $MEDIA" -# target hardware platform -if [ "$TARGET" != "" ] ; then - echo "= U-Boot Target : $TARGET" -fi -# SE Linux On/Off -if [ "$SELINUX" != "" ] ; then - echo "= SELINUX = $SELINUX" -fi -# Remove root password -if [ "$NOROOTPASS" != "" ] ; then - echo "= Root Password will be removed." -fi -# Resize root filesystem to fill media device -if [ "$RESIZEFS" != "" ] ; then - echo "= Root partition will be resized" -fi -# Console to be added -if [ "$CONSOLE" != "" ] ; then - echo "= Console for $TARGET will be added." -fi -# User ssh key -if [ "$SSH_KEY" != "" ] ; then - echo "= SSH Public Key $SSH_KEY will be added." -fi -echo "=====================================================" -echo " " -echo "*****************************************************" -echo "*****************************************************" -echo "******** WARNING! ALL DATA WILL BE DESTROYED ********" -echo "*****************************************************" -echo "*****************************************************" -if [ "$NOASK" != 1 ] ; then - echo " " - echo " Type 'YES' to proceed, anything else to exit now " - echo " " - # wait for agreement - read -p "= Proceed? " PROCEED - if [ "$(echo ${PROCEED} | tr [:lower:] [:upper:])" != "YES" ] ; then - echo "User exit, no image written." - exit 0 - fi -fi # umount before starting -umount $MEDIA* &> /dev/null - # Write the disk image to media -if [ "$IMAGE" != "" ] ; then - echo "= Writing: " - echo "= $IMAGE " - echo "= To: $MEDIA ...." xzcat $IMAGE | dd of=$MEDIA bs=4M; sync; sleep 3 - echo "= Writing image complete!" - # read the new partition table - partprobe "$MEDIA" -fi + # check to see how many partitions on the image partprobe "$MEDIA" case $MEDIA in @@ -262,7 +182,7 @@ if [ "$RESIZEFS" != "" ] ; then sleep 5 echo ", +" | sfdisk -N "$PARTNUM" "$MEDIA" done - + fsck.ext4 -fy "$ROOTPART" partprobe "$MEDIA" resize2fs "$ROOTPART" @@ -300,9 +220,9 @@ if [ "$SSH_KEY" != "" ] ; then echo "= SSH key $SSH_KEY : Not Found!" echo "= WARNING: No SSH Key Added." fi - + fi -# determine uboot and write to disk +# determine uboot and write to disk if [ "$TARGET" = "" ]; then echo "= No U-boot will be written." TARGET="Mystery Board" @@ -317,10 +237,11 @@ else fi -sync +sync umount $ROOTPART $BOOTPART &> /dev/null echo "" echo "= Installation Complete! Insert into the "$TARGET" and boot." exit 0 +"""