Repository navigation
Openzfs smhp #622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Openzfs smhp #622
Changes from 8 commits
f29528d
0b28d42
e8f67e4
98d9f5d
fd77d0c
c3b0664
feb876a
019b2fd
711f8d5
031da2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| #!/bin/bash | ||
|
|
||
| # must be run as sudo | ||
|
|
||
| set -x | ||
| set -e | ||
|
|
||
| # FSx OpenZFS Endpoints | ||
| FSX_OPENZFS_DNS_NAME="$1" | ||
| OPENZFS_MOUNT_POINT="$2" | ||
|
|
||
| is_mounted() { | ||
| mountpoint -q "$1" | ||
| return $? | ||
| } | ||
|
|
||
| check_already_mounted() { | ||
| # Check if FSx OpenZFS is already mounted to $OPENZFS_MOUNT_POINT | ||
| if is_mounted $OPENZFS_MOUNT_POINT; then | ||
| if grep -qs "$FSX_OPENZFS_DNS_NAME:/fsx $OPENZFS_MOUNT_POINT nfs" /proc/mounts; then | ||
| echo "FSx OpenZFS already mounted to $OPENZFS_MOUNT_POINT. Exiting." | ||
| exit 0 | ||
| else | ||
| echo "$OPENZFS_MOUNT_POINT is mounted, but not to DNS name: $FSX_OPENZFS_DNS_NAME. Exiting." | ||
| exit 1 | ||
| fi | ||
| fi | ||
| } | ||
|
|
||
| install_nfs_client() { | ||
| # Install NFS client based on the OS | ||
| if [ -f /etc/lsb-release ]; then | ||
| # Ubuntu | ||
| apt-get update | ||
| apt-get -y install nfs-common | ||
| elif [ -f /etc/redhat-release ]; then | ||
| # CentOS/RHEL | ||
| yum -y install nfs-utils | ||
| fi | ||
| } | ||
|
|
||
| add_to_fstab() { | ||
| # Add FSx OpenZFS to /etc/fstab | ||
| echo "$FSX_OPENZFS_DNS_NAME:/fsx $OPENZFS_MOUNT_POINT nfs nfsvers=4.2,_netdev 0 0" | tee -a /etc/fstab | ||
|
amanshanbhag marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| mount_fs() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So much duplication. Once you add the entry in fstab, you should just do
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also add the |
||
| if [[ ! -d $OPENZFS_MOUNT_POINT ]]; then | ||
| mkdir -p $OPENZFS_MOUNT_POINT | ||
| chmod 755 $OPENZFS_MOUNT_POINT | ||
| fi | ||
|
|
||
| if mount -t nfs -o nfsvers=4.2 "$FSX_OPENZFS_DNS_NAME:/fsx" "$OPENZFS_MOUNT_POINT"; then | ||
| if ! is_mounted $OPENZFS_MOUNT_POINT; then | ||
| echo "Mounting FSx OpenZFS to $OPENZFS_MOUNT_POINT directory successful, but mountpoint was not detected. Exiting." | ||
| exit 1 | ||
| fi | ||
| else | ||
| echo "FAILED to mount FSx OpenZFS to $OPENZFS_MOUNT_POINT directory. Exiting." | ||
| exit 1 | ||
| fi | ||
| } | ||
|
|
||
| # create a systemd service to check mount periodically and remount FSx if necessary | ||
| install_remount_service() { | ||
| if [[ ! -d /opt/ml/scripts ]]; then | ||
| mkdir -p /opt/ml/scripts | ||
| chmod 755 /opt/ml/scripts | ||
| echo "Created dir /opt/ml/scripts" | ||
| fi | ||
|
|
||
| CHECK_MOUNT_FILE=/opt/ml/scripts/check_mount_openzfs.sh | ||
|
|
||
| cat > $CHECK_MOUNT_FILE << EOF | ||
| #!/bin/bash | ||
| OPENZFS_MOUNT_POINT=$OPENZFS_MOUNT_POINT | ||
| if ! grep -qs "\$OPENZFS_MOUNT_POINT" /proc/mounts; then | ||
| mount -t nfs -o nfsvers=4.2 "$FSX_OPENZFS_DNS_NAME:/fsx" "\$OPENZFS_MOUNT_POINT" | ||
| echo "Mounted FSx OpenZFS to \$OPENZFS_MOUNT_POINT" | ||
| else | ||
| echo "FSx OpenZFS already mounted to \$OPENZFS_MOUNT_POINT. Stopping services check_fsx_openzfs_mount.timer and check_fsx_openzfs_mount.service" | ||
| systemctl stop check_fsx_openzfs_mount.timer | ||
| fi | ||
| EOF | ||
|
|
||
| chmod +x $CHECK_MOUNT_FILE | ||
|
|
||
| cat > /etc/systemd/system/check_fsx_openzfs_mount.service << EOF | ||
| [Unit] | ||
| Description=Check and remount FSx OpenZFS filesystems if necessary | ||
|
|
||
| [Service] | ||
| ExecStart=$CHECK_MOUNT_FILE | ||
| EOF | ||
|
|
||
| cat > /etc/systemd/system/check_fsx_openzfs_mount.timer << EOF | ||
| [Unit] | ||
| Description=Run check_fsx_openzfs_mount.service every minute | ||
|
|
||
| [Timer] | ||
| OnBootSec=1min | ||
| OnUnitActiveSec=1min | ||
|
|
||
| [Install] | ||
| WantedBy=timers.target | ||
| EOF | ||
|
|
||
| systemctl daemon-reload | ||
| systemctl enable --now check_fsx_openzfs_mount.timer | ||
| } | ||
|
|
||
| main() { | ||
| echo "Mount_fsx_openzfs called with fsx_openzfs_dns_name: $FSX_OPENZFS_DNS_NAME" | ||
| echo "Using openzfs_mount_point: $OPENZFS_MOUNT_POINT" | ||
| install_nfs_client | ||
| check_already_mounted | ||
| add_to_fstab | ||
| mount_fs | ||
| install_remount_service | ||
| echo "FSx OpenZFS mounted successfully to $OPENZFS_MOUNT_POINT" | ||
| } | ||
|
|
||
| main "$@" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,65 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Wait for FSx to be properly mounted (timeout after 60 seconds) | ||
| # RETRY CONFIG | ||
| ATTEMPTS=6 | ||
| WAIT=10 | ||
| for ((i=1; i<=ATTEMPTS; i++)); do | ||
| if mountpoint -q "/fsx" && touch /fsx/.test_write 2>/dev/null; then | ||
| rm -f /fsx/.test_write | ||
| break | ||
| FSX_OPENZFS_DNS_NAME="/home" | ||
| FSX_L_DNS_NAME="/fsx" | ||
|
|
||
| # Function to check mount | ||
| check_mount() | ||
| { | ||
| local mount_point="$1" | ||
| if mountpoint -q "$mount_point" && touch "$mount_point/.test_write" 2>/dev/null; then | ||
| rm -f "$mount_point/.test_write" | ||
| return 0 | ||
| fi | ||
| if [ $i -eq $ATTEMPTS ]; then | ||
| echo "FSx mount not ready after $((ATTEMPTS * WAIT)) seconds" | ||
| return 1 | ||
| } | ||
|
|
||
| # Wait for mount (both OpenZFS and FSxL) | ||
| wait_for_mount() | ||
| { | ||
| local mount_point="$1" | ||
| for ((i=1; i<=$ATTEMPTS; i++)); do | ||
| if check_mount "$mount_point"; then | ||
| echo "Successfully verified mount at $mount_point" | ||
| return 0 | ||
| fi | ||
| if [ $i -eq $ATTEMPTS ]; then | ||
| echo "Mount not ready after $((ATTEMPTS * WAIT)) seconds" | ||
| return 1 | ||
| fi | ||
| echo "Waiting for FSx mount: $mount_point to be ready... (attempt $i/$ATTEMPTS)" | ||
| sleep $WAIT | ||
| done | ||
| } | ||
|
|
||
| # Check if OpenZFS is mounted | ||
| if wait_for_mount "$FSX_OPENZFS_DNS_NAME"; then | ||
| echo "OpenZFS is mounted at $FSX_OPENZFS_DNS_NAME" | ||
| if [ -d "$FSX_OPENZFS_DNS_NAME" ]; then | ||
| # Set home directory to /home/ubuntu | ||
| sudo usermod -m -d "$FSX_OPENZFS_DNS_NAME/ubuntu" ubuntu | ||
| echo "Home directory set to $FSX_OPENZFS_DNS_NAME/ubuntu" | ||
|
|
||
| # Maintain access to /fsx/ubuntu | ||
| if wait_for_mount "$FSX_L_DNS_NAME"; then | ||
| sudo mkdir -p "$FSX_L_DNS_NAME/ubuntu" | ||
| sudo chown ubuntu:ubuntu "$FSX_L_DNS_NAME/ubuntu" | ||
| else | ||
| echo "Warning: FSx mount not available, skipping $FSX_L_DNS_NAME/ubuntu setup" | ||
| fi | ||
| fi | ||
| else | ||
| echo "OpenZFS is not mounted. Using FSxL file system as home" | ||
| if ! wait_for_mount "$FSX_L_DNS_NAME"; then | ||
| echo "Warning: FSx mount not available. Exiting." | ||
| exit 1 | ||
| fi | ||
| sleep $WAIT | ||
| done | ||
|
|
||
| # move the ubuntu user to the shared /fsx filesystem | ||
| if [ -d "/fsx/ubuntu" ]; then | ||
| sudo usermod -d /fsx/ubuntu ubuntu | ||
| elif [ -d "/fsx" ]; then | ||
| sudo usermod -m -d /fsx/ubuntu ubuntu | ||
| fi | ||
| if [ -d "$FSX_L_DNS_NAME/ubuntu" ]; then | ||
| sudo usermod -d "$FSX_L_DNS_NAME/ubuntu" ubuntu | ||
| elif [ -d "$FSX_L_DNS_NAME" ]; then | ||
| sudo usermod -m -d "$FSX_L_DNS_NAME/ubuntu" ubuntu | ||
| fi | ||
| fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we be more distriptive? fsxl vs fsxz?