util-disk.sh 36.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
# !/bin/bash
#
# Architect Installation Framework (2016-2017)
#
# Written by Carl Duff and @mandog for Archlinux
# Heavily modified and re-written by @Chrysostomus to install Manjaro instead
# Contributors: @papajoker, @oberon and the Manjaro-Community.
#
# This program is free software, provided under the GNU General Public License
# as published by the Free Software Foundation. So feel free to copy, distribute,
# or modify it as you wish.

Bernhard Landauer's avatar
Bernhard Landauer committed
13
# Unmount partitions.
Bernhard Landauer's avatar
Bernhard Landauer committed
14 15 16 17
umount_partitions() {
    MOUNTED=""
    MOUNTED=$(mount | grep "${MOUNTPOINT}" | awk '{print $3}' | sort -r)
    swapoff -a
Bernhard Landauer's avatar
Bernhard Landauer committed
18

19
    for i in ${MOUNTED[@]}; do
Bernhard Landauer's avatar
Bernhard Landauer committed
20
        umount $i >/dev/null 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
21
        check_for_error "unmount $i" $?
Bernhard Landauer's avatar
Bernhard Landauer committed
22 23
 #       local err=$(umount $i >/dev/null 2>$ERR)
 #       (( err !=0 )) && check_for_error "$FUNCNAME $i" $err
24
    done
Bernhard Landauer's avatar
Bernhard Landauer committed
25 26 27 28 29 30 31
}

# This function does not assume that the formatted device is the Root installation device as
# more than one device may be formatted. Root is set in the mount_partitions function.
select_device() {
    DEVICE=""
    devices_list=$(lsblk -lno NAME,SIZE,TYPE | grep 'disk' | awk '{print "/dev/" $1 " " $2}' | sort -u);
Bernhard Landauer's avatar
Bernhard Landauer committed
32

Bernhard Landauer's avatar
Bernhard Landauer committed
33 34 35
    for i in ${devices_list[@]}; do
        DEVICE="${DEVICE} ${i}"
    done
Bernhard Landauer's avatar
Bernhard Landauer committed
36

Bernhard Landauer's avatar
Bernhard Landauer committed
37
    DIALOG " $_DevSelTitle " --menu "\n$_DevSelBody\n " 0 0 4 ${DEVICE} 2>${ANSWER} || return 1
Bernhard Landauer's avatar
Bernhard Landauer committed
38 39
    DEVICE=$(cat ${ANSWER})
}
patrick's avatar
patrick committed
40

41 42
create_partitions() {
    # Partitioning Menu
Bernhard Landauer's avatar
Bernhard Landauer committed
43
    DIALOG " $_PrepPartDisk " --menu "\n$_PartToolBody\n " 0 0 7 \
44 45 46 47 48 49
      "$_PartOptWipe" "BIOS & UEFI" \
      "$_PartOptAuto" "BIOS & UEFI" \
      "cfdisk" "BIOS" \
      "cgdisk" "UEFI" \
      "fdisk"  "BIOS & UEFI" \
      "gdisk"  "UEFI" \
50
      "parted" "BIOS & UEFI" 2>${ANSWER} || return 0
51 52 53 54

    clear
    # If something selected
    if [[ $(cat ${ANSWER}) != "" ]]; then
Bernhard Landauer's avatar
Bernhard Landauer committed
55
        if ([[ $(cat ${ANSWER}) != "$_PartOptWipe" ]] && [[ $(cat ${ANSWER}) != "$_PartOptAuto" ]]); then
56 57 58 59 60 61
            $(cat ${ANSWER}) ${DEVICE}
        else
            [[ $(cat ${ANSWER}) == "$_PartOptWipe" ]] && secure_wipe && create_partitions
            [[ $(cat ${ANSWER}) == "$_PartOptAuto" ]] && auto_partition
        fi
    fi
62 63
}

64 65 66
# Securely destroy all data on a given device.
secure_wipe() {
    # Warn the user. If they proceed, wipe the selected device.
Bernhard Landauer's avatar
Bernhard Landauer committed
67
    DIALOG " $_PartOptWipe " --yesno "\n$_AutoPartWipeBody1 ${DEVICE} $_AutoPartWipeBody2\n " 0 0
68 69
    if [[ $? -eq 0 ]]; then
        # Install wipe where not already installed. Much faster than dd
Bernhard Landauer's avatar
Bernhard Landauer committed
70
        inst_needed wipe
71 72

        wipe -Ifre ${DEVICE} 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
73
        check_for_error "wipe ${DEVICE}" $?
74 75 76 77

        # Alternate dd command - requires pv to be installed
        #dd if=/dev/zero | pv | dd of=${DEVICE} iflag=nocache oflag=direct bs=4096 2>$ERR
    fi
78 79
}

80 81 82
# BIOS and UEFI
auto_partition() {
    # Provide warning to user
Bernhard Landauer's avatar
Bernhard Landauer committed
83
    DIALOG " $_PrepPartDisk " --yesno "\n$_AutoPartBody1 $DEVICE $_AutoPartBody2 $_AutoPartBody3\n " 0 0
84 85 86 87 88 89 90

    if [[ $? -eq 0 ]]; then
        # Find existing partitions (if any) to remove
        parted -s ${DEVICE} print | awk '/^ / {print $1}' > /tmp/.del_parts

        for del_part in $(tac /tmp/.del_parts); do
            parted -s ${DEVICE} rm ${del_part} 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
91
            check_for_error "rm ${del_part} on ${DEVICE}" $?
92 93 94 95
        done

        # Identify the partition table
        part_table=$(parted -s ${DEVICE} print | grep -i 'partition table' | awk '{print $3}' >/dev/null 2>&1)
Bernhard Landauer's avatar
Bernhard Landauer committed
96
        check_for_error "${DEVICE} is $part_table"
97 98 99 100

        # Create partition table if one does not already exist
        if [[ $SYSTEM == "BIOS" ]] && [[ $part_table != "msdos" ]] ; then 
            parted -s ${DEVICE} mklabel msdos 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
101
            check_for_error "${DEVICE} mklabel msdos" $?
patrick's avatar
patrick committed
102
        fi
103 104
        if [[ $SYSTEM == "UEFI" ]] && [[ $part_table != "gpt" ]] ; then 
            parted -s ${DEVICE} mklabel gpt 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
105
            check_for_error "${DEVICE} mklabel gpt" $?
106 107 108 109 110
        fi

        # Create partitions (same basic partitioning scheme for BIOS and UEFI)
        if [[ $SYSTEM == "BIOS" ]]; then
            parted -s ${DEVICE} mkpart primary ext3 1MiB 513MiB 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
111
            check_for_error "create ext3 513MiB on ${DEVICE}" $?
112 113
        else
            parted -s ${DEVICE} mkpart ESP fat32 1MiB 513MiB 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
114
            check_for_error "create ESP on ${DEVICE}" $?
115 116 117 118 119
        fi

        parted -s ${DEVICE} set 1 boot on 2>$ERR
        check_for_error "set boot flag for ${DEVICE}" $?
        parted -s ${DEVICE} mkpart primary ext3 513MiB 100% 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
120
        check_for_error "create ext3 100% on ${DEVICE}" $?
121 122 123 124 125

        # Show created partitions
        lsblk ${DEVICE} -o NAME,TYPE,FSTYPE,SIZE > /tmp/.devlist
        DIALOG "" --textbox /tmp/.devlist 0 0
    fi
patrick's avatar
patrick committed
126
}
Bernhard Landauer's avatar
Bernhard Landauer committed
127
    
