From 469efb9ee1e2cb949b830d9c70b79730658cb702 Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Wed, 28 Feb 2018 00:11:00 +0100 Subject: [PATCH] Implement userfs move --- addon/lib/rmupdate.tcl | 70 +++++++++++++++++++++++++++--------------- addon/www/index.html | 55 +++++++++++++++++++++++++++++---- addon/www/rest.cgi | 6 +++- 3 files changed, 100 insertions(+), 31 deletions(-) diff --git a/addon/lib/rmupdate.tcl b/addon/lib/rmupdate.tcl index bec0954..e706119 100644 --- a/addon/lib/rmupdate.tcl +++ b/addon/lib/rmupdate.tcl @@ -354,6 +354,16 @@ proc ::rmupdate::get_partitions {{device ""}} { return [array get partitions] } +proc ::rmupdate::get_disk_device {partition_device} { + if { [regexp {^(\S+mmcblk\d)p\d$} $partition_device match disk_device] } { + return $disk_device + } + if { [regexp {^(\S+)\d$} $partition_device match disk_device] } { + return $disk_device + } + return $partition_device +} + proc ::rmupdate::get_partition_device {device partition} { if { [regexp {mmcblk} $device match] } { return "${device}p${partition}" @@ -692,6 +702,13 @@ proc ::rmupdate::move_userfs_to_device {target_device {sync_data 0} {repartition error [i18n "Target device does not exist."] } + set target_partition_device "" + if {[get_disk_device $target_device] != $target_device} { + set target_partition_device $target_device + set target_device [get_disk_device $target_partition_device] + set repartition 0 + } + set source_partition_device [get_mounted_device "/usr/local"] set source_device [string range $source_partition_device 0 end-1] if { [regexp {mmcblk} $source_partition_device match] } { @@ -705,35 +722,39 @@ proc ::rmupdate::move_userfs_to_device {target_device {sync_data 0} {repartition error [i18n "Source and target are the same device."] } - set partition_number 0 - if {$repartition == 1} { - set exitcode [catch { - exec /usr/sbin/parted --script ${target_device} \ - mklabel msdos \ - mkpart primary ext4 0% 100% - } output] - if { $exitcode != 0 && $exitcode != 1 } { - error $output - } - set partition_number 1 - } else { - array set partitions [get_partitions $target_device] - set keys [array names partitions] - foreach key $keys { - regexp {^(.+)::([^:]+)$} $key match id opt - if {$opt == "filesystem_label"} { - if {[regexp "^.*userfs$" $partitions($key) match]} { - set partition_number $partitions(${id}::partition) + + if {$target_partition_device == ""} { + set partition_number 0 + if {$repartition == 1} { + set exitcode [catch { + exec /usr/sbin/parted --script ${target_device} \ + mklabel msdos \ + mkpart primary ext4 0% 100% + } output] + if { $exitcode != 0 && $exitcode != 1 } { + error $output + } + set partition_number 1 + } else { + array set partitions [get_partitions $target_device] + set keys [array names partitions] + foreach key $keys { + regexp {^(.+)::([^:]+)$} $key match id opt + if {$opt == "filesystem_label"} { + if {[regexp "^.*userfs$" $partitions($key) match]} { + set partition_number $partitions(${id}::partition) + } } } + if {$partition_number == 0} { + error [format [i18n "Failed to find userfs partition on %s, and repartition is not desired."] $target_device] + } } - if {$partition_number == 0} { - error [format [i18n "Failed to find userfs partition on %s, and repartition is not desired."] $target_device] - } + set target_partition_device [get_partition_device $target_device $partition_number] } - set target_partition_device [get_partition_device $target_device $partition_number] if {$sync_data == 1} { + catch { exec /bin/umount $target_partition_device } set exitcode [catch { exec /sbin/mkfs.ext4 -F -L userfs $target_partition_device } output] if { $exitcode != 0 && $exitcode != 1 } { error $output @@ -1508,6 +1529,7 @@ proc ::rmupdate::wlan_disconnect {} { #puts [rmupdate::get_system_device] #puts $rmupdate::sys_dev #rmupdate::clone_system /dev/sda 1 +#puts [rmupdate::get_disk_device /dev/mmcblk0p3] #puts [rmupdate::get_partitions] #puts [array_to_json [rmupdate::get_partitions]] -#puts [rmupdate::move_userfs_to_device /dev/sda 1 0] +#rmupdate::move_userfs_to_device /dev/sda1 1 0 diff --git a/addon/www/index.html b/addon/www/index.html index 700d67e..15e0565 100644 --- a/addon/www/index.html +++ b/addon/www/index.html @@ -301,7 +301,8 @@ along with this program. If not, see . rest("GET", "/get_system_info", null, function(data) { $("#system-info").empty(); $("#system-info").append( - $('
').html(i18next.t('system_type', {'system_type': data.system_type})) + $('
').html(i18next.t('system_type', {'system_type': data.system_type})), + $('
').html(i18next.t('uptime', {'uptime': data.uptime})) ); }); } @@ -344,13 +345,31 @@ along with this program. If not, see . var partition = disks[disk_device].partitions[part]; var menu_items = []; - if ((partition.partition == 0) && (! disks[disk_device].usr_local_on_disk)) { - menu_items.push($('').append(i18next.t('use_disk_for_userfs')).attr('data-disk-device', partition.disk_device).click(function() { - alert("test: " + this.getAttribute('data-disk-device')); + if (! disks[disk_device].usr_local_on_disk) { + var device = partition.disk_device; + var label = i18next.t('use_disk_for_userfs'); + if (partition.partition > 0) { + device = partition.partition_device; + var label = i18next.t('use_partition_for_userfs'); + } + menu_items.push($('').append(label).attr('data-device', device).click(function() { + var target_device = this.getAttribute('data-device'); + $('#modal-move-userfs').modal({ + onDeny: function(){ + return true; + }, + onApprove: function() { + rest('POST', '/move_userfs_to_device', JSON.stringify({"target_device":target_device}), function(data) { + display_message('success', i18next.t('userfs_moved'), 120000); + rest('POST', '/system_reboot'); + }); + return true; + } + }) + .modal('show'); })); } var menu = null; - /* if (menu_items.length > 0) { menu = $(' + +