case "$MODE" in
    commandline)
        add_option "create-ext2-image" "`eval_gettext "create ext2 image for use with NBD"`" "advanced" "false"
        add_option "create-fs-image" "`eval_gettext "create image for use with NBD of the specified filesystem"`" "advanced" "true"
        ;;
    configure)
        if [ -n "$option_create_ext2_image_value" ]; then
            FS_IMAGE_TYPE=ext2 
        fi
        if [ -n "$option_create_fs_image_value" ]; then
            FS_IMAGE_TYPE="$option_create_fs_image_value"
        fi
        ;;
    before-install)
        if [ -n "$FS_IMAGE_TYPE" ]; then
            FS_IMAGE_SIZE=${FS_IMAGE_SIZE:-"2024"}
            FS_IMAGE_FILE=${FS_IMAGE_FILE:-"$BASE/images/$CHROOT.img"}

            # ensure needed directories exist
            mkdir -p $ROOT
            mkdir -p $BASE/images/

            # create a sparse file
            dd if=/dev/zero of=$FS_IMAGE_FILE.new bs=1024k count=0 seek=$FS_IMAGE_SIZE 2> /dev/null

            if [ -z "$MKFS_OPTS" ]; then
                case $FS_IMAGE_TYPE in
                    ext2|ext3) MKFS_OPTS="-q -F" ;;
                esac
            fi

            mkfs.$FS_IMAGE_TYPE $MKFS_OPTS $FS_IMAGE_FILE.new

            mount -o loop $FS_IMAGE_FILE.new $ROOT
        fi
    ;;
    finalization)
        if [ -n "$FS_IMAGE_TYPE" ]; then
            # call ltsp-update-image to update the nbd-server connfiguration
            # and set pxelinux and other boot parameters.
            ltsp-update-image --skip-image

            # configure ltsp-update-image to not handle image generation.
            echo SKIP_IMAGE=True >> /etc/ltsp/ltsp-update-image.conf

            # add $ROOT to list of files to be unmounted.  done in finalization
            # phase so it is the last to be unmounted.
            CHROOT_MOUNTED="$CHROOT_MOUNTED $ROOT"

            if [ -f "$FS_IMAGE_FILE" ]; then
                rm "$FS_IMAGE_FILE"
            fi
            if [ -f "$FS_IMAGE_FILE".new ]; then
                mv "$FS_IMAGE_FILE".new "$FS_IMAGE_FILE"
            fi
        fi
    ;;
esac