Bernhard Landauer's avatar
Bernhard Landauer committed
128 129 130 131 132 133
# Finds all available partitions according to type(s) specified and generates a list
# of them. This also includes partitions on different devices.
find_partitions() {
    PARTITIONS=""
    NUMBER_PARTITIONS=0
    partition_list=$(lsblk -lno NAME,SIZE,TYPE | grep $INCLUDE_PART | sed 's/part$/\/dev\//g' | sed 's/lvm$\|crypt$/\/dev\/mapper\//g' | \
134
    awk '{print $3$1 " " $2}' | awk '!/mapper/{a[++i]=$0;next}1;END{while(x<length(a))print a[++x]}')
Bernhard Landauer's avatar
Bernhard Landauer committed
135 136 137 138 139 140 141 142 143

    for i in ${partition_list}; do
        PARTITIONS="${PARTITIONS} ${i}"
        NUMBER_PARTITIONS=$(( NUMBER_PARTITIONS + 1 ))
    done

    # Double-partitions will be counted due to counting sizes, so fix
    NUMBER_PARTITIONS=$(( NUMBER_PARTITIONS / 2 ))

patrick's avatar
patrick committed
144 145 146 147 148 149 150 151 152 153 154
    check_for_error "--------- [lsblk] ------------"
    local parts=($PARTITIONS)
    for i in ${!parts[@]}; do
        (( $i % 2 == 0 )) || continue
        local j=$((i+1))
        check_for_error "${parts[i]} ${parts[j]}"
    done    

    #for test delete /dev:sda8
    #delete_partition_in_list "/dev/sda8"

Bernhard Landauer's avatar
Bernhard Landauer committed
155 156 157 158 159
    # Deal with partitioning schemes appropriate to mounting, lvm, and/or luks.
    case $INCLUDE_PART in
        'part\|lvm\|crypt')
            # Deal with incorrect partitioning for main mounting function
            if ([[ $SYSTEM == "UEFI" ]] && [[ $NUMBER_PARTITIONS -lt 2 ]]) || ([[ $SYSTEM == "BIOS" ]] && [[ $NUMBER_PARTITIONS -eq 0 ]]); then
Bernhard Landauer's avatar
Bernhard Landauer committed
160
                DIALOG " $_ErrTitle " --msgbox "\n$_PartErrBody\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
161
                create_partitions
Bernhard Landauer's avatar
Bernhard Landauer committed
162 163 164 165 166
            fi
            ;;
        'part\|crypt')
            # Ensure there is at least one partition for LVM
            if [[ $NUMBER_PARTITIONS -eq 0 ]]; then
Bernhard Landauer's avatar
Bernhard Landauer committed
167
                DIALOG " $_ErrTitle " --msgbox "\n$_LvmPartErrBody\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
168
                create_partitions
Bernhard Landauer's avatar
Bernhard Landauer committed
169 170 171 172
            fi
            ;;
        'part\|lvm') # Ensure there are at least two partitions for LUKS
            if [[ $NUMBER_PARTITIONS -lt 2 ]]; then
Bernhard Landauer's avatar
Bernhard Landauer committed
173
                DIALOG " $_ErrTitle " --msgbox "\n$_LuksPartErrBody\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
174
                create_partitions
Bernhard Landauer's avatar
Bernhard Landauer committed
175 176 177 178
            fi
            ;;
    esac    
}
Bernhard Landauer's avatar
Bernhard Landauer committed
179

180 181 182
## List partitions to be hidden from the mounting menu
list_mounted() {
    lsblk -l | awk '$7 ~ /mnt/ {print $1}' > /tmp/.mounted
Bernhard Landauer's avatar
Bernhard Landauer committed
183
    check_for_error "already mounted: $(cat /tmp/.mounted)"
184
    echo /dev/* /dev/mapper/* | xargs -n1 2>/dev/null | grep -f /tmp/.mounted
185
}
Bernhard Landauer's avatar
Bernhard Landauer committed
186

187 188 189
list_containing_crypt() {
    blkid | awk '/TYPE="crypto_LUKS"/{print $1}' | sed 's/.$//'
}
Bernhard Landauer's avatar
Bernhard Landauer committed
190

Matti Hyttinen's avatar
Matti Hyttinen committed
191 192 193 194
list_non_crypt() {
    blkid | awk '!/TYPE="crypto_LUKS"/{print $1}' | sed 's/.$//'
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
# delete partition in list $PARTITIONS
# param: partition to delete
delete_partition_in_list() {
    [ -z "$1" ] && return 127
    local parts=($PARTITIONS)
    for i in ${!parts[@]}; do
        (( $i % 2 == 0 )) || continue
        if [[ "${parts[i]}" = "$1" ]]; then
            local j=$((i+1))
            unset parts[$j]
            unset parts[$i]
            check_for_error "in partitions delete item $1 no: $i / $j"
            PARTITIONS="${parts[*]}"
            check_for_error "partitions: $PARTITIONS"
            NUMBER_PARTITIONS=$(( "${#parts[*]}" / 2 ))
Matti Hyttinen's avatar
adjust  
Matti Hyttinen committed
210
            return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
211
        fi
212
    done
Matti Hyttinen's avatar
Matti Hyttinen committed
213
    return 0
214
}
Bernhard Landauer's avatar
Bernhard Landauer committed
215

216 217 218
# Revised to deal with partion sizes now being displayed to the user
confirm_mount() {
    if [[ $(mount | grep $1) ]]; then
Bernhard Landauer's avatar
Bernhard Landauer committed
219
        DIALOG " $_MntStatusTitle " --infobox "\n$_MntStatusSucc\n " 0 0
220 221 222 223
        sleep 2
        PARTITIONS=$(echo $PARTITIONS | sed "s~${PARTITION} [0-9]*[G-M]~~" | sed "s~${PARTITION} [0-9]*\.[0-9]*[G-M]~~" | sed s~${PARTITION}$' -'~~)
        NUMBER_PARTITIONS=$(( NUMBER_PARTITIONS - 1 ))
    else
Bernhard Landauer's avatar
Bernhard Landauer committed
224
        DIALOG " $_MntStatusTitle " --infobox "\n$_MntStatusFail\n " 0 0
225
        sleep 2
226
        return 1
Bernhard Landauer's avatar
Bernhard Landauer committed
227 228
    fi
}
Bernhard Landauer's avatar
Bernhard Landauer committed
229

Bernhard Landauer's avatar
Bernhard Landauer committed
230
# Set static list of filesystems rather than on-the-fly. Partially as most require additional flags, and
Bernhard Landauer's avatar
Bernhard Landauer committed
231 232 233 234 235 236 237
# partially because some don't seem to be viable.
# Set static list of filesystems rather than on-the-fly.
select_filesystem() {
    # prep variables
    fs_opts=""
    CHK_NUM=0

238 239 240 241 242 243 244 245 246 247 248
    DIALOG " $_FSTitle " --menu "\n$_FSBody\n " 0 0 12 \
      "$_FSSkip" "-" \
        "btrfs" "mkfs.btrfs -f" \
        "ext3" "mkfs.ext3 -q" \
        "ext4" "mkfs.ext4 -q" \
        "jfs" "mkfs.jfs -q" \
        "nilfs2" "mkfs.nilfs2 -fq" \
        "ntfs" "mkfs.ntfs -q" \
        "reiserfs" "mkfs.reiserfs -q" \
        "vfat" "mkfs.vfat -F32" \
        "xfs" "mkfs.xfs -f" 2>${ANSWER} || return 1
Bernhard Landauer's avatar
Bernhard Landauer committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
        
    case $(cat ${ANSWER}) in
        "$_FSSkip") FILESYSTEM="$_FSSkip"
            ;;
        "btrfs") FILESYSTEM="mkfs.btrfs -f"
            CHK_NUM=16
            fs_opts="autodefrag compress=zlib compress=lzo compress=no compress-force=zlib compress-force=lzo discard \
            noacl noatime nodatasum nospace_cache recovery skip_balance space_cache ssd ssd_spread"
            modprobe btrfs
            ;;
        "ext2") FILESYSTEM="mkfs.ext2 -q"
            ;;
        "ext3") FILESYSTEM="mkfs.ext3 -q"
            ;;
        "ext4") FILESYSTEM="mkfs.ext4 -q"
            CHK_NUM=8
Bernhard Landauer's avatar
Bernhard Landauer committed
265
            fs_opts="data=journal data=writeback dealloc discard noacl noatime nobarrier nodelalloc"
Bernhard Landauer's avatar
Bernhard Landauer committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
            ;;
        "f2fs") FILESYSTEM="mkfs.f2fs -q"
            fs_opts="data_flush disable_roll_forward disable_ext_identify discard fastboot flush_merge \
            inline_xattr inline_data inline_dentry no_heap noacl nobarrier noextent_cache noinline_data norecovery"
            CHK_NUM=16
            modprobe f2fs
            ;;
        "jfs") FILESYSTEM="mkfs.jfs -q"
            CHK_NUM=4
            fs_opts="discard errors=continue errors=panic nointegrity"
            ;;
        "nilfs2") FILESYSTEM="mkfs.nilfs2 -fq"
            CHK_NUM=7
            fs_opts="discard nobarrier errors=continue errors=panic order=relaxed order=strict norecovery"
            ;;
        "ntfs") FILESYSTEM="mkfs.ntfs -q"
            ;;
        "reiserfs") FILESYSTEM="mkfs.reiserfs -q"
            CHK_NUM=5
            fs_opts="acl nolog notail replayonly user_xattr"
            ;;
        "vfat") FILESYSTEM="mkfs.vfat -F32"
            ;;
        "xfs") FILESYSTEM="mkfs.xfs -f"
            CHK_NUM=9
            fs_opts="discard filestreams ikeep largeio noalign nobarrier norecovery noquota wsync"
            ;;
Bernhard Landauer's avatar
Bernhard Landauer committed
293
        *)  return 1
Bernhard Landauer's avatar
Bernhard Landauer committed
294 295 296 297 298
            ;;
    esac

    # Warn about formatting!
    if [[ $FILESYSTEM != $_FSSkip ]]; then
Bernhard Landauer's avatar
Bernhard Landauer committed
299
        DIALOG " $_FSTitle " --yesno "\n$_FSMount $FILESYSTEM\n\n! $_FSWarn1 $PARTITION $_FSWarn2 !\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
300 301
        if (( $? != 1 )); then
            ${FILESYSTEM} ${PARTITION} >/dev/null 2>$ERR
302 303
            check_for_error "mount ${PARTITION} as ${FILESYSTEM}." $? || return 1
            ini "mount.${PARTITION}" $(echo "${FILESYSTEM:5}|cut -d' ' -f1")
Bernhard Landauer's avatar
Bernhard Landauer committed
304
        fi
Bernhard Landauer's avatar
Bernhard Landauer committed
305
    fi
Bernhard Landauer's avatar
Bernhard Landauer committed
306
}
307

308 309 310 311
# This subfunction allows for special mounting options to be applied for relevant fs's.
# Seperate subfunction for neatness.
mount_opts() {
    FS_OPTS=""
312

313 314 315
    for i in ${fs_opts}; do
        FS_OPTS="${FS_OPTS} ${i} - off"
    done
316

Bernhard Landauer's avatar
Bernhard Landauer committed
317
    DIALOG " $(echo $FILESYSTEM | sed "s/.*\.//g;s/-.*//g") " --checklist "\n$_btrfsMntBody\n " 0 0 \
Bernhard Landauer's avatar
Bernhard Landauer committed
318
      $CHK_NUM $FS_OPTS 2>${MOUNT_OPTS}
319

320 321 322
    # Now clean up the file
    sed -i 's/ /,/g' ${MOUNT_OPTS}
    sed -i '$s/,$//' ${MOUNT_OPTS}
Bernhard Landauer's avatar
Bernhard Landauer committed
323

324 325
    # If mount options selected, confirm choice
    if [[ $(cat ${MOUNT_OPTS}) != "" ]]; then
Bernhard Landauer's avatar
Bernhard Landauer committed
326
        DIALOG " $_MntStatusTitle " --yesno "\n${_btrfsMntConfBody}$(cat ${MOUNT_OPTS})\n " 10 75
Bernhard Landauer's avatar
Bernhard Landauer committed
327
        [[ $? -eq 1 ]] && echo "" > ${MOUNT_OPTS}
328 329
    fi
}
Bernhard Landauer's avatar
Bernhard Landauer committed
330

