├── SUPPORT.md ├── OWNERS.md ├── MANIFEST.md ├── .circleci └── config.yml ├── CONTRIBUTING.md ├── packet-block-storage-detach ├── README.md ├── CODE_OF_CONDUCT.md ├── LICENSE └── packet-block-storage-attach /SUPPORT.md: -------------------------------------------------------------------------------- 1 | If you require support, please email support@packet.com, visit the Packet IRC channel (#packethost on freenode), subscribe to the [Packet Community Slack](https://slack.packet.com/) channel or post an issue within this repository. 2 | 3 | [Contributions](CONTRIBUTING.md) are welcome to help extend this work! 4 | -------------------------------------------------------------------------------- /OWNERS.md: -------------------------------------------------------------------------------- 1 | # Owners 2 | 3 | This project is governed by [Packet](https://packet.com) and benefits from a community of users that collaborate and contribute to its use in Kubernetes on Packet. 4 | 5 | Members of the Packet Github organization will strive to triage issues in a timely manner. 6 | 7 | See the [packethost/standards glossary](https://github.com/packethost/standards/blob/master/glossary.md#ownersmd) for more details about this file. 8 | -------------------------------------------------------------------------------- /MANIFEST.md: -------------------------------------------------------------------------------- 1 | # Manifest File 2 | 3 | ## Manifest Version 4 | 5 | 1 6 | 7 | ## Name 8 | 9 | Packet Block Storage 10 | 11 | ## Repository Version 12 | 13 | 1.0 14 | 15 | ## Default Locale 16 | 17 | en 18 | 19 | ## Description 20 | 21 | This utility/helper script is made available to assist in the attachment and detachment of Packet block storage volumes and is currently in testing. Use at your own risk and always have a backup/snapshot just in case! 22 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # Use the latest 2.1 version of CircleCI pipeline process engine. See: https://circleci.com/docs/2.0/configuration-reference 2 | version: 2.1 3 | # Use a package of configuration called an orb. 4 | orbs: 5 | # Declare a dependency on the welcome-orb 6 | welcome: circleci/welcome-orb@0.4.1 7 | # Orchestrate or schedule a set of jobs 8 | workflows: 9 | # Name the workflow "welcome" 10 | welcome: 11 | # Run the welcome/run job in its own container 12 | jobs: 13 | - welcome/run -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Hello Contributors! 2 | Thx for your interest! We're so glad you're here. 3 | 4 | ### Important Resources 5 | - bugs: [https://github.com/packethost/packet-block-storage/issues](https://github.com/packethost/packet-block-storage/issues) 6 | 7 | ### Code of Conduct 8 | Available via [https://github.com/packethost/packet-block-storage/blob/master/.github/CODE_OF_CONDUCT.md](https://github.com/packethost/packet-block-storage/blob/master/.github/CODE_OF_CONDUCT.md) 9 | 10 | ### Environment Details 11 | [https://github.com/packethost/packet-block-storage/blob/master/MANIFEST.md](https://github.com/packethost/terraform-provider-packet/blob/master/MANIFEST.md) 12 | 13 | ### How to Submit Change Requests 14 | Please submit change requests and / or features via [Issues](https://github.com/packethost/packet-block-storage/issues). There's no guarantee it'll be changed, but you never know until you try. We'll try to add comments as soon as possible, though. 15 | 16 | ### How to Report a Bug 17 | Bugs are problems in code, in the functionality of an application or in its UI design; you can submit them through [Issues](https://github.com/packethost/packet-block-storage/issues) as well. 18 | -------------------------------------------------------------------------------- /packet-block-storage-detach: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | usage(){ 4 | echo "usage: $0 [-h] [volume_name]" 5 | echo " -h display help" 6 | echo " volume_name specify the volume name to detach, eg: volume-f9a8a263" 7 | exit 1 8 | } 9 | 10 | while getopts "h" OPTION 11 | do 12 | case $OPTION in 13 | h) usage 14 | ;; 15 | esac 16 | done 17 | 18 | shift $(($OPTIND - 1)) 19 | if [ $1 ] ; then 20 | target=$1 21 | fi 22 | 23 | function detach_volume () { 24 | if [ -z "$1" ] 25 | then 26 | echo "Error: no iqn passed to detach_volume () function" 27 | exit 28 | fi 29 | 30 | if [ -z "$2" ] 31 | then 32 | echo "Error: no volume name passed to detach_volume () function" 33 | exit 34 | fi 35 | 36 | iqntarget="$1" 37 | volname="$2" 38 | if grep $volname /proc/mounts &>/dev/null; then 39 | wheremnt=$(grep $volname /proc/mounts | awk '{print $2}') 40 | echo "Error: volume $volname is currently mounted on $wheremnt. Please unmount it first and try again." 41 | exit 1 42 | fi 43 | 44 | for iqn in `iscsiadm -m session | awk {'print $4'}`; do 45 | if [ "$iqntarget" = "$iqn" ] ; then 46 | portal=`iscsiadm -m session | grep $iqn | awk {'print $3'} | egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'` 47 | echo "Logging out $iqn on $portal" 48 | iscsiadm --mode node --targetname $iqn --portal $portal --logout 49 | sed -i "/^$volname.*/d" /etc/multipath/bindings 50 | multipath -f $volname 51 | fi 52 | done 53 | } 54 | 55 | export LOCALMD=/tmp/metadata.tmp 56 | curl -sSL https://metadata.packet.net/metadata > $LOCALMD 57 | volumecnt=`jq '.volumes[].iqn' $LOCALMD | wc -l` 58 | 59 | # cleanup mpath entries in bindings file 60 | sed -i "/^mpath.*/d" /etc/multipath/bindings 61 | if [ `ls /dev/mapper/mpath* >/dev/null 2>/dev/null` ]; then 62 | [ $_V -eq 1 ] && echo "mpath volume entries found, cleaning up" 63 | for i in `ls /dev/mapper/mpath* | cut -d / -f4`; do 64 | multipath -f $i 65 | done 66 | fi 67 | 68 | for (( idx=0; idx<$volumecnt; idx++ )); do 69 | volname=`jq '.volumes['$idx'].name ' $LOCALMD | sed 's/"//g'` 70 | if [ "$target" ] ; then 71 | if [ "$volname" = "$target" ] ; then 72 | iqn=`jq '.volumes['$idx'].iqn ' $LOCALMD | sed 's/"//g'` 73 | detach_volume $iqn $volname 74 | continue 75 | fi 76 | else 77 | iqn=`jq '.volumes['$idx'].iqn ' $LOCALMD | sed 's/"//g'` 78 | detach_volume $iqn $volname 79 | fi 80 | done 81 | 82 | # debian / centos 83 | #rm -rf /var/lib/iscsi/send_targets/* 84 | #rm -rf /var/lib/iscsi/nodes/* 85 | 86 | #ubuntu 87 | #rm -rf /etc/iscsi/send_targets/* 88 | #rm -rf /etc/iscsi/nodes/* 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # packet-block-storage 2 | 3 | ![](https://img.shields.io/badge/Stability-Maintained-green.svg) 4 | 5 | This repository is [Maintained](https://github.com/packethost/standards/blob/master/maintained-statement.md) meaning that this software is supported by Packet and its community - available to use in production environments. 6 | 7 | ---- 8 | ## Quick overview 9 | This utility/helper script is made available to assist in the attachment and detachment of Packet block storage volumes and is currently in testing. Use at your own risk and always have a backup/snapshot just in case! 10 | 11 | # Availability 12 | The packet-block storage helper scripts are pre-installed on the following Packet operating systems: 13 | * CentOS 14 | * Ubuntu 15 | * Debian 16 | * OpenSUSE 17 | * Redhat Enterprise Linux 18 | * SLES 12 SP3 19 | 20 | # Features 21 | * Attach block storage block devices 22 | * Detach block storage block devices 23 | * Configure multipath for HA 24 | * Validate block device creation 25 | 26 | # Dependencies 27 | 28 | **Ubuntu/Debian**: 29 | 30 | apt-get install open-iscsi multipath-tools jq 31 | 32 | 33 | **CentOS**: 34 | 35 | yum -y install iscsi-initiator-utils device-mapper-multipath jq 36 | 37 | ## Installation 38 | 39 | **Using git:** 40 | 41 | sudo su - 42 | git clone git@github.com:packethost/packet-block-storage.git 43 | cp ./packet-block-storage/packet-block-storage-* /usr/bin/ 44 | chmod u+x /usr/bin/packet-block-storage-* 45 | 46 | **or** 47 | 48 | **Using wget** 49 | 50 | sudo su - 51 | wget -O /usr/bin/packet-block-storage-attach https://raw.githubusercontent.com/packethost/packet-block-storage/master/packet-block-storage-attach 52 | wget -O /usr/bin/packet-block-storage-detach https://raw.githubusercontent.com/packethost/packet-block-storage/master/packet-block-storage-detach 53 | chmod u+x /usr/bin/packet-block-storage-* 54 | 55 | ## Usage 56 | 57 | **Attach a volume** 58 | 59 | 1. Create a volume under the Storage tab in your Packet.net portal account 60 | 2. Click on the storage volume, select the server you wish to attach the volume to and click on Attach. 61 | 3. Execute packet-block-storage-attach from within the OS on the server in question 62 | 4. Partition the block device at /dev/mapper/\{volume\_name\_here\} disk using parted, fdisk, etc 63 | 5. Make a filesystem on the block device 64 | 6. Mount the block device on the mount point of you choice 65 | 66 | Example : 67 | 68 | [root@cent7-pbs-client dlaube]# packet-block-storage-attach 69 | Block device /dev/mapper/volume-9ab99df5 is available for use 70 | Block device /dev/mapper/volume-7eab8fc1 is available for use 71 | 72 | **IMPORTANT NOTE:** Consider using the "-m queue" flag when attaching storage to configure multipath queuing. If block storage becomes unreachable, the option "fail" results in FS read-only and "queue" will keep IO in memory buffer until reachable. See "-h" for usage details. 73 | 74 | 75 | 76 | **Detach a volume** 77 | 78 | 1. Unmount the filesystem on any block storage volume that may be in use 79 | 2. Execute packet-block-storage-detach from within the OS on the server in question 80 | 3. Click on the storage volume from the storage tab within your Packet.net project and click on Detach 81 | 82 | Example: 83 | 84 | [root@cent7-pbs-client dlaube]# packet-block-storage-detach 85 | 86 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at support@packet.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /packet-block-storage-attach: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Packet.net block storage auto-attach - dlaube 3 | 4 | set -e 5 | 6 | [ -n "$DEBUG" ] && set -x 7 | 8 | usage(){ 9 | echo "usage: $0 [-vhmnc] [volume_name]" 10 | echo " -v Make verbose" 11 | echo " -h Display help" 12 | echo " -n Do not try to attach any volumes; just ensure that iscsid and multipath are set up correctly" 13 | echo " -c Do not start or restart multipathd or iscsid; just set up the correct configuration and ensure dependencies are installed" 14 | echo " -m Set multipath feature no_path_retry, ex: -m []" 15 | echo " If block storage is unreachable, the option "fail" results in FS read-only and "queue" will keep IO in memory buffer until reachable" 16 | echo " volume_name Specify the volume name to attach, eg: volume-f9a8a263" 17 | exit 1 18 | } 19 | 20 | hasSystemctl=0 21 | set +e 22 | systemctl 2>/dev/null | grep -q '^\s*\-\.mount' 23 | [ $? -eq 0 ] && hasSystemctl=1 24 | set -e 25 | 26 | 27 | restart_all() { 28 | local configonly="$1" 29 | if [ $configonly -ne 1 ]; then 30 | [ $_V -eq 1 ] && echo "restarting iscsid and resetting multipath" 31 | restart_iscsid 32 | sleep 2 33 | multipath 34 | fi 35 | } 36 | 37 | restart_iscsid() { 38 | if [ "$hasSystemctl" -eq 1 ]; then 39 | [ $_V -eq 1 ] && echo "Restarting iscsid with systemctl" 40 | systemctl restart iscsid &>/dev/null 41 | if systemctl -q is-active open-iscsi; then 42 | systemctl restart open-iscsi &>/dev/null 43 | fi 44 | # for alpine 45 | elif [ -f /etc/init.d/iscsid -a ! -h /etc/init.d/iscsid ]; then 46 | [ $_V -eq 1 ] && echo "Restarting iscsid with sysv init" 47 | /etc/init.d/iscsid restart &>/dev/null 48 | fi 49 | if [ -f /etc/init.d/open-iscsi -a ! -h /etc/init.d/open-iscsi ]; then 50 | [ $_V -eq 1 ] && echo "Restarting iscsid with sysv init" 51 | /etc/init.d/open-iscsi restart &>/dev/null 52 | fi 53 | } 54 | 55 | attach_volumes() { 56 | local volumecnt="$1" 57 | local targetidx="$2" 58 | local metadatapath="$3" 59 | if [ "$targetidx" ] ; then 60 | attach_volume "$targetidx" "$metadatapath" 61 | else 62 | idx=0 63 | while [ $idx -lt $volumecnt ]; do 64 | attach_volume "$idx" "$metadatapath" 65 | idx=$(( $idx + 1 )) 66 | done 67 | fi 68 | } 69 | 70 | attach_volume() { 71 | local volume="$1" 72 | local metadatapath="$2" 73 | if [ -z "$volume" ]; then 74 | echo "Error: no volume index passed to attach_volume () function" 75 | exit 1 76 | fi 77 | 78 | [ $_V -eq 1 ] && echo "attaching volume index \"$volume\"." 79 | 80 | iqn=`jq -r '.volumes['$volume'].iqn ' $metadatapath ` 81 | portals=`jq -r '.volumes['$volume'].ips[] ' $metadatapath ` 82 | volname=`jq -r '.volumes['$volume'].name ' $metadatapath ` 83 | 84 | for portal in ${portals}; do 85 | echo "portal: $portal iqn: $iqn" 86 | # Discover 87 | if iscsiadm --mode discovery --type sendtargets --portal $portal --discover &>/dev/null ; then 88 | [ $_V -eq 1 ] && echo "Discovery success on $portal" 89 | else 90 | echo "Error: We couldn't discover targets on $portal" 91 | fi 92 | 93 | # Login and attach 94 | if iscsiadm --mode node --targetname $iqn --portal $portal --login &>/dev/null ; then 95 | [ $_V -eq 1 ] && echo "Logged in iqn $iqn" 96 | 97 | sleep 3 98 | 99 | bdname=`ls -l /dev/disk/by-path/ | grep "$iqn" | grep $portal | awk {'print $11'} | sed 's/..\/..\///'` 100 | wwid=`/lib/udev/scsi_id -g -u -d /dev/$bdname` 101 | [ $_V -eq 1 ] && echo "Block device $bdname aka $volname is mapped to $iqn with WWID $wwid" 102 | 103 | [ $_V -eq 1 ] && echo "updating /etc/multipath/bindings with $volname $wwid" 104 | if `grep -q "^$volname" /etc/multipath/bindings`; then 105 | sed -i "s/^$volname .*/$volname $wwid/" /etc/multipath/bindings 106 | else 107 | echo "$volname $wwid" >> /etc/multipath/bindings 108 | fi 109 | 110 | multipath $volname 111 | else 112 | echo "Error: We couldn't log in iqn $iqn" 113 | fi 114 | done 115 | } 116 | 117 | # install the right package for the given package manager 118 | # arg order: apt (debian/ubuntu), yum (centos/rhel), apk (alpine) 119 | install_package() { 120 | local ranonce="$1" 121 | local apt="$2" 122 | local yum="$3" 123 | local apk="$4" 124 | local install_command="" 125 | local update_command="" 126 | local pkgs="" 127 | local dep="" 128 | 129 | if type apt &>/dev/null; then 130 | install_command="apt install -y" 131 | update_command="apt-get update -y" 132 | pkgs="$apt" 133 | elif type yum &>/dev/null; then 134 | install_command="yum install -y" 135 | update_command="yum makecache fast" 136 | pkgs="$yum" 137 | elif type apk &>/dev/null; then 138 | install_command="apk add" 139 | update_command="apk update" 140 | pkgs="$apk" 141 | else 142 | echo "Unknown packaging system. Exiting." >&2 143 | exit 1 144 | fi 145 | 146 | # if necessary, update package system cache 147 | if [ -z "$ranonce" ]; then 148 | $update_command 149 | fi 150 | for dep in $pkgs; do 151 | $install_command $dep 152 | done 153 | } 154 | 155 | ensure_deps() { 156 | # Check for deps - check each one independently, because each has its own unique package name 157 | 158 | # track if we have run update or not 159 | local ranonce="" 160 | # jq 161 | if [ ! `which jq` ]; then 162 | echo "JQ was not found. Installing..." 163 | # jq requires epel first 164 | install_package "$ranonce" jq "epel-release jq" jq 165 | ranonce="true" 166 | fi 167 | 168 | # iscsi requirements 169 | if [ ! `which iscsiadm` ]; then 170 | echo "iscsi was not found. Installing..." 171 | install_package "$ranonce" open-iscsi iscsi-initiator-utils open-iscsi 172 | ranonce="true" 173 | fi 174 | 175 | # multipath 176 | if [ ! `which multipath` ]; then 177 | echo "multipath was not found. Installing..." 178 | install_package "$ranonce" multipath-tools device-mapper-multipath multipath-tools 179 | ranonce="true" 180 | fi 181 | } 182 | 183 | enable_multipath() { 184 | # Check for multipath support 185 | set +e 186 | lsmod | grep -q dm_multipath 187 | local found=$? 188 | set -e 189 | if [ $found -eq 0 ]; then 190 | [ $_V -eq 1 ] && echo "Multipath module is loaded." 191 | return 0 192 | else 193 | [ $_V -eq 1 ] && echo "Multipath module not yet loaded. Restarting service." 194 | # Run multipath 195 | if [ "$hasSystemctl" -eq 1 ]; then 196 | [ $_V -eq 1 ] && echo "Restarting multipath with systemctl" 197 | modprobe dm_multipath 198 | modprobe dm_round_robin 199 | systemctl restart multipathd 200 | systemctl enable multipathd 201 | elif [ -f /etc/init.d/multipath-tools -a ! -h /etc/init.d/multipath-tools ]; then 202 | [ $_V -eq 1 ] && echo "Restarting multipath with sysv init" 203 | /etc/init.d/multipath-tools restart &>/dev/null 204 | # for alpine linux 205 | elif [ -f /etc/init.d/multipathd -a ! -h /etc/init.d/multipathtd ]; then 206 | [ $_V -eq 1 ] && echo "Restarting multipath with sysv init" 207 | /etc/init.d/multipathd restart &>/dev/null 208 | fi 209 | 210 | # Restart multipath with sysv init to fix ubuntu/deb 211 | if [ -f /etc/init.d/multipath-tools -a ! -h /etc/init.d/multipath-tools ]; then 212 | [ $_V -eq 1 ] && echo "Restarting multipath with sysv init" 213 | /etc/init.d/multipath-tools restart &>/dev/null 214 | fi 215 | fi 216 | true 217 | } 218 | 219 | create_multipath_config() { 220 | local mpnpropt="$1" 221 | # create the multipath config 222 | cat <<- EOF_mpconf > /etc/multipath.conf 223 | defaults { 224 | 225 | polling_interval 3 226 | fast_io_fail_tmo 5 227 | path_selector "round-robin 0" 228 | rr_min_io 100 229 | rr_weight priorities 230 | failback immediate 231 | no_path_retry $mpnpropt 232 | user_friendly_names yes 233 | 234 | } 235 | 236 | blacklist { 237 | devnode "^(ram|raw|loop|fd|md|dm-|sr|scd|st)[0-9]*" 238 | devnode "^hd[a-z][[0-9]*]" 239 | devnode "^vd[a-z]" 240 | devnode "^cciss!c[0-9]d[0-9]*[p[0-9]*]" 241 | 242 | device { 243 | vendor "Micron" 244 | product ".*" 245 | } 246 | 247 | device { 248 | vendor "Intel" 249 | product ".*" 250 | } 251 | 252 | device { 253 | vendor "DELL" 254 | product ".*" 255 | } 256 | } 257 | 258 | devices { 259 | device { 260 | vendor "DATERA" 261 | product "IBLOCK" 262 | path_grouping_policy group_by_prio 263 | path_checker tur 264 | #checker_timer 5 265 | #prio_callout "/sbin/mpath_prio_alua /dev/%n" 266 | hardware_handler "1 alua" 267 | } 268 | } 269 | EOF_mpconf 270 | } 271 | 272 | validate_iscsi_config() { 273 | # iscsid config check - fix for Ubuntu/Debian since they default to manual 274 | iscsiconf="/etc/iscsi/iscsid.conf" 275 | if `grep -q 'node.startup = manual' "$iscsiconf"` || \ 276 | `grep -q 'node.session.timeo.replacement_timeout = 120' "$iscsiconf"` || \ 277 | `grep -q 'node.session.timeo.replacement_timeout = 15' "$iscsiconf"` || \ 278 | `grep -q 'node.conn\[0\].timeo.noop_out_interval = 5' "$iscsiconf"` || \ 279 | `grep -q 'node.conn\[0\].timeo.noop_out_timeout = 5' "$iscsiconf"`; then 280 | echo "updating iscsid.conf" 281 | sed -i.bak 's/node.startup = manual/node.startup = automatic/g' /etc/iscsi/iscsid.conf 282 | # Adjust timeout settings for decreased queue time during failover 283 | # Adjust for Ubuntu/Debian specific defaults 284 | sed -i.bak 's/node.session.timeo.replacement_timeout = 120/node.session.timeo.replacement_timeout = 5/g' $iscsiconf 285 | # Adjust for RHEL/CentOS specific defaults 286 | sed -i.bak 's/node.session.timeo.replacement_timeout = 15/node.session.timeo.replacement_timeout = 5/g' $iscsiconf 287 | sed -i.bak 's/node.conn\[0\].timeo.noop_out_interval = 5/node.conn\[0\].timeo.noop_out_interval = 3/g' $iscsiconf 288 | sed -i.bak 's/node.conn\[0\].timeo.noop_out_timeout = 5/node.conn\[0\].timeo.noop_out_timeout = 3/g' $iscsiconf 289 | fi 290 | } 291 | 292 | update_initiator() { 293 | # Initiator check 294 | local initiator="$1" 295 | if [ ! `grep $initiator\$ /etc/iscsi/initiatorname.iscsi` ]; then 296 | [ $_V -eq 1 ] && echo "Initiator name mismatch! Updating from metadata..." 297 | echo "InitiatorName=$initiator" > /etc/iscsi/initiatorname.iscsi 298 | fi 299 | } 300 | 301 | get_target_idx() { 302 | local target="$1" 303 | local volumecnt="$2" 304 | local metadatapath="$3" 305 | local targetidx= 306 | 307 | if [ $volumecnt -lt "1" ]; then 308 | echo "Error: No volume(s) associated with this server. Have you attached a volume to this server via the Packet.net API/Portal?" >&2 309 | exit 1 310 | fi 311 | 312 | if [ "$target" ] ; then 313 | idx=0 314 | while [ $idx -lt $volumecnt ]; do 315 | volname=`jq -r '.volumes['$idx'].name ' $metadatapath ` 316 | if [ "$volname" = "$target" ] ; then 317 | targetidx=$idx 318 | idx=$(( $idx + 1 )) 319 | continue 320 | fi 321 | idx=$(( $idx + 1 )) 322 | done 323 | 324 | if [ ! "$targetidx" ] ; then 325 | echo "target not found, is it connected to this host? : $target" >&2 326 | exit 1 327 | fi 328 | fi 329 | echo $targetidx 330 | } 331 | 332 | clean_mpath_bindings() { 333 | # cleanup mpath entries in bindings file 334 | # remove old ones if the file already exists 335 | [ -f /etc/multipath/bindings ] && sed -i "/^mpath.*/d" /etc/multipath/bindings 336 | # add all of the existing known ones 337 | if `ls /dev/mapper/mpath* >/dev/null 2>/dev/null`; then 338 | [ $_V -eq 1 ] && echo "mpath volume entries found, cleaning up" 339 | for i in `ls /dev/mapper/mpath* | cut -d / -f4`; do 340 | multipath -f $i 341 | done 342 | multipath 343 | fi 344 | } 345 | 346 | report_status() { 347 | # Check for block device(s) 348 | local volumecnt="$1" 349 | local metadatapath="$2" 350 | 351 | volume=0 352 | while [ $volume -lt $volumecnt ]; do 353 | volname=`jq -r '.volumes['$volume'].name ' $metadatapath` 354 | thiswwid=$(grep $volname /etc/multipath/bindings | awk {'print $2'}) 355 | if [ -b /dev/mapper/$volname ]; then 356 | echo "Block device /dev/mapper/$volname is available for use" 357 | elif [ -b /dev/mapper/$thiswwid ]; then 358 | echo "Found $volname as WWID $thiswwid. Reloading devmap..." 359 | multipath -r 360 | if [ -b /dev/mapper/$volname ]; then 361 | echo "Block device /dev/mapper/$volname is available for use" 362 | fi 363 | else 364 | echo "Error: Block device /dev/mapper/$volname is NOT available for use" 365 | fi 366 | volume=$(($volume + 1)) 367 | done 368 | } 369 | 370 | # Get cli options after setting defaults 371 | _V=0 372 | mpnpropt="fail" # multipath no_path_retry default 373 | noattach=0 374 | configonly=0 375 | 376 | while getopts "m:vhnc" OPTION 377 | do 378 | case $OPTION in 379 | v) _V=1;; 380 | h) usage;; 381 | m) mpnpropt="$OPTARG";; 382 | n) noattach=1;; 383 | c) configonly=1;; 384 | *) exit 1;; 385 | esac 386 | done 387 | 388 | shift $(($OPTIND - 1)) 389 | if [ $1 ] ; then 390 | target=$1 391 | fi 392 | 393 | [ $_V -eq 1 ] && echo "Multipath no_path_retry feature option set as '$mpnpropt'" 394 | 395 | ensure_deps 396 | [ $configonly -ne 1 ] && enable_multipath 397 | 398 | export LOCALMD=/tmp/metadata.tmp 399 | curl -sSL https://metadata.packet.net/metadata > $LOCALMD 400 | volumecnt=`jq '.volumes | length' $LOCALMD` 401 | initiator=`jq -r '.iqn' $LOCALMD` 402 | targetidx= 403 | 404 | # Check for volumes - but only if noattach was not set 405 | if [ $noattach -ne 1 ]; then 406 | targetidx=$(get_target_idx "$target" "$volumecnt" "$LOCALMD") 407 | [ "$targetidx" ] && echo "target has been set to: $target , index $targetidx" 408 | fi 409 | 410 | create_multipath_config "$mpnpropt" 411 | update_initiator "$initiator" 412 | restart_all "$configonly" 413 | validate_iscsi_config 414 | restart_all "$configonly" 415 | 416 | # attach volumes - but only if noattach was not set 417 | [ $noattach -ne 1 ] && attach_volumes "$volumecnt" "$targetidx" "$LOCALMD" 418 | 419 | clean_mpath_bindings 420 | 421 | # output the paths when in verbose mode 422 | [ $_V -eq 1 ] && multipath -ll 423 | 424 | [ $noattach -ne 1 ] && report_status "$volumecnt" "$LOCALMD" 425 | 426 | exit 0 427 | 428 | --------------------------------------------------------------------------------