Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions packages/ns-storage/files/remove-storage
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ set -e
rom_part=$(lsblk -l --json | jq -r '.blockdevices[] | select(any(.mountpoints[]; . == "/boot")) | .name')
rom_disk=$(lsblk -lno pkname /dev/$rom_part)

# Both stay empty when no storage is mounted, e.g. on factory reset
data_part=$(lsblk -l --json | jq -r '.blockdevices[] | select(any(.mountpoints[]; . == "/mnt/data")) | .name')
data_disk=$(lsblk -lno pkname /dev/$data_part)
data_disk=''
[ -n "$data_part" ] && data_disk=$(lsblk -lno pkname "/dev/$data_part")

# Removing auto-mount
uci delete fstab.ns_data
uci -q delete fstab.ns_data || :
uci commit fstab

# Configuring rsyslog
uci delete rsyslog.ns_data
uci -q delete rsyslog.ns_data || :
uci commit rsyslog
/etc/init.d/rsyslog restart

# Removing sync-data cron job
crontab -l | grep -v "/usr/sbin/sync-data" | sort | uniq | crontab -
(crontab -l 2>/dev/null || :) | grep -v "/usr/sbin/sync-data" | sort | uniq | crontab -
/etc/init.d/cron restart

# Stop process that will prevent umount
Expand All @@ -34,12 +36,23 @@ crontab -l | grep -v "/usr/sbin/sync-data" | sort | uniq | crontab -
sync && sleep 5

# Umounting data device
umount -f /mnt/data
if grep -q " /mnt/data " /proc/mounts; then
umount -f /mnt/data
fi
rm -rf /mnt/data

# Removing parition
if [ "$rom_disk" == "$data_disk" ]; then
parted "/dev/${data_disk}" rm 3
# Release the partition being removed. Only one partition may carry the
# ns_data label, otherwise a storage created later would be mounted over
# /mnt/data alongside this one.
if [ -z "$data_part" ]; then
: # no storage was mounted, nothing to release
elif [ "$rom_disk" == "$data_disk" ]; then
# Partition carved out of the OS disk: give the space back
parted -s "/dev/${data_disk}" rm "$(cat "/sys/class/block/${data_part}/partition")"
else
# Secondary drive: keep the partition so its logs can still be
# inspected, just drop the label
tune2fs -L "" "/dev/${data_part}" >/dev/null
fi

# Restore dnsmasq to /tmp before the storage disappears.
Expand Down
Loading