331 332 333
mount_current_partition() {
    # Make the mount directory
    mkdir -p ${MOUNTPOINT}${MOUNT} 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
334
    check_for_error "create mountpoint ${MOUNTPOINT}${MOUNT}" "$?"
Bernhard Landauer's avatar
Bernhard Landauer committed
335

336
    echo "" > ${MOUNT_OPTS}
337 338
    # Get mounting options for appropriate filesystems
    [[ $fs_opts != "" ]] && mount_opts
Bernhard Landauer's avatar
Bernhard Landauer committed
339

340 341
    # Use special mounting options if selected, else standard mount
    if [[ $(cat ${MOUNT_OPTS}) != "" ]]; then
Bernhard Landauer's avatar
Bernhard Landauer committed
342
        check_for_error "mount ${PARTITION} $(cat ${MOUNT_OPTS})"
343 344
        mount -o $(cat ${MOUNT_OPTS}) ${PARTITION} ${MOUNTPOINT}${MOUNT} 2>>$LOGFILE
    else
Bernhard Landauer's avatar
Bernhard Landauer committed
345
        check_for_error "mount ${PARTITION}"
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
        mount ${PARTITION} ${MOUNTPOINT}${MOUNT} 2>>$LOGFILE
    fi
    confirm_mount ${MOUNTPOINT}${MOUNT}

    # Identify if mounted partition is type "crypt" (LUKS on LVM, or LUKS alone)
    if [[ $(lsblk -lno TYPE ${PARTITION} | grep "crypt") != "" ]]; then
        # cryptname for bootloader configuration either way
        LUKS=1
        LUKS_NAME=$(echo ${PARTITION} | sed "s~^/dev/mapper/~~g")

        # Check if LUKS on LVM (parent = lvm /dev/mapper/...)
        cryptparts=$(lsblk -lno NAME,FSTYPE,TYPE | grep "lvm" | grep -i "crypto_luks" | uniq | awk '{print "/dev/mapper/"$1}')
        for i in ${cryptparts}; do
            if [[ $(lsblk -lno NAME ${i} | grep $LUKS_NAME) != "" ]]; then
                LUKS_DEV="$LUKS_DEV cryptdevice=${i}:$LUKS_NAME"
                LVM=1
Matti Hyttinen's avatar
Matti Hyttinen committed
362
                return 0;
363 364
            fi
        done
Bernhard Landauer's avatar
Bernhard Landauer committed
365

366 367 368 369 370 371
        # Check if LUKS alone (parent = part /dev/...)
        cryptparts=$(lsblk -lno NAME,FSTYPE,TYPE | grep "part" | grep -i "crypto_luks" | uniq | awk '{print "/dev/"$1}')
        for i in ${cryptparts}; do
            if [[ $(lsblk -lno NAME ${i} | grep $LUKS_NAME) != "" ]]; then
                LUKS_UUID=$(lsblk -lno UUID,TYPE,FSTYPE ${i} | grep "part" | grep -i "crypto_luks" | awk '{print $1}')
                LUKS_DEV="$LUKS_DEV cryptdevice=UUID=$LUKS_UUID:$LUKS_NAME"
Matti Hyttinen's avatar
Matti Hyttinen committed
372
                return 0;
373 374
            fi
        done
Bernhard Landauer's avatar
Bernhard Landauer committed
375

376 377 378
        # If LVM logical volume....
    elif [[ $(lsblk -lno TYPE ${PARTITION} | grep "lvm") != "" ]]; then
        LVM=1
Bernhard Landauer's avatar
Bernhard Landauer committed
379

380 381 382 383 384
        # First get crypt name (code above would get lv name)
        cryptparts=$(lsblk -lno NAME,TYPE,FSTYPE | grep "crypt" | grep -i "lvm2_member" | uniq | awk '{print "/dev/mapper/"$1}')
        for i in ${cryptparts}; do
            if [[ $(lsblk -lno NAME ${i} | grep $(echo $PARTITION | sed "s~^/dev/mapper/~~g")) != "" ]]; then
                LUKS_NAME=$(echo ${i} | sed s~/dev/mapper/~~g)
Matti Hyttinen's avatar
Matti Hyttinen committed
385
                return 0;
386 387
            fi
        done
Bernhard Landauer's avatar
Bernhard Landauer committed
388

389 390 391 392 393 394
        # Now get the device (/dev/...) for the crypt name
        cryptparts=$(lsblk -lno NAME,FSTYPE,TYPE | grep "part" | grep -i "crypto_luks" | uniq | awk '{print "/dev/"$1}')
        for i in ${cryptparts}; do
            if [[ $(lsblk -lno NAME ${i} | grep $LUKS_NAME) != "" ]]; then
                # Create UUID for comparison
                LUKS_UUID=$(lsblk -lno UUID,TYPE,FSTYPE ${i} | grep "part" | grep -i "crypto_luks" | awk '{print $1}')
Bernhard Landauer's avatar
Bernhard Landauer committed
395

396 397 398 399
                # Check if not already added as a LUKS DEVICE (i.e. multiple LVs on one crypt). If not, add.
                if [[ $(echo $LUKS_DEV | grep $LUKS_UUID) == "" ]]; then
                    LUKS_DEV="$LUKS_DEV cryptdevice=UUID=$LUKS_UUID:$LUKS_NAME"
                    LUKS=1
Bernhard Landauer's avatar
Bernhard Landauer committed
400
                fi
Bernhard Landauer's avatar
Bernhard Landauer committed
401

Matti Hyttinen's avatar
Matti Hyttinen committed
402
                return 0;
Bernhard Landauer's avatar
Bernhard Landauer committed
403
            fi
404
        done
Bernhard Landauer's avatar
Bernhard Landauer committed
405
    fi
406
}
Bernhard Landauer's avatar
Bernhard Landauer committed
407

408 409
make_swap() {
    # Ask user to select partition or create swapfile
Bernhard Landauer's avatar
Bernhard Landauer committed
410
    DIALOG " $_PrepMntPart " --menu "\n$_SelSwpBody\n " 0 0 12 "$_SelSwpNone" $"-" "$_SelSwpFile" $"-" ${PARTITIONS} 2>${ANSWER} || return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
411

412 413
    if [[ $(cat ${ANSWER}) != "$_SelSwpNone" ]]; then
        PARTITION=$(cat ${ANSWER})
Bernhard Landauer's avatar
Bernhard Landauer committed
414

415 416
        if [[ $PARTITION == "$_SelSwpFile" ]]; then
            total_memory=$(grep MemTotal /proc/meminfo | awk '{print $2/1024}' | sed 's/\..*//')
Bernhard Landauer's avatar
Bernhard Landauer committed
417
            DIALOG " $_SelSwpFile " --inputbox "\nM = MB, G = GB\n " 9 30 "${total_memory}M" 2>${ANSWER} || return 0
418
            m_or_g=$(cat ${ANSWER})
Bernhard Landauer's avatar
Bernhard Landauer committed
419

420
            while [[ $(echo ${m_or_g: -1} | grep "M\|G") == "" ]]; do
Bernhard Landauer's avatar
Bernhard Landauer committed
421 422
                DIALOG " $_SelSwpFile " --msgbox "\n$_SelSwpFile $_ErrTitle: M = MB, G = GB\n " 0 0
                DIALOG " $_SelSwpFile " --inputbox "\nM = MB, G = GB\n " 9 30 "${total_memory}M" 2>${ANSWER} || return 0
423
                m_or_g=$(cat ${ANSWER})
Bernhard Landauer's avatar
Bernhard Landauer committed
424
            done
Bernhard Landauer's avatar
Bernhard Landauer committed
425

426
            fallocate -l ${m_or_g} ${MOUNTPOINT}/swapfile 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
427
            check_for_error "Swapfile fallocate" "$?"
428
            chmod 600 ${MOUNTPOINT}/swapfile 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
429
            check_for_error "Swapfile chmod" "$?"
430
            mkswap ${MOUNTPOINT}/swapfile 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
431
            check_for_error "Swapfile mkswap" "$?"
432
            swapon ${MOUNTPOINT}/swapfile 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
433
            check_for_error "Swapfile swapon" "$?"
434 435 436 437

        else # Swap Partition
            # Warn user if creating a new swap
            if [[ $(lsblk -o FSTYPE  ${PARTITION} | grep -i "swap") != "swap" ]]; then
Bernhard Landauer's avatar
Bernhard Landauer committed
438
                DIALOG " $_PrepMntPart " --yesno "\nmkswap ${PARTITION}\n " 0 0
439 440
                if [[ $? -eq 0 ]]; then
                    mkswap ${PARTITION} >/dev/null 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
441
                    check_for_error "Swap partition: mkswap" "$?"
442
                else
Matti Hyttinen's avatar
Matti Hyttinen committed
443
                    return 0
444
                fi
Bernhard Landauer's avatar
Bernhard Landauer committed
445
            fi
446 447
            # Whether existing to newly created, activate swap
            swapon  ${PARTITION} >/dev/null 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
448
            check_for_error "Swap partition: swapon" "$?"
449 450 451
            # Since a partition was used, remove that partition from the list
            PARTITIONS=$(echo $PARTITIONS | sed "s~${PARTITION} [0-9]*[G-M]~~" | sed "s~${PARTITION} [0-9]*\.[0-9]*[G-M]~~" | sed s~${PARTITION}$' -'~~)
            NUMBER_PARTITIONS=$(( NUMBER_PARTITIONS - 1 ))
Bernhard Landauer's avatar
Bernhard Landauer committed
452
        fi
453
    fi
454
    ini mount.swap "${PARTITION}"
Bernhard Landauer's avatar
Bernhard Landauer committed
455
}
Bernhard Landauer's avatar
Bernhard Landauer committed
456

Bernhard Landauer's avatar
Bernhard Landauer committed
457 458 459 460
# Had to write it in this way due to (bash?) bug(?), as if/then statements in a single
# "create LUKS" function for default and "advanced" modes were interpreted as commands,
# not mere string statements. Not happy with it, but it works...
luks_password() {
Bernhard Landauer's avatar
Bernhard Landauer committed
461
    DIALOG " $_PrepLUKS " --clear --insecure --passwordbox "\n$_LuksPassBody\n " 0 0 2> ${ANSWER} || return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
462
    PASSWD=$(cat ${ANSWER})
Bernhard Landauer's avatar
Bernhard Landauer committed
463

Bernhard Landauer's avatar
Bernhard Landauer committed
464
    DIALOG " $_PrepLUKS " --clear --insecure --passwordbox "\n$_PassReEntBody\n " 0 0 2> ${ANSWER} || return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
465
    PASSWD2=$(cat ${ANSWER})
Bernhard Landauer's avatar
Bernhard Landauer committed
466

Bernhard Landauer's avatar
Bernhard Landauer committed
467
    if [[ $PASSWD != $PASSWD2 ]]; then
Bernhard Landauer's avatar
Bernhard Landauer committed
468
        DIALOG " $_ErrTitle " --msgbox "\n$_PassErrBody\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
469
        luks_password
Bernhard Landauer's avatar
Bernhard Landauer committed
470 471
    fi
}
Bernhard Landauer's avatar
Bernhard Landauer committed
472

Bernhard Landauer's avatar
Bernhard Landauer committed
473 474 475 476 477
luks_open() {
    LUKS_ROOT_NAME=""
    INCLUDE_PART='part\|crypt\|lvm'
    umount_partitions
    find_partitions
Matti Hyttinen's avatar
Matti Hyttinen committed
478 479 480 481 482 483
    # Filter out partitions that don't contain crypt device
    list_non_crypt > /tmp/.ignore_part
 
    for part in $(cat /tmp/.ignore_part); do
        delete_partition_in_list $part
    done
Bernhard Landauer's avatar
Bernhard Landauer committed
484

Bernhard Landauer's avatar
Bernhard Landauer committed
485
    # Select encrypted partition to open
Bernhard Landauer's avatar
Bernhard Landauer committed
486
    DIALOG " $_LuksOpen " --menu "\n$_LuksMenuBody\n " 0 0 12 ${PARTITIONS} 2>${ANSWER} || return 1
Bernhard Landauer's avatar
Bernhard Landauer committed
487
    PARTITION=$(cat ${ANSWER})
Bernhard Landauer's avatar
Bernhard Landauer committed
488

Bernhard Landauer's avatar
Bernhard Landauer committed
489
    # Enter name of the Luks partition and get password to open it
Bernhard Landauer's avatar
Bernhard Landauer committed
490
    DIALOG " $_LuksOpen " --inputbox "\n$_LuksOpenBody\n " 10 50 "cryptroot" 2>${ANSWER} || return 1
Bernhard Landauer's avatar
Bernhard Landauer committed
491 492
    LUKS_ROOT_NAME=$(cat ${ANSWER})
    luks_password
Bernhard Landauer's avatar
Bernhard Landauer committed
493

Bernhard Landauer's avatar
Bernhard Landauer committed
494 495
    # Try to open the luks partition with the credentials given. If successful show this, otherwise
    # show the error
Bernhard Landauer's avatar
Bernhard Landauer committed
496
    DIALOG " $_LuksOpen " --infobox "\n$_PlsWaitBody\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
497
    echo $PASSWD | cryptsetup open --type luks ${PARTITION} ${LUKS_ROOT_NAME} 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
498
    check_for_error "luks pwd ${PARTITION} ${LUKS_ROOT_NAME}" "$?"
Bernhard Landauer's avatar
Bernhard Landauer committed
499

Bernhard Landauer's avatar
Bernhard Landauer committed
500 501 502
    lsblk -o NAME,TYPE,FSTYPE,SIZE,MOUNTPOINT ${PARTITION} | grep "crypt\|NAME\|MODEL\|TYPE\|FSTYPE\|SIZE" > /tmp/.devlist
    DIALOG " $_DevShowOpt " --textbox /tmp/.devlist 0 0
}
Bernhard Landauer's avatar
Bernhard Landauer committed
503

Bernhard Landauer's avatar
Bernhard Landauer committed
504 505 506 507 508 509
luks_setup() {
    modprobe -a dm-mod dm_crypt
    INCLUDE_PART='part\|lvm'
    umount_partitions
    find_partitions
    # Select partition to encrypt
Bernhard Landauer's avatar
Bernhard Landauer committed
510
    DIALOG " $_LuksEncrypt " --menu "\n$_LuksCreateBody\n " 0 0 12 ${PARTITIONS} 2>${ANSWER} || return 1
Bernhard Landauer's avatar
Bernhard Landauer committed
511
    PARTITION=$(cat ${ANSWER})
Bernhard Landauer's avatar
Bernhard Landauer committed
512

Bernhard Landauer's avatar
Bernhard Landauer committed
513
    # Enter name of the Luks partition and get password to create it
Bernhard Landauer's avatar
Bernhard Landauer committed
514
    DIALOG " $_LuksEncrypt " --inputbox "\n$_LuksOpenBody\n " 10 50 "cryptroot" 2>${ANSWER} || return 1
Bernhard Landauer's avatar
Bernhard Landauer committed
515 516 517 518 519 520
    LUKS_ROOT_NAME=$(cat ${ANSWER})
    luks_password
}

luks_default() {
    # Encrypt selected partition or LV with credentials given
Bernhard Landauer's avatar
Bernhard Landauer committed
521
    DIALOG " $_LuksEncrypt " --infobox "\n$_PlsWaitBody\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
522
    sleep 2
Bernhard Landauer's avatar
Bernhard Landauer committed
523
    echo $PASSWD | cryptsetup -q luksFormat ${PARTITION} 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
524
    check_for_error "luksFormat ${PARTITION}" $?
Bernhard Landauer's avatar
Bernhard Landauer committed
525 526

    # Now open the encrypted partition or LV
Bernhard Landauer's avatar
Bernhard Landauer committed
527
    echo $PASSWD | cryptsetup open ${PARTITION} ${LUKS_ROOT_NAME} 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
528
    check_for_error "open ${PARTITION} ${LUKS_ROOT_NAME}" $?
Bernhard Landauer's avatar
Bernhard Landauer committed
529 530 531
}

luks_key_define() {
Bernhard Landauer's avatar
Bernhard Landauer committed
532
    DIALOG " $_PrepLUKS " --inputbox "\n$_LuksCipherKey\n " 0 0 "-s 512 -c aes-xts-plain64" 2>${ANSWER} || return 1
Bernhard Landauer's avatar
Bernhard Landauer committed
533 534

    # Encrypt selected partition or LV with credentials given
Bernhard Landauer's avatar
Bernhard Landauer committed
535
    DIALOG " $_LuksEncryptAdv " --infobox "\n$_PlsWaitBody\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
536 537
    sleep 2

Bernhard Landauer's avatar
Bernhard Landauer committed
538
    echo $PASSWD | cryptsetup -q $(cat ${ANSWER}) luksFormat ${PARTITION} 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
539
    check_for_error "encrypt ${PARTITION}" "$?"
Bernhard Landauer's avatar
Bernhard Landauer committed
540 541

    # Now open the encrypted partition or LV
Bernhard Landauer's avatar
Bernhard Landauer committed
542
    echo $PASSWD | cryptsetup open ${PARTITION} ${LUKS_ROOT_NAME} 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
543
    check_for_error "open ${PARTITION} ${LUKS_ROOT_NAME}" "$?"
Bernhard Landauer's avatar
Bernhard Landauer committed
544 545 546 547 548 549 550 551 552 553 554
}

luks_show() {
    echo -e ${_LuksEncruptSucc} > /tmp/.devlist
    lsblk -o NAME,TYPE,FSTYPE,SIZE ${PARTITION} | grep "part\|crypt\|NAME\|TYPE\|FSTYPE\|SIZE" >> /tmp/.devlist
    DIALOG " $_LuksEncrypt " --textbox /tmp/.devlist 0 0
}

luks_menu() {
    LUKS_OPT=""

Bernhard Landauer's avatar
Bernhard Landauer committed
555
    DIALOG " $_PrepLUKS " --menu "\n$_LuksMenuBody$_LuksMenuBody2$_LuksMenuBody3\n " 0 0 4 \
Bernhard Landauer's avatar
Bernhard Landauer committed
556 557 558 559 560 561 562 563
      "$_LuksOpen" "cryptsetup open --type luks" \
      "$_LuksEncrypt" "cryptsetup -q luksFormat" \
      "$_LuksEncryptAdv" "cryptsetup -q -s -c luksFormat" \
      "$_Back" "-" 2>${ANSWER}

    case $(cat ${ANSWER}) in
        "$_LuksOpen") luks_open
            ;;
564
        "$_LuksEncrypt") luks_setup && luks_default && luks_show
Bernhard Landauer's avatar
Bernhard Landauer committed
565
            ;;
566
        "$_LuksEncryptAdv") luks_setup && luks_key_define && luks_show
Bernhard Landauer's avatar
Bernhard Landauer committed
567
            ;;
Matti Hyttinen's avatar
Matti Hyttinen committed
568
        *) return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
569 570 571 572 573 574 575 576 577 578
            ;;
    esac
}

lvm_detect() {
    LVM_PV=$(pvs -o pv_name --noheading 2>/dev/null)
    LVM_VG=$(vgs -o vg_name --noheading 2>/dev/null)
    LVM_LV=$(lvs -o vg_name,lv_name --noheading --separator - 2>/dev/null)

    if [[ $LVM_LV != "" ]] && [[ $LVM_VG != "" ]] && [[ $LVM_PV != "" ]]; then
Bernhard Landauer's avatar
Bernhard Landauer committed
579
        DIALOG " $_PrepLVM " --infobox "\n$_LvmDetBody\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
580
        modprobe dm-mod 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
581
        check_for_error "modprobe dm-mod" "$?"
Bernhard Landauer's avatar
Bernhard Landauer committed
582 583 584 585
        vgscan >/dev/null 2>&1
        vgchange -ay >/dev/null 2>&1
    fi
}
Bernhard Landauer's avatar
Bernhard Landauer committed
586

Bernhard Landauer's avatar
Bernhard Landauer committed
587 588 589
lvm_show_vg() {
    VG_LIST=""
    vg_list=$(lvs --noheadings | awk '{print $2}' | uniq)
Bernhard Landauer's avatar
Bernhard Landauer committed
590

Bernhard Landauer's avatar
Bernhard Landauer committed
591 592 593 594 595 596 597 598 599 600 601 602 603
    for i in ${vg_list}; do
        VG_LIST="${VG_LIST} ${i} $(vgdisplay ${i} | grep -i "vg size" | awk '{print $3$4}')"
    done
}

# Create Volume Group and Logical Volumes
lvm_create() {
    # Find LVM appropriate partitions.
    INCLUDE_PART='part\|crypt'
    umount_partitions
    find_partitions
    # Amend partition(s) found for use in check list
    PARTITIONS=$(echo $PARTITIONS | sed 's/M\|G\|T/& off/g')
Bernhard Landauer's avatar
Bernhard Landauer committed
604

Bernhard Landauer's avatar
Bernhard Landauer committed
605
    # Name the Volume Group
Bernhard Landauer's avatar
Bernhard Landauer committed
606
    DIALOG " $_LvmCreateVG " --inputbox "\n$_LvmNameVgBody\n " 0 0 2>${ANSWER} || return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
607
    LVM_VG=$(cat ${ANSWER})
Bernhard Landauer's avatar
Bernhard Landauer committed
608

Bernhard Landauer's avatar
Bernhard Landauer committed
609 610
    # Loop while the Volume Group name starts with a "/", is blank, has spaces, or is already being used
    while [[ ${LVM_VG:0:1} == "/" ]] || [[ ${#LVM_VG} -eq 0 ]] || [[ $LVM_VG =~ \ |\' ]] || [[ $(lsblk | grep ${LVM_VG}) != "" ]]; do
Bernhard Landauer's avatar
Bernhard Landauer committed
611 612
        DIALOG " $_ErrTitle " --msgbox "\n$_LvmNameVgErr\n " 0 0
        DIALOG " $_LvmCreateVG " --inputbox "\n$_LvmNameVgBody\n " 0 0 "" 2>${ANSWER} || return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
613
        LVM_VG=$(cat ${ANSWER})
Bernhard Landauer's avatar
Bernhard Landauer committed
614
    done
Bernhard Landauer's avatar
Bernhard Landauer committed
615

Bernhard Landauer's avatar
Bernhard Landauer committed
616
    # Select the partition(s) for the Volume Group
Bernhard Landauer's avatar
Bernhard Landauer committed
617
    DIALOG " $_LvmCreateVG " --checklist "\n$_LvmPvSelBody\n\n$_UseSpaceBar\n " 0 0 12 ${PARTITIONS} 2>${ANSWER} || return 0
Matti Hyttinen's avatar
Matti Hyttinen committed
618
    [[ $(cat ${ANSWER}) != "" ]] && VG_PARTS=$(cat ${ANSWER}) || return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
619

Bernhard Landauer's avatar
Bernhard Landauer committed
620 621
    # Once all the partitions have been selected, show user. On confirmation, use it/them in 'vgcreate' command.
    # Also determine the size of the VG, to use for creating LVs for it.
Bernhard Landauer's avatar
Bernhard Landauer committed
622
    DIALOG " $_LvmCreateVG " --yesno "\n$_LvmPvConfBody1 [${LVM_VG}] $_LvmPvConfBody2\n${VG_PARTS}\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
623

Bernhard Landauer's avatar
Bernhard Landauer committed
624
    if [[ $? -eq 0 ]]; then
Bernhard Landauer's avatar
Bernhard Landauer committed
625
        DIALOG " $_LvmCreateVG " --infobox "\n$_LvmPvActBody1 [${LVM_VG}].\n$_PlsWaitBody\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
626
        sleep 1
Bernhard Landauer's avatar
Bernhard Landauer committed
627
        vgcreate -f ${LVM_VG} ${VG_PARTS} >/dev/null 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
628
        check_for_error "vgcreate -f ${LVM_VG} ${VG_PARTS}" "$?"
Bernhard Landauer's avatar
Bernhard Landauer committed
629

Bernhard Landauer's avatar
Bernhard Landauer committed
630 631 632
        # Once created, get size and size type for display and later number-crunching for lv creation
        VG_SIZE=$(vgdisplay $LVM_VG | grep 'VG Size' | awk '{print $3}' | sed 's/\..*//')
        VG_SIZE_TYPE=$(vgdisplay $LVM_VG | grep 'VG Size' | awk '{print $4}')
Bernhard Landauer's avatar
Bernhard Landauer committed
633

Bernhard Landauer's avatar
Bernhard Landauer committed
634 635
        # Convert the VG size into GB and MB. These variables are used to keep tabs on space available and remaining
        [[ ${VG_SIZE_TYPE:0:1} == "G" ]] && LVM_VG_MB=$(( VG_SIZE * 1000 )) || LVM_VG_MB=$VG_SIZE
Bernhard Landauer's avatar
Bernhard Landauer committed
636

Bernhard Landauer's avatar
Bernhard Landauer committed
637
        DIALOG " $_LvmCreateVG " --msgbox "\n$_LvmPvDoneBody1 '${LVM_VG}' (${VG_SIZE} ${VG_SIZE_TYPE}) $_LvmPvDoneBody2.\n " 0 0 || return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
638
    fi
Bernhard Landauer's avatar
Bernhard Landauer committed
639

Bernhard Landauer's avatar
Bernhard Landauer committed
640 641 642
    #
    # Once VG created, create Logical Volumes
    #
Bernhard Landauer's avatar
Bernhard Landauer committed
643

Bernhard Landauer's avatar
Bernhard Landauer committed
644 645 646 647 648 649
    # Specify number of Logical volumes to create.
    DIALOG " $_LvmCreateVG " --inputbox "\n$_LvmLvNumBody1 [${LVM_VG}]. $_LvmLvNumBody2\n " 0 0 2>${ANSWER}

    # repeat if answer is not a number
    while [[ $(cat ${ANSWER}) != ?(-)+([0-9]) ]]; do
        DIALOG " $_ErrTitle " --inputbox "\n$_LvmLvNumBody1 [${LVM_VG}]. $_LvmLvNumBody2\n " 0 0 2>${ANSWER}
Bernhard Landauer's avatar
Bernhard Landauer committed
650
    done
Bernhard Landauer's avatar
Bernhard Landauer committed
651

Bernhard Landauer's avatar
Bernhard Landauer committed
652 653
    NUMBER_LOGICAL_VOLUMES=$(cat ${ANSWER})

Bernhard Landauer's avatar
Bernhard Landauer committed
654 655
    # Loop while the number of LVs is greater than 1. This is because the size of the last LV is automatic.
    while [[ $NUMBER_LOGICAL_VOLUMES -gt 1 ]]; do
Bernhard Landauer's avatar
Bernhard Landauer committed
656
        DIALOG " $_LvmCreateVG (LV:$NUMBER_LOGICAL_VOLUMES) " --inputbox "\n$_LvmLvNameBody1\n " 0 0 "lvol" 2>${ANSWER} || return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
657
        LVM_LV_NAME=$(cat ${ANSWER})
Bernhard Landauer's avatar
Bernhard Landauer committed
658

Bernhard Landauer's avatar
Bernhard Landauer committed
659 660
        # Loop if preceeded with a "/", if nothing is entered, if there is a space, or if that name already exists.
        while [[ ${LVM_LV_NAME:0:1} == "/" ]] || [[ ${#LVM_LV_NAME} -eq 0 ]] || [[ ${LVM_LV_NAME} =~ \ |\' ]] || [[ $(lsblk | grep ${LVM_LV_NAME}) != "" ]]; do
Bernhard Landauer's avatar
Bernhard Landauer committed
661 662
            DIALOG " $_ErrTitle " --msgbox "\n$_LvmLvNameErrBody\n " 0 0
            DIALOG " $_LvmCreateVG (LV:$NUMBER_LOGICAL_VOLUMES) " --inputbox "\n$_LvmLvNameBody1\n " 0 0 "lvol" 2>${ANSWER} || return 0
663
            LVM_LV_NAME=$(cat ${ANSWER})
Bernhard Landauer's avatar
Bernhard Landauer committed
664
        done
Bernhard Landauer's avatar
Bernhard Landauer committed
665

Bernhard Landauer's avatar
Bernhard Landauer committed
666
        DIALOG " $_LvmCreateVG (LV:$NUMBER_LOGICAL_VOLUMES) " --inputbox "\n${LVM_VG}: ${VG_SIZE}${VG_SIZE_TYPE} (${LVM_VG_MB}MB \
Bernhard Landauer's avatar
Bernhard Landauer committed
667
          $_LvmLvSizeBody1).$_LvmLvSizeBody2\n " 0 0 "" 2>${ANSWER} || return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
668 669
        LVM_LV_SIZE=$(cat ${ANSWER})
        check_lv_size
Bernhard Landauer's avatar
Bernhard Landauer committed
670

Bernhard Landauer's avatar
Bernhard Landauer committed
671 672
        # Loop while an invalid value is entered.
        while [[ $LV_SIZE_INVALID -eq 1 ]]; do
Bernhard Landauer's avatar
Bernhard Landauer committed
673
            DIALOG " $_ErrTitle " --msgbox "\n$_LvmLvSizeErrBody\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
674
            DIALOG " $_LvmCreateVG (LV:$NUMBER_LOGICAL_VOLUMES) " --inputbox "\n${LVM_VG}: ${VG_SIZE}${VG_SIZE_TYPE} \
Bernhard Landauer's avatar
Bernhard Landauer committed
675
              (${LVM_VG_MB}MB $_LvmLvSizeBody1).$_LvmLvSizeBody2\n " 0 0 "" 2>${ANSWER} || return 0
676 677 678
            LVM_LV_SIZE=$(cat ${ANSWER})
            check_lv_size
        done
Bernhard Landauer's avatar
Bernhard Landauer committed
679

Bernhard Landauer's avatar
Bernhard Landauer committed
680
        # Create the LV
Bernhard Landauer's avatar
Bernhard Landauer committed
681
        lvcreate -L ${LVM_LV_SIZE} ${LVM_VG} -n ${LVM_LV_NAME} 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
682
        check_for_error "lvcreate -L ${LVM_LV_SIZE} ${LVM_VG} -n ${LVM_LV_NAME}" "$?"
Bernhard Landauer's avatar
Bernhard Landauer committed
683
        DIALOG " $_LvmCreateVG (LV:$NUMBER_LOGICAL_VOLUMES) " --msgbox "\n$_Done\n\nLV ${LVM_LV_NAME} (${LVM_LV_SIZE}) $_LvmPvDoneBody2.\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
684
        NUMBER_LOGICAL_VOLUMES=$(( NUMBER_LOGICAL_VOLUMES - 1 ))
Bernhard Landauer's avatar
Bernhard Landauer committed
685 686 687
    done

    # Now the final LV. Size is automatic.
Bernhard Landauer's avatar
Bernhard Landauer committed
688
    DIALOG " $_LvmCreateVG (LV:$NUMBER_LOGICAL_VOLUMES) " --inputbox "\n$_LvmLvNameBody1 $_LvmLvNameBody2 (${LVM_VG_MB}MB).\n " 0 0 "lvol" 2>${ANSWER} || return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
689 690 691 692
    LVM_LV_NAME=$(cat ${ANSWER})
     
    # Loop if preceeded with a "/", if nothing is entered, if there is a space, or if that name already exists.
    while [[ ${LVM_LV_NAME:0:1} == "/" ]] || [[ ${#LVM_LV_NAME} -eq 0 ]] || [[ ${LVM_LV_NAME} =~ \ |\' ]] || [[ $(lsblk | grep ${LVM_LV_NAME}) != "" ]]; do
Bernhard Landauer's avatar
Bernhard Landauer committed
693 694
        DIALOG " $_ErrTitle " --msgbox "\n$_LvmLvNameErrBody\n " 0 0
        DIALOG " $_LvmCreateVG (LV:$NUMBER_LOGICAL_VOLUMES) " --inputbox "\n$_LvmLvNameBody1 $_LvmLvNameBody2 (${LVM_VG_MB}MB).\n " 0 0 "lvol" 2>${ANSWER} || return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
695 696 697 698
        LVM_LV_NAME=$(cat ${ANSWER})
    done

    # Create the final LV
Bernhard Landauer's avatar
Bernhard Landauer committed
699
    lvcreate -l +100%FREE ${LVM_VG} -n ${LVM_LV_NAME} 2>$ERR
Bernhard Landauer's avatar
Bernhard Landauer committed
700
    check_for_error "lvcreate -l +100%FREE ${LVM_VG} -n ${LVM_LV_NAME}" "$?"
Bernhard Landauer's avatar
Bernhard Landauer committed
701 702
    NUMBER_LOGICAL_VOLUMES=$(( NUMBER_LOGICAL_VOLUMES - 1 ))
    LVM=1
Bernhard Landauer's avatar
Bernhard Landauer committed
703
    DIALOG " $_LvmCreateVG " --yesno "\n$_LvmCompBody\n " 0 0 && show_devices || return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
704 705
}

706 707 708 709 710 711 712 713 714 715
check_lv_size() {
    LV_SIZE_INVALID=0
    chars=0

    # Check to see if anything was actually entered and if first character is '0'
    ([[ ${#LVM_LV_SIZE} -eq 0 ]] || [[ ${LVM_LV_SIZE:0:1} -eq "0" ]]) && LV_SIZE_INVALID=1

    # If not invalid so far, check for non numberic characters other than the last character
    if [[ $LV_SIZE_INVALID -eq 0 ]]; then
        while [[ $chars -lt $(( ${#LVM_LV_SIZE} - 1 )) ]]; do
Matti Hyttinen's avatar
Matti Hyttinen committed
716
            [[ ${LVM_LV_SIZE:chars:1} != [0-9] ]] && LV_SIZE_INVALID=1 && return 0;
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
            chars=$(( chars + 1 ))
        done
    fi

    # If not invalid so far, check that last character is a M/m or G/g
    if [[ $LV_SIZE_INVALID -eq 0 ]]; then
        LV_SIZE_TYPE=$(echo ${LVM_LV_SIZE:$(( ${#LVM_LV_SIZE} - 1 )):1})

        case $LV_SIZE_TYPE in
            "m"|"M"|"g"|"G") LV_SIZE_INVALID=0 ;;
            *) LV_SIZE_INVALID=1 ;;
        esac

    fi

    # If not invalid so far, check whether the value is greater than or equal to the LV remaining Size.
    # If not, convert into MB for VG space remaining.
    if [[ ${LV_SIZE_INVALID} -eq 0 ]]; then
        case ${LV_SIZE_TYPE} in
            "G"|"g")
                if [[ $(( $(echo ${LVM_LV_SIZE:0:$(( ${#LVM_LV_SIZE} - 1 ))}) * 1000 )) -ge ${LVM_VG_MB} ]]; then
                    LV_SIZE_INVALID=1
                else
                    LVM_VG_MB=$(( LVM_VG_MB - $(( $(echo ${LVM_LV_SIZE:0:$(( ${#LVM_LV_SIZE} - 1 ))}) * 1000 )) ))
                fi
                ;;
            "M"|"m")
                if [[ $(echo ${LVM_LV_SIZE:0:$(( ${#LVM_LV_SIZE} - 1 ))}) -ge ${LVM_VG_MB} ]]; then
                    LV_SIZE_INVALID=1
                else
                    LVM_VG_MB=$(( LVM_VG_MB - $(echo ${LVM_LV_SIZE:0:$(( ${#LVM_LV_SIZE} - 1 ))}) ))
                fi
                ;;
            *) LV_SIZE_INVALID=1
                ;;
        esac

    fi
}

Bernhard Landauer's avatar
Bernhard Landauer committed
757 758 759 760
lvm_del_vg() {
    # Generate list of VGs for selection
    lvm_show_vg

Bernhard Landauer's avatar
Bernhard Landauer committed
761 762
    # If no VGs, no point in continuing
    if [[ $VG_LIST == "" ]]; then
Bernhard Landauer's avatar
Bernhard Landauer committed
763
        DIALOG " $_ErrTitle " --msgbox "\n$_LvmVGErr\n " 0 0
Matti Hyttinen's avatar
Matti Hyttinen committed
764
        return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
765 766 767
    fi

    # Select VG
Bernhard Landauer's avatar
Bernhard Landauer committed
768
    DIALOG " $_PrepLVM " --menu "\n$_LvmSelVGBody\n " 0 0 5 ${VG_LIST} 2>${ANSWER} || return 0
Bernhard Landauer's avatar
Bernhard Landauer committed
769

Bernhard Landauer's avatar
Bernhard Landauer committed
770
    # Ask for confirmation
Bernhard Landauer's avatar
Bernhard Landauer committed
771
    DIALOG " $_LvmDelVG " --yesno "\n$_LvmDelQ\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
772 773 774

    # if confirmation given, delete
    if [[ $? -eq 0 ]]; then
775 776
        vgremove -f $(cat ${ANSWER}) 2>$ERR
        check_for_error "delete lvm-VG $(cat ${ANSWER})" $?
Bernhard Landauer's avatar
Bernhard Landauer committed
777 778
    fi
}
Bernhard Landauer's avatar
Bernhard Landauer committed
779

Bernhard Landauer's avatar
Bernhard Landauer committed
780
lvm_del_all() {
Bernhard Landauer's avatar
Bernhard Landauer committed
781 782 783 784 785 786
    # check if VG exist at all
    if [[ $(lvs) == "" ]];
        DIALOG " $_ErrTitle " --msgbox "\n$_LvmVGErr\n " 0 0
        return 0
    fi

Bernhard Landauer's avatar
Bernhard Landauer committed
787 788 789
    LVM_PV=$(pvs -o pv_name --noheading 2>/dev/null)
    LVM_VG=$(vgs -o vg_name --noheading 2>/dev/null)
    LVM_LV=$(lvs -o vg_name,lv_name --noheading --separator - 2>/dev/null)
Bernhard Landauer's avatar
Bernhard Landauer committed
790

Bernhard Landauer's avatar
Bernhard Landauer committed
791
    # Ask for confirmation
Bernhard Landauer's avatar
Bernhard Landauer committed
792
    DIALOG " $_LvmDelLV " --yesno "\n$_LvmDelQ\n " 0 0
Bernhard Landauer's avatar
Bernhard Landauer committed
793

Bernhard Landauer's avatar
Bernhard Landauer committed
794 795 796
    # if confirmation given, delete
    if [[ $? -eq 0 ]]; then
        for i in ${LVM_LV}; do
797 798
            lvremove -f /dev/mapper/${i} 2>$ERR
            check_for_error "remove LV ${i}" $?
Bernhard Landauer's avatar
Bernhard Landauer committed
799
        done
Bernhard Landauer's avatar
Bernhard Landauer committed
800

Bernhard Landauer's avatar
Bernhard Landauer committed
801
        for i in ${LVM_VG}; do
802 803
            vgremove -f ${i} 2>$ERR
            check_for_error "remove VG ${i}" $?
Bernhard Landauer's avatar
Bernhard Landauer committed
804
        done
805

Bernhard Landauer's avatar
Bernhard Landauer committed
806
        for i in ${LV_PV}; do
807 808
            pvremove -f ${i} 2>$ERR
            check_for_error "remove LV-PV ${i}" $?
Bernhard Landauer's avatar
Bernhard Landauer committed
809 810 811
        done
    fi
}
Bernhard Landauer's avatar
Bernhard Landauer committed
812

Bernhard Landauer's avatar
Bernhard Landauer committed
813
lvm_menu() {
Bernhard Landauer's avatar
Bernhard Landauer committed
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
    declare -i loopmenu=1
    while ((loopmenu)); do
        DIALOG " $_PrepLVM $_PrepLVM2 " --infobox "\n$_PlsWaitBody\n " 0 0
        sleep 1
        lvm_detect

        DIALOG " $_PrepLVM $_PrepLVM2 " --menu "\n$_LvmMenu\n " 0 0 4 \
          "$_LvmCreateVG" "vgcreate -f, lvcreate -L -n" \
          "$_LvmDelVG" "vgremove -f" \
          "$_LvMDelAll" "lvrmeove, vgremove, pvremove -f" \
          "$_Back" "-" 2>${ANSWER}

        case $(cat ${ANSWER}) in
            "$_LvmCreateVG") lvm_create
               ;;
            "$_LvmDelVG") lvm_del_vg
               ;;
            "$_LvMDelAll") lvm_del_all
               ;;
            *) loopmenu=0
               return 0
               ;;
        esac
    done
Bernhard Landauer's avatar
Bernhard Landauer committed
838
}
839 840 841

mount_partitions() {
    # Warn users that they CAN mount partitions without formatting them!
Bernhard Landauer's avatar
Bernhard Landauer committed
842
    DIALOG " $_PrepMntPart " --msgbox "\n$_WarnMount1 '$_FSSkip' $_WarnMount2\n " 0 0
843 844 845 846 847 848 849 850 851 852 853

    # LVM Detection. If detected, activate.
    lvm_detect

    # Ensure partitions are unmounted (i.e. where mounted previously), and then list available partitions
    INCLUDE_PART='part\|lvm\|crypt'
    umount_partitions
    find_partitions
    # Filter out partitions that have already been mounted and partitions that just contain crypt device
    list_mounted > /tmp/.ignore_part
    list_containing_crypt >> /tmp/.ignore_part
Bernhard Landauer's avatar
Bernhard Landauer committed
854
    check_for_error "ignore crypted: $(list_containing_crypt)"
855 856 857 858 859 860

    for part in $(cat /tmp/.ignore_part); do
        delete_partition_in_list $part
    done

    # Identify and mount root
861 862
    DIALOG " $_PrepMntPart " --menu "\n$_SelRootBody\n " 0 0 12 ${PARTITIONS} 2>${ANSWER} || return 0
    PARTITION=$(cat ${ANSWER})
863 864
    ROOT_PART=${PARTITION}

Bernhard Landauer's avatar
Bernhard Landauer committed
865 866
    # Format with FS (or skip) -> # Make the directory and mount. Also identify LUKS and/or LVM
    select_filesystem && mount_current_partition || return 0
867

868 869 870
    ini mount.root "${PARTITION}"
    delete_partition_in_list "${ROOT_PART}"

871 872 873 874 875
    # Identify and create swap, if applicable
    make_swap

    # Extra Step for VFAT UEFI Partition. This cannot be in an LVM container.
    if [[ $SYSTEM == "UEFI" ]]; then
876 877 878 879 